Browse Source

raft: commitIndex=min(leaderCommit, index of last new entry)

Xiang Li 11 years ago
parent
commit
b3c1bd5616
1 changed files with 5 additions and 2 deletions
  1. 5 2
      raft/log.go

+ 5 - 2
raft/log.go

@@ -47,6 +47,7 @@ func (l *raftLog) String() string {
 }
 }
 
 
 func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...pb.Entry) bool {
 func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...pb.Entry) bool {
+	lastnewi := index + int64(len(ents))
 	if l.matchTerm(index, logTerm) {
 	if l.matchTerm(index, logTerm) {
 		from := index + 1
 		from := index + 1
 		ci := l.findConflict(from, ents)
 		ci := l.findConflict(from, ents)
@@ -57,8 +58,10 @@ func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...pb.Entry)
 		default:
 		default:
 			l.append(ci-1, ents[ci-from:]...)
 			l.append(ci-1, ents[ci-from:]...)
 		}
 		}
-		if l.committed < committed {
-			l.committed = min(committed, l.lastIndex())
+		tocommit := min(committed, lastnewi)
+		// if toCommit > commitIndex, set commitIndex = toCommit
+		if l.committed < tocommit {
+			l.committed = tocommit
 		}
 		}
 		return true
 		return true
 	}
 	}