From 4e0291ff91757899cfb3e064e94d9ef816badcaf Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Tue, 23 Jan 2018 15:05:11 -0500 Subject: [PATCH] raft: Clarify conditions for granting votes and prevotes. This includes one theoretical logic change: A node that knows the leader of the current term will no longer grant votes, even if it has not yet voted in this term. It also adds a `m.Type == MsgPreVote` guard on the `m.Term > r.Term` check, which was previously thought to be incorrect (see #8517) but was actually just unclear. Closes #8517 Closes #8571 --- raft/raft.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/raft/raft.go b/raft/raft.go index 0520e7180..cea36940d 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -855,9 +855,14 @@ func (r *raft) Step(m pb.Message) error { r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term) return nil } - // 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) { + // We can vote if this is a repeat of a vote we've already cast... + canVote := r.Vote == m.From || + // ...we haven't voted and we don't think there's a leader yet in this term... + (r.Vote == None && r.lead == None) || + // ...or this is a PreVote for a future term... + (m.Type == pb.MsgPreVote && m.Term > r.Term) + // ...and we believe the candidate is up to date. + if canVote && 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) // When responding to Msg{Pre,}Vote messages we include the term