Skip to content

Commit

Permalink
Retry to take full snapshot if prev full snapshot fails due to some r…
Browse files Browse the repository at this point in the history
…eason. (#765) (#767)

* Retry to take full snapshot if prev full snapshot fails.

* Retry with backoff.

* Move checking inside func IsFullSnapshotRequiredAtStartup().

* Fixed unit tests.

* Added unit test.

Address review comments.
  • Loading branch information
ishan16696 authored Aug 30, 2024
1 parent 7c9d9fa commit f69d584
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
5 changes: 4 additions & 1 deletion pkg/server/backuprestoreserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,17 @@ func (b *BackupRestoreServer) runEtcdProbeLoopWithSnapshotter(ctx context.Contex
if !initialDeltaSnapshotTaken {
// need to take a full snapshot here
// if initial deltaSnapshot is not taken
// or previous full snapshot wasn't successful
var snapshot *brtypes.Snapshot
metrics.SnapshotRequired.With(prometheus.Labels{metrics.LabelKind: brtypes.SnapshotKindDelta}).Set(0)
metrics.SnapshotRequired.With(prometheus.Labels{metrics.LabelKind: brtypes.SnapshotKindFull}).Set(1)
if snapshot, err = ssr.TakeFullSnapshotAndResetTimer(false); err != nil {
metrics.SnapshotterOperationFailure.With(prometheus.Labels{metrics.LabelError: err.Error()}).Inc()
b.logger.Errorf("Failed to take substitute first full snapshot: %v", err)
ssr.PrevFullSnapshotSucceeded = false
b.logger.Errorf("Failed to take substitute full snapshot: %v", err)
continue
}
ssr.PrevFullSnapshotSucceeded = true
if b.config.HealthConfig.SnapshotLeaseRenewalEnabled {
leaseUpdatectx, cancel := context.WithTimeout(ctx, brtypes.LeaseUpdateTimeoutDuration)
defer cancel()
Expand Down
46 changes: 26 additions & 20 deletions pkg/snapshot/snapshotter/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type Snapshotter struct {
K8sClientset client.Client
snapstoreConfig *brtypes.SnapstoreConfig
lastSecretModifiedTime time.Time
PrevFullSnapshotSucceeded bool
}

// NewSnapshotter returns the snapshotter object.
Expand Down Expand Up @@ -149,25 +150,26 @@ func NewSnapshotter(logger *logrus.Entry, config *brtypes.SnapshotterConfig, sto
}

return &Snapshotter{
logger: logger.WithField("actor", "snapshotter"),
store: store,
config: config,
etcdConnectionConfig: etcdConnectionConfig,
compressionConfig: compressionConfig,
HealthConfig: healthConfig,
schedule: sdl,
PrevSnapshot: prevSnapshot,
PrevFullSnapshot: fullSnap,
PrevDeltaSnapshots: deltaSnapList,
SsrState: brtypes.SnapshotterInactive,
SsrStateMutex: &sync.Mutex{},
fullSnapshotReqCh: make(chan bool),
deltaSnapshotReqCh: make(chan struct{}),
fullSnapshotAckCh: make(chan result),
deltaSnapshotAckCh: make(chan result),
cancelWatch: func() {},
K8sClientset: clientSet,
snapstoreConfig: storeConfig,
logger: logger.WithField("actor", "snapshotter"),
store: store,
config: config,
etcdConnectionConfig: etcdConnectionConfig,
compressionConfig: compressionConfig,
HealthConfig: healthConfig,
schedule: sdl,
PrevSnapshot: prevSnapshot,
PrevFullSnapshot: fullSnap,
PrevDeltaSnapshots: deltaSnapList,
SsrState: brtypes.SnapshotterInactive,
SsrStateMutex: &sync.Mutex{},
fullSnapshotReqCh: make(chan bool),
deltaSnapshotReqCh: make(chan struct{}),
fullSnapshotAckCh: make(chan result),
deltaSnapshotAckCh: make(chan result),
cancelWatch: func() {},
K8sClientset: clientSet,
snapstoreConfig: storeConfig,
PrevFullSnapshotSucceeded: true,
}, nil
}

Expand Down Expand Up @@ -648,8 +650,10 @@ func (ssr *Snapshotter) snapshotEventHandler(stopCh <-chan struct{}) error {
}
ssr.fullSnapshotAckCh <- res
if err != nil {
ssr.PrevFullSnapshotSucceeded = false
return err
}
ssr.PrevFullSnapshotSucceeded = true
if ssr.HealthConfig.SnapshotLeaseRenewalEnabled {
ssr.FullSnapshotLeaseUpdateTimer.Stop()
ssr.FullSnapshotLeaseUpdateTimer.Reset(time.Nanosecond)
Expand All @@ -675,8 +679,10 @@ func (ssr *Snapshotter) snapshotEventHandler(stopCh <-chan struct{}) error {

case <-ssr.fullSnapshotTimer.C:
if _, err := ssr.TakeFullSnapshotAndResetTimer(false); err != nil {
ssr.PrevFullSnapshotSucceeded = false
return err
}
ssr.PrevFullSnapshotSucceeded = true
if ssr.HealthConfig.SnapshotLeaseRenewalEnabled {
ssr.FullSnapshotLeaseUpdateTimer.Stop()
ssr.FullSnapshotLeaseUpdateTimer.Reset(time.Nanosecond)
Expand Down Expand Up @@ -764,7 +770,7 @@ func (ssr *Snapshotter) hasSnapStoreSecretUpdated() (bool, error) {

// IsFullSnapshotRequiredAtStartup checks whether to take a full snapshot or not during the startup of backup-restore.
func (ssr *Snapshotter) IsFullSnapshotRequiredAtStartup(timeWindow float64) bool {
if ssr.PrevFullSnapshot == nil || ssr.PrevFullSnapshot.IsFinal || time.Since(ssr.PrevFullSnapshot.CreatedOn).Hours() > timeWindow {
if ssr.PrevFullSnapshot == nil || ssr.PrevFullSnapshot.IsFinal || time.Since(ssr.PrevFullSnapshot.CreatedOn).Hours() > timeWindow || !ssr.PrevFullSnapshotSucceeded {
return true
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/snapshot/snapshotter/snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,22 @@ var _ = Describe("Snapshotter", func() {
})
})

Context("Previous full snapshot was not successful", func() {
It("should return true", func() {
snapshotterConfig := &brtypes.SnapshotterConfig{
FullSnapshotSchedule: fmt.Sprintf("%d %d * * *", (currentMin+1)%60, (currentHour+2)%24),
}

ssr, err = NewSnapshotter(logger, snapshotterConfig, store, etcdConnectionConfig, compressionConfig, healthConfig, snapstoreConfig)
Expect(err).ShouldNot(HaveOccurred())

// previous full snapshot wasn't successful
ssr.PrevFullSnapshotSucceeded = false
isFullSnapMissed := ssr.IsFullSnapshotRequiredAtStartup(fullSnapshotTimeWindow)
Expect(isFullSnapMissed).Should(BeTrue())
})
})

Context("Previous full snapshot was taken exactly at scheduled snapshot time, no FullSnapshot was missed", func() {
It("should return false", func() {
snapshotterConfig := &brtypes.SnapshotterConfig{
Expand All @@ -803,6 +819,7 @@ var _ = Describe("Snapshotter", func() {
ssr.PrevFullSnapshot = &brtypes.Snapshot{
CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, (currentHour+2)%24, (currentMin+1)%60, 0, 0, time.Local),
}
ssr.PrevFullSnapshotSucceeded = true

isFullSnapMissed := ssr.IsFullSnapshotRequiredAtStartup(fullSnapshotTimeWindow)
Expect(isFullSnapMissed).Should(BeFalse())
Expand All @@ -823,6 +840,7 @@ var _ = Describe("Snapshotter", func() {
ssr.PrevFullSnapshot = &brtypes.Snapshot{
CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), (currentHour-4)%24, (currentMin-10)%60, 0, 0, time.Local),
}
ssr.PrevFullSnapshotSucceeded = true
isFullSnapCanBeMissed := ssr.IsFullSnapshotRequiredAtStartup(fullSnapshotTimeWindow)
Expect(isFullSnapCanBeMissed).Should(BeFalse())
})
Expand All @@ -842,6 +860,7 @@ var _ = Describe("Snapshotter", func() {
ssr.PrevFullSnapshot = &brtypes.Snapshot{
CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), (currentHour-18)%24, (currentMin)%60, 0, 0, time.Local),
}
ssr.PrevFullSnapshotSucceeded = true
isFullSnapCanBeMissed := ssr.IsFullSnapshotRequiredAtStartup(fullSnapshotTimeWindow)
Expect(isFullSnapCanBeMissed).Should(BeTrue())
})
Expand Down

0 comments on commit f69d584

Please sign in to comment.