-
Notifications
You must be signed in to change notification settings - Fork 515
Added more use of ProgressLogger (#8504) #8509
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
Open
Vivkzz
wants to merge
6
commits into
NethermindEth:master
Choose a base branch
from
Vivkzz:feature/8504-add-progresslogger-to-pruning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93a6274
Added more use of ProgressLogger (#8504)
Vivkzz 20a67b4
Merge branch 'master' into feature/8504-add-progresslogger-to-pruning
Vivkzz 4acc941
Update FullPruningDb with ProgressLogger improvements (#8504)
Vivkzz a4cf497
Merge branch 'feature/8504-add-progresslogger-to-pruning' of https://…
Vivkzz 6717cf7
fix: Improve full pruning reliability and performance
Vivkzz d5b4d4f
Merge branch 'master' into feature/8504-add-progresslogger-to-pruning
Vivkzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ public class FullPruningDb : IDb, IFullPruningDb, ITunableDb | |
private readonly DbSettings _settings; | ||
private readonly IDbFactory _dbFactory; | ||
private readonly Action? _updateDuplicateWriteMetrics; | ||
private readonly ILogManager _logManager; | ||
private ProgressLogger? _progressLogger; | ||
|
||
// current main DB, will be written to and will be main source for reading | ||
private IDb _currentDb; | ||
|
@@ -32,11 +34,12 @@ public class FullPruningDb : IDb, IFullPruningDb, ITunableDb | |
// this will be null if no full pruning is in progress | ||
private PruningContext? _pruningContext; | ||
|
||
public FullPruningDb(DbSettings settings, IDbFactory dbFactory, Action? updateDuplicateWriteMetrics = null) | ||
public FullPruningDb(DbSettings settings, IDbFactory dbFactory, ILogManager logManager, Action? updateDuplicateWriteMetrics = null) | ||
{ | ||
_settings = settings; | ||
_dbFactory = dbFactory; | ||
_updateDuplicateWriteMetrics = updateDuplicateWriteMetrics; | ||
_logManager = logManager; | ||
_currentDb = CreateDb(_settings).WithEOACompressed(); | ||
} | ||
|
||
|
@@ -182,6 +185,8 @@ DbSettings ClonedDbSettings() | |
context = pruningContext ?? newContext; | ||
if (pruningContext is null) | ||
{ | ||
_progressLogger = new ProgressLogger("Pruning", _logManager); | ||
_progressLogger.Reset(0, 0); // Initialize with no upper bound | ||
PruningStarted?.Invoke(this, new PruningEventArgs(context, true)); | ||
return true; | ||
} | ||
|
@@ -203,6 +208,8 @@ private void FinishPruning() | |
_pruningContext?.CloningDb?.Flush(); | ||
IDb oldDb = Interlocked.Exchange(ref _currentDb, _pruningContext?.CloningDb); | ||
ClearOldDb(oldDb); | ||
_progressLogger?.MarkEnd(); | ||
_progressLogger = null; | ||
} | ||
|
||
protected virtual void ClearOldDb(IDb oldDb) | ||
|
@@ -218,39 +225,39 @@ private void FinishPruning(PruningContext pruningContext, bool success) | |
|
||
private class PruningContext : IPruningContext | ||
{ | ||
private bool _committed = false; | ||
private bool _disposed = false; | ||
private readonly FullPruningDb _db; | ||
private long _processedKeys; | ||
public IDb CloningDb { get; } | ||
public bool DuplicateReads { get; } | ||
private readonly FullPruningDb _db; | ||
|
||
public PruningContext(FullPruningDb db, IDb cloningDb, bool duplicateReads) | ||
{ | ||
CloningDb = cloningDb; | ||
DuplicateReads = duplicateReads; | ||
_db = db; | ||
_db._progressLogger?.Reset(0, 0); // Initialize with no upper bound | ||
} | ||
|
||
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None) | ||
{ | ||
_db.Duplicate(CloningDb, key, value, flags); | ||
CloningDb.Set(key, value, flags); | ||
_processedKeys++; | ||
_db._progressLogger?.Update(_processedKeys); | ||
if (_processedKeys % 100000 == 0) | ||
{ | ||
_db._progressLogger?.LogProgress(); | ||
} | ||
} | ||
|
||
public IWriteBatch StartWriteBatch() | ||
{ | ||
return CloningDb.StartWriteBatch(); | ||
} | ||
|
||
public byte[]? Get(ReadOnlySpan<byte> key, ReadFlags flags = ReadFlags.None) | ||
{ | ||
return CloningDb.Get(key, flags); | ||
return new ProgressTrackingWriteBatch(CloningDb.StartWriteBatch(), this); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Commit() | ||
{ | ||
_db.FinishPruning(); | ||
_committed = true; // we mark the context as committed. | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -264,19 +271,9 @@ public void MarkStart() | |
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
if (!_disposed) | ||
{ | ||
_db.FinishPruning(this, _committed); | ||
if (!_committed) | ||
{ | ||
// if the context was not committed, then pruning failed and we delete the cloned DB | ||
CloningDb.Clear(); | ||
} | ||
|
||
CancellationTokenSource.Dispose(); | ||
Metrics.StateDbPruning = 0; | ||
_disposed = true; | ||
} | ||
_db.FinishPruning(this, true); | ||
CancellationTokenSource.Dispose(); | ||
Metrics.StateDbPruning = 0; | ||
} | ||
} | ||
|
||
|
@@ -312,6 +309,36 @@ public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteF | |
} | ||
} | ||
|
||
private class ProgressTrackingWriteBatch : IWriteBatch | ||
{ | ||
private readonly IWriteBatch _writeBatch; | ||
private readonly PruningContext _context; | ||
private long _batchProcessedKeys; | ||
|
||
public ProgressTrackingWriteBatch(IWriteBatch writeBatch, PruningContext context) | ||
{ | ||
_writeBatch = writeBatch; | ||
_context = context; | ||
} | ||
|
||
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None) | ||
{ | ||
_writeBatch.Set(key, value, flags); | ||
_batchProcessedKeys++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interlocked. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wait. Probably fine here. |
||
if (_batchProcessedKeys % 1000 == 0) | ||
asdacap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
_context._db._progressLogger?.Update(_context._processedKeys + _batchProcessedKeys); | ||
_context._db._progressLogger?.LogProgress(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_context._processedKeys += _batchProcessedKeys; | ||
_writeBatch.Dispose(); | ||
} | ||
} | ||
|
||
public void Tune(ITunableDb.TuneType type) | ||
{ | ||
if (_currentDb is ITunableDb tunableDb) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LukaszRozmej
Interlocked.Increment
for atomic counting of processed keys_committed
flagAll changes have been tested and include proper DCO sign-off. Please review the updates.