diff --git a/raft/progress.go b/raft/progress.go index f88cedecc..21c04fcca 100644 --- a/raft/progress.go +++ b/raft/progress.go @@ -291,8 +291,11 @@ func (in *inflights) reset() { // the nodes and learners in it. In particular, it tracks the match index for // each peer which in turn allows reasoning about the committed index. type prs struct { - nodes map[uint64]*Progress - learners map[uint64]*Progress + nodes map[uint64]*Progress + learners map[uint64]*Progress + + votes map[uint64]bool + maxInflight int matchBuf uint64Slice } diff --git a/raft/raft.go b/raft/raft.go index e7f3c0616..d1b623127 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -267,8 +267,6 @@ type raft struct { // isLearner is true if the local raft node is a learner. isLearner bool - votes map[uint64]bool - msgs []pb.Message // the leader id @@ -575,7 +573,7 @@ func (r *raft) reset(term uint64) { r.abortLeaderTransfer() - r.votes = make(map[uint64]bool) + r.prs.votes = make(map[uint64]bool) r.prs.visit(func(id uint64, pr *Progress) { *pr = Progress{ Match: 0, @@ -683,7 +681,7 @@ func (r *raft) becomePreCandidate() { // but doesn't change anything else. In particular it does not increase // r.Term or change r.Vote. r.step = stepCandidate - r.votes = make(map[uint64]bool) + r.prs.votes = make(map[uint64]bool) r.tick = r.tickElection r.lead = None r.state = StatePreCandidate @@ -770,10 +768,10 @@ func (r *raft) poll(id uint64, t pb.MessageType, v bool) (granted int) { } else { r.logger.Infof("%x received %s rejection from %x at term %d", r.id, t, id, r.Term) } - if _, ok := r.votes[id]; !ok { - r.votes[id] = v + if _, ok := r.prs.votes[id]; !ok { + r.prs.votes[id] = v } - for _, vv := range r.votes { + for _, vv := range r.prs.votes { if vv { granted++ } @@ -1181,7 +1179,7 @@ func stepCandidate(r *raft, m pb.Message) error { r.handleSnapshot(m) case myVoteRespType: gr := r.poll(m.From, m.Type, !m.Reject) - r.logger.Infof("%x [quorum:%d] has received %d %s votes and %d vote rejections", r.id, r.prs.quorum(), gr, m.Type, len(r.votes)-gr) + r.logger.Infof("%x [quorum:%d] has received %d %s votes and %d vote rejections", r.id, r.prs.quorum(), gr, m.Type, len(r.prs.votes)-gr) switch r.prs.quorum() { case gr: if r.state == StatePreCandidate { @@ -1190,7 +1188,7 @@ func stepCandidate(r *raft, m pb.Message) error { r.becomeLeader() r.bcastAppend() } - case len(r.votes) - gr: + case len(r.prs.votes) - gr: // pb.MsgPreVoteResp contains future term of pre-candidate // m.Term > r.Term; reuse r.Term r.becomeFollower(r.Term, None) diff --git a/raft/raft_paper_test.go b/raft/raft_paper_test.go index c40f279c2..3b767b4fa 100644 --- a/raft/raft_paper_test.go +++ b/raft/raft_paper_test.go @@ -169,7 +169,7 @@ func testNonleaderStartElection(t *testing.T, state StateType) { if r.state != StateCandidate { t.Errorf("state = %s, want %s", r.state, StateCandidate) } - if !r.votes[r.id] { + if !r.prs.votes[r.id] { t.Errorf("vote for self = false, want true") } msgs := r.readMessages()