From f03c3bce05c1e569172c2edc916f201be53c54ec Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Fri, 22 Aug 2014 12:59:34 -0700 Subject: [PATCH] raft: seperate dequeuing from slicing --- raft/log.go | 31 +++++++++++++++++++------------ raft/log_test.go | 1 + raft/raft.go | 4 +++- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/raft/log.go b/raft/log.go index 3f5743a09..54ed1f907 100644 --- a/raft/log.go +++ b/raft/log.go @@ -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 diff --git a/raft/log_test.go b/raft/log_test.go index 46cd8dcaf..5af620b4f 100644 --- a/raft/log_test.go +++ b/raft/log_test.go @@ -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) } diff --git a/raft/raft.go b/raft/raft.go index fa4fb79ee..f550d0570 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -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) {