Skip to content

Commit 7397bb9

Browse files
committed
Fix Raft Panic
The lastIndex cache I had introduced, caused Raft panics because it was incorrect. This PR fixes that by updating it correctly in two places: addEntries and setSnapshot. With this PR, blockade runs OK.
1 parent 0b945bd commit 7397bb9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

raftwal/storage.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,16 @@ func (w *DiskStorage) setSnapshot(batch *badger.WriteBatch, s pb.Snapshot) error
313313
return err
314314
}
315315

316-
// Cache it.
316+
// Update the last index cache here. This is useful so when a follower gets a jump due to
317+
// receiving a snapshot and Save is called, addEntries wouldn't have much. So, the last index
318+
// cache would need to rely upon this update here.
319+
if val, ok := w.cache.Load(lastKey); ok {
320+
le := val.(uint64)
321+
if le < e.Index {
322+
w.cache.Store(lastKey, e.Index)
323+
}
324+
}
325+
// Cache snapshot.
317326
w.cache.Store(snapshotKey, proto.Clone(&s))
318327
return nil
319328
}
@@ -614,9 +623,9 @@ func (w *DiskStorage) addEntries(batch *badger.WriteBatch, entries []pb.Entry) e
614623
}
615624
}
616625
laste := entries[len(entries)-1].Index
626+
w.cache.Store(lastKey, laste) // Update the last index cache.
617627
if laste < last {
618628
return w.deleteFrom(batch, laste+1)
619629
}
620-
w.cache.Store(lastKey, laste)
621630
return nil
622631
}

0 commit comments

Comments
 (0)