From 368a7ff14767c2abefc5b257074e4c4cb8a75659 Mon Sep 17 00:00:00 2001 From: sakno Date: Thu, 2 May 2024 15:04:40 +0300 Subject: [PATCH] Avoid transfer of snapshot log entry for the minority of the cluster (see #232) --- .../Net/Cluster/Consensus/Raft/LeaderState.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs index bb5e33bb0..81abd5c43 100644 --- a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs +++ b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs @@ -169,24 +169,28 @@ private async Task DoHeartbeats(TimeSpan period, IAuditTrail audi { RenewLease(startTime.Elapsed); UpdateLeaderStickiness(); - await configurationStorage.ApplyAsync(Token).ConfigureAwait(false); } - if (result.Value && ++commitQuorum == majority) - { - // majority of nodes accept entries with at least one entry from the current term - var count = await auditTrail.CommitAsync(currentIndex, Token).ConfigureAwait(false); // commit all entries starting from the first uncommitted index to the end - Logger.CommitSuccessful(currentIndex, count); - } + commitQuorum += Unsafe.BitCast(result.Value); } } - if (commitQuorum < majority) + if (commitQuorum >= majority) + { + // majority of nodes accept entries with at least one entry from the current term + var count = await auditTrail.CommitAsync(currentIndex, Token).ConfigureAwait(false); // commit all entries starting from the first uncommitted index to the end + Logger.CommitSuccessful(currentIndex, count); + } + else { Logger.CommitFailed(quorum, commitIndex); } - if (quorum < majority) + if (quorum >= majority) + { + await configurationStorage.ApplyAsync(Token).ConfigureAwait(false); + } + else { MoveToFollowerState(randomizeTimeout: false); return;