Skip to content

Commit fd65f6c

Browse files
committed
Ensure that the appliedIndex is changed before the trigger
1 parent bb4415c commit fd65f6c

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/StateMachine/WriteAheadLog.Applier.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private async Task ApplyAsync(CancellationToken token)
3131
await lockManager.AcquireReadLockAsync(token).ConfigureAwait(false);
3232
try
3333
{
34-
await ApplyAsync(appliedIndex + 1L, newIndex, token).ConfigureAwait(false);
34+
await ApplyAsync(LastAppliedIndex + 1L, newIndex, token).ConfigureAwait(false);
3535
}
3636
catch (Exception e) when (e is not OperationCanceledException canceledEx || canceledEx.CancellationToken != token)
3737
{
@@ -54,9 +54,11 @@ private async Task ApplyAsync(CancellationToken token)
5454
public long LastAppliedIndex
5555
{
5656
get => Volatile.Read(in appliedIndex);
57+
[MethodImpl(MethodImplOptions.NoInlining)]
5758
private set
5859
{
59-
Volatile.Write(ref appliedIndex, value);
60+
appliedIndex = value;
61+
Interlocked.MemoryBarrier();
6062
appliedEvent.Signal(resumeAll: true);
6163
}
6264
}

src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/StateMachine/WriteAheadLog.Flusher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private long Commit(long index)
169169
return index - oldCommitIndex;
170170
}
171171

172-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
172+
[MethodImpl(MethodImplOptions.NoInlining)]
173173
private void OnCommitted(long count)
174174
{
175175
if (flushOnCommit)

src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/StateMachine/WriteAheadLog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public async ValueTask AppendAsync<TEntry>(TEntry entry, long startIndex, Cancel
255255
if (startIndex <= LastCommittedEntryIndex)
256256
throw new InvalidOperationException(ExceptionMessages.InvalidAppendIndex);
257257

258-
appliedIndex = await stateMachine.ApplyAsync(new LogEntry(entry, startIndex), token).ConfigureAwait(false);
258+
LastAppliedIndex = await stateMachine.ApplyAsync(new LogEntry(entry, startIndex), token).ConfigureAwait(false);
259259
var snapshotIndex = stateMachine.Snapshot?.Index ?? startIndex;
260260
LastEntryIndex = long.Max(tailIndex, LastCommittedEntryIndex = snapshotIndex);
261261
}

0 commit comments

Comments
 (0)