Browse Source

raft: leader updates its own match; tries to commit after a prop

Xiang Li 11 years ago
parent
commit
c32d34166e
2 changed files with 14 additions and 0 deletions
  1. 2 0
      raft/raft.go
  2. 12 0
      raft/raft_test.go

+ 2 - 0
raft/raft.go

@@ -258,6 +258,8 @@ func (sm *stateMachine) Step(m Message) {
 		switch sm.lead {
 		case sm.addr:
 			sm.log.append(sm.log.lastIndex(), Entry{Term: sm.term, Data: m.Data})
+			sm.ins[sm.addr].update(sm.log.lastIndex())
+			sm.log.maybeCommit(sm.log.lastIndex(), sm.term)
 			sm.bcastAppend()
 		case none:
 			panic("msgProp given without leader")

+ 12 - 0
raft/raft_test.go

@@ -112,6 +112,18 @@ func TestLogReplication(t *testing.T) {
 	}
 }
 
+func TestSingleNodeCommit(t *testing.T) {
+	tt := newNetwork(nil)
+	tt.Step(Message{To: 0, Type: msgHup})
+	tt.Step(Message{To: 0, Type: msgProp, Data: []byte("some data")})
+	tt.Step(Message{To: 0, Type: msgProp, Data: []byte("some data")})
+
+	sm := tt.ss[0].(*nsm)
+	if sm.log.committed != 2 {
+		t.Errorf("committed = %d, want %d", sm.log.committed, 2)
+	}
+}
+
 func TestDualingCandidates(t *testing.T) {
 	a := &nsm{stateMachine{log: defaultLog()}, nil}
 	c := &nsm{stateMachine{log: defaultLog()}, nil}