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

Feature/disable wal flag #5702

Merged
merged 7 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -182,6 +182,8 @@ protected virtual void RunPruning(IPruningContext pruning, Keccak statRoot)
writeFlags = WriteFlags.None;
}

writeFlags |= WriteFlags.DisableWAL;
Copy link
Member

Choose a reason for hiding this comment

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

Why not start with it and then set LowPriority based on _pruningConfig.FullPruningDisableLowPriorityWrites ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would do too.


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

[Flags]
public enum WriteFlags
{
None,

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

// Hint that this write does not require durable writes, as if it crash, it'll start over anyway.
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;
Comment on lines +38 to +41
Copy link
Member

Choose a reason for hiding this comment

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

Can/should they be static?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, WriteAheadLogSync is from config.


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.LowPriority) != 0 && (flags & WriteFlags.DisableWAL) != 0)
Copy link
Member

Choose a reason for hiding this comment

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

Either save flags to bool and reuse them or check for both flags in one go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

{
return _lowPriorityAndNoWalWrite;
}

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

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

return WriteOptions;
Expand Down
Loading