Skip to content

Commit

Permalink
Refactor the (*raft)Step for the case m.Term > r.Term
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Feb 20, 2024
1 parent 83d8dec commit 9762825
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,17 +1075,22 @@ func (r *raft) Step(m pb.Message) error {
r.id, last.term, last.index, r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term, r.electionTimeout-r.electionElapsed)
return nil
}
}
switch {
case m.Type == pb.MsgPreVote:
// Never change our term in response to a PreVote
case m.Type == pb.MsgPreVoteResp && !m.Reject:

if m.Type == pb.MsgPreVote {
// Never change our term in response to a PreVote
} else {
// 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 {
// We send pre-vote requests with a term in our future. If the
// pre-vote is granted, we will increment our term when we get a
// quorum. If it is not, the term comes from the node that
// rejected our vote so we should become a follower at the new
// term.
default:
} else {
r.logger.Infof("%x [term: %d] received a %s message with higher term from %x [term: %d]",
r.id, r.Term, m.Type, m.From, m.Term)
if m.Type == pb.MsgApp || m.Type == pb.MsgHeartbeat || m.Type == pb.MsgSnap {
Expand Down

0 comments on commit 9762825

Please sign in to comment.