Raft Vote Grant

medium · distributed-systems, consensus, raft

Raft Vote Grant

In Raft, a follower grants its vote to a candidate if:

  1. candidateTerm >= currentTerm
  2. votedFor is -1 (no vote yet) or equals candidateID
  3. 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

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 = -1 means no vote cast in the current term.
Run tests to see results
No issues detected