From da0a36e4a204ee5219d624df446c2f1b193eb6f6 Mon Sep 17 00:00:00 2001 From: Ahmed Yasin Koculu Date: Sat, 17 Jun 2023 02:14:36 +0200 Subject: [PATCH 1/3] Load initial sparse array in Open and OperOrCreate methods. --- src/ZoneTree/ZoneTreeFactory.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ZoneTree/ZoneTreeFactory.cs b/src/ZoneTree/ZoneTreeFactory.cs index 25b3dc0..f1151b2 100644 --- a/src/ZoneTree/ZoneTreeFactory.cs +++ b/src/ZoneTree/ZoneTreeFactory.cs @@ -475,6 +475,20 @@ void FillValueSerializer() new ByteArraySerializer() as ISerializer; } + void LoadInitialSparseArrays(ZoneTree zoneTree) + { + if (InitialSparseArrayLength <= 1) + return; + + var t1 = Task.Run(() => + zoneTree.Maintenance.DiskSegment.InitSparseArray(InitialSparseArrayLength)); + Parallel.ForEach(zoneTree.Maintenance.BottomSegments, (bs) => + { + bs.InitSparseArray(InitialSparseArrayLength); + }); + t1.Wait(); + } + /// /// Opens or creates a ZoneTree. /// @@ -487,13 +501,7 @@ void FillValueSerializer() if (loader.ZoneTreeMetaExists) { var zoneTree = loader.LoadZoneTree(); - var t1 = Task.Run(() => - zoneTree.Maintenance.DiskSegment.InitSparseArray(InitialSparseArrayLength)); - Parallel.ForEach(zoneTree.Maintenance.BottomSegments, (bs) => - { - bs.InitSparseArray(InitialSparseArrayLength); - }); - t1.Wait(); + LoadInitialSparseArrays(zoneTree); return zoneTree; } return new ZoneTree(Options); @@ -526,7 +534,9 @@ void FillValueSerializer() var loader = new ZoneTreeLoader(Options); if (!loader.ZoneTreeMetaExists) throw new DatabaseNotFoundException(); - return loader.LoadZoneTree(); + var zoneTree = loader.LoadZoneTree(); + LoadInitialSparseArrays(zoneTree); + return zoneTree; } /// From d10e0ccbe8b68acf9b8cc5a92217785c65f02b0e Mon Sep 17 00:00:00 2001 From: Ahmed Yasin Koculu Date: Sat, 17 Jun 2023 02:57:21 +0200 Subject: [PATCH 2/3] Add optional startup garbage collection if applicable. --- .../FixedSizeKeyAndValueTests.cs | 35 ++++++++++++++++++ src/ZoneTree/Core/ZoneTreeLoader.cs | 12 ++++-- src/ZoneTree/Core/ZoneTreeMeta.cs | 4 +- src/ZoneTree/Directory.Build.props | 4 +- src/ZoneTree/Options/ZoneTreeOptions.cs | 13 +++++++ .../Segments/InMemory/MutableSegment.cs | 37 ++++++++++++++++++- .../Segments/InMemory/MutableSegmentLoader.cs | 16 ++++++-- 7 files changed, 109 insertions(+), 12 deletions(-) diff --git a/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs b/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs index a49063a..9910610 100644 --- a/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs +++ b/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs @@ -104,6 +104,41 @@ public void IntNullableIntDeleteTest() Assert.That(data.ContainsKey(3), Is.True); } + [Test] + public void IntStringGarbageCollectionTest() + { + var dataPath = "data/IntStringGarbageCollectionTest"; + if (Directory.Exists(dataPath)) + Directory.Delete(dataPath, true); + + // load and populate tree + { + using var data = new ZoneTreeFactory() + .SetDataDirectory(dataPath) + .OpenOrCreate(); + data.TryAtomicAdd(1, "1"); + data.TryAtomicAdd(2, "2"); + data.TryAtomicAdd(3, "3"); + data.TryDelete(2); + Assert.That(data.ContainsKey(1), Is.True); + Assert.That(data.ContainsKey(2), Is.False); + Assert.That(data.ContainsKey(3), Is.True); + Assert.That(data.Maintenance.MutableSegment.Length, Is.EqualTo(3)); + } + + // reload tree and check the length + { + using var data = new ZoneTreeFactory() + .Configure(options => options.EnableSingleSegmentGarbageCollection = true) + .SetDataDirectory(dataPath) + .Open(); + Assert.That(data.ContainsKey(1), Is.True); + Assert.That(data.ContainsKey(2), Is.False); + Assert.That(data.ContainsKey(3), Is.True); + Assert.That(data.Maintenance.MutableSegment.Length, Is.EqualTo(2)); + } + } + [TestCase(true)] [TestCase(false)] public void StringIntTreeTest(bool useSparseArray) diff --git a/src/ZoneTree/Core/ZoneTreeLoader.cs b/src/ZoneTree/Core/ZoneTreeLoader.cs index bf208eb..9e36287 100644 --- a/src/ZoneTree/Core/ZoneTreeLoader.cs +++ b/src/ZoneTree/Core/ZoneTreeLoader.cs @@ -145,11 +145,15 @@ void ValidateSegmentOrder() } } - void LoadMutableSegment(long maximumOpIndex) + void LoadMutableSegment(long maximumOpIndex, + bool collectGarbage) { var loader = new MutableSegmentLoader(Options); MutableSegment = loader - .LoadMutableSegment(ZoneTreeMeta.MutableSegment, maximumOpIndex); + .LoadMutableSegment( + ZoneTreeMeta.MutableSegment, + maximumOpIndex, + collectGarbage); } long LoadReadOnlySegments() @@ -225,13 +229,15 @@ void SetMaximumId() maximumId = bs.Count > 0 ? bs.Max() : 0; SetMaximumSegmentId(maximumId); } + public ZoneTree LoadZoneTree() { LoadZoneTreeMeta(); LoadZoneTreeMetaWAL(); SetMaximumId(); var maximumOpIndex = LoadReadOnlySegments(); - LoadMutableSegment(maximumOpIndex); + bool collectGarbage = Options.EnableSingleSegmentGarbageCollection && !ZoneTreeMeta.HasDiskSegment && ReadOnlySegments.Count == 0; + LoadMutableSegment(maximumOpIndex, collectGarbage); LoadDiskSegment(); LoadBottomSegments(); var zoneTree = new ZoneTree(Options, ZoneTreeMeta, diff --git a/src/ZoneTree/Core/ZoneTreeMeta.cs b/src/ZoneTree/Core/ZoneTreeMeta.cs index 5da88cf..d79e6e5 100644 --- a/src/ZoneTree/Core/ZoneTreeMeta.cs +++ b/src/ZoneTree/Core/ZoneTreeMeta.cs @@ -21,7 +21,7 @@ public sealed class ZoneTreeMeta public int DiskSegmentMaxItemCount { get; set; } = 20_000_000; public WriteAheadLogOptions WriteAheadLogOptions { get; set; } - + public DiskSegmentOptions DiskSegmentOptions { get; set; } public long MutableSegment { get; set; } @@ -31,4 +31,6 @@ public sealed class ZoneTreeMeta public long DiskSegment { get; set; } public IReadOnlyList BottomSegments { get; set; } + + public bool HasDiskSegment => DiskSegment != 0 && BottomSegments?.Count > 0; } diff --git a/src/ZoneTree/Directory.Build.props b/src/ZoneTree/Directory.Build.props index f2b68dd..8520e53 100644 --- a/src/ZoneTree/Directory.Build.props +++ b/src/ZoneTree/Directory.Build.props @@ -5,8 +5,8 @@ Ahmed Yasin Koculu ZoneTree ZoneTree - 1.6.4.0 - 1.6.4.0 + 1.6.5.0 + 1.6.5.0 Ahmed Yasin Koculu ZoneTree ZoneTree is a persistent, high-performance, transactional, ACID-compliant ordered key-value database for NET. It can operate in memory or on local/cloud storage. diff --git a/src/ZoneTree/Options/ZoneTreeOptions.cs b/src/ZoneTree/Options/ZoneTreeOptions.cs index 3c01ad2..d293158 100644 --- a/src/ZoneTree/Options/ZoneTreeOptions.cs +++ b/src/ZoneTree/Options/ZoneTreeOptions.cs @@ -256,6 +256,19 @@ public void Validate() /// public DeleteValueConfigurationValidation DeleteValueConfigurationValidation { get; set; } + /// + /// If ZoneTree contains single segment (which is the mutable segment), + /// there is the opportunity to hard delete the soft deleted values. + /// If enabled, tree performs a garbage collection on load if applicable. + /// + /// + /// + /// If the ZoneTree contains only a single segment (which is the mutable segment), + /// there is an opportunity to perform a hard delete of the soft deleted values. + /// If enabled, the tree performs garbage collection on load if it is applicable. + /// + public bool EnableSingleSegmentGarbageCollection { get; set; } + /// /// Creates default delete delegates for nullable types. /// diff --git a/src/ZoneTree/Segments/InMemory/MutableSegment.cs b/src/ZoneTree/Segments/InMemory/MutableSegment.cs index 1818181..0cc24ab 100644 --- a/src/ZoneTree/Segments/InMemory/MutableSegment.cs +++ b/src/ZoneTree/Segments/InMemory/MutableSegment.cs @@ -68,7 +68,8 @@ public sealed class MutableSegment : IMutableSegment ZoneTreeOptions options, IReadOnlyList keys, IReadOnlyList values, - long nextOpIndex) + long nextOpIndex, + bool collectGarbage) { SegmentId = segmentId; WriteAheadLog = wal; @@ -85,7 +86,16 @@ public sealed class MutableSegment : IMutableSegment MarkValueDeleted = options.MarkValueDeleted; MutableSegmentMaxItemCount = options.MutableSegmentMaxItemCount; - LoadLogEntries(keys, values); + if (collectGarbage) + { + // If there isn't any disk segment and readonly segment, + // it is safe to hard delete the soft deleted values. + LoadLogEntriesWithGarbageCollection(keys, values); + } + else + { + LoadLogEntries(keys, values); + } } void LoadLogEntries(IReadOnlyList keys, IReadOnlyList values) @@ -101,6 +111,29 @@ void LoadLogEntries(IReadOnlyList keys, IReadOnlyList values) } } + void LoadLogEntriesWithGarbageCollection( + IReadOnlyList keys, + IReadOnlyList values) + { + var distinctKeys = + new BTree(Options.Comparer, Collections.BTree.Lock.BTreeLockMode.NoLock); + + var isValueDeleted = Options.IsValueDeleted; + for (var i = keys.Count - 1; i >= 0; --i) + { + var key = keys[i]; + if (distinctKeys.ContainsKey(in key)) + continue; + var value = values[i]; + distinctKeys.Upsert(in key, 1, out _); + if (isValueDeleted(in value)) + { + continue; + } + BTree.Upsert(in key, in value, out var _); + } + } + public bool ContainsKey(in TKey key) { return BTree.ContainsKey(key); diff --git a/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs b/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs index ed45855..d8adbf7 100644 --- a/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs +++ b/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs @@ -14,7 +14,10 @@ public sealed class MutableSegmentLoader Options = options; } - public IMutableSegment LoadMutableSegment(long segmentId, long maximumOpIndex) + public IMutableSegment LoadMutableSegment( + long segmentId, + long maximumOpIndex, + bool collectGarbage) { var wal = Options.WriteAheadLogProvider .GetOrCreateWAL( @@ -40,8 +43,13 @@ public sealed class MutableSegmentLoader } } maximumOpIndex = Math.Max(result.MaximumOpIndex, maximumOpIndex); - return new MutableSegment - (segmentId, wal, Options, result.Keys, - result.Values, maximumOpIndex + 1); + return new MutableSegment( + segmentId, + wal, + Options, + result.Keys, + result.Values, + maximumOpIndex + 1, + collectGarbage); } } From ea5e49e2cb01b9e3d32cb7f11febcf3e74d3cc67 Mon Sep 17 00:00:00 2001 From: Ahmed Yasin Koculu Date: Sat, 17 Jun 2023 03:07:32 +0200 Subject: [PATCH 3/3] Colleect garbage from write ahead log of mutable segment. --- .../FixedSizeKeyAndValueTests.cs | 1 + src/ZoneTree/Core/ZoneTreeLoader.cs | 23 ++++++++++++++++--- src/ZoneTree/Options/ZoneTreeOptions.cs | 6 ----- .../Segments/InMemory/MutableSegmentLoader.cs | 6 +++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs b/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs index 9910610..72454cb 100644 --- a/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs +++ b/src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs @@ -127,6 +127,7 @@ public void IntStringGarbageCollectionTest() } // reload tree and check the length + for (var i = 0; i < 3; ++i) { using var data = new ZoneTreeFactory() .Configure(options => options.EnableSingleSegmentGarbageCollection = true) diff --git a/src/ZoneTree/Core/ZoneTreeLoader.cs b/src/ZoneTree/Core/ZoneTreeLoader.cs index 9e36287..f85fbbf 100644 --- a/src/ZoneTree/Core/ZoneTreeLoader.cs +++ b/src/ZoneTree/Core/ZoneTreeLoader.cs @@ -6,6 +6,7 @@ using Tenray.ZoneTree.Segments.InMemory; using Tenray.ZoneTree.Segments.MultiPart; using Tenray.ZoneTree.Segments.NullDisk; +using Tenray.ZoneTree.WAL; namespace Tenray.ZoneTree.Core; @@ -145,7 +146,7 @@ void ValidateSegmentOrder() } } - void LoadMutableSegment(long maximumOpIndex, + IWriteAheadLog LoadMutableSegment(long maximumOpIndex, bool collectGarbage) { var loader = new MutableSegmentLoader(Options); @@ -153,7 +154,9 @@ void ValidateSegmentOrder() .LoadMutableSegment( ZoneTreeMeta.MutableSegment, maximumOpIndex, - collectGarbage); + collectGarbage, + out var wal); + return wal; } long LoadReadOnlySegments() @@ -237,7 +240,21 @@ void SetMaximumId() SetMaximumId(); var maximumOpIndex = LoadReadOnlySegments(); bool collectGarbage = Options.EnableSingleSegmentGarbageCollection && !ZoneTreeMeta.HasDiskSegment && ReadOnlySegments.Count == 0; - LoadMutableSegment(maximumOpIndex, collectGarbage); + var mutableSegmentWal = LoadMutableSegment(maximumOpIndex, collectGarbage); + if (collectGarbage) + { + var len = MutableSegment.Length; + var keys = new TKey[len]; + var values = new TValue[len]; + var iterator = MutableSegment.GetSeekableIterator(); + var i = 0; + while (iterator.Next()) + { + keys[i] = iterator.CurrentKey; + values[i++] = iterator.CurrentValue; + } + mutableSegmentWal.ReplaceWriteAheadLog(keys, values, true); + } LoadDiskSegment(); LoadBottomSegments(); var zoneTree = new ZoneTree(Options, ZoneTreeMeta, diff --git a/src/ZoneTree/Options/ZoneTreeOptions.cs b/src/ZoneTree/Options/ZoneTreeOptions.cs index d293158..a3ae461 100644 --- a/src/ZoneTree/Options/ZoneTreeOptions.cs +++ b/src/ZoneTree/Options/ZoneTreeOptions.cs @@ -256,12 +256,6 @@ public void Validate() /// public DeleteValueConfigurationValidation DeleteValueConfigurationValidation { get; set; } - /// - /// If ZoneTree contains single segment (which is the mutable segment), - /// there is the opportunity to hard delete the soft deleted values. - /// If enabled, tree performs a garbage collection on load if applicable. - /// - /// /// /// If the ZoneTree contains only a single segment (which is the mutable segment), /// there is an opportunity to perform a hard delete of the soft deleted values. diff --git a/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs b/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs index d8adbf7..d01d7f1 100644 --- a/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs +++ b/src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs @@ -1,6 +1,7 @@ using Tenray.ZoneTree.Exceptions; using Tenray.ZoneTree.Core; using Tenray.ZoneTree.Options; +using Tenray.ZoneTree.WAL; namespace Tenray.ZoneTree.Segments.InMemory; @@ -17,9 +18,10 @@ public sealed class MutableSegmentLoader public IMutableSegment LoadMutableSegment( long segmentId, long maximumOpIndex, - bool collectGarbage) + bool collectGarbage, + out IWriteAheadLog wal) { - var wal = Options.WriteAheadLogProvider + wal = Options.WriteAheadLogProvider .GetOrCreateWAL( segmentId, ZoneTree.SegmentWalCategory,