forked from 0zAND1z/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshotter.go
334 lines (307 loc) · 8.36 KB
/
snapshotter.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2017-2019 Lei Ni ([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dragonboat
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"github.com/lni/dragonboat/internal/rsm"
"github.com/lni/dragonboat/internal/server"
"github.com/lni/dragonboat/internal/utils/fileutil"
"github.com/lni/dragonboat/raftio"
pb "github.com/lni/dragonboat/raftpb"
"github.com/lni/dragonboat/statemachine"
)
const (
snapshotsToKeep = 3
)
var (
// ErrNoSnapshot is the error used to indicate that there is no snapshot
// available.
ErrNoSnapshot = errors.New("no snapshot available")
errSnapshotOutOfDate = errors.New("snapshot being generated is out of date")
snapshotDirNameRe = regexp.MustCompile(`^snapshot-[0-9A-F]+$`)
genSnapshotDirNameRe = regexp.MustCompile(`^snapshot-[0-9A-F]+-[0-9A-F]+\.generating$`)
recvSnapshotDirNameRe = regexp.MustCompile(`^snapshot-[0-9A-F]+-[0-9A-F]+\.receiving$`)
)
type snapshotter struct {
rootDirFunc server.GetSnapshotDirFunc
dir string
clusterID uint64
nodeID uint64
logdb raftio.ILogDB
stopc chan struct{}
}
func newSnapshotter(clusterID uint64,
nodeID uint64, rootDirFunc server.GetSnapshotDirFunc,
ldb raftio.ILogDB, stopc chan struct{}) *snapshotter {
return &snapshotter{
rootDirFunc: rootDirFunc,
dir: rootDirFunc(clusterID, nodeID),
logdb: ldb,
clusterID: clusterID,
nodeID: nodeID,
stopc: stopc,
}
}
func (s *snapshotter) Save(lastApplied uint64,
lastAppliedTerm uint64, savable rsm.IManagedStateMachine) (*pb.Snapshot,
*server.SnapshotEnv, error) {
env := s.getSnapshotEnv(lastApplied)
if err := env.CreateTempDir(); err != nil {
return nil, env, err
}
files := newFileCollection()
fp := env.GetTempFilepath()
sz, err := savable.SaveSnapshot(fp, files)
if err != nil {
return nil, env, err
}
fs, err := files.prepareFiles(env.GetTempDir(), env.GetFinalDir())
if err != nil {
return nil, env, err
}
ss := &pb.Snapshot{
Filepath: env.GetFilepath(),
FileSize: sz,
Index: lastApplied,
Term: lastAppliedTerm,
Files: fs,
}
return ss, env, nil
}
func (s *snapshotter) Commit(snapshot pb.Snapshot) error {
env := s.getSnapshotEnv(snapshot.Index)
if err := env.CreateFlagFile(&snapshot); err != nil {
return err
}
if readyToReturnTestKnob(s.stopc, "final dir check") {
return statemachine.ErrSnapshotStopped
}
if env.IsFinalDirExists() {
return errSnapshotOutOfDate
}
if outOfDate, err := env.RenameTempDirToFinalDir(); err != nil {
if outOfDate {
return errSnapshotOutOfDate
}
return err
}
if readyToReturnTestKnob(s.stopc, "saving to logdb") {
return statemachine.ErrSnapshotStopped
}
if err := s.saveToLogDB(snapshot); err != nil {
return err
}
if readyToReturnTestKnob(s.stopc, "removing flag file") {
return statemachine.ErrSnapshotStopped
}
return env.RemoveFlagFile()
}
func (s *snapshotter) GetFilePath(index uint64) string {
env := s.getSnapshotEnv(index)
return env.GetFilepath()
}
func (s *snapshotter) GetSnapshot(index uint64) (pb.Snapshot, error) {
snaps, err := s.logdb.ListSnapshots(s.clusterID, s.nodeID)
if err != nil {
return pb.Snapshot{}, err
}
for _, snap := range snaps {
if snap.Index == index {
return snap, nil
}
}
return pb.Snapshot{}, ErrNoSnapshot
}
func (s *snapshotter) GetMostRecentSnapshot() (pb.Snapshot, error) {
snaps, err := s.logdb.ListSnapshots(s.clusterID, s.nodeID)
if err != nil {
return pb.Snapshot{}, err
}
if len(snaps) > 0 {
return snaps[len(snaps)-1], nil
}
return pb.Snapshot{}, ErrNoSnapshot
}
func (s *snapshotter) IsNoSnapshotError(e error) bool {
return e == ErrNoSnapshot
}
func (s *snapshotter) Compaction(clusterID uint64, nodeID uint64,
removeUpTo uint64) error {
snapshots, err := s.logdb.ListSnapshots(s.clusterID, s.nodeID)
if err != nil {
return err
}
if len(snapshots) <= snapshotsToKeep {
return nil
}
shortListed := snapshots[:len(snapshots)-snapshotsToKeep]
for _, snap := range shortListed {
if snap.Index < removeUpTo {
if err := s.logdb.DeleteSnapshot(clusterID, nodeID, snap.Index); err != nil {
return err
}
env := s.getSnapshotEnv(snap.Index)
if err := env.RemoveFinalDir(); err != nil {
return err
}
}
}
return nil
}
func (s *snapshotter) ProcessOrphans() error {
fiList, err := ioutil.ReadDir(s.dir)
if err != nil {
return err
}
for _, fi := range fiList {
if !fi.IsDir() {
continue
}
fdir := filepath.Join(s.dir, fi.Name())
if s.isOrphanDir(fi.Name()) {
plog.Infof("found a orphan snapshot dir %s, %s", fi.Name(), fdir)
var ss pb.Snapshot
if err := fileutil.GetFlagFileContent(fdir,
fileutil.SnapshotFlagFilename, &ss); err != nil {
return err
}
if pb.IsEmptySnapshot(ss) {
plog.Panicf("empty snapshot found in %s", fdir)
}
deleteDir := false
mrss, err := s.GetMostRecentSnapshot()
if err != nil {
if err == ErrNoSnapshot {
plog.Infof("no snapshot in logdb, delete the folder")
deleteDir = true
} else {
return err
}
} else {
if mrss.Index != ss.Index {
deleteDir = true
}
}
env := s.getSnapshotEnv(ss.Index)
if deleteDir {
plog.Infof("going to delete orphan dir %s", fdir)
if err := env.RemoveFinalDir(); err != nil {
return err
}
} else {
plog.Infof("will keep the dir with flag file removed, %s", fdir)
if err := env.RemoveFlagFile(); err != nil {
return err
}
}
} else if s.isZombieDir(fi.Name()) {
plog.Infof("going to delete a zombie dir %s", fdir)
if err := os.RemoveAll(fdir); err != nil {
return err
}
plog.Infof("going to sync the folder %s", s.dir)
if err := fileutil.SyncDir(s.dir); err != nil {
return err
}
}
}
return nil
}
func (s *snapshotter) removeFlagFile(index uint64) error {
env := s.getSnapshotEnv(index)
return env.RemoveFlagFile()
}
func (s *snapshotter) getSnapshotEnv(index uint64) *server.SnapshotEnv {
return server.NewSnapshotEnv(s.rootDirFunc,
s.clusterID, s.nodeID, index, s.nodeID, server.SnapshottingMode)
}
func (s *snapshotter) saveToLogDB(snapshot pb.Snapshot) error {
rec := pb.Update{
ClusterID: s.clusterID,
NodeID: s.nodeID,
Snapshot: snapshot,
}
return s.logdb.SaveSnapshots([]pb.Update{rec})
}
func (s *snapshotter) dirNameMatch(dir string) bool {
return snapshotDirNameRe.Match([]byte(dir))
}
func (s *snapshotter) isZombieDir(dir string) bool {
return genSnapshotDirNameRe.Match([]byte(dir)) ||
recvSnapshotDirNameRe.Match([]byte(dir))
}
func (s *snapshotter) isOrphanDir(dir string) bool {
if !s.dirNameMatch(dir) {
return false
}
fdir := filepath.Join(s.dir, dir)
return fileutil.HasFlagFile(fdir, fileutil.SnapshotFlagFilename)
}
type files struct {
files []*pb.SnapshotFile
idmap map[uint64]struct{}
}
func newFileCollection() *files {
return &files{
files: make([]*pb.SnapshotFile, 0),
idmap: make(map[uint64]struct{}),
}
}
func (fc *files) AddFile(fileID uint64,
path string, metadata []byte) {
if _, ok := fc.idmap[fileID]; ok {
plog.Panicf("trying to add file %d again", fileID)
}
f := &pb.SnapshotFile{
Filepath: path,
FileId: fileID,
Metadata: metadata,
}
fc.files = append(fc.files, f)
fc.idmap[fileID] = struct{}{}
}
func (fc *files) Size() uint64 {
return uint64(len(fc.files))
}
func (fc *files) GetFileAt(idx uint64) *pb.SnapshotFile {
return fc.files[idx]
}
func (fc *files) prepareFiles(tmpdir string,
finaldir string) ([]*pb.SnapshotFile, error) {
for _, file := range fc.files {
fn := file.Filename()
fp := filepath.Join(tmpdir, fn)
if err := os.Link(file.Filepath, fp); err != nil {
return nil, err
}
fi, err := os.Stat(fp)
if err != nil {
return nil, err
}
if fi.IsDir() {
plog.Panicf("%s is a dir", fp)
}
if fi.Size() == 0 {
plog.Panicf("empty file found, id %d",
file.FileId)
}
file.Filepath = filepath.Join(finaldir, fn)
file.FileSize = uint64(fi.Size())
}
return fc.files, nil
}