Merge pull request #25 from xiangli-cmu/master

receive older committed index should not be error
pull/820/head
Ben Johnson 2013-06-27 11:41:49 -07:00
commit a69c3f4478
1 changed files with 21 additions and 5 deletions

26
log.go
View File

@ -237,7 +237,7 @@ func (l *Log) GetEntriesAfter(index uint64) ([]*LogEntry, uint64) {
// Return an error if the index doesn't exist.
if index > (uint64(len(l.entries)) + l.startIndex) {
panic(fmt.Sprintf("raft: Index is beyond end of log: % v%v", len(l.entries), index))
panic(fmt.Sprintf("raft: Index is beyond end of log: %v %v", len(l.entries), index))
}
// If we're going from the beginning of the log then return the whole log.
@ -320,14 +320,30 @@ func (l *Log) SetCommitIndex(index uint64) error {
l.mutex.Lock()
defer l.mutex.Unlock()
// Do not allow previous indices to be committed again.
if index < l.commitIndex {
return fmt.Errorf("raft.Log: Commit index (%d) ahead of requested commit index (%d)", l.commitIndex, index)
}
if index > l.startIndex+uint64(len(l.entries)) {
return fmt.Errorf("raft.Log: Commit index (%d) out of range (%d)", index, len(l.entries))
}
// Do not allow previous indices to be committed again.
// This could happens, since the guarantee is that the new leader has up-to-dated
// log entires rather than has most up-to-dated committed index
// For example, Leader 1 send log 80 to follower 2 and follower 3
// follower 2 and follow 3 all got the new entries and reply
// leader 1 committed entry 80 and send reply to follower 2 and follower3
// follower 2 receive the new committed index and update committed index to 80
// leader 1 fail to send the committed index to follower 3
// follower 3 promote to leader (server 1 and server 2 will vote, since leader 3
// has up-to-dated the entries)
// when new leader 3 send heartbeat with committed index = 0 to follower 2,
// follower 2 should reply success and let leader 3 update the committed index to 80
if index < l.commitIndex {
return nil
}
// Find all entries whose index is between the previous index and the current index.
for i := l.commitIndex + 1; i <= index; i++ {
entryIndex := i - 1 - l.startIndex