From 22aa710c1f36743452dad4b017f8e8a9cf4f0076 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Mon, 24 Oct 2016 22:29:33 +0900 Subject: [PATCH] raft: Improve comments and formatting for PreVote change --- raft/doc.go | 10 +++++----- raft/raft.go | 5 +++-- raft/raft_paper_test.go | 2 ++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/raft/doc.go b/raft/doc.go index 43fc8e204..b55c591ff 100644 --- a/raft/doc.go +++ b/raft/doc.go @@ -257,11 +257,11 @@ stale log entries: If candidate receives majority of votes of denials, it reverts back to follower. - 'MsgPreVote' and 'MsgPreVoteResp' are used in an optional two-phase election - protocol. When Config.PreVote is true, a pre-election is carried out first - (using the same rules as a regular election, and no node increases its term - number unless the pre-election indicates that the campaigining node would win. - This minimizes disruption when a partitioned node rejoins the cluster. + 'MsgPreVote' and 'MsgPreVoteResp' are used in an optional two-phase election + protocol. When Config.PreVote is true, a pre-election is carried out first + (using the same rules as a regular election), and no node increases its term + number unless the pre-election indicates that the campaigining node would win. + This minimizes disruption when a partitioned node rejoins the cluster. 'MsgSnap' requests to install a snapshot message. When a node has just become a leader or the leader receives 'MsgProp' message, it calls diff --git a/raft/raft.go b/raft/raft.go index 34898ffd4..13f62da24 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -697,7 +697,7 @@ func (r *raft) Step(m pb.Message) error { } switch { case m.Type == pb.MsgPreVote: - // Never change our term in response to a PreVote + // Never change our term in response to a PreVote case 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 @@ -757,6 +757,8 @@ func (r *raft) Step(m pb.Message) error { } case pb.MsgVote, pb.MsgPreVote: + // The m.Term > r.Term clause is for MsgPreVote. For MsgVote m.Term should + // always equal r.Term. if (r.Vote == None || m.Term > r.Term || r.Vote == m.From) && r.raftLog.isUpToDate(m.Index, m.LogTerm) { r.logger.Infof("%x [logterm: %d, index: %d, vote: %x] cast %s for %x [logterm: %d, index: %d] at term %d", r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term) @@ -1015,7 +1017,6 @@ func stepCandidate(r *raft, m pb.Message) { } case pb.MsgTimeoutNow: r.logger.Debugf("%x [term %d state %v] ignored MsgTimeoutNow from %x", r.id, r.Term, r.state, m.From) - } } diff --git a/raft/raft_paper_test.go b/raft/raft_paper_test.go index 4f7f1255a..2911e8aa3 100644 --- a/raft/raft_paper_test.go +++ b/raft/raft_paper_test.go @@ -757,6 +757,8 @@ func TestLeaderSyncFollowerLog(t *testing.T) { // first node needs the vote from the third node to become the leader. n := newNetwork(lead, follower, nopStepper) n.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) + // The election occurs in the term after the one we loaded with + // lead.loadState above. n.send(pb.Message{From: 3, To: 1, Type: pb.MsgVoteResp, Term: term + 1}) n.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{}}})