Browse Source

raft: add log.maybeCommit

Xiang Li 11 years ago
parent
commit
b70be19653
2 changed files with 9 additions and 6 deletions
  1. 8 0
      raft/log.go
  2. 1 6
      raft/raft.go

+ 8 - 0
raft/log.go

@@ -65,6 +65,14 @@ func (l *log) matchTerm(i, term int) bool {
 	return l.ents[i].Term == term
 	return l.ents[i].Term == term
 }
 }
 
 
+func (l *log) maybeCommit(maxIndex, term int) bool {
+	if maxIndex > l.commit && l.term(maxIndex) == term {
+		l.commit = maxIndex
+		return true
+	}
+	return false
+}
+
 func (l *log) nextEnts() (ents []Entry) {
 func (l *log) nextEnts() (ents []Entry) {
 	if l.commit > l.applied {
 	if l.commit > l.applied {
 		ents = l.ents[l.applied+1 : l.commit+1]
 		ents = l.ents[l.applied+1 : l.commit+1]

+ 1 - 6
raft/raft.go

@@ -166,12 +166,7 @@ func (sm *stateMachine) maybeCommit() bool {
 	sort.Sort(sort.Reverse(sort.IntSlice(mis)))
 	sort.Sort(sort.Reverse(sort.IntSlice(mis)))
 	mci := mis[sm.q()-1]
 	mci := mis[sm.q()-1]
 
 
-	if mci > sm.log.commit && sm.log.term(mci) == sm.term {
-		sm.log.commit = mci
-		return true
-	}
-
-	return false
+	return sm.log.maybeCommit(mci, sm.term)
 }
 }
 
 
 // nextEnts returns the appliable entries and updates the applied index
 // nextEnts returns the appliable entries and updates the applied index