Browse Source

raft: move votes into prs

This is purely mechanical. Cleanup deferred to the next commit.
Tobias Schottdorf 6 years ago
parent
commit
26eaadb1d1
3 changed files with 13 additions and 12 deletions
  1. 5 2
      raft/progress.go
  2. 7 9
      raft/raft.go
  3. 1 1
      raft/raft_paper_test.go

+ 5 - 2
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
 // 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.
 // each peer which in turn allows reasoning about the committed index.
 type prs struct {
 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
 	maxInflight int
 	matchBuf    uint64Slice
 	matchBuf    uint64Slice
 }
 }

+ 7 - 9
raft/raft.go

@@ -267,8 +267,6 @@ type raft struct {
 	// isLearner is true if the local raft node is a learner.
 	// isLearner is true if the local raft node is a learner.
 	isLearner bool
 	isLearner bool
 
 
-	votes map[uint64]bool
-
 	msgs []pb.Message
 	msgs []pb.Message
 
 
 	// the leader id
 	// the leader id
@@ -575,7 +573,7 @@ func (r *raft) reset(term uint64) {
 
 
 	r.abortLeaderTransfer()
 	r.abortLeaderTransfer()
 
 
-	r.votes = make(map[uint64]bool)
+	r.prs.votes = make(map[uint64]bool)
 	r.prs.visit(func(id uint64, pr *Progress) {
 	r.prs.visit(func(id uint64, pr *Progress) {
 		*pr = Progress{
 		*pr = Progress{
 			Match:     0,
 			Match:     0,
@@ -683,7 +681,7 @@ func (r *raft) becomePreCandidate() {
 	// but doesn't change anything else. In particular it does not increase
 	// but doesn't change anything else. In particular it does not increase
 	// r.Term or change r.Vote.
 	// r.Term or change r.Vote.
 	r.step = stepCandidate
 	r.step = stepCandidate
-	r.votes = make(map[uint64]bool)
+	r.prs.votes = make(map[uint64]bool)
 	r.tick = r.tickElection
 	r.tick = r.tickElection
 	r.lead = None
 	r.lead = None
 	r.state = StatePreCandidate
 	r.state = StatePreCandidate
@@ -770,10 +768,10 @@ func (r *raft) poll(id uint64, t pb.MessageType, v bool) (granted int) {
 	} else {
 	} else {
 		r.logger.Infof("%x received %s rejection from %x at term %d", r.id, t, id, r.Term)
 		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 {
 		if vv {
 			granted++
 			granted++
 		}
 		}
@@ -1181,7 +1179,7 @@ func stepCandidate(r *raft, m pb.Message) error {
 		r.handleSnapshot(m)
 		r.handleSnapshot(m)
 	case myVoteRespType:
 	case myVoteRespType:
 		gr := r.poll(m.From, m.Type, !m.Reject)
 		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() {
 		switch r.prs.quorum() {
 		case gr:
 		case gr:
 			if r.state == StatePreCandidate {
 			if r.state == StatePreCandidate {
@@ -1190,7 +1188,7 @@ func stepCandidate(r *raft, m pb.Message) error {
 				r.becomeLeader()
 				r.becomeLeader()
 				r.bcastAppend()
 				r.bcastAppend()
 			}
 			}
-		case len(r.votes) - gr:
+		case len(r.prs.votes) - gr:
 			// pb.MsgPreVoteResp contains future term of pre-candidate
 			// pb.MsgPreVoteResp contains future term of pre-candidate
 			// m.Term > r.Term; reuse r.Term
 			// m.Term > r.Term; reuse r.Term
 			r.becomeFollower(r.Term, None)
 			r.becomeFollower(r.Term, None)

+ 1 - 1
raft/raft_paper_test.go

@@ -169,7 +169,7 @@ func testNonleaderStartElection(t *testing.T, state StateType) {
 	if r.state != StateCandidate {
 	if r.state != StateCandidate {
 		t.Errorf("state = %s, want %s", 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")
 		t.Errorf("vote for self = false, want true")
 	}
 	}
 	msgs := r.readMessages()
 	msgs := r.readMessages()