Browse Source

raft: update unstable when calling stableTo with 0

It should update unstable in this case because it may happen that raft
only writes entry 0 into stable storage.
Yicheng Qin 11 years ago
parent
commit
7dba92dd53
3 changed files with 23 additions and 7 deletions
  1. 0 3
      raft/log.go
  2. 22 1
      raft/log_test.go
  3. 1 3
      raft/node.go

+ 0 - 3
raft/log.go

@@ -134,9 +134,6 @@ func (l *raftLog) appliedTo(i uint64) {
 }
 
 func (l *raftLog) stableTo(i uint64) {
-	if i == 0 {
-		return
-	}
 	l.unstable = i + 1
 }
 

+ 22 - 1
raft/log_test.go

@@ -335,6 +335,7 @@ func TestUnstableEnts(t *testing.T) {
 	}{
 		{3, nil, 3},
 		{1, previousEnts, 3},
+		{0, append([]pb.Entry{{}}, previousEnts...), 3},
 	}
 
 	for i, tt := range tests {
@@ -342,7 +343,9 @@ func TestUnstableEnts(t *testing.T) {
 		raftLog.append(0, previousEnts...)
 		raftLog.unstable = tt.unstable
 		ents := raftLog.unstableEnts()
-		raftLog.stableTo(raftLog.lastIndex())
+		if l := len(ents); l > 0 {
+			raftLog.stableTo(ents[l-1].Index)
+		}
 		if !reflect.DeepEqual(ents, tt.wents) {
 			t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
 		}
@@ -352,6 +355,24 @@ func TestUnstableEnts(t *testing.T) {
 	}
 }
 
+func TestStableTo(t *testing.T) {
+	tests := []struct {
+		stable    uint64
+		wunstable uint64
+	}{
+		{0, 1},
+		{1, 2},
+		{2, 3},
+	}
+	for i, tt := range tests {
+		raftLog := newLog()
+		raftLog.stableTo(tt.stable)
+		if raftLog.unstable != tt.wunstable {
+			t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable, tt.wunstable)
+		}
+	}
+}
+
 //TestCompaction ensures that the number of log entreis is correct after compactions.
 func TestCompaction(t *testing.T) {
 	tests := []struct {

+ 1 - 3
raft/node.go

@@ -298,9 +298,7 @@ func (n *node) run(r *raft) {
 			if prevHardSt.Commit != 0 {
 				r.raftLog.appliedTo(prevHardSt.Commit)
 			}
-			if prevLastUnstablei != 0 {
-				r.raftLog.stableTo(prevLastUnstablei)
-			}
+			r.raftLog.stableTo(prevLastUnstablei)
 			advancec = nil
 		case <-n.done:
 			return