forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshotter_test.go
550 lines (528 loc) · 15.7 KB
/
snapshotter_test.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// 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.
// +build !dragonboat_slowtest
// +build !dragonboat_errorinjectiontest
package dragonboat
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/lni/dragonboat/internal/logdb"
"github.com/lni/dragonboat/internal/utils/fileutil"
"github.com/lni/dragonboat/internal/utils/leaktest"
"github.com/lni/dragonboat/raftio"
pb "github.com/lni/dragonboat/raftpb"
)
const (
tmpSnapshotDirSuffix = "generating"
rdbTestDirectory = "rdb_test_dir_safe_to_delete"
)
func getNewTestDB(dir string, lldir string) raftio.ILogDB {
d := filepath.Join(rdbTestDirectory, dir)
lld := filepath.Join(rdbTestDirectory, lldir)
os.MkdirAll(d, 0777)
os.MkdirAll(lld, 0777)
db, err := logdb.OpenLogDB([]string{d}, []string{lld})
if err != nil {
panic(err.Error())
}
return db
}
func deleteTestRDB() {
os.RemoveAll(rdbTestDirectory)
}
func getTestSnapshotter(ldb raftio.ILogDB) *snapshotter {
fp := filepath.Join(rdbTestDirectory, "snapshot")
os.MkdirAll(fp, 0777)
f := func(cid uint64, nid uint64) string {
return fp
}
return newSnapshotter(1, 1, f, ldb, nil)
}
func runSnapshotterTest(t *testing.T,
fn func(t *testing.T, logdb raftio.ILogDB, snapshotter *snapshotter)) {
defer leaktest.AfterTest(t)()
dir := "db-dir"
lldir := "wal-db-dir"
ldb := getNewTestDB(dir, lldir)
s := getTestSnapshotter(ldb)
defer deleteTestRDB()
defer ldb.Close()
fn(t, ldb, s)
}
func TestFinalizeSnapshotReturnExpectedErrorWhenOutOfDate(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
ss := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 100,
Term: 200,
}
env := s.getSnapshotEnv(ss.Index)
finalSnapDir := env.GetFinalDir()
if err := os.MkdirAll(finalSnapDir, 0755); err != nil {
t.Errorf("failed to create final snap dir")
}
if err := env.CreateTempDir(); err != nil {
t.Errorf("create tmp snapshot dir failed %v", err)
}
if err := s.Commit(ss); err != errSnapshotOutOfDate {
t.Errorf("unexpected error result %v", err)
}
}
runSnapshotterTest(t, fn)
}
func TestSnapshotCanBeFinalized(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
ss := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 100,
Term: 200,
}
env := s.getSnapshotEnv(ss.Index)
finalSnapDir := env.GetFinalDir()
tmpDir := env.GetTempDir()
err := env.CreateTempDir()
if err != nil {
t.Errorf("create tmp snapshot dir failed %v", err)
}
_, err = os.Stat(tmpDir)
if err != nil {
t.Errorf("failed to get stat for tmp dir, %v", err)
}
testfp := filepath.Join(tmpDir, "test.data")
f, err := os.Create(testfp)
if err != nil {
t.Errorf("failed to create test file")
}
f.Write(make([]byte, 12))
f.Close()
if err = s.Commit(ss); err != nil {
t.Errorf("finalize snapshot failed %v", err)
}
snapshots, err := ldb.ListSnapshots(1, 1)
if err != nil {
t.Errorf("failed to list snapshot")
}
if len(snapshots) != 1 {
t.Errorf("returned %d snapshot records, want 1", len(snapshots))
}
rs, err := s.GetSnapshot(100)
if err != nil {
t.Errorf("failed to get snapshot")
}
if rs.Index != 100 {
t.Errorf("returned an unexpected snapshot")
}
_, err = s.GetSnapshot(200)
if err != ErrNoSnapshot {
t.Errorf("unexpected err %v", err)
}
if _, err = os.Stat(tmpDir); !os.IsNotExist(err) {
t.Errorf("tmp dir not removed, %v", err)
}
fi, err := os.Stat(finalSnapDir)
if err != nil {
t.Errorf("failed to get stats, %v", err)
}
if !fi.IsDir() {
t.Errorf("not a dir")
}
if fileutil.HasFlagFile(finalSnapDir, fileutil.SnapshotFlagFilename) {
t.Errorf("flag file not removed")
}
vfp := filepath.Join(finalSnapDir, "test.data")
fi, err = os.Stat(vfp)
if err != nil {
t.Errorf("failed to get stat %v", err)
}
if fi.IsDir() || fi.Size() != 12 {
t.Errorf("not the same test file. ")
}
}
runSnapshotterTest(t, fn)
}
func TestSnapshotCanBeSavedToLogDB(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
s1 := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 1,
Term: 2,
}
if err := s.saveToLogDB(s1); err != nil {
t.Errorf("failed to save snapshot record %v", err)
}
snapshots, err := ldb.ListSnapshots(1, 1)
if err != nil {
t.Errorf("failed to list snapshot")
}
if len(snapshots) != 1 {
t.Errorf("returned %d snapshot records, want 1", len(snapshots))
}
if !reflect.DeepEqual(&s1, &snapshots[0]) {
t.Errorf("snapshot record changed")
}
}
runSnapshotterTest(t, fn)
}
func TestZombieSnapshotDirsCanBeRemoved(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
env1 := s.getSnapshotEnv(100)
env2 := s.getSnapshotEnv(200)
fd1 := env1.GetFinalDir()
fd2 := env2.GetFinalDir()
fd1 = filepath.Join(fd1, tmpSnapshotDirSuffix)
fd2 = filepath.Join(fd2, ".receiving")
if err := os.MkdirAll(fd1, 0755); err != nil {
t.Errorf("failed to create dir %v", err)
}
if err := os.MkdirAll(fd2, 0755); err != nil {
t.Errorf("failed to create dir %v", err)
}
if err := s.ProcessOrphans(); err != nil {
t.Errorf("failed to process orphaned snapshtos %s", err)
}
if _, err := os.Stat(fd1); os.IsNotExist(err) {
t.Errorf("fd1 not removed")
}
if _, err := os.Stat(fd2); os.IsNotExist(err) {
t.Errorf("fd2 not removed")
}
}
runSnapshotterTest(t, fn)
}
func TestFirstSnapshotBecomeOrphanedIsHandled(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
s1 := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 100,
Term: 200,
}
env := s.getSnapshotEnv(100)
fd1 := env.GetFinalDir()
if err := os.MkdirAll(fd1, 0755); err != nil {
t.Errorf("failed to create dir %v", err)
}
if err := fileutil.CreateFlagFile(fd1, fileutil.SnapshotFlagFilename, &s1); err != nil {
t.Errorf("failed to create flag file %s", err)
}
if err := s.ProcessOrphans(); err != nil {
t.Errorf("failed to process orphaned snapshtos %s", err)
}
if _, err := os.Stat(fd1); !os.IsNotExist(err) {
t.Errorf("fd1 not removed")
}
}
runSnapshotterTest(t, fn)
}
func TestOrphanedSnapshotsCanBeProcessed(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
s1 := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 100,
Term: 200,
}
s2 := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 200,
Term: 200,
}
s3 := pb.Snapshot{
FileSize: 1234,
Filepath: "f2",
Index: 300,
Term: 200,
}
env1 := s.getSnapshotEnv(s1.Index)
env2 := s.getSnapshotEnv(s2.Index)
env3 := s.getSnapshotEnv(s3.Index)
fd1 := env1.GetFinalDir()
fd2 := env2.GetFinalDir()
fd3 := env3.GetFinalDir()
fd4 := fmt.Sprintf("%s%s", fd3, "xx")
if err := os.MkdirAll(fd1, 0755); err != nil {
t.Errorf("failed to create dir %v", err)
}
if err := os.MkdirAll(fd2, 0755); err != nil {
t.Errorf("failed to create dir %v", err)
}
if err := os.MkdirAll(fd4, 0755); err != nil {
t.Errorf("failed to create dir %v", err)
}
if err := fileutil.CreateFlagFile(fd1, fileutil.SnapshotFlagFilename, &s1); err != nil {
t.Errorf("failed to create flag file %s", err)
}
if err := fileutil.CreateFlagFile(fd2, fileutil.SnapshotFlagFilename, &s2); err != nil {
t.Errorf("failed to create flag file %s", err)
}
if err := fileutil.CreateFlagFile(fd4, fileutil.SnapshotFlagFilename, &s3); err != nil {
t.Errorf("failed to create flag file %s", err)
}
if err := s.saveToLogDB(s1); err != nil {
t.Errorf("failed to save snapshot to logdb")
}
// fd1 has record in logdb. flag file expected to be removed while the fd1
// foler is expected to be kept
// fd2 doesn't has its record in logdb, while the most recent snapshot record
// in logdb is not for fd2, fd2 will be entirely removed
if err := s.ProcessOrphans(); err != nil {
t.Errorf("failed to process orphaned snapshtos %s", err)
}
if fileutil.HasFlagFile(fd1, fileutil.SnapshotFlagFilename) {
t.Errorf("flag for fd1 not removed")
}
if fileutil.HasFlagFile(fd2, fileutil.SnapshotFlagFilename) {
t.Errorf("flag for fd2 not removed")
}
if !fileutil.HasFlagFile(fd4, fileutil.SnapshotFlagFilename) {
t.Errorf("flag for fd4 is missing")
}
if _, err := os.Stat(fd1); os.IsNotExist(err) {
t.Errorf("fd1 removed by mistake")
}
if _, err := os.Stat(fd2); !os.IsNotExist(err) {
t.Errorf("fd2 not removed")
}
}
runSnapshotterTest(t, fn)
}
func TestRemoveUnusedSnapshotRemoveSnapshots(t *testing.T) {
defer leaktest.AfterTest(t)()
// normal case
testRemoveUnusedSnapshotRemoveSnapshots(t, 32, 7, 7)
// snapshotsToKeep snapshots will be kept
testRemoveUnusedSnapshotRemoveSnapshots(t, 4, 5, 2)
// snapshotsToKeep snapshots will be kept
testRemoveUnusedSnapshotRemoveSnapshots(t, 3, 3, 1)
}
func testRemoveUnusedSnapshotRemoveSnapshots(t *testing.T,
total uint64, upTo uint64, removed uint64) {
fn := func(t *testing.T, ldb raftio.ILogDB, snapshotter *snapshotter) {
for i := uint64(1); i <= total; i++ {
fn := fmt.Sprintf("f%d.data", i)
s := pb.Snapshot{
FileSize: 1234,
Filepath: fn,
Index: i,
Term: 2,
}
env := snapshotter.getSnapshotEnv(s.Index)
if err := env.CreateTempDir(); err != nil {
t.Errorf("failed to create snapshot dir")
}
if err := snapshotter.Commit(s); err != nil {
t.Errorf("failed to save snapshot record")
}
fp := snapshotter.GetFilePath(s.Index)
f, err := os.Create(fp)
if err != nil {
t.Errorf("failed to create the file, %v", err)
}
f.Close()
}
snapshots, err := ldb.ListSnapshots(1, 1)
if err != nil {
t.Errorf("failed to list snapshot")
}
if uint64(len(snapshots)) != total {
t.Errorf("didn't return %d snapshot records", total)
}
for i := uint64(1); i < removed; i++ {
env := snapshotter.getSnapshotEnv(i)
snapDir := env.GetFinalDir()
if _, err = os.Stat(snapDir); os.IsNotExist(err) {
t.Errorf("snapshot dir didn't get created, %s", snapDir)
}
}
if err = snapshotter.Compaction(1, 1, upTo); err != nil {
t.Errorf("failed to remove unused snapshots, %v", err)
}
snapshots, err = ldb.ListSnapshots(1, 1)
if err != nil {
t.Errorf("failed to list snapshot")
}
if uint64(len(snapshots)) != total-removed+1 {
t.Errorf("got %d, want %d, first index %d",
len(snapshots), total-removed+1, snapshots[0].Index)
}
for _, s := range snapshots {
if s.Index < removed {
t.Errorf("didn't remove snapshot %d", s.Index)
}
}
for i := uint64(0); i < removed; i++ {
fp := snapshotter.GetFilePath(i)
if _, err := os.Stat(fp); !os.IsNotExist(err) {
t.Errorf("snapshot file didn't get deleted")
}
env := snapshotter.getSnapshotEnv(i)
snapDir := env.GetFinalDir()
if _, err := os.Stat(snapDir); !os.IsNotExist(err) {
t.Errorf("snapshot dir didn't get removed")
}
}
}
runSnapshotterTest(t, fn)
}
func TestFileCanBeAddedToFileCollection(t *testing.T) {
defer leaktest.AfterTest(t)()
fc := newFileCollection()
fc.AddFile(1, "test.data", make([]byte, 12))
fc.AddFile(2, "test.data2", make([]byte, 16))
if len(fc.files) != 2 || len(fc.idmap) != 2 {
t.Errorf("file count is %d, want 2", len(fc.files))
}
if fc.files[0].Filepath != "test.data" ||
fc.files[0].FileId != 1 || len(fc.files[0].Metadata) != 12 {
t.Errorf("not the expected first file record")
}
if fc.files[1].Filepath != "test.data2" ||
fc.files[1].FileId != 2 || len(fc.files[1].Metadata) != 16 {
t.Errorf("not the expected first file record")
}
}
func TestFileWithDuplicatedIDCanNotBeAdded(t *testing.T) {
defer leaktest.AfterTest(t)()
defer func() {
if r := recover(); r == nil {
t.Errorf("AddFile didn't panic")
}
}()
fc := newFileCollection()
fc.AddFile(1, "test.data", make([]byte, 12))
fc.AddFile(2, "test.data", make([]byte, 12))
fc.AddFile(1, "test.data2", make([]byte, 16))
}
func TestPrepareFiles(t *testing.T) {
defer leaktest.AfterTest(t)()
if err := os.MkdirAll(rdbTestDirectory, 0755); err != nil {
t.Errorf("failed to make dir %v", err)
}
defer func() {
os.RemoveAll(rdbTestDirectory)
}()
f1, err := os.Create(filepath.Join(rdbTestDirectory, "test1.data"))
if err != nil {
t.Fatalf("failed to create the file, %v", err)
}
n, err := f1.Write(make([]byte, 16))
if n != 16 || err != nil {
t.Fatalf("failed to write file %v", err)
}
f1.Close()
f2, err := os.Create(filepath.Join(rdbTestDirectory, "test2.data"))
if err != nil {
t.Fatalf("failed to create the file, %v", err)
}
n, err = f2.Write(make([]byte, 32))
if n != 32 || err != nil {
t.Fatalf("failed to write file %v", err)
}
f2.Close()
fc := newFileCollection()
fc.AddFile(1, filepath.Join(rdbTestDirectory, "test1.data"), make([]byte, 8))
fc.AddFile(2, filepath.Join(rdbTestDirectory, "test2.data"), make([]byte, 2))
if fc.Size() != 2 {
t.Errorf("unexpected collection size")
}
rf := fc.GetFileAt(0)
if rf.Filepath != filepath.Join(rdbTestDirectory, "test1.data") {
t.Errorf("unexpected path, got %s, want %s",
rf.Filepath, filepath.Join(rdbTestDirectory, "test1.data"))
}
files, err := fc.prepareFiles(rdbTestDirectory, rdbTestDirectory)
if err != nil {
t.Fatalf("prepareFiles failed %v", err)
}
if files[0].FileId != 1 || files[0].Filename() != "external-file-1" || files[0].FileSize != 16 {
t.Errorf("unexpected returned file record %v", files[0])
}
if files[1].FileId != 2 || files[1].Filename() != "external-file-2" || files[1].FileSize != 32 {
t.Errorf("unexpected returned file record %v", files[1])
}
fi1, err := os.Stat(filepath.Join(rdbTestDirectory, "external-file-1"))
if err != nil {
t.Errorf("failed to get stat, %v", err)
}
if fi1.Size() != 16 {
t.Errorf("unexpected size")
}
fi2, err := os.Stat(filepath.Join(rdbTestDirectory, "external-file-2"))
if err != nil {
t.Errorf("failed to get stat, %v", err)
}
if fi2.Size() != 32 {
t.Errorf("unexpected size")
}
}
func TestSnapshotDirNameMatchWorks(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
tests := []struct {
dirName string
valid bool
}{
{"snapshot-AB", true},
{"snapshot", false},
{"xxxsnapshot-AB", false},
{"snapshot-ABd", false},
{"snapshot-", false},
}
for idx, tt := range tests {
v := s.dirNameMatch(tt.dirName)
if v != tt.valid {
t.Errorf("dir name %s (%d) failed to match", tt.dirName, idx)
}
}
}
runSnapshotterTest(t, fn)
}
func TestZombieSnapshotDirNameMatchWorks(t *testing.T) {
fn := func(t *testing.T, ldb raftio.ILogDB, s *snapshotter) {
tests := []struct {
dirName string
valid bool
}{
{"snapshot-AB", false},
{"snapshot", false},
{"xxxsnapshot-AB", false},
{"snapshot-", false},
{"snapshot-AB-01.receiving", true},
{"snapshot-AB-1G.receiving", false},
{"snapshot-AB.receiving", false},
{"snapshot-XX.receiving", false},
{"snapshot-AB.receivingd", false},
{"dsnapshot-AB.receiving", false},
{"snapshot-AB.generating", false},
{"snapshot-AB-01.generating", true},
{"snapshot-AB-0G.generating", false},
{"snapshot-XX.generating", false},
{"snapshot-AB.generatingd", false},
{"dsnapshot-AB.generating", false},
}
for idx, tt := range tests {
v := s.isZombieDir(tt.dirName)
if v != tt.valid {
t.Errorf("dir name %s (%d) failed to match", tt.dirName, idx)
}
}
}
runSnapshotterTest(t, fn)
}