diff --git a/raft/log.go b/raft/log.go index a6b1fa452..0269a4094 100644 --- a/raft/log.go +++ b/raft/log.go @@ -84,10 +84,11 @@ func (l *raftLog) findConflict(from int64, ents []Entry) int64 { return -1 } -func (l *raftLog) unstableEnts() []Entry { +func (l *raftLog) unstableEnts() (int64, []Entry) { + offset := l.unstable ents := l.entries(l.unstable) l.unstable = l.lastIndex() + 1 - return ents + return offset, ents } func (l *raftLog) lastIndex() int64 { diff --git a/raft/log_test.go b/raft/log_test.go index a8bbd7736..0fb12d106 100644 --- a/raft/log_test.go +++ b/raft/log_test.go @@ -98,7 +98,10 @@ func TestCompactionSideEffects(t *testing.T) { } } - unstableEnts := raftLog.unstableEnts() + offset, unstableEnts := raftLog.unstableEnts() + if offset != 501 { + t.Errorf("offset(unstableEntries) = %d, want = %d", offset, 500) + } if g := len(unstableEnts); g != 500 { t.Errorf("len(unstableEntries) = %d, want = %d", g, 500) } @@ -119,18 +122,22 @@ func TestUnstableEnts(t *testing.T) { previousEnts := []Entry{{Term: 1}, {Term: 2}} tests := []struct { unstable int64 + woffset int64 wents []Entry wunstable int64 }{ - {3, nil, 3}, - {1, []Entry{{Term: 1}, {Term: 2}}, 3}, + {3, 3, nil, 3}, + {1, 1, []Entry{{Term: 1}, {Term: 2}}, 3}, } for i, tt := range tests { raftLog := newLog() raftLog.ents = append(raftLog.ents, previousEnts...) raftLog.unstable = tt.unstable - ents := raftLog.unstableEnts() + offset, ents := raftLog.unstableEnts() + if offset != tt.woffset { + t.Errorf("#%d: offset = %d, want = %d", i, offset, tt.woffset) + } if !reflect.DeepEqual(ents, tt.wents) { t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents) } diff --git a/raft/node.go b/raft/node.go index f2711f792..da2c97d7b 100644 --- a/raft/node.go +++ b/raft/node.go @@ -206,7 +206,8 @@ func (n *Node) UpdateConf(t int64, c *Config) { } // UnstableEnts retuens all the entries that need to be persistent. -func (n *Node) UnstableEnts() []Entry { +// The first return value is offset, and the second one is unstable entries. +func (n *Node) UnstableEnts() (int64, []Entry) { return n.sm.raftLog.unstableEnts() }