-
Notifications
You must be signed in to change notification settings - Fork 151
/
snapshot.go
672 lines (566 loc) · 24.7 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package snapshot
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/syncutils"
"github.com/iotaledger/hornet/pkg/common"
"github.com/iotaledger/hornet/pkg/dag"
"github.com/iotaledger/hornet/pkg/database"
"github.com/iotaledger/hornet/pkg/model/hornet"
"github.com/iotaledger/hornet/pkg/model/milestone"
"github.com/iotaledger/hornet/pkg/model/storage"
"github.com/iotaledger/hornet/pkg/model/syncmanager"
"github.com/iotaledger/hornet/pkg/model/utxo"
"github.com/iotaledger/hornet/pkg/utils"
)
var (
// Returned when a critical error stops the execution of a task.
ErrCritical = errors.New("critical error")
// Returned when unsupported snapshot data is read.
ErrUnsupportedSnapshot = errors.New("unsupported snapshot data")
// Returned when a child message wasn't found.
ErrChildMsgNotFound = errors.New("child message not found")
// Returned when the milestone diff that should be applied is not the current or next milestone.
ErrWrongMilestoneDiffIndex = errors.New("wrong milestone diff index")
// Returned when the final milestone after loading the snapshot is not equal to the solid entry point index.
ErrFinalLedgerIndexDoesNotMatchSEPIndex = errors.New("final ledger index does not match solid entry point index")
// Returned when a delta snapshot is available, but no full snapshot is found.
ErrInvalidSnapshotAvailabilityState = errors.New("invalid snapshot files availability")
ErrNoSnapshotSpecified = errors.New("no snapshot file was specified in the config")
ErrNoSnapshotDownloadURL = errors.New("no download URL specified for snapshot files in config")
ErrSnapshotDownloadWasAborted = errors.New("snapshot download was aborted")
ErrSnapshotDownloadNoValidSource = errors.New("no valid source found, snapshot download not possible")
ErrSnapshotCreationWasAborted = errors.New("operation was aborted")
ErrSnapshotCreationFailed = errors.New("creating snapshot failed")
ErrTargetIndexTooNew = errors.New("snapshot target is too new")
ErrTargetIndexTooOld = errors.New("snapshot target is too old")
ErrNotEnoughHistory = errors.New("not enough history")
ErrNoPruningNeeded = errors.New("no pruning needed")
ErrPruningAborted = errors.New("pruning was aborted")
ErrDatabaseCompactionNotSupported = errors.New("database compaction not supported")
ErrDatabaseCompactionRunning = errors.New("database compaction is running")
ErrExistingDeltaSnapshotWrongLedgerIndex = errors.New("existing delta ledger snapshot has wrong ledger index")
)
type snapshotAvailability byte
const (
snapshotAvailBoth snapshotAvailability = iota
snapshotAvailOnlyFull
snapshotAvailNone
)
// SnapshotManager handles reading and writing snapshot data.
type SnapshotManager struct {
// the logger used to log events.
*utils.WrappedLogger
tangleDatabase *database.Database
utxoDatabase *database.Database
storage *storage.Storage
syncManager *syncmanager.SyncManager
utxoManager *utxo.Manager
snapshotCreationEnabled bool
networkID uint64
networkIDSource string
snapshotFullPath string
snapshotDeltaPath string
deltaSnapshotSizeThresholdPercentage float64
downloadTargets []*DownloadTarget
solidEntryPointCheckThresholdPast milestone.Index
solidEntryPointCheckThresholdFuture milestone.Index
additionalPruningThreshold milestone.Index
snapshotDepth milestone.Index
snapshotInterval milestone.Index
pruningMilestonesEnabled bool
pruningMilestonesMaxMilestonesToKeep milestone.Index
pruningSizeEnabled bool
pruningSizeTargetSizeBytes int64
pruningSizeThresholdPercentage float64
pruningSizeCooldownTime time.Duration
pruneReceipts bool
snapshotLock syncutils.Mutex
statusLock syncutils.RWMutex
isSnapshotting bool
isPruning bool
lastPruningBySizeTime time.Time
Events *Events
}
// NewSnapshotManager creates a new snapshot manager instance.
func NewSnapshotManager(
log *logger.Logger,
tangleDatabase *database.Database,
utxoDatabase *database.Database,
storage *storage.Storage,
syncManager *syncmanager.SyncManager,
utxoManager *utxo.Manager,
snapshotCreationEnabled bool,
networkID uint64,
networkIDSource string,
snapshotFullPath string,
snapshotDeltaPath string,
deltaSnapshotSizeThresholdPercentage float64,
downloadTargets []*DownloadTarget,
solidEntryPointCheckThresholdPast milestone.Index,
solidEntryPointCheckThresholdFuture milestone.Index,
additionalPruningThreshold milestone.Index,
snapshotDepth milestone.Index,
snapshotInterval milestone.Index,
pruningMilestonesEnabled bool,
pruningMilestonesMaxMilestonesToKeep milestone.Index,
pruningSizeEnabled bool,
pruningSizeTargetSizeBytes int64,
pruningSizeThresholdPercentage float64,
pruningSizeCooldownTime time.Duration,
pruneReceipts bool) *SnapshotManager {
return &SnapshotManager{
WrappedLogger: utils.NewWrappedLogger(log),
tangleDatabase: tangleDatabase,
utxoDatabase: utxoDatabase,
snapshotCreationEnabled: snapshotCreationEnabled,
storage: storage,
syncManager: syncManager,
utxoManager: utxoManager,
networkID: networkID,
networkIDSource: networkIDSource,
snapshotFullPath: snapshotFullPath,
snapshotDeltaPath: snapshotDeltaPath,
deltaSnapshotSizeThresholdPercentage: deltaSnapshotSizeThresholdPercentage,
downloadTargets: downloadTargets,
solidEntryPointCheckThresholdPast: solidEntryPointCheckThresholdPast,
solidEntryPointCheckThresholdFuture: solidEntryPointCheckThresholdFuture,
additionalPruningThreshold: additionalPruningThreshold,
snapshotDepth: snapshotDepth,
snapshotInterval: snapshotInterval,
pruningMilestonesEnabled: pruningMilestonesEnabled,
pruningMilestonesMaxMilestonesToKeep: pruningMilestonesMaxMilestonesToKeep,
pruningSizeEnabled: pruningSizeEnabled,
pruningSizeTargetSizeBytes: pruningSizeTargetSizeBytes,
pruningSizeThresholdPercentage: pruningSizeThresholdPercentage,
pruningSizeCooldownTime: pruningSizeCooldownTime,
pruneReceipts: pruneReceipts,
Events: &Events{
SnapshotMilestoneIndexChanged: events.NewEvent(milestone.IndexCaller),
SnapshotMetricsUpdated: events.NewEvent(SnapshotMetricsCaller),
PruningMilestoneIndexChanged: events.NewEvent(milestone.IndexCaller),
PruningMetricsUpdated: events.NewEvent(PruningMetricsCaller),
},
}
}
func (s *SnapshotManager) IsSnapshottingOrPruning() bool {
s.statusLock.RLock()
defer s.statusLock.RUnlock()
return s.isSnapshotting || s.isPruning
}
func (s *SnapshotManager) shouldTakeSnapshot(confirmedMilestoneIndex milestone.Index) bool {
if !s.snapshotCreationEnabled {
return false
}
snapshotInfo := s.storage.SnapshotInfo()
if snapshotInfo == nil {
s.LogPanic("No snapshotInfo found!")
}
if (confirmedMilestoneIndex < s.snapshotDepth+s.snapshotInterval) || (confirmedMilestoneIndex-s.snapshotDepth) < snapshotInfo.PruningIndex+1+s.solidEntryPointCheckThresholdPast {
// Not enough history to calculate solid entry points
return false
}
return confirmedMilestoneIndex-(s.snapshotDepth+s.snapshotInterval) >= snapshotInfo.SnapshotIndex
}
func forEachSolidEntryPoint(
ctx context.Context,
dbStorage *storage.Storage,
targetIndex milestone.Index,
solidEntryPointCheckThresholdPast milestone.Index,
solidEntryPointConsumer func(sep *storage.SolidEntryPoint) bool) error {
solidEntryPoints := make(map[string]milestone.Index)
metadataMemcache := storage.NewMetadataMemcache(dbStorage.CachedMessageMetadata)
memcachedParentsTraverserStorage := dag.NewMemcachedParentsTraverserStorage(dbStorage, metadataMemcache)
memcachedChildrenTraverserStorage := dag.NewMemcachedChildrenTraverserStorage(dbStorage, metadataMemcache)
defer func() {
// all releases are forced since the cone is referenced and not needed anymore
memcachedParentsTraverserStorage.Cleanup(true)
memcachedChildrenTraverserStorage.Cleanup(true)
// Release all message metadata at the end
metadataMemcache.Cleanup(true)
}()
// we share the same traverser for all milestones, so we don't cleanup the cachedMessages in between.
parentsTraverser := dag.NewParentsTraverser(memcachedParentsTraverserStorage)
// isSolidEntryPoint checks whether any direct child of the given message was referenced by a milestone which is above the target milestone.
isSolidEntryPoint := func(messageID hornet.MessageID, targetIndex milestone.Index) (bool, error) {
childMessageIDs, err := memcachedChildrenTraverserStorage.ChildrenMessageIDs(messageID)
if err != nil {
return false, err
}
for _, childMessageID := range childMessageIDs {
cachedMsgMeta, err := memcachedChildrenTraverserStorage.CachedMessageMetadata(childMessageID) // meta +1
if err != nil {
return false, err
}
if cachedMsgMeta == nil {
// Ignore this message since it doesn't exist anymore
continue
}
if referenced, at := cachedMsgMeta.Metadata().ReferencedWithIndex(); referenced && (at > targetIndex) {
// referenced by a later milestone than targetIndex => solidEntryPoint
cachedMsgMeta.Release(true) // meta -1
return true, nil
}
cachedMsgMeta.Release(true) // meta -1
}
return false, nil
}
// Iterate from a reasonable old milestone to the target index to check for solid entry points
for milestoneIndex := targetIndex - solidEntryPointCheckThresholdPast; milestoneIndex <= targetIndex; milestoneIndex++ {
if err := utils.ReturnErrIfCtxDone(ctx, ErrSnapshotCreationWasAborted); err != nil {
// stop snapshot creation if node was shutdown
return err
}
cachedMilestone := dbStorage.CachedMilestoneOrNil(milestoneIndex) // milestone +1
if cachedMilestone == nil {
return errors.Wrapf(ErrCritical, "milestone (%d) not found!", milestoneIndex)
}
// Get all parents of that milestone
milestoneMessageID := cachedMilestone.Milestone().MessageID
cachedMilestone.Release(true) // milestone -1
// traverse the milestone and collect all messages that were referenced by this milestone or newer
if err := parentsTraverser.Traverse(
ctx,
hornet.MessageIDs{milestoneMessageID},
// traversal stops if no more messages pass the given condition
// Caution: condition func is not in DFS order
func(cachedMsgMeta *storage.CachedMetadata) (bool, error) { // meta +1
defer cachedMsgMeta.Release(true) // meta -1
// collect all msgs that were referenced by that milestone or newer
referenced, at := cachedMsgMeta.Metadata().ReferencedWithIndex()
return referenced && at >= milestoneIndex, nil
},
// consumer
func(cachedMsgMeta *storage.CachedMetadata) error { // meta +1
defer cachedMsgMeta.Release(true) // meta -1
if err := utils.ReturnErrIfCtxDone(ctx, ErrSnapshotCreationWasAborted); err != nil {
// stop snapshot creation if node was shutdown
return err
}
messageID := cachedMsgMeta.Metadata().MessageID()
isEntryPoint, err := isSolidEntryPoint(messageID, targetIndex)
if err != nil {
return err
}
if !isEntryPoint {
return nil
}
referenced, at := cachedMsgMeta.Metadata().ReferencedWithIndex()
if !referenced {
return errors.Wrapf(ErrCritical, "solid entry point (%v) not referenced!", messageID.ToHex())
}
messageIDMapKey := messageID.ToMapKey()
if _, exists := solidEntryPoints[messageIDMapKey]; !exists {
solidEntryPoints[messageIDMapKey] = at
if !solidEntryPointConsumer(&storage.SolidEntryPoint{MessageID: messageID, Index: at}) {
return ErrSnapshotCreationWasAborted
}
}
return nil
},
// called on missing parents
// return error on missing parents
nil,
// called on solid entry points
// Ignore solid entry points (snapshot milestone included)
nil,
// the pruning target index is also a solid entry point => traverse it anyways
true); err != nil {
if errors.Is(err, common.ErrOperationAborted) {
return ErrSnapshotCreationWasAborted
}
}
}
return nil
}
func checkSnapshotLimits(
snapshotInfo *storage.SnapshotInfo,
confirmedMilestoneIndex milestone.Index,
targetIndex milestone.Index,
globalSnapshot bool,
solidEntryPointCheckThresholdPast milestone.Index,
solidEntryPointCheckThresholdFuture milestone.Index,
checkIncreasingSnapshotIndex bool) error {
var minimumIndex milestone.Index
var maximumIndex milestone.Index
if globalSnapshot {
// if we create a global snapshot, we do not need to calculate the SEP.
// we can simply take the milestone messageID of the target milestone.
minimumIndex = snapshotInfo.PruningIndex + 1
maximumIndex = confirmedMilestoneIndex
} else {
if confirmedMilestoneIndex < solidEntryPointCheckThresholdFuture {
return errors.Wrapf(ErrNotEnoughHistory, "minimum confirmed index: %d, actual confirmed index: %d", solidEntryPointCheckThresholdFuture+1, confirmedMilestoneIndex)
}
minimumIndex = solidEntryPointCheckThresholdPast + 1
maximumIndex = confirmedMilestoneIndex - solidEntryPointCheckThresholdFuture
if checkIncreasingSnapshotIndex && minimumIndex < snapshotInfo.SnapshotIndex+1 {
minimumIndex = snapshotInfo.SnapshotIndex + 1
}
if minimumIndex < snapshotInfo.PruningIndex+1+solidEntryPointCheckThresholdPast {
// since we always generate new solid entry points, we need enough history
minimumIndex = snapshotInfo.PruningIndex + 1 + solidEntryPointCheckThresholdPast
}
}
switch {
case minimumIndex > maximumIndex:
return errors.Wrapf(ErrNotEnoughHistory, "minimum index (%d) exceeds maximum index (%d)", minimumIndex, maximumIndex)
case targetIndex > maximumIndex:
return errors.Wrapf(ErrTargetIndexTooNew, "maximum: %d, actual: %d", maximumIndex, targetIndex)
case targetIndex < minimumIndex:
return errors.Wrapf(ErrTargetIndexTooOld, "minimum: %d, actual: %d", minimumIndex, targetIndex)
}
return nil
}
func (s *SnapshotManager) setIsSnapshotting(value bool) {
s.statusLock.Lock()
s.isSnapshotting = value
s.statusLock.Unlock()
}
// CreateFullSnapshot creates a full snapshot for the given target milestone index.
func (s *SnapshotManager) CreateFullSnapshot(ctx context.Context, targetIndex milestone.Index, filePath string, writeToDatabase bool) error {
s.snapshotLock.Lock()
defer s.snapshotLock.Unlock()
return s.createSnapshotWithoutLocking(ctx, Full, targetIndex, filePath, writeToDatabase)
}
// CreateDeltaSnapshot creates a delta snapshot for the given target milestone index.
func (s *SnapshotManager) CreateDeltaSnapshot(ctx context.Context, targetIndex milestone.Index, filePath string, writeToDatabase bool, snapshotFullPath ...string) error {
s.snapshotLock.Lock()
defer s.snapshotLock.Unlock()
return s.createSnapshotWithoutLocking(ctx, Delta, targetIndex, filePath, writeToDatabase, snapshotFullPath...)
}
// LoadSnapshotFromFile loads a snapshot file from the given file path into the storage.
func (s *SnapshotManager) LoadSnapshotFromFile(ctx context.Context, snapshotType Type, networkID uint64, filePath string) (err error) {
s.LogInfof("importing %s snapshot file...", snapshotNames[snapshotType])
ts := time.Now()
header, err := loadSnapshotFileToStorage(ctx, s.storage, snapshotType, filePath, networkID)
if err != nil {
return err
}
if err := s.syncManager.SetConfirmedMilestoneIndex(header.SEPMilestoneIndex, false); err != nil {
return fmt.Errorf("SetConfirmedMilestoneIndex failed: %w", err)
}
s.LogInfof("imported %s snapshot file, took %v", snapshotNames[snapshotType], time.Since(ts).Truncate(time.Millisecond))
s.LogInfof("solid entry points: %d, outputs: %d, ms diffs: %d", header.SEPCount, header.OutputCount, header.MilestoneDiffCount)
s.LogInfof(`
SnapshotInfo:
Type: %s
NetworkID: %d
SnapshotIndex: %d
EntryPointIndex: %d
PruningIndex: %d
Timestamp: %v`, snapshotNames[snapshotType], header.NetworkID, header.SEPMilestoneIndex, header.SEPMilestoneIndex, header.SEPMilestoneIndex, time.Unix(int64(header.Timestamp), 0))
return nil
}
// optimalSnapshotType returns the optimal snapshot type
// based on the file size of the last full and delta snapshot file.
func (s *SnapshotManager) optimalSnapshotType() (Type, error) {
if s.deltaSnapshotSizeThresholdPercentage == 0.0 {
// special case => always create a delta snapshot to keep entire milestone diff history
return Delta, nil
}
fullSnapshotFileInfo, err := os.Stat(s.snapshotFullPath)
fullSnapshotFileExists := !os.IsNotExist(err)
if !fullSnapshotFileExists {
// full snapshot doesn't exist => create a full snapshot
return Full, nil
}
if err != nil {
// there was another unknown error
return Full, err
}
deltaSnapshotFileInfo, err := os.Stat(s.snapshotDeltaPath)
deltaSnapshotFileExists := !os.IsNotExist(err)
if !deltaSnapshotFileExists {
// delta snapshot doesn't exist => create a delta snapshot
return Delta, nil
}
if err != nil {
// there was another unknown error
return Delta, err
}
// if the file size of the last delta snapshot is bigger than a certain percentage
// of the full snapshot file, it's more efficient to create a new full snapshot.
if int64(float64(fullSnapshotFileInfo.Size())*s.deltaSnapshotSizeThresholdPercentage/100.0) < deltaSnapshotFileInfo.Size() {
return Full, nil
}
return Delta, nil
}
// snapshotTypeFilePath returns the default file path
// for the given snapshot type.
func (s *SnapshotManager) snapshotTypeFilePath(snapshotType Type) string {
switch snapshotType {
case Full:
return s.snapshotFullPath
case Delta:
return s.snapshotDeltaPath
default:
panic("unknown snapshot type")
}
}
// HandleNewConfirmedMilestoneEvent handles new confirmed milestone events which may trigger a delta snapshot creation and pruning.
func (s *SnapshotManager) HandleNewConfirmedMilestoneEvent(ctx context.Context, confirmedMilestoneIndex milestone.Index) {
if !s.syncManager.IsNodeSynced() {
// do not prune or create snapshots while we are not synced
return
}
s.snapshotLock.Lock()
defer s.snapshotLock.Unlock()
if s.shouldTakeSnapshot(confirmedMilestoneIndex) {
snapshotType, err := s.optimalSnapshotType()
if err != nil {
s.LogWarnf("%s: %s", ErrSnapshotCreationFailed, err)
return
}
if err := s.createSnapshotWithoutLocking(ctx, snapshotType, confirmedMilestoneIndex-s.snapshotDepth, s.snapshotTypeFilePath(snapshotType), true); err != nil {
if errors.Is(err, ErrCritical) {
s.LogPanicf("%s: %s", ErrSnapshotCreationFailed, err)
}
s.LogWarnf("%s: %s", ErrSnapshotCreationFailed, err)
}
if !s.syncManager.IsNodeSynced() {
// do not prune while we are not synced
return
}
}
var targetIndex milestone.Index = 0
if s.pruningMilestonesEnabled && confirmedMilestoneIndex > s.pruningMilestonesMaxMilestonesToKeep {
targetIndex = confirmedMilestoneIndex - s.pruningMilestonesMaxMilestonesToKeep
}
pruningBySize := false
if s.pruningSizeEnabled && (s.lastPruningBySizeTime.IsZero() || time.Since(s.lastPruningBySizeTime) > s.pruningSizeCooldownTime) {
targetIndexSize, err := s.calcTargetIndexBySize()
if err == nil && ((targetIndex == 0) || (targetIndex < targetIndexSize)) {
targetIndex = targetIndexSize
pruningBySize = true
}
}
if targetIndex == 0 {
// no pruning needed
return
}
if _, err := s.pruneDatabase(ctx, targetIndex); err != nil {
s.LogDebugf("pruning aborted: %v", err)
}
if pruningBySize {
s.lastPruningBySizeTime = time.Now()
}
}
// SnapshotsFilesLedgerIndex returns the final ledger index if the snapshots from the configured file paths would be applied.
func (s *SnapshotManager) SnapshotsFilesLedgerIndex() (milestone.Index, error) {
snapAvail, err := s.checkSnapshotFilesAvailability(s.snapshotFullPath, s.snapshotDeltaPath)
if err != nil {
return 0, err
}
if snapAvail == snapshotAvailNone {
return 0, errors.New("no snapshot files available")
}
fullHeader, err := ReadSnapshotHeaderFromFile(s.snapshotFullPath)
if err != nil {
return 0, err
}
var deltaHeader *ReadFileHeader
if snapAvail == snapshotAvailBoth {
deltaHeader, err = ReadSnapshotHeaderFromFile(s.snapshotDeltaPath)
if err != nil {
return 0, err
}
}
return getSnapshotFilesLedgerIndex(fullHeader, deltaHeader), nil
}
// ImportSnapshots imports snapshot data from the configured file paths.
// automatically downloads snapshot data if no files are available.
func (s *SnapshotManager) ImportSnapshots(ctx context.Context) error {
snapAvail, err := s.checkSnapshotFilesAvailability(s.snapshotFullPath, s.snapshotDeltaPath)
if err != nil {
return err
}
if snapAvail == snapshotAvailNone {
if err = s.downloadSnapshotFiles(ctx, s.networkID, s.snapshotFullPath, s.snapshotDeltaPath); err != nil {
return err
}
}
snapAvail, err = s.checkSnapshotFilesAvailability(s.snapshotFullPath, s.snapshotDeltaPath)
if err != nil {
return err
}
if snapAvail == snapshotAvailNone {
return errors.New("no snapshot files available after snapshot download")
}
if err = s.LoadSnapshotFromFile(ctx, Full, s.networkID, s.snapshotFullPath); err != nil {
_ = s.storage.MarkDatabasesCorrupted()
return err
}
if snapAvail == snapshotAvailOnlyFull {
return nil
}
if err = s.LoadSnapshotFromFile(ctx, Delta, s.networkID, s.snapshotDeltaPath); err != nil {
_ = s.storage.MarkDatabasesCorrupted()
return err
}
return nil
}
// checks that either both snapshot files are available, only the full snapshot or none.
func (s *SnapshotManager) checkSnapshotFilesAvailability(fullPath string, deltaPath string) (snapshotAvailability, error) {
switch {
case len(fullPath) == 0:
return 0, fmt.Errorf("%w: full snapshot file path not defined", ErrNoSnapshotSpecified)
case len(deltaPath) == 0:
return 0, fmt.Errorf("%w: delta snapshot file path not defined", ErrNoSnapshotSpecified)
}
_, fullSnapshotStatErr := os.Stat(fullPath)
_, deltaSnapshotStatErr := os.Stat(deltaPath)
switch {
case os.IsNotExist(fullSnapshotStatErr) && deltaSnapshotStatErr == nil:
// only having the delta snapshot file does not make sense,
// as it relies on a full snapshot file to be available.
// downloading the full snapshot would not help, as it will probably
// be incompatible with the delta snapshot index.
return 0, fmt.Errorf("%w: there exists a delta snapshot but not a full snapshot file, delete the delta snapshot file and restart", ErrInvalidSnapshotAvailabilityState)
case os.IsNotExist(fullSnapshotStatErr) && os.IsNotExist(deltaSnapshotStatErr):
return snapshotAvailNone, nil
case fullSnapshotStatErr == nil && os.IsNotExist(deltaSnapshotStatErr):
return snapshotAvailOnlyFull, nil
default:
return snapshotAvailBoth, nil
}
}
// ensures that the folders to both paths exists and then downloads the appropriate snapshot files.
func (s *SnapshotManager) downloadSnapshotFiles(ctx context.Context, wantedNetworkID uint64, fullPath string, deltaPath string) error {
fullPathDir := filepath.Dir(fullPath)
deltaPathDir := filepath.Dir(deltaPath)
if err := os.MkdirAll(fullPathDir, 0700); err != nil {
return fmt.Errorf("could not create snapshot dir '%s': %w", fullPath, err)
}
if err := os.MkdirAll(deltaPathDir, 0700); err != nil {
return fmt.Errorf("could not create snapshot dir '%s': %w", fullPath, err)
}
if len(s.downloadTargets) == 0 {
return ErrNoSnapshotDownloadURL
}
targetsJSON, err := json.MarshalIndent(s.downloadTargets, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal targets into formatted JSON: %w", err)
}
s.LogInfof("downloading snapshot files from one of the provided sources %s", string(targetsJSON))
if err := s.DownloadSnapshotFiles(ctx, wantedNetworkID, fullPath, deltaPath, s.downloadTargets); err != nil {
return fmt.Errorf("unable to download snapshot files: %w", err)
}
s.LogInfo("snapshot download finished")
return nil
}
// CheckCurrentSnapshot checks that the current snapshot info is valid regarding its network ID and the ledger state.
func (s *SnapshotManager) CheckCurrentSnapshot(snapshotInfo *storage.SnapshotInfo) error {
// check that the stored snapshot corresponds to the wanted network ID
if snapshotInfo.NetworkID != s.networkID {
s.LogPanicf("node is configured to operate in network %d/%s but the stored snapshot data corresponds to %d", s.networkID, s.networkIDSource, snapshotInfo.NetworkID)
}
return nil
}