Skip to content

Commit

Permalink
Move TrieStore to single batch (#3462)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored Oct 4, 2021
1 parent 4a8c52c commit 75f96af
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/Nethermind/Nethermind.Trie/Pruning/TrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,15 @@ public void SaveInCache(TrieNode node)

public TrieNode FindCachedOrUnknown(Keccak hash)
{
bool isMissing = !_objectsCache.TryGetValue(hash, out TrieNode trieNode);
if (isMissing)
if (_objectsCache.TryGetValue(hash, out TrieNode trieNode))
{
if (_trieStore._logger.IsTrace) _trieStore._logger.Trace($"Creating new node {trieNode}");
trieNode = new TrieNode(NodeType.Unknown, hash);
if (trieNode.Keccak is null)
{
throw new InvalidOperationException($"Adding node with null hash {trieNode}");
}

SaveInCache(trieNode);
Metrics.LoadedFromCacheNodesCount++;
}
else
{
Metrics.LoadedFromCacheNodesCount++;
if (_trieStore._logger.IsTrace) _trieStore._logger.Trace($"Creating new node {trieNode}");
trieNode = new TrieNode(NodeType.Unknown, hash);
SaveInCache(trieNode);
}

return trieNode;
Expand All @@ -77,12 +71,7 @@ public TrieNode FindCachedOrUnknown(Keccak hash)
public TrieNode FromCachedRlpOrUnknown(Keccak hash)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
bool isMissing = !_objectsCache.TryGetValue(hash, out TrieNode? trieNode);
if (isMissing)
{
trieNode = new TrieNode(NodeType.Unknown, hash);
}
else
if (_objectsCache.TryGetValue(hash, out TrieNode? trieNode))
{
if (trieNode!.FullRlp is null)
{
Expand All @@ -98,12 +87,17 @@ public TrieNode FromCachedRlpOrUnknown(Keccak hash)

Metrics.LoadedFromCacheNodesCount++;
}
else
{
trieNode = new TrieNode(NodeType.Unknown, hash);
}

if (_trieStore._logger.IsTrace) _trieStore._logger.Trace($"Creating new node {trieNode}");
return trieNode;
}

public bool IsNodeCached(Keccak hash) => _objectsCache.ContainsKey(hash);
public bool
IsNodeCached(Keccak hash) => _objectsCache.ContainsKey(hash);

public ConcurrentDictionary<Keccak, TrieNode> AllNodes => _objectsCache;

Expand Down Expand Up @@ -136,8 +130,8 @@ public void Clear()
}

private int _isFirst;
private readonly ThreadLocal<IBatch> _currentBatch = new();

private IBatch? _currentBatch = null;

private readonly DirtyNodesCache _dirtyNodes;

Expand Down Expand Up @@ -315,8 +309,8 @@ public void FinishBlockCommit(TrieType trieType, long blockNumber, TrieNode? roo
}
finally
{
_currentBatch.Value?.Dispose();
_currentBatch.Value = null;
_currentBatch?.Dispose();
_currentBatch = null;
}
}

Expand All @@ -330,7 +324,7 @@ public void HackPersistOnShutdown()
internal byte[] LoadRlp(Keccak keccak, IKeyValueStore? keyValueStore)
{
keyValueStore ??= _keyValueStore;
byte[]? rlp = _currentBatch.Value?[keccak.Bytes] ?? keyValueStore[keccak.Bytes];
byte[]? rlp = _currentBatch?[keccak.Bytes] ?? keyValueStore[keccak.Bytes];

if (rlp is null)
{
Expand Down Expand Up @@ -611,7 +605,7 @@ private void Persist(BlockCommitSet commitSet)

try
{
_currentBatch.Value ??= _keyValueStore.StartBatch();
_currentBatch ??= _keyValueStore.StartBatch();
if (_logger.IsDebug) _logger.Debug($"Persisting from root {commitSet.Root} in {commitSet.BlockNumber}");

Stopwatch stopwatch = Stopwatch.StartNew();
Expand All @@ -629,16 +623,16 @@ private void Persist(BlockCommitSet commitSet)
{
// For safety we prefer to commit half of the batch rather than not commit at all.
// Generally hanging nodes are not a problem in the DB but anything missing from the DB is.
_currentBatch.Value?.Dispose();
_currentBatch.Value = null;
_currentBatch?.Dispose();
_currentBatch = null;
}

PruneCurrentSet();
}

private void Persist(TrieNode currentNode, long blockNumber)
{
_currentBatch.Value ??= _keyValueStore.StartBatch();
_currentBatch ??= _keyValueStore.StartBatch();
if (currentNode is null)
{
throw new ArgumentNullException(nameof(currentNode));
Expand All @@ -655,7 +649,7 @@ private void Persist(TrieNode currentNode, long blockNumber)

if (_logger.IsTrace)
_logger.Trace($"Persisting {nameof(TrieNode)} {currentNode} in snapshot {blockNumber}.");
_currentBatch.Value[currentNode.Keccak.Bytes] = currentNode.FullRlp;
_currentBatch[currentNode.Keccak.Bytes] = currentNode.FullRlp;
currentNode.IsPersisted = true;
currentNode.LastSeen = blockNumber;
PersistedNodesCount++;
Expand Down

0 comments on commit 75f96af

Please sign in to comment.