Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cherry-pick #765] Retry to take full snapshot if prev full snapshot fails due to any reason. #767

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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