Prechádzať zdrojové kódy

raft: fix raft node start bug

raft node should set initial prev hard state to empty.
Or it will not send the first hard coded state to application
until the state changes again.

This commit fixs the issue. It introduce a small overhead, that
the same tate might send to application twice when restarting.
But this is fine.
Xiang Li 10 rokov pred
rodič
commit
085447ed85
2 zmenil súbory, kde vykonal 3 pridanie a 3 odobranie
  1. 1 1
      raft/node.go
  2. 2 2
      raft/node_test.go

+ 1 - 1
raft/node.go

@@ -243,7 +243,7 @@ func (n *node) run(r *raft) {
 
 
 	lead := None
 	lead := None
 	prevSoftSt := r.softState()
 	prevSoftSt := r.softState()
-	prevHardSt := r.HardState
+	prevHardSt := emptyState
 
 
 	for {
 	for {
 		if advancec != nil {
 		if advancec != nil {

+ 2 - 2
raft/node_test.go

@@ -362,7 +362,7 @@ func TestNodeRestart(t *testing.T) {
 	st := raftpb.HardState{Term: 1, Commit: 1}
 	st := raftpb.HardState{Term: 1, Commit: 1}
 
 
 	want := Ready{
 	want := Ready{
-		HardState: emptyState,
+		HardState: st,
 		// commit up to index commit index in st
 		// commit up to index commit index in st
 		CommittedEntries: entries[:st.Commit],
 		CommittedEntries: entries[:st.Commit],
 	}
 	}
@@ -405,7 +405,7 @@ func TestNodeRestartFromSnapshot(t *testing.T) {
 	st := raftpb.HardState{Term: 1, Commit: 3}
 	st := raftpb.HardState{Term: 1, Commit: 3}
 
 
 	want := Ready{
 	want := Ready{
-		HardState: emptyState,
+		HardState: st,
 		// commit up to index commit index in st
 		// commit up to index commit index in st
 		CommittedEntries: entries,
 		CommittedEntries: entries,
 	}
 	}