-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
snapshot.go
535 lines (446 loc) · 13 KB
/
snapshot.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
package raft
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/golang/protobuf/proto"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/plugin/pb"
"github.com/rboyer/safeio"
bolt "go.etcd.io/bbolt"
"go.uber.org/atomic"
"github.com/hashicorp/raft"
)
const (
// boltSnapshotID is the stable ID for any boltDB snapshot. Keeping the ID
// stable means there is only ever one bolt snapshot in the system
boltSnapshotID = "bolt-snapshot"
tmpSuffix = ".tmp"
snapPath = "snapshots"
)
// BoltSnapshotStore implements the SnapshotStore interface and allows snapshots
// to be stored in BoltDB files on local disk. Since we always have an up to
// date FSM we use a special snapshot ID to indicate that the snapshot can be
// pulled from the BoltDB file that is currently backing the FSM. This allows us
// to provide just-in-time snapshots without doing incremental data dumps.
//
// When a snapshot is being installed on the node we will Create and Write data
// to it. This will cause the snapshot store to create a new BoltDB file and
// write the snapshot data to it. Then, we can simply rename the snapshot to the
// FSM's filename. This allows us to atomically install the snapshot and
// reduces the amount of disk i/o. Older snapshots are reaped on startup and
// before each subsequent snapshot write. This ensures we only have one snapshot
// on disk at a time.
type BoltSnapshotStore struct {
// path is the directory in which to store file based snapshots
path string
// We hold a copy of the FSM so we can stream snapshots straight out of the
// database.
fsm *FSM
logger log.Logger
}
// BoltSnapshotSink implements SnapshotSink optionally choosing to write to a
// file.
type BoltSnapshotSink struct {
store *BoltSnapshotStore
logger log.Logger
meta raft.SnapshotMeta
trans raft.Transport
// These fields will be used if we are writing a snapshot (vs. reading
// one)
written atomic.Bool
writer io.WriteCloser
writeError error
dir string
parentDir string
doneWritingCh chan struct{}
l sync.Mutex
closed bool
}
// NewBoltSnapshotStore creates a new BoltSnapshotStore based
// on a base directory.
func NewBoltSnapshotStore(base string, logger log.Logger, fsm *FSM) (*BoltSnapshotStore, error) {
if logger == nil {
return nil, fmt.Errorf("no logger provided")
}
// Ensure our path exists
path := filepath.Join(base, snapPath)
if err := os.MkdirAll(path, 0o700); err != nil && !os.IsExist(err) {
return nil, fmt.Errorf("snapshot path not accessible: %v", err)
}
// Setup the store
store := &BoltSnapshotStore{
logger: logger,
fsm: fsm,
path: path,
}
// Cleanup any old or failed snapshots on startup.
if err := store.ReapSnapshots(); err != nil {
return nil, err
}
return store, nil
}
// Create is used to start a new snapshot
func (f *BoltSnapshotStore) Create(version raft.SnapshotVersion, index, term uint64, configuration raft.Configuration, configurationIndex uint64, trans raft.Transport) (raft.SnapshotSink, error) {
// We only support version 1 snapshots at this time.
if version != 1 {
return nil, fmt.Errorf("unsupported snapshot version %d", version)
}
// Create the sink
sink := &BoltSnapshotSink{
store: f,
logger: f.logger,
meta: raft.SnapshotMeta{
Version: version,
ID: boltSnapshotID,
Index: index,
Term: term,
Configuration: configuration,
ConfigurationIndex: configurationIndex,
},
trans: trans,
}
return sink, nil
}
// List returns available snapshots in the store. It only returns bolt
// snapshots. No snapshot will be returned if there are no indexes in the
// FSM.
func (f *BoltSnapshotStore) List() ([]*raft.SnapshotMeta, error) {
meta, err := f.getMetaFromFSM()
if err != nil {
return nil, err
}
// If we haven't seen any data yet do not return a snapshot
if meta.Index == 0 {
return nil, nil
}
return []*raft.SnapshotMeta{meta}, nil
}
// getBoltSnapshotMeta returns the fsm's latest state and configuration.
func (f *BoltSnapshotStore) getMetaFromFSM() (*raft.SnapshotMeta, error) {
latestIndex, latestConfig := f.fsm.LatestState()
meta := &raft.SnapshotMeta{
Version: 1,
ID: boltSnapshotID,
Index: latestIndex.Index,
Term: latestIndex.Term,
}
if latestConfig != nil {
meta.ConfigurationIndex, meta.Configuration = protoConfigurationToRaftConfiguration(latestConfig)
}
return meta, nil
}
// Open takes a snapshot ID and returns a ReadCloser for that snapshot.
func (f *BoltSnapshotStore) Open(id string) (*raft.SnapshotMeta, io.ReadCloser, error) {
if id == boltSnapshotID {
return f.openFromFSM()
}
return f.openFromFile(id)
}
func (f *BoltSnapshotStore) openFromFSM() (*raft.SnapshotMeta, io.ReadCloser, error) {
meta, err := f.getMetaFromFSM()
if err != nil {
return nil, nil, err
}
// If we don't have any data return an error
if meta.Index == 0 {
return nil, nil, errors.New("no snapshot data")
}
// Stream data out of the FSM to calculate the size
readCloser, writeCloser := io.Pipe()
metaReadCloser, metaWriteCloser := io.Pipe()
go func() {
f.fsm.writeTo(context.Background(), metaWriteCloser, writeCloser)
}()
// Compute the size
n, err := io.Copy(ioutil.Discard, metaReadCloser)
if err != nil {
f.logger.Error("failed to read state file", "error", err)
metaReadCloser.Close()
readCloser.Close()
return nil, nil, err
}
meta.Size = n
metaReadCloser.Close()
return meta, readCloser, nil
}
func (f *BoltSnapshotStore) getMetaFromDB(id string) (*raft.SnapshotMeta, error) {
if len(id) == 0 {
return nil, errors.New("can not open empty snapshot ID")
}
filename := filepath.Join(f.path, id, databaseFilename)
boltDB, err := bolt.Open(filename, 0o600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, err
}
defer boltDB.Close()
meta := &raft.SnapshotMeta{
Version: 1,
ID: id,
}
err = boltDB.View(func(tx *bolt.Tx) error {
b := tx.Bucket(configBucketName)
val := b.Get(latestIndexKey)
if val != nil {
var snapshotIndexes IndexValue
err := proto.Unmarshal(val, &snapshotIndexes)
if err != nil {
return err
}
meta.Index = snapshotIndexes.Index
meta.Term = snapshotIndexes.Term
}
// Read in our latest config and populate it inmemory
val = b.Get(latestConfigKey)
if val != nil {
var config ConfigurationValue
err := proto.Unmarshal(val, &config)
if err != nil {
return err
}
meta.ConfigurationIndex, meta.Configuration = protoConfigurationToRaftConfiguration(&config)
}
return nil
})
if err != nil {
return nil, err
}
return meta, nil
}
func (f *BoltSnapshotStore) openFromFile(id string) (*raft.SnapshotMeta, io.ReadCloser, error) {
meta, err := f.getMetaFromDB(id)
if err != nil {
return nil, nil, err
}
filename := filepath.Join(f.path, id, databaseFilename)
installer := &boltSnapshotInstaller{
meta: meta,
ReadCloser: ioutil.NopCloser(strings.NewReader(filename)),
filename: filename,
}
return meta, installer, nil
}
// ReapSnapshots reaps all snapshots.
func (f *BoltSnapshotStore) ReapSnapshots() error {
snapshots, err := ioutil.ReadDir(f.path)
switch {
case err == nil:
case os.IsNotExist(err):
return nil
default:
f.logger.Error("failed to scan snapshot directory", "error", err)
return err
}
for _, snap := range snapshots {
// Ignore any files
if !snap.IsDir() {
continue
}
// Warn about temporary snapshots, this indicates a previously failed
// snapshot attempt. We still want to clean these up.
dirName := snap.Name()
if strings.HasSuffix(dirName, tmpSuffix) {
f.logger.Warn("found temporary snapshot", "name", dirName)
}
path := filepath.Join(f.path, dirName)
f.logger.Info("reaping snapshot", "path", path)
if err := os.RemoveAll(path); err != nil {
f.logger.Error("failed to reap snapshot", "path", snap.Name(), "error", err)
return err
}
}
return nil
}
// ID returns the ID of the snapshot, can be used with Open()
// after the snapshot is finalized.
func (s *BoltSnapshotSink) ID() string {
s.l.Lock()
defer s.l.Unlock()
return s.meta.ID
}
func (s *BoltSnapshotSink) writeBoltDBFile() error {
// Create a new path
name := snapshotName(s.meta.Term, s.meta.Index)
path := filepath.Join(s.store.path, name+tmpSuffix)
s.logger.Info("creating new snapshot", "path", path)
// Make the directory
if err := os.MkdirAll(path, 0o700); err != nil {
s.logger.Error("failed to make snapshot directory", "error", err)
return err
}
// Create the BoltDB file
dbPath := filepath.Join(path, databaseFilename)
boltDB, err := bolt.Open(dbPath, 0o600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return err
}
// Write the snapshot metadata
if err := writeSnapshotMetaToDB(&s.meta, boltDB); err != nil {
return err
}
// Set the snapshot ID to the generated name.
s.meta.ID = name
// Create the done channel
s.doneWritingCh = make(chan struct{})
// Store the directories so we can commit the changes on success or abort
// them on failure.
s.dir = path
s.parentDir = s.store.path
// Create a pipe so we pipe writes into the go routine below.
reader, writer := io.Pipe()
s.writer = writer
// Start a go routine in charge of piping data from the snapshot's Write
// call to the delimtedreader and the BoltDB file.
go func() {
defer close(s.doneWritingCh)
defer boltDB.Close()
// The delimted reader will parse full proto messages from the snapshot
// data.
protoReader := NewDelimitedReader(reader, math.MaxInt32)
defer protoReader.Close()
var done bool
var keys int
entry := new(pb.StorageEntry)
for !done {
err := boltDB.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(dataBucketName)
if err != nil {
return err
}
// Commit in batches of 50k. Bolt holds all the data in memory and
// doesn't split the pages until commit so we do incremental writes.
for i := 0; i < 50000; i++ {
err := protoReader.ReadMsg(entry)
if err != nil {
if err == io.EOF {
done = true
return nil
}
return err
}
err = b.Put([]byte(entry.Key), entry.Value)
if err != nil {
return err
}
keys += 1
}
return nil
})
if err != nil {
s.logger.Error("snapshot write: failed to write transaction", "error", err)
s.writeError = err
return
}
s.logger.Trace("snapshot write: writing keys", "num_written", keys)
}
}()
return nil
}
// Write is used to append to the bolt file. The first call to write ensures we
// have the file created.
func (s *BoltSnapshotSink) Write(b []byte) (int, error) {
s.l.Lock()
defer s.l.Unlock()
// If this is the first call to Write we need to setup the boltDB file and
// kickoff the pipeline write
if previouslyWritten := s.written.Swap(true); !previouslyWritten {
// Reap any old snapshots
if err := s.store.ReapSnapshots(); err != nil {
return 0, err
}
if err := s.writeBoltDBFile(); err != nil {
return 0, err
}
}
return s.writer.Write(b)
}
// Close is used to indicate a successful end.
func (s *BoltSnapshotSink) Close() error {
s.l.Lock()
defer s.l.Unlock()
// Make sure close is idempotent
if s.closed {
return nil
}
s.closed = true
if s.writer != nil {
s.writer.Close()
<-s.doneWritingCh
if s.writeError != nil {
// If we encountered an error while writing then we should remove
// the directory and return the error
_ = os.RemoveAll(s.dir)
return s.writeError
}
// Move the directory into place
newPath := strings.TrimSuffix(s.dir, tmpSuffix)
var err error
if runtime.GOOS != "windows" {
err = safeio.Rename(s.dir, newPath)
} else {
err = os.Rename(s.dir, newPath)
}
if err != nil {
s.logger.Error("failed to move snapshot into place", "error", err)
return err
}
}
return nil
}
// Cancel is used to indicate an unsuccessful end.
func (s *BoltSnapshotSink) Cancel() error {
s.l.Lock()
defer s.l.Unlock()
// Make sure close is idempotent
if s.closed {
return nil
}
s.closed = true
if s.writer != nil {
s.writer.Close()
<-s.doneWritingCh
// Attempt to remove all artifacts
return os.RemoveAll(s.dir)
}
return nil
}
type boltSnapshotInstaller struct {
io.ReadCloser
meta *raft.SnapshotMeta
filename string
}
func (i *boltSnapshotInstaller) Filename() string {
return i.filename
}
func (i *boltSnapshotInstaller) Metadata() *raft.SnapshotMeta {
return i.meta
}
func (i *boltSnapshotInstaller) Install(filename string) error {
if len(i.filename) == 0 {
return errors.New("snapshot filename empty")
}
if len(filename) == 0 {
return errors.New("fsm filename empty")
}
// Rename the snapshot to the FSM location
if runtime.GOOS != "windows" {
return safeio.Rename(i.filename, filename)
} else {
return os.Rename(i.filename, filename)
}
}
// snapshotName generates a name for the snapshot.
func snapshotName(term, index uint64) string {
now := time.Now()
msec := now.UnixNano() / int64(time.Millisecond)
return fmt.Sprintf("%d-%d-%d", term, index, msec)
}