From 7dba92dd534af6afbf083fa0b0b33d41dc205541 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Tue, 11 Nov 2014 16:10:03 -0800 Subject: [PATCH] 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. --- raft/log.go | 3 --- raft/log_test.go | 23 ++++++++++++++++++++++- raft/node.go | 4 +--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/raft/log.go b/raft/log.go index 496a1043c..49666c80c 100644 --- a/raft/log.go +++ b/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 } diff --git a/raft/log_test.go b/raft/log_test.go index 9ea3aba51..9654d76aa 100644 --- a/raft/log_test.go +++ b/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 { diff --git a/raft/node.go b/raft/node.go index df299c56a..c1b0ce8c7 100644 --- a/raft/node.go +++ b/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