raft: seperate dequeuing from slicing

This commit is contained in:
Blake Mizerany 2014-08-22 12:59:34 -07:00 committed by Yicheng Qin
parent e17f79ee70
commit f03c3bce05
3 changed files with 23 additions and 13 deletions

View File

@ -85,9 +85,26 @@ func (l *raftLog) findConflict(from int64, ents []Entry) int64 {
}
func (l *raftLog) unstableEnts() []Entry {
ents := l.entries(l.unstable)
return l.entries(l.unstable)
}
func (l *raftLog) resetUnstable() {
l.unstable = l.lastIndex() + 1
return ents
}
// nextEnts returns all the available entries for execution.
// all the returned entries will be marked as applied.
func (l *raftLog) nextEnts() (ents []Entry) {
if l.committed > l.applied {
return l.slice(l.applied+1, l.committed+1)
}
return nil
}
func (l *raftLog) resetNextEnts() {
if l.committed > l.applied {
l.applied = l.committed
}
}
func (l *raftLog) lastIndex() int64 {
@ -131,16 +148,6 @@ func (l *raftLog) maybeCommit(maxIndex, term int64) bool {
return false
}
// nextEnts returns all the available entries for execution.
// all the returned entries will be marked as applied.
func (l *raftLog) nextEnts() (ents []Entry) {
if l.committed > l.applied {
ents = l.slice(l.applied+1, l.committed+1)
l.applied = l.committed
}
return ents
}
// compact compacts all log entries until i.
// It removes the log entries before i, exclusive.
// i must be not smaller than the index of the first entry

View File

@ -134,6 +134,7 @@ func TestUnstableEnts(t *testing.T) {
raftLog.ents = append(raftLog.ents, previousEnts...)
raftLog.unstable = tt.unstable
ents := raftLog.unstableEnts()
raftLog.resetUnstable()
if !reflect.DeepEqual(ents, tt.wents) {
t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
}

View File

@ -273,7 +273,9 @@ func (sm *stateMachine) maybeCommit() bool {
// nextEnts returns the appliable entries and updates the applied index
func (sm *stateMachine) nextEnts() (ents []Entry) {
return sm.raftLog.nextEnts()
ents = sm.raftLog.nextEnts()
sm.raftLog.resetNextEnts()
return ents
}
func (sm *stateMachine) reset(term int64) {