raft: return offset for unstableEnts

This commit is contained in:
Yicheng Qin 2014-07-25 12:03:03 -07:00
parent 311db876b0
commit e850c644da
3 changed files with 16 additions and 7 deletions

View File

@ -84,10 +84,11 @@ func (l *raftLog) findConflict(from int64, ents []Entry) int64 {
return -1 return -1
} }
func (l *raftLog) unstableEnts() []Entry { func (l *raftLog) unstableEnts() (int64, []Entry) {
offset := l.unstable
ents := l.entries(l.unstable) ents := l.entries(l.unstable)
l.unstable = l.lastIndex() + 1 l.unstable = l.lastIndex() + 1
return ents return offset, ents
} }
func (l *raftLog) lastIndex() int64 { func (l *raftLog) lastIndex() int64 {

View File

@ -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 { if g := len(unstableEnts); g != 500 {
t.Errorf("len(unstableEntries) = %d, want = %d", 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}} previousEnts := []Entry{{Term: 1}, {Term: 2}}
tests := []struct { tests := []struct {
unstable int64 unstable int64
woffset int64
wents []Entry wents []Entry
wunstable int64 wunstable int64
}{ }{
{3, nil, 3}, {3, 3, nil, 3},
{1, []Entry{{Term: 1}, {Term: 2}}, 3}, {1, 1, []Entry{{Term: 1}, {Term: 2}}, 3},
} }
for i, tt := range tests { for i, tt := range tests {
raftLog := newLog() raftLog := newLog()
raftLog.ents = append(raftLog.ents, previousEnts...) raftLog.ents = append(raftLog.ents, previousEnts...)
raftLog.unstable = tt.unstable 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) { if !reflect.DeepEqual(ents, tt.wents) {
t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents) t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
} }

View File

@ -206,7 +206,8 @@ func (n *Node) UpdateConf(t int64, c *Config) {
} }
// UnstableEnts retuens all the entries that need to be persistent. // 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() return n.sm.raftLog.unstableEnts()
} }