Skip to content

Commit

Permalink
contrib/raftexample: Save snapshot and WAL before hard state
Browse files Browse the repository at this point in the history
Update raftexample to save the snapshot file and WAL snapshot entry
before hardstate to ensure the snapshot exists during recovery.
Otherwise if there is a failure after storing the hard state there may
be reference to a non-existent snapshot.
This PR introduces the fix from etcd-io#10219 to the raftexample.
  • Loading branch information
wyrobnik committed Apr 11, 2022
1 parent dd08e15 commit 3152dc8
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion contrib/raftexample/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3152dc8

Please sign in to comment.