raft: Allow an election immediately after start with checkQuorum

Previously, the checkQuorum flag required an election timeout to
expire before a node could cast its first vote. This change permits
the node to cast a vote at any time when the leader is not known,
including immediately after startup.
This commit is contained in:
Ben Darnell 2016-08-29 05:35:19 +08:00
parent f4c30425c0
commit a7a867c1e6
2 changed files with 3 additions and 5 deletions

View File

@ -598,7 +598,7 @@ func (r *raft) Step(m pb.Message) error {
lead := m.From
if m.Type == pb.MsgVote {
force := bytes.Equal(m.Context, []byte(campaignTransfer))
inLease := r.checkQuorum && r.state != StateCandidate && r.electionElapsed < r.electionTimeout
inLease := r.checkQuorum && r.lead != None && r.electionElapsed < r.electionTimeout
if !force && inLease {
// If a server receives a RequestVote request within the minimum election timeout
// of hearing from a current leader, it does not update its term or grant its vote

View File

@ -1281,10 +1281,8 @@ func TestLeaderElectionWithCheckQuorum(t *testing.T) {
setRandomizedElectionTimeout(a, a.electionTimeout+1)
setRandomizedElectionTimeout(b, b.electionTimeout+2)
// Letting b's electionElapsed reach to timeout so that it can vote for a
for i := 0; i < b.electionTimeout; i++ {
b.tick()
}
// Immediately after creation, votes are cast regardless of the
// election timeout.
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
if a.state != StateLeader {