Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move TrieStore to single batch #3462

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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