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:
Yicheng Qin
2014-09-10 11:32:01 -07:00
parent 3d272c2686
commit d31443f5a3
2 changed files with 24 additions and 10 deletions

View File

@ -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