forked from absolute8511/ZanRedisDB
/
raft_storage.go
49 lines (43 loc) · 1.48 KB
/
raft_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package node
import (
"errors"
"github.com/youzan/ZanRedisDB/raft/raftpb"
"github.com/youzan/ZanRedisDB/snap"
"github.com/youzan/ZanRedisDB/wal"
"github.com/youzan/ZanRedisDB/wal/walpb"
)
type raftPersistStorage struct {
*wal.WAL
*snap.Snapshotter
}
func NewRaftPersistStorage(w *wal.WAL, s *snap.Snapshotter) IRaftPersistStorage {
return &raftPersistStorage{w, s}
}
// SaveSnap saves the snapshot to disk and release the locked
// wal files since they will not be used.
func (st *raftPersistStorage) SaveSnap(snap raftpb.Snapshot) error {
if enableSnapSaveTest {
return errors.New("failed to save snapshot to raft in failed test")
}
walsnap := walpb.Snapshot{
Index: snap.Metadata.Index,
Term: snap.Metadata.Term,
}
// save the snapshot file before writing the snapshot to the wal.
// This makes it possible for the snapshot file to become orphaned, but prevents
// a WAL snapshot entry from having no corresponding snapshot file.
err := st.Snapshotter.SaveSnap(snap)
if err != nil {
return err
}
return st.WAL.SaveSnapshot(walsnap)
}
// Release releases resources older than the given snap and are no longer needed:
// - releases the locks to the wal files that are older than the provided wal for the given snap.
// - deletes any .snap.db files that are older than the given snap.
func (st *raftPersistStorage) Release(snap raftpb.Snapshot) error {
if err := st.WAL.ReleaseLockTo(snap.Metadata.Index); err != nil {
return err
}
return st.Snapshotter.ReleaseSnapDBs(snap)
}