From 74e020b715d1a3d42c7eab1a365e76a4281d2bc6 Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Mon, 12 Jun 2017 13:56:01 -0700 Subject: [PATCH] contrib/raftexample: save snapshot to WAL first Save the snapshot index to the WAL before saving the snapshot to the filesystem. This ensures that we'll only ever call wal.Open with a snapshot that was previously saved to the WAL. --- contrib/raftexample/raft.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/contrib/raftexample/raft.go b/contrib/raftexample/raft.go index 84c8ffda35c..b212dcb7c00 100644 --- a/contrib/raftexample/raft.go +++ b/contrib/raftexample/raft.go @@ -107,9 +107,9 @@ func newRaftNode(id int, peers []string, join bool, getSnapshot func() ([]byte, } func (rc *raftNode) saveSnap(snap raftpb.Snapshot) error { - if err := rc.snapshotter.SaveSnap(snap); err != nil { - return err - } + // must save the snapshot index to the WAL before saving the + // snapshot to maintain the invariant that we only Open the + // wal at previously-saved snapshot indexes. walSnap := walpb.Snapshot{ Index: snap.Metadata.Index, Term: snap.Metadata.Term, @@ -117,6 +117,9 @@ func (rc *raftNode) saveSnap(snap raftpb.Snapshot) error { if err := rc.wal.SaveSnapshot(walSnap); err != nil { return err } + if err := rc.snapshotter.SaveSnap(snap); err != nil { + return err + } return rc.wal.ReleaseLockTo(snap.Metadata.Index) }