Raft Vote Grant
Raft Vote Grant
In Raft, a follower grants its vote to a candidate if:
candidateTerm >= currentTermvotedForis-1(no vote yet) or equalscandidateID- The candidate's log is at least as up-to-date as the follower's:
- If
candidateLastTerm > localLastTerm, it's up-to-date - If terms equal,
candidateLastIndex >= localLastIndex
- If
You are given the follower's state and the candidate's state. Return whether the follower should grant the vote.
Function signature
func ShouldGrantVote(currentTerm int, votedFor int, candidateID int, candidateTerm int,
candidateLastIndex int, candidateLastTerm int, localLastIndex int, localLastTerm int) bool
Example
currentTerm=3, votedFor=-1
candidateTerm=3, candidateLastTerm=2, candidateLastIndex=8
localLastTerm=2, localLastIndex=7
output = true
Notes
votedFor = -1means no vote cast in the current term.
Run tests to see results
No issues detected