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

Compact dbs manually #6595

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Nethermind/Nethermind.Db.Rocks/ColumnsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public ColumnsDb(string basePath, DbSettings settings, IDbConfig dbConfig, ILogM
}
}

public override void Compact()
{
foreach (T key in ColumnKeys)
{
_columnDbs[key].Compact();
}
}

private static IReadOnlyList<T> GetEnumKeys(IReadOnlyList<T> keys)
{
if (typeof(T).IsEnum && keys.Count == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Db.Rocks/DbOnTheRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ public void Flush()
InnerFlush();
}

public void Compact()
public virtual void Compact()
{
_db.CompactRange(Keccak.Zero.BytesToArray(), Keccak.MaxValue.BytesToArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Nethermind.Merge.Plugin.Test;

[TestFixture]
[Parallelizable(ParallelScope.Self)]
public class ProcessedTransactionsDbCleanerTests
public class BlobTransactionsDbCleanerTests
{
private readonly ILogManager _logManager = LimboLogs.Instance;
private readonly ISpecProvider _specProvider = MainnetSpecProvider.Instance;
Expand Down Expand Up @@ -48,7 +48,7 @@ Transaction GetTx(PrivateKey sender)
returnedTxs!.Length.Should().Be(2);

IBlockFinalizationManager finalizationManager = Substitute.For<IBlockFinalizationManager>();
ProcessedTransactionsDbCleaner dbCleaner = new(finalizationManager, columnsDb.GetColumnDb(BlobTxsColumns.ProcessedTxs), _logManager);
BlobTransactionsDbCleaner dbCleaner = new(finalizationManager, columnsDb, _logManager);

finalizationManager.BlocksFinalized += Raise.EventWith(
new FinalizeEventArgs(Build.A.BlockHeader.TestObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Linq;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Core;
Expand All @@ -11,18 +12,20 @@

namespace Nethermind.Merge.Plugin;

public class ProcessedTransactionsDbCleaner : IDisposable
public class BlobTransactionsDbCleaner : IDisposable
{
private readonly IBlockFinalizationManager _finalizationManager;
private readonly IDb _processedTxsDb;
private readonly IColumnsDb<BlobTxsColumns> _db;
private readonly ILogger _logger;
private long _lastFinalizedBlock = 0;
public Task CleaningTask { get; private set; } = Task.CompletedTask;

public ProcessedTransactionsDbCleaner(IBlockFinalizationManager finalizationManager, IDb processedTxsDb, ILogManager logManager)
public BlobTransactionsDbCleaner(IBlockFinalizationManager finalizationManager, IColumnsDb<BlobTxsColumns> db, ILogManager logManager)
{
_finalizationManager = finalizationManager ?? throw new ArgumentNullException(nameof(finalizationManager));
_processedTxsDb = processedTxsDb ?? throw new ArgumentNullException(nameof(processedTxsDb));
_db = db ?? throw new ArgumentNullException(nameof(db));
_processedTxsDb = db?.GetColumnDb(BlobTxsColumns.ProcessedTxs) ?? throw new ArgumentNullException(nameof(db));
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

_finalizationManager.BlocksFinalized += OnBlocksFinalized;
Expand Down Expand Up @@ -55,6 +58,10 @@ private void CleanProcessedTransactionsDb(long newlyFinalizedBlockNumber)

if (_logger.IsDebug) _logger.Debug($"Cleaned processed blob txs from block {_lastFinalizedBlock} to block {newlyFinalizedBlockNumber}");

_db.Compact();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we guard the compaction not use to many resources? (Threads? I/O?) So it won't starve anything else in block processing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we only run compaction only after enough transactions were deleted from the db? Put some threshold?


if (_logger.IsDebug) _logger.Debug($"Blob transactions database columns have been compacted");

_lastFinalizedBlock = newlyFinalizedBlockNumber;
}
catch (Exception exception)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public virtual Task Init(INethermindApi nethermindApi)
_blockFinalizationManager = new ManualBlockFinalizationManager();
if (_txPoolConfig.BlobsSupport.SupportsReorgs())
{
ProcessedTransactionsDbCleaner processedTransactionsDbCleaner = new(_blockFinalizationManager, _api.DbProvider.BlobTransactionsDb.GetColumnDb(BlobTxsColumns.ProcessedTxs), _api.LogManager);
BlobTransactionsDbCleaner processedTransactionsDbCleaner = new(_blockFinalizationManager, _api.DbProvider.BlobTransactionsDb, _api.LogManager);
_api.DisposeStack.Push(processedTransactionsDbCleaner);
}

Expand Down