Raft Milestone: Follower RPC Handling
Raft Milestone: Follower RPC Handling
Implement a simplified Raft follower that can handle RequestVote and AppendEntries RPCs. The follower maintains:
CurrentTermVotedFor(candidate ID or-1)LogTerms(terms per entry, 1-based indexing)CommitIndex(1-based)
You must update the follower state and return an RPC response.
Types
type State struct {
CurrentTerm int
VotedFor int
LogTerms []int
CommitIndex int
}
type RequestVote struct {
Term int
CandidateID int
LastLogIndex int
LastLogTerm int
}
type AppendEntries struct {
Term int
LeaderID int
PrevLogIndex int
PrevLogTerm int
Entries []int
LeaderCommit int
}
type RPC struct {
Kind string // "vote" or "append"
Vote RequestVote
Append AppendEntries
}
type Response struct {
Kind string
Term int
VoteGranted bool
AppendSuccess bool
MatchIndex int
}
Function signature
func HandleRPC(state State, rpc RPC) (State, Response)
Rules
Term handling
- If
rpc.Term > state.CurrentTerm, updateCurrentTermand setVotedFor = -1.
RequestVote Grant the vote iff:
rpc.Term >= state.CurrentTermstate.VotedFor == -1orstate.VotedFor == rpc.CandidateID- Candidate log is at least as up-to-date as follower log:
candidateLastTerm > localLastTerm, or- terms equal and
candidateLastIndex >= localLastIndex
If granted, set VotedFor = CandidateID.
AppendEntries Accept iff:
rpc.Term >= state.CurrentTermPrevLogIndex == 0, orPrevLogIndex <= len(LogTerms)andLogTerms[PrevLogIndex-1] == PrevLogTerm
On accept:
- Truncate log to
PrevLogIndex, then appendEntries. - Set
CommitIndex = min(LeaderCommit, len(LogTerms)). - Return
AppendSuccess = trueandMatchIndex = len(LogTerms).
On reject, the log must remain unchanged.
Notes
- Indices are 1-based. If the log is empty,
LastLogIndex=0,LastLogTerm=0.
Run tests to see results
No issues detected