raft: add log.maybeCommit

This commit is contained in:
Xiang Li
2014-05-28 08:24:09 -07:00
committed by Yicheng Qin
parent 092461d7c8
commit b70be19653
2 changed files with 9 additions and 6 deletions

View File

@@ -65,6 +65,14 @@ func (l *log) matchTerm(i, term int) bool {
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) {
if l.commit > l.applied {
ents = l.ents[l.applied+1 : l.commit+1]

View File

@@ -166,12 +166,7 @@ func (sm *stateMachine) maybeCommit() bool {
sort.Sort(sort.Reverse(sort.IntSlice(mis)))
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