Skip to content

Commit

Permalink
raft: move votes into prs
Browse files Browse the repository at this point in the history
This is purely mechanical. Cleanup deferred to the next commit.
  • Loading branch information
tbg committed May 21, 2019
1 parent a115637 commit 26eaadb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
7 changes: 5 additions & 2 deletions raft/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 7 additions & 9 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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++
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion raft/raft_paper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 26eaadb

Please sign in to comment.