Skip to content

Commit

Permalink
Ignore outdated ae reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenMathew committed Oct 30, 2024
1 parent 7a85eae commit 211c5e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions toy-raft/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,14 @@ func (rn *RaftNodeImpl) handleAppendEntriesRequest(appendEntriesRequest *AppendE
return
}

// NOTE: optimization to ignore outdated requests below commit index
if appendEntriesRequest.PrevLogIdx < rn.commitIndex {
// we've matched at least up to the entries we've already achieved quorum on
resp.MatchIndex = rn.commitIndex
rn.SendMessage(appendEntriesRequest.LeaderId, resp)
return
}

// check if log state is consistent with leader
if appendEntriesRequest.PrevLogTerm != NoPreviousTerm {
// no entry exists
Expand Down
6 changes: 3 additions & 3 deletions toy-raft/raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,14 +712,14 @@ func TestFollowerHandleAppendEntries(t *testing.T) {

// TODO: remove these vars and all like it, use req struct instead
var (
prevLogEntryIdx uint64 = 2
prevLogEntryTerm uint64 = 1
prevLogEntryIdx uint64 = 3
prevLogEntryTerm uint64 = 2
)
reqTerm = 2
duplicateAndNewAEReq := &AppendEntriesRequest{
Term: reqTerm,
LeaderId: leaderId,
Entries: []Entry{{Term: 2, Cmd: []byte("baz")}, {Term: 2, Cmd: []byte("faz")}},
Entries: []Entry{{Term: 2, Cmd: []byte("faz")}},
PrevLogIdx: prevLogEntryIdx,
PrevLogTerm: prevLogEntryTerm,
LeaderCommitIdx: leaderCommit,
Expand Down

0 comments on commit 211c5e9

Please sign in to comment.