Browse Source

raft: Improve comments and formatting for PreVote change

Ben Darnell 9 years ago
parent
commit
22aa710c1f
3 changed files with 10 additions and 7 deletions
  1. 5 5
      raft/doc.go
  2. 3 2
      raft/raft.go
  3. 2 0
      raft/raft_paper_test.go

+ 5 - 5
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

+ 3 - 2
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)
-
 	}
 }
 

+ 2 - 0
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{{}}})