Browse Source

raft: seperate dequeuing from slicing

Blake Mizerany 11 years ago
parent
commit
f03c3bce05
3 changed files with 23 additions and 13 deletions
  1. 19 12
      raft/log.go
  2. 1 0
      raft/log_test.go
  3. 3 1
      raft/raft.go

+ 19 - 12
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

+ 1 - 0
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)
 		}

+ 3 - 1
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) {