feat: implement strict raft consensus and convergence parity

This commit is contained in:
Joseph Doherty
2026-02-23 14:53:18 -05:00
parent 56177a7099
commit 0413ff6ae9
5 changed files with 116 additions and 3 deletions

View File

@@ -49,12 +49,24 @@ public sealed class RaftNode
TryBecomeLeader(clusterSize);
}
public VoteResponse GrantVote(int term)
public VoteResponse GrantVote(int term, string candidateId = "")
{
if (term < TermState.CurrentTerm)
return new VoteResponse { Granted = false };
TermState.CurrentTerm = term;
if (term > TermState.CurrentTerm)
{
TermState.CurrentTerm = term;
TermState.VotedFor = null;
}
if (!string.IsNullOrEmpty(TermState.VotedFor)
&& !string.Equals(TermState.VotedFor, candidateId, StringComparison.Ordinal))
{
return new VoteResponse { Granted = false };
}
TermState.VotedFor = candidateId;
return new VoteResponse { Granted = true };
}