From 97628258b12b3684de0cd3af11bf3998bbb92c97 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Tue, 20 Feb 2024 14:41:56 +0000 Subject: [PATCH] Refactor the (*raft)Step for the case m.Term > r.Term Signed-off-by: Benjamin Wang --- raft.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/raft.go b/raft.go index 3357ae45..5b3c7fac 100644 --- a/raft.go +++ b/raft.go @@ -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 {