receive older committed index should not be error
parent
938b649b08
commit
9597f80857
27
log.go
27
log.go
|
@ -237,7 +237,7 @@ func (l *Log) GetEntriesAfter(index uint64) ([]*LogEntry, uint64) {
|
||||||
|
|
||||||
// Return an error if the index doesn't exist.
|
// Return an error if the index doesn't exist.
|
||||||
if index > (uint64(len(l.entries)) + l.startIndex) {
|
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.
|
// 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()
|
l.mutex.Lock()
|
||||||
defer l.mutex.Unlock()
|
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)) {
|
if index > l.startIndex+uint64(len(l.entries)) {
|
||||||
return fmt.Errorf("raft.Log: Commit index (%d) out of range (%d)", index, 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.
|
// Find all entries whose index is between the previous index and the current index.
|
||||||
for i := l.commitIndex + 1; i <= index; i++ {
|
for i := l.commitIndex + 1; i <= index; i++ {
|
||||||
entryIndex := i - 1 - l.startIndex
|
entryIndex := i - 1 - l.startIndex
|
||||||
|
@ -335,6 +351,7 @@ func (l *Log) SetCommitIndex(index uint64) error {
|
||||||
|
|
||||||
// Write to storage.
|
// Write to storage.
|
||||||
if err := entry.Encode(l.file); err != nil {
|
if err := entry.Encode(l.file); err != nil {
|
||||||
|
panic(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue