raftLog: compact applied entries only
compact MUST happen on entries that have been applied, or 1. it may screw up the log by setting wrong commitIndex 2. discard unapplied entries
This commit is contained in:
11
raft/log.go
11
raft/log.go
@ -152,8 +152,8 @@ func (l *raftLog) maybeCommit(maxIndex, term int64) bool {
|
||||
// and not greater than the index of the last entry.
|
||||
// the number of entries after compaction will be returned.
|
||||
func (l *raftLog) compact(i int64) int64 {
|
||||
if l.isOutOfBounds(i) {
|
||||
panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.lastIndex()))
|
||||
if l.isOutOfAppliedBounds(i) {
|
||||
panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
|
||||
}
|
||||
l.ents = l.slice(i, l.lastIndex()+1)
|
||||
l.unstable = max(i+1, l.unstable)
|
||||
@ -208,6 +208,13 @@ func (l *raftLog) isOutOfBounds(i int64) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *raftLog) isOutOfAppliedBounds(i int64) bool {
|
||||
if i < l.offset || i > l.applied {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func min(a, b int64) int64 {
|
||||
if a > b {
|
||||
return b
|
||||
|
Reference in New Issue
Block a user