Skip to content

Commit

Permalink
Do not reset the electionElapsed if the node doesn't grant vote or pr…
Browse files Browse the repository at this point in the history
…eVote

Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Feb 20, 2024
1 parent 9762825 commit 00f5d2e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,6 @@ func (r *raft) Step(m pb.Message) error {
// m.Type == pb.MsgVote
r.logger.Infof("%x [term: %d] received a pb.MsgVote message with higher term from %x [term: %d]",
r.id, r.Term, m.From, m.Term)
r.becomeFollower(m.Term, None)
}
} else if m.Type == pb.MsgPreVoteResp && !m.Reject {

Check warning on line 1086 in raft.go

View workflow job for this annotation

GitHub Actions / run

empty-block: this block is empty, you can remove it (revive)
// We send pre-vote requests with a term in our future. If the
Expand Down Expand Up @@ -1190,6 +1189,9 @@ func (r *raft) Step(m pb.Message) error {
lastID := r.raftLog.lastEntryID()
candLastID := entryID{term: m.LogTerm, index: m.Index}
if canVote && r.raftLog.isUpToDate(candLastID) {
// The local node should turn into a follower as long as it
// grants the vote (or pre-vote).
r.becomeFollower(m.Term, None)
// Note: it turns out that that learners must be allowed to cast votes.
// This seems counter- intuitive but is necessary in the situation in which
// a learner has been promoted (i.e. is now a voter) but has not learned
Expand Down Expand Up @@ -1226,6 +1228,21 @@ func (r *raft) Step(m pb.Message) error {
r.Vote = m.From
}
} else {
if m.Term > r.Term {
// If the local node receives a message with higher term,
// but it doesn't grant the vote (or pre-vote); it turns
// into a follower, but it shouldn't reset the electionElapsed,
// to ensure it has higher priority to start a campaign
// in the next round of election. If we reject a node, it's
// highly likely we will reject it again if it immediately
// campaigns again. So it may waste a long time to elect
// a leader if we reset the electionElapsed.
bakElectionElapsed, bakRandomizedElectionTimeout := r.electionElapsed, r.randomizedElectionTimeout
r.becomeFollower(m.Term, None)
r.electionElapsed = bakElectionElapsed
r.randomizedElectionTimeout = bakRandomizedElectionTimeout
}

r.logger.Infof("%x [logterm: %d, index: %d, vote: %x] rejected %s from %x [logterm: %d, index: %d] at term %d",
r.id, lastID.term, lastID.index, r.Vote, m.Type, m.From, candLastID.term, candLastID.index, r.Term)
r.send(pb.Message{To: m.From, Term: r.Term, Type: voteRespMsgType(m.Type), Reject: true})
Expand Down

0 comments on commit 00f5d2e

Please sign in to comment.