Skip to content

Commit

Permalink
Feature/disable wal flag (#5702)
Browse files Browse the repository at this point in the history
* Add disablewal flag

* Integrate disable wal on snap

* Integrate with full pruner

* Fix build

* Add and fix tests

* Whitespace

* Addressing comment
  • Loading branch information
asdacap authored May 17, 2023
1 parent 386d772 commit 0169378
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private class TestContext
public IStateReader StateReader { get; }
public FullPruner Pruner { get; }
public MemDb TrieDb { get; }
public MemDb CopyDb { get; }
public TestMemDb CopyDb { get; }

public IProcessExitSource ProcessExitSource { get; } = Substitute.For<IProcessExitSource>();

Expand Down Expand Up @@ -250,6 +250,7 @@ public void ShouldCopyAllValues()
foreach (KeyValuePair<byte[], byte[]?> keyValuePair in TrieDb.GetAll())
{
CopyDb[keyValuePair.Key].Should().BeEquivalentTo(keyValuePair.Value);
CopyDb.KeyWasWrittenWithFlags(keyValuePair.Key, WriteFlags.LowPriority | WriteFlags.DisableWAL);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Extensions;
using Nethermind.Blockchain.FullPruning;
using Nethermind.Config;
using Nethermind.Core;
Expand Down Expand Up @@ -141,7 +142,10 @@ private static async Task RunPruning(PruningTestBlockchain chain, int time, bool

await WriteFileStructure(chain);

chain.PruningDb.InnerDbName.Should().Be($"State{time + 1}");
Assert.That(
() => chain.PruningDb.InnerDbName,
Is.EqualTo($"State{time + 1}").After(1000, 100)
);

HashSet<byte[]> currentItems = chain.DbProvider.StateDb.GetAllValues().ToHashSet(Bytes.EqualityComparer);
currentItems.IsSubsetOf(allItems).Should().BeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ protected virtual void RunPruning(IPruningContext pruning, Keccak statRoot)
{
pruning.MarkStart();

WriteFlags writeFlags = WriteFlags.LowPriority;
if (_pruningConfig.FullPruningDisableLowPriorityWrites)
WriteFlags writeFlags = WriteFlags.DisableWAL;
if (!_pruningConfig.FullPruningDisableLowPriorityWrites)
{
writeFlags = WriteFlags.None;
writeFlags |= WriteFlags.LowPriority;
}

using CopyTreeVisitor copyTreeVisitor = new(pruning, writeFlags, _logManager);
Expand Down
10 changes: 8 additions & 2 deletions src/Nethermind/Nethermind.Core/IKeyValueStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ public enum ReadFlags
HintReadAhead = 2,
}

[Flags]
public enum WriteFlags
{
None,
None = 0,

// Hint that this is a low priority write
LowPriority,
LowPriority = 1,

// Hint that this write does not require durable writes, as if it crash, it'll start over anyway.
DisableWAL = 2,

LowPriorityAndNoWAL = LowPriority | DisableWAL,
}
}
15 changes: 1 addition & 14 deletions src/Nethermind/Nethermind.Db.Rocks/ColumnDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ public void Dispose()

public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None)
{
UpdateWriteMetrics();
if (value is null)
{
_rocksDb.Remove(key, _columnFamily, _mainDb.WriteFlagsToWriteOptions(flags));
}
else
{
_rocksDb.Put(key, value, _columnFamily, _mainDb.WriteFlagsToWriteOptions(flags));
}
_mainDb.SetWithColumnFamily(key, _columnFamily, value, flags);
}

public KeyValuePair<byte[], byte[]?>[] this[byte[][] keys] =>
Expand Down Expand Up @@ -122,11 +114,6 @@ public void Flush()
/// </summary>
/// <exception cref="NotSupportedException"></exception>
public void Clear() { throw new NotSupportedException(); }

private void UpdateWriteMetrics() => _mainDb.UpdateWriteMetrics();

private void UpdateReadMetrics() => _mainDb.UpdateReadMetrics();

public long GetSize() => _mainDb.GetSize();
public long GetCacheSize() => _mainDb.GetCacheSize();
public long GetIndexSize() => _mainDb.GetIndexSize();
Expand Down
45 changes: 35 additions & 10 deletions src/Nethermind/Nethermind.Db.Rocks/DbOnTheRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public class DbOnTheRocks : IDbWithSpan, ITunableDb

private IntPtr? _rateLimiter;
internal WriteOptions? WriteOptions { get; private set; }
internal WriteOptions? LowPriorityWriteOptions { get; private set; }

internal ReadOptions? _readAheadReadOptions = null;
private WriteOptions? _noWalWrite;
private WriteOptions? _lowPriorityAndNoWalWrite;
private WriteOptions? _lowPriorityWriteOptions;
private ReadOptions? _readAheadReadOptions = null;

internal DbOptions? DbOptions { get; private set; }

Expand Down Expand Up @@ -396,9 +397,18 @@ protected virtual void BuildOptions<T>(PerTableDbConfig dbConfig, Options<T> opt
WriteOptions.SetSync(dbConfig
.WriteAheadLogSync); // potential fix for corruption on hard process termination, may cause performance degradation

LowPriorityWriteOptions = new WriteOptions();
LowPriorityWriteOptions.SetSync(dbConfig.WriteAheadLogSync);
Native.Instance.rocksdb_writeoptions_set_low_pri(LowPriorityWriteOptions.Handle, true);
_noWalWrite = new WriteOptions();
_noWalWrite.SetSync(dbConfig.WriteAheadLogSync);
_noWalWrite.DisableWal(1);

_lowPriorityWriteOptions = new WriteOptions();
_lowPriorityWriteOptions.SetSync(dbConfig.WriteAheadLogSync);
Native.Instance.rocksdb_writeoptions_set_low_pri(_lowPriorityWriteOptions.Handle, true);

_lowPriorityAndNoWalWrite = new WriteOptions();
_lowPriorityAndNoWalWrite.SetSync(dbConfig.WriteAheadLogSync);
_lowPriorityAndNoWalWrite.DisableWal(1);
Native.Instance.rocksdb_writeoptions_set_low_pri(_lowPriorityAndNoWalWrite.Handle, true);

// When readahead flag is on, the next keys are expected to be after the current key. Increasing this value,
// will increase the chances that the next keys will be in the cache, which reduces iops and latency. This
Expand Down Expand Up @@ -471,6 +481,11 @@ public byte[]? this[ReadOnlySpan<byte> key]
}

public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None)
{
SetWithColumnFamily(key, null, value, flags);
}

internal void SetWithColumnFamily(ReadOnlySpan<byte> key, ColumnFamilyHandle? cf, byte[]? value, WriteFlags flags = WriteFlags.None)
{
if (_isDisposing)
{
Expand All @@ -483,11 +498,11 @@ public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteF
{
if (value is null)
{
_db.Remove(key, null, WriteFlagsToWriteOptions(flags));
_db.Remove(key, cf, WriteFlagsToWriteOptions(flags));
}
else
{
_db.Put(key, value, null, WriteFlagsToWriteOptions(flags));
_db.Put(key, value, cf, WriteFlagsToWriteOptions(flags));
}
}
catch (RocksDbSharpException e)
Expand All @@ -499,9 +514,19 @@ public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteF

public WriteOptions? WriteFlagsToWriteOptions(WriteFlags flags)
{
if (flags == WriteFlags.LowPriority)
if ((flags & WriteFlags.LowPriorityAndNoWAL) != 0)
{
return _lowPriorityAndNoWalWrite;
}

if ((flags & WriteFlags.DisableWAL) != 0)
{
return _noWalWrite;
}

if ((flags & WriteFlags.LowPriority) != 0)
{
return LowPriorityWriteOptions;
return _lowPriorityWriteOptions;
}

return WriteOptions;
Expand Down
Loading

0 comments on commit 0169378

Please sign in to comment.