Browse Source

raft: do not reset vote if term is not changed

raft MUST keep the voting information for the same term. reset
should not reset vote if term is not changed.
Xiang Li 10 years ago
parent
commit
7fe608532a
3 changed files with 8 additions and 6 deletions
  1. 2 2
      raft/multinode_test.go
  2. 2 2
      raft/node_test.go
  3. 4 2
      raft/raft.go

+ 2 - 2
raft/multinode_test.go

@@ -255,7 +255,7 @@ func TestMultiNodeStart(t *testing.T) {
 	wants := []Ready{
 	wants := []Ready{
 		{
 		{
 			SoftState: &SoftState{Lead: 1, RaftState: StateLeader},
 			SoftState: &SoftState{Lead: 1, RaftState: StateLeader},
-			HardState: raftpb.HardState{Term: 2, Commit: 2},
+			HardState: raftpb.HardState{Term: 2, Commit: 2, Vote: 1},
 			Entries: []raftpb.Entry{
 			Entries: []raftpb.Entry{
 				{Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
 				{Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
 				{Term: 2, Index: 2},
 				{Term: 2, Index: 2},
@@ -266,7 +266,7 @@ func TestMultiNodeStart(t *testing.T) {
 			},
 			},
 		},
 		},
 		{
 		{
-			HardState:        raftpb.HardState{Term: 2, Commit: 3},
+			HardState:        raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
 			Entries:          []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 			Entries:          []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 			CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 			CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 		},
 		},

+ 2 - 2
raft/node_test.go

@@ -304,7 +304,7 @@ func TestNodeStart(t *testing.T) {
 	wants := []Ready{
 	wants := []Ready{
 		{
 		{
 			SoftState: &SoftState{Lead: 1, RaftState: StateLeader},
 			SoftState: &SoftState{Lead: 1, RaftState: StateLeader},
-			HardState: raftpb.HardState{Term: 2, Commit: 2},
+			HardState: raftpb.HardState{Term: 2, Commit: 2, Vote: 1},
 			Entries: []raftpb.Entry{
 			Entries: []raftpb.Entry{
 				{Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
 				{Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
 				{Term: 2, Index: 2},
 				{Term: 2, Index: 2},
@@ -315,7 +315,7 @@ func TestNodeStart(t *testing.T) {
 			},
 			},
 		},
 		},
 		{
 		{
-			HardState:        raftpb.HardState{Term: 2, Commit: 3},
+			HardState:        raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
 			Entries:          []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 			Entries:          []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 			CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 			CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
 		},
 		},

+ 4 - 2
raft/raft.go

@@ -358,9 +358,11 @@ func (r *raft) maybeCommit() bool {
 }
 }
 
 
 func (r *raft) reset(term uint64) {
 func (r *raft) reset(term uint64) {
-	r.Term = term
+	if r.Term != term {
+		r.Term = term
+		r.Vote = None
+	}
 	r.lead = None
 	r.lead = None
-	r.Vote = None
 	r.elapsed = 0
 	r.elapsed = 0
 	r.votes = make(map[uint64]bool)
 	r.votes = make(map[uint64]bool)
 	for i := range r.prs {
 	for i := range r.prs {