Merge pull request #13929 from wyrobnik/raftexample-snap-pre-hard-state

contrib/raftexample: Save snapshot and WAL before hard state
This commit is contained in:
Piotr Tabor 2022-04-13 18:30:34 +02:00 committed by GitHub
commit d888118171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -449,9 +449,13 @@ func (rc *raftNode) serveChannels() {
// store raft entries to wal, then publish over commit channel
case rd := <-rc.node.Ready():
rc.wal.Save(rd.HardState, rd.Entries)
// Must save the snapshot file and WAL snapshot entry before saving any other entries
// or hardstate to ensure that recovery after a snapshot restore is possible.
if !raft.IsEmptySnap(rd.Snapshot) {
rc.saveSnap(rd.Snapshot)
}
rc.wal.Save(rd.HardState, rd.Entries)
if !raft.IsEmptySnap(rd.Snapshot) {
rc.raftStorage.ApplySnapshot(rd.Snapshot)
rc.publishSnapshot(rd.Snapshot)
}