func snapshot(fsm raft.FSM, fss raft.SnapshotStore, numLogs uint64) error { snapshot, err := fsm.Snapshot() if err != nil { return fmt.Errorf("Unexpected error in fsm.Snapshot(): %v", err) } robustsnap, ok := snapshot.(*robustSnapshot) if !ok { return fmt.Errorf("fsm.Snapshot() return value is not a robustSnapshot") } if robustsnap.lastIndex != numLogs { return fmt.Errorf("snapshot does not retain the last message, got: %d, want: %d", robustsnap.lastIndex, numLogs) } sink, err := fss.Create(numLogs, 1, []byte{}) if err != nil { return fmt.Errorf("fss.Create: %v", err) } if err := snapshot.Persist(sink); err != nil { return fmt.Errorf("Unexpected error in snapshot.Persist(): %v", err) } sink.Close() return nil }
func restore(fsm raft.FSM, fss raft.SnapshotStore, numLogs uint64) error { snapshots, err := fss.List() if err != nil { return fmt.Errorf("fss.List(): %v", err) } // snapshots[0] is the most recent snapshot snapshotId := snapshots[0].ID _, readcloser, err := fss.Open(snapshotId) if err != nil { return fmt.Errorf("fss.Open(%s): %v", snapshotId, err) } if err := fsm.Restore(readcloser); err != nil { return fmt.Errorf("fsm.Restore(): %v", err) } first, _ := fsm.(*FSM).ircstore.FirstIndex() last, _ := fsm.(*FSM).ircstore.LastIndex() if last-first >= numLogs { return fmt.Errorf("Compaction did not decrease log size. got: %d, want: < %d", last-first, numLogs) } return nil }