Skip to content

Commit 6006747

Browse files
MarekM25kamilchodola
authored andcommitted
MaxDegreeOfParallelism defaults for full pruning (#5662)
* other defaults for full pruning? * degreeOfParalleism * small refactor * fix build * Fixing BatchedTrieVistior * 25% of cores * Update Pruning config * add logger * fix
1 parent b1ce97e commit 6006747

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

src/Nethermind/Nethermind.Blockchain/FullPruning/FullPruner.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void OnUpdateMainChain(object? sender, OnUpdateMainChainArgs e)
138138
BlockHeader? header = _blockTree.FindHeader(_stateToCopy);
139139
if (header is not null && Interlocked.CompareExchange(ref _waitingForStateReady, 0, 1) == 1)
140140
{
141-
if (_logger.IsInfo) _logger.Info($"Full Pruning Ready to start: pruning garbage before state {_stateToCopy} with root {header.StateRoot}.");
141+
if (_logger.IsInfo) _logger.Info($"Full Pruning Ready to start: pruning garbage before state {_stateToCopy} with root {header.StateRoot}");
142142
Task.Run(() => RunPruning(_currentPruning, header.StateRoot!));
143143
_blockTree.OnUpdateMainChain -= OnUpdateMainChain;
144144
}
@@ -222,6 +222,7 @@ protected virtual void RunPruning(IPruningContext pruning, Keccak statRoot)
222222
MaxDegreeOfParallelism = _pruningConfig.FullPruningMaxDegreeOfParallelism,
223223
FullScanMemoryBudget = ((long)_pruningConfig.FullPruningMemoryBudgetMb).MiB(),
224224
};
225+
if (_logger.IsInfo) _logger.Info($"Full pruning started with MaxDegreeOfParallelism: {visitingOptions.MaxDegreeOfParallelism} and FullScanMemoryBudget: {visitingOptions.FullScanMemoryBudget}");
225226
_stateReader.RunTreeVisitor(copyTreeVisitor, statRoot, visitingOptions);
226227

227228
if (!pruning.CancellationTokenSource.IsCancellationRequested)

src/Nethermind/Nethermind.Db/IPruningConfig.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ public interface IPruningConfig : IConfig
3737
FullPruningTrigger FullPruningTrigger { get; set; }
3838

3939
[ConfigItem(
40-
Description = "'Full' pruning: Defines how many parallel tasks and potentially used threads can be created by full pruning. 0 - number of logical processors, 1 - full pruning will run on single thread. " +
41-
"Recommended value depends on the type of the node. If the node needs to be responsive (its RPC or Validator node) then recommended value is below the number of logical processors. " +
42-
"If the node doesn't have much other responsibilities but needs to be reliably be able to follow the chain without any delays and produce live logs - the default value is recommended. " +
43-
"If the node doesn't have to be responsive, has very fast I/O (like NVME) and the shortest pruning time is to be achieved, this can be set to 2-3x of the number of logical processors.",
40+
Description = "'Full' pruning: Defines how many parallel tasks and potentially used threads can be created by full pruning. -1 - number of logical processors, 0 - 25% of logical processors, 1 - full pruning will run on single thread. " +
41+
"Recommended value depends on the type of the node. If the node needs to be responsive (its RPC or Validator node) then recommended value is the default value or below is recommended. " +
42+
"If the node doesn't have much other responsibilities but needs to be reliably be able to follow the chain without any delays and produce live logs - the default value or above is recommended. " +
43+
"If the node doesn't have to be responsive, has very fast I/O (like NVME) and the shortest pruning time is to be achieved, this can be set to the number of logical processors (-1).",
4444
DefaultValue = "0")]
4545
int FullPruningMaxDegreeOfParallelism { get; set; }
4646

4747
[ConfigItem(
4848
Description = "Set the memory budget used for the trie visit. Increasing this significantly reduces read iops requirement at expense of RAM. Default depend on network. Set to 0 to disable.",
49-
DefaultValue = "0")]
49+
DefaultValue = "4000")]
5050
int FullPruningMemoryBudgetMb { get; set; }
5151

5252
[ConfigItem(

src/Nethermind/Nethermind.Db/PruningConfig.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public bool Enabled
2626
public long PersistenceInterval { get; set; } = 8192;
2727
public long FullPruningThresholdMb { get; set; } = 256000;
2828
public FullPruningTrigger FullPruningTrigger { get; set; } = FullPruningTrigger.Manual;
29-
public int FullPruningMaxDegreeOfParallelism { get; set; } = 0;
30-
public int FullPruningMemoryBudgetMb { get; set; } = 0;
29+
public int FullPruningMaxDegreeOfParallelism { get; set; }
30+
public int FullPruningMemoryBudgetMb { get; set; } = 4000;
3131
public bool FullPruningDisableLowPriorityWrites { get; set; } = false;
3232
public int FullPruningMinimumDelayHours { get; set; } = 240;
3333
public FullPruningCompletionBehavior FullPruningCompletionBehavior { get; set; } = FullPruningCompletionBehavior.None;

src/Nethermind/Nethermind.Trie/BatchedTrieVisitor.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ public BatchedTrieVisitor(
8383
// Get estimated num of file (expected db size / 64MiB), multiplied by a reasonable num of thread we want to
8484
// confine to a file. If its too high, the overhead of looping through the stack can get a bit high at the end
8585
// of the visit. But then again its probably not much.
86-
int degreeOfParallelism = visitingOptions.MaxDegreeOfParallelism;
87-
if (degreeOfParallelism == 0)
88-
{
89-
degreeOfParallelism = Math.Max(Environment.ProcessorCount, 1);
90-
}
91-
long maxPartitionCount = (expectedDbSize / 64.MiB()) * Math.Min(4, degreeOfParallelism);
86+
long maxPartitionCount = (expectedDbSize / 64.MiB()) * Math.Min(4, visitingOptions.MaxDegreeOfParallelism);
9287

9388
if (_partitionCount > maxPartitionCount)
9489
{
@@ -140,13 +135,7 @@ public void Start(
140135

141136
try
142137
{
143-
int degreeOfParallelism = trieVisitContext.MaxDegreeOfParallelism;
144-
if (degreeOfParallelism == 0)
145-
{
146-
degreeOfParallelism = Math.Max(Environment.ProcessorCount, 1);
147-
}
148-
149-
Task[]? tasks = Enumerable.Range(0, degreeOfParallelism)
138+
Task[]? tasks = Enumerable.Range(0, trieVisitContext.MaxDegreeOfParallelism)
150139
.Select((_) => Task.Run(BatchedThread))
151140
.ToArray();
152141

src/Nethermind/Nethermind.Trie/VisitContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class TrieVisitContext : IDisposable
2222
public int MaxDegreeOfParallelism
2323
{
2424
get => _maxDegreeOfParallelism;
25-
internal init => _maxDegreeOfParallelism = value == 0 ? Environment.ProcessorCount : value;
25+
internal init => _maxDegreeOfParallelism = VisitingOptions.AdjustMaxDegreeOfParallelism(value);
2626
}
2727

2828
public SemaphoreSlim Semaphore

src/Nethermind/Nethermind.Trie/VisitingOptions.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Nethermind.Trie
1111
public class VisitingOptions
1212
{
1313
public static readonly VisitingOptions Default = new();
14+
private readonly int _maxDegreeOfParallelism = 1;
1415

1516
/// <summary>
1617
/// Should visit accounts.
@@ -20,7 +21,14 @@ public class VisitingOptions
2021
/// <summary>
2122
/// Maximum number of threads that will be used to visit the trie.
2223
/// </summary>
23-
public int MaxDegreeOfParallelism { get; init; } = 1;
24+
public int MaxDegreeOfParallelism
25+
{
26+
get => _maxDegreeOfParallelism;
27+
init
28+
{
29+
_maxDegreeOfParallelism = AdjustMaxDegreeOfParallelism(value);
30+
}
31+
}
2432

2533
/// <summary>
2634
/// Specify memory budget to run a batched trie visitor. Significantly reduce read iops as memory budget
@@ -30,5 +38,14 @@ public class VisitingOptions
3038
/// with slower SSD. Set to 0 to disable batched trie visitor.
3139
/// </summary>
3240
public long FullScanMemoryBudget { get; set; }
41+
42+
public static int AdjustMaxDegreeOfParallelism(int rawMaxDegreeOfParallelism)
43+
{
44+
if (rawMaxDegreeOfParallelism == 0)
45+
return Math.Max(Environment.ProcessorCount / 4, 1);
46+
if (rawMaxDegreeOfParallelism <= -1)
47+
return Environment.ProcessorCount;
48+
return rawMaxDegreeOfParallelism;
49+
}
3350
}
3451
}

0 commit comments

Comments
 (0)