-
Notifications
You must be signed in to change notification settings - Fork 640
Feature/adjust rocksdb config by memory #9000
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| namespace Nethermind.Core.Test; | ||
|
|
||
| public class TestHardwareInfo(long availableMemory = 10000000) : IHardwareInfo | ||
| { | ||
| public long AvailableMemoryBytes => availableMemory; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
|
|
||
| namespace Nethermind.Core; | ||
|
|
||
| public class HardwareInfo : IHardwareInfo | ||
| { | ||
| public long AvailableMemoryBytes { get; } | ||
|
|
||
| public HardwareInfo() | ||
| { | ||
| // Note: Not the same as memory capacity. This take into account current system memory pressure such as | ||
| // other process as well as OS level limit such as rlimit. Eh, its good enough. | ||
| AvailableMemoryBytes = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using Nethermind.Core.Extensions; | ||
|
|
||
| namespace Nethermind.Core; | ||
|
|
||
| public interface IHardwareInfo | ||
| { | ||
| public static readonly long StateDbLargerMemoryThreshold = 20.GiB(); | ||
| long AvailableMemoryBytes { get; } | ||
| } |
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
40 changes: 40 additions & 0 deletions
40
src/Nethermind/Nethermind.Db.Rocks/Config/AdjustedRocksdbConfig.cs
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| namespace Nethermind.Db.Rocks.Config; | ||
|
|
||
| public class AdjustedRocksdbConfig( | ||
| IRocksDbConfig baseConfig, | ||
| string additionalRocksDbOptions, | ||
| ulong writeBufferSize | ||
| ) : IRocksDbConfig | ||
| { | ||
| public ulong? WriteBufferSize => writeBufferSize; | ||
|
|
||
| public ulong? WriteBufferNumber => baseConfig.WriteBufferNumber; | ||
|
|
||
| // Note: not AdditionalRocksDbOptions so that user can still override option. | ||
| public string RocksDbOptions => baseConfig.RocksDbOptions + additionalRocksDbOptions; | ||
|
|
||
| public string AdditionalRocksDbOptions => baseConfig.AdditionalRocksDbOptions; | ||
|
|
||
| public int? MaxOpenFiles => baseConfig.MaxOpenFiles; | ||
|
|
||
| public bool WriteAheadLogSync => baseConfig.WriteAheadLogSync; | ||
|
|
||
| public ulong? ReadAheadSize => baseConfig.ReadAheadSize; | ||
|
|
||
| public bool EnableDbStatistics => baseConfig.EnableDbStatistics; | ||
|
|
||
| public uint StatsDumpPeriodSec => baseConfig.StatsDumpPeriodSec; | ||
|
|
||
| public bool? VerifyChecksum => baseConfig.VerifyChecksum; | ||
|
|
||
| public ulong? RowCacheSize => baseConfig.RowCacheSize; | ||
|
|
||
| public bool EnableFileWarmer => baseConfig.EnableFileWarmer; | ||
|
|
||
| public double CompressibilityHint => baseConfig.CompressibilityHint; | ||
|
|
||
| public bool FlushOnExit => baseConfig.FlushOnExit; | ||
| } |
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
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
22 changes: 22 additions & 0 deletions
22
src/Nethermind/Nethermind.Db.Rocks/Config/IRocksDbConfig.cs
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| namespace Nethermind.Db.Rocks.Config; | ||
|
|
||
| public interface IRocksDbConfig | ||
| { | ||
| ulong? WriteBufferSize { get; } | ||
| ulong? WriteBufferNumber { get; } | ||
| string RocksDbOptions { get; } | ||
| string AdditionalRocksDbOptions { get; } | ||
| int? MaxOpenFiles { get; } | ||
| bool WriteAheadLogSync { get; } | ||
| ulong? ReadAheadSize { get; } | ||
| bool EnableDbStatistics { get; } | ||
| uint StatsDumpPeriodSec { get; } | ||
| bool? VerifyChecksum { get; } | ||
| ulong? RowCacheSize { get; } | ||
| bool EnableFileWarmer { get; } | ||
| double CompressibilityHint { get; } | ||
| bool FlushOnExit { get; } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/Nethermind/Nethermind.Db.Rocks/Config/IRocksDbConfigFactory.cs
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| namespace Nethermind.Db.Rocks.Config; | ||
|
|
||
| public interface IRocksDbConfigFactory | ||
| { | ||
| IRocksDbConfig GetForDatabase(string databaseName, string? columnName); | ||
| } |
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
69 changes: 69 additions & 0 deletions
69
src/Nethermind/Nethermind.Db.Rocks/Config/RocksDbConfigFactory.cs
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
| using Nethermind.Core; | ||
| using Nethermind.Core.Extensions; | ||
| using Nethermind.Logging; | ||
|
|
||
| namespace Nethermind.Db.Rocks.Config; | ||
|
|
||
| public class RocksDbConfigFactory(IDbConfig dbConfig, IPruningConfig pruningConfig, IHardwareInfo hardwareInfo, ILogManager logManager) : IRocksDbConfigFactory | ||
| { | ||
| private readonly ILogger _logger = logManager.GetClassLogger<IRocksDbConfigFactory>(); | ||
|
|
||
| public IRocksDbConfig GetForDatabase(string databaseName, string? columnName) | ||
| { | ||
| IRocksDbConfig rocksDbConfig = new PerTableDbConfig(dbConfig, databaseName, columnName); | ||
| if (databaseName.StartsWith("State")) | ||
| { | ||
| if (!pruningConfig.Mode.IsMemory()) | ||
| { | ||
| if (_logger.IsInfo) _logger.Info($"Using archive mode State Db config."); | ||
|
|
||
| ulong writeBufferSize = rocksDbConfig.WriteBufferSize ?? 0; | ||
| if (writeBufferSize < dbConfig.StateDbArchiveModeWriteBufferSize) writeBufferSize = dbConfig.StateDbArchiveModeWriteBufferSize; | ||
|
|
||
| rocksDbConfig = new AdjustedRocksdbConfig( | ||
| rocksDbConfig, | ||
| dbConfig.StateDbArchiveModeRocksDbOptions!, | ||
| writeBufferSize | ||
| ); | ||
| } | ||
| else if (hardwareInfo.AvailableMemoryBytes >= IHardwareInfo.StateDbLargerMemoryThreshold) | ||
| { | ||
| if (_logger.IsInfo) _logger.Info($"Detected {hardwareInfo.AvailableMemoryBytes / 1.GiB()} GB of available memory. Applying large memory State Db config."); | ||
|
|
||
| ulong writeBufferSize = rocksDbConfig.WriteBufferSize ?? 0; | ||
| if (writeBufferSize < dbConfig.StateDbLargeMemoryWriteBufferSize) writeBufferSize = dbConfig.StateDbLargeMemoryWriteBufferSize; | ||
|
|
||
| rocksDbConfig = new AdjustedRocksdbConfig( | ||
| rocksDbConfig, | ||
| dbConfig.StateDbLargeMemoryRocksDbOptions!, | ||
| writeBufferSize | ||
| ); | ||
| } | ||
|
|
||
| if (pruningConfig.Mode.IsMemory()) | ||
| { | ||
| ulong totalWriteBufferMb = rocksDbConfig.WriteBufferNumber!.Value * rocksDbConfig.WriteBufferSize!.Value / (ulong)1.MB(); | ||
| double minimumWriteBufferMb = 0.2 * pruningConfig.DirtyCacheMb; | ||
| if (totalWriteBufferMb < minimumWriteBufferMb) | ||
| { | ||
| ulong minimumWriteBufferSize = (ulong)Math.Ceiling((minimumWriteBufferMb * 1.MB()) / rocksDbConfig.WriteBufferNumber!.Value); | ||
|
|
||
| if (_logger.IsInfo) _logger.Info($"Adjust state DB write buffer size to {minimumWriteBufferSize / (ulong)1.MB()} MB to account for pruning cache."); | ||
|
|
||
| rocksDbConfig = new AdjustedRocksdbConfig( | ||
| rocksDbConfig, | ||
| "", | ||
| minimumWriteBufferSize | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return rocksDbConfig; | ||
| } | ||
| } |
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
Oops, something went wrong.
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 TODO comment indicates incomplete functionality where options are not applied to column families. This should be addressed or properly documented with a plan for resolution.