Skip to content

Commit

Permalink
raft: cherry pick of #8334 to release-3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
lishuai87 authored and sbenderli committed Mar 21, 2019
1 parent c696442 commit ec22eb9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ func (r *raft) becomePreCandidate() {
r.step = stepCandidate
r.votes = make(map[uint64]bool)
r.tick = r.tickElection
r.lead = None
r.state = StatePreCandidate
r.logger.Infof("%x became pre-candidate at term %d", r.id, r.Term)
}
Expand Down
49 changes: 49 additions & 0 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3562,6 +3562,55 @@ func TestPreVoteWithSplitVote(t *testing.T) {
}
}

// TestPreVoteWithCheckQuorum ensures that after a node become pre-candidate,
// it will checkQuorum correctly.
func TestPreVoteWithCheckQuorum(t *testing.T) {
n1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
n2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
n3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())

n1.becomeFollower(1, None)
n2.becomeFollower(1, None)
n3.becomeFollower(1, None)

n1.preVote = true
n2.preVote = true
n3.preVote = true

n1.checkQuorum = true
n2.checkQuorum = true
n3.checkQuorum = true

nt := newNetwork(n1, n2, n3)
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup})

// isolate node 1. node 2 and node 3 have leader info
nt.isolate(1)

// check state
sm := nt.peers[1].(*raft)
if sm.state != StateLeader {
t.Fatalf("peer 1 state: %s, want %s", sm.state, StateLeader)
}
sm = nt.peers[2].(*raft)
if sm.state != StateFollower {
t.Fatalf("peer 2 state: %s, want %s", sm.state, StateFollower)
}
sm = nt.peers[3].(*raft)
if sm.state != StateFollower {
t.Fatalf("peer 3 state: %s, want %s", sm.state, StateFollower)
}

// node 2 will ignore node 3's PreVote
nt.send(pb.Message{From: 3, To: 3, Type: pb.MsgHup})
nt.send(pb.Message{From: 2, To: 2, Type: pb.MsgHup})

// Do we have a leader?
if n2.state != StateLeader && n3.state != StateFollower {
t.Errorf("no leader")
}
}

func entsWithConfig(configFunc func(*Config), terms ...uint64) *raft {
storage := NewMemoryStorage()
for i, term := range terms {
Expand Down

0 comments on commit ec22eb9

Please sign in to comment.