Skip to content

Commit

Permalink
contrib/raftexample: save snapshot to WAL first
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jbowens committed Jun 13, 2017
1 parent 3993f37 commit 74e020b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions contrib/raftexample/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,19 @@ 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,
}
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)
}

Expand Down

0 comments on commit 74e020b

Please sign in to comment.