raft: add log.maybeAppend

This commit is contained in:
Xiang Li 2014-05-28 08:41:52 -07:00 committed by Yicheng Qin
parent b70be19653
commit bee9d8bea5
2 changed files with 3 additions and 4 deletions

View File

@ -19,9 +19,10 @@ func newLog() *log {
}
}
func (l *log) maybeAppend(index, logTerm int, ents ...Entry) bool {
func (l *log) maybeAppend(index, logTerm, commit int, ents ...Entry) bool {
if l.matchTerm(index, logTerm) {
l.append(index, ents...)
l.commit = commit
return true
}
return false

View File

@ -263,9 +263,7 @@ func (sm *stateMachine) Step(m Message) {
}
handleAppendEntries := func() {
if sm.log.matchTerm(m.Index, m.LogTerm) {
sm.log.commit = m.Commit
sm.log.append(m.Index, m.Entries...)
if sm.log.maybeAppend(m.Index, m.LogTerm, m.Commit, m.Entries...) {
sm.send(Message{To: m.From, Type: msgAppResp, Index: sm.log.lastIndex()})
} else {
sm.send(Message{To: m.From, Type: msgAppResp, Index: -1})