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 c9af84c
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,13 +1076,9 @@ func (r *raft) Step(m pb.Message) error {
return nil
}

if m.Type == pb.MsgPreVote {
// Never change our term in response to a PreVote
} else {
// m.Type == pb.MsgVote
if 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 1083 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 +1186,11 @@ 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.
if m.Term > r.Term && m.Type == pb.MsgVote {
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 +1227,21 @@ func (r *raft) Step(m pb.Message) error {
r.Vote = m.From
}
} else {
if m.Term > r.Term && m.Type == pb.MsgVote {
// If the local node receives a message with higher term,
// but it doesn't grant the 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 c9af84c

Please sign in to comment.