From 620cde0a57ad6e725d5175595271b828e07c6ebd Mon Sep 17 00:00:00 2001 From: sakno Date: Thu, 4 Jul 2024 20:34:50 +0300 Subject: [PATCH] Repro #244 --- .../Raft/MemoryBasedStateMachineTests.cs | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/DotNext.Tests/Net/Cluster/Consensus/Raft/MemoryBasedStateMachineTests.cs b/src/DotNext.Tests/Net/Cluster/Consensus/Raft/MemoryBasedStateMachineTests.cs index 0c25c15a5..06021eb7b 100644 --- a/src/DotNext.Tests/Net/Cluster/Consensus/Raft/MemoryBasedStateMachineTests.cs +++ b/src/DotNext.Tests/Net/Cluster/Consensus/Raft/MemoryBasedStateMachineTests.cs @@ -1087,11 +1087,43 @@ public static async Task EnsureMetadataPersistence() [Fact] public static async Task RegressionIssue244() { - var entries = new Int64LogEntry[RecordsPerPartition + 1]; + Int64LogEntry snapshot; + long snapshotIndex; + + var entries = new Int64LogEntry[RecordsPerPartition * 2 + 1]; entries.AsSpan().ForEach((ref Int64LogEntry entry, int index) => entry = new Int64LogEntry { Content = 42L + index, Term = index }); var dir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); - using var state = new PersistentStateWithSnapshot(dir, true); - Equal(0L, await state.As().AppendAndCommitAsync(new LogEntryList(entries), 1L, false, entries.Length + 1L)); - Equal(0L, state.LastCommittedEntryIndex); + using (var state = new PersistentStateWithSnapshot(dir, true)) + { + Equal(5L, await state.As().AppendAndCommitAsync(new LogEntryList(entries), 1L, false, 5L)); + Equal(5L, state.LastCommittedEntryIndex); + + // take snapshot + Func, long?, CancellationToken, ValueTask<(Int64LogEntry, long)>> snapshotReader = + static async (entries, snapshotIndex, token) => + { + NotNull(snapshotIndex); + Equal(snapshotIndex, 5L); + + var entry = entries[0]; + var snapshot = await entry.ToByteArrayAsync(token: token); + return (new Int64LogEntry() { Term = entry.Term, IsSnapshot = true, Content = BitConverter.ToInt64(snapshot) }, + snapshotIndex.Value); + }; + (snapshot, snapshotIndex) = await state + .As() + .ReadAsync(new IO.Log.LogEntryConsumer(snapshotReader), 1L); + } + + Directory.Delete(dir, recursive: true); + + using (var state = new PersistentStateWithSnapshot(dir, true)) + { + // install snapshot + await state.AppendAsync(snapshot, snapshotIndex); + Equal(5L, state.LastCommittedEntryIndex); + + await state.AppendAsync(new Int64LogEntry { Content = 10L, Term = 20L }, 6L); + } } } \ No newline at end of file