---------------- Raft.Storage interface complete. CreateSnapshot generates a snapshot with the given ConfState and data and writes it to disk.
(i uint64, cs *raftpb.ConfState, data []byte)
| 271 | |
| 272 | // CreateSnapshot generates a snapshot with the given ConfState and data and writes it to disk. |
| 273 | func (w *DiskStorage) CreateSnapshot(i uint64, cs *raftpb.ConfState, data []byte) error { |
| 274 | glog.V(2).Infof("CreateSnapshot i=%d, cs=%+v", i, cs) |
| 275 | |
| 276 | w.lock.Lock() |
| 277 | defer w.lock.Unlock() |
| 278 | |
| 279 | first := w.firstIndex() |
| 280 | if i < first { |
| 281 | glog.Errorf("i=%d<first=%d, ErrSnapOutOfDate", i, first) |
| 282 | return raft.ErrSnapOutOfDate |
| 283 | } |
| 284 | |
| 285 | e, err := w.wal.seekEntry(i) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | |
| 290 | var snap raftpb.Snapshot |
| 291 | snap.Metadata.Index = i |
| 292 | snap.Metadata.Term = e.Term() |
| 293 | x.AssertTrue(cs != nil) |
| 294 | snap.Metadata.ConfState = *cs |
| 295 | snap.Data = data |
| 296 | |
| 297 | if err := w.meta.StoreSnapshot(&snap); err != nil { |
| 298 | return err |
| 299 | } |
| 300 | // Now we delete all the files which are below the snapshot index. |
| 301 | w.wal.deleteBefore(snap.Metadata.Index) |
| 302 | return nil |
| 303 | } |
| 304 | |
| 305 | // TODO(Aman): In the raft example here, we store the snapshot first, followed by entries |
| 306 | // and then, hard state. https://github.com/etcd-io/etcd/blob/main/contrib/raftexample/raft.go |