diff --git a/internal/backupcontroller/altinitystrategy_archive_test.go b/internal/backupcontroller/altinitystrategy_archive_test.go index e5871ef80..b8865c71a 100644 --- a/internal/backupcontroller/altinitystrategy_archive_test.go +++ b/internal/backupcontroller/altinitystrategy_archive_test.go @@ -566,6 +566,101 @@ func TestCleanupAltinityBackup_FailedJobBacksOff(t *testing.T) { } } +// Recording the failure updates the Backup, which starts the next reconcile +// at once, and the Job cache can still hold the failed Job the controller has +// just deleted. That Job is counted once: seen again, it is deleted without a +// second Event and without doubling the wait. +func TestCleanupAltinityBackup_CountsAFailedJobOnce(t *testing.T) { + backup := altinityCleanupBackup("ch-bk") + retryAt := time.Now().Add(altinityCleanupRetryBase).UTC().Format(time.RFC3339) + backup.Annotations = map[string]string{ + altinityCleanupFailuresAnnotation: "1", + altinityCleanupRetryAfterAnnotation: retryAt, + altinityCleanupFailedJobAnnotation: "failed-job-uid", + } + failed := ownCleanupJob(backup) + failed.UID = "failed-job-uid" + failed.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobFailed, Status: corev1.ConditionTrue, Reason: "BackoffLimitExceeded"}} + c, s := newAltinityCleanupClient(append(altinityCleanupFixtures(backup), failed)...) + rec := record.NewFakeRecorder(10) + r := &BackupReconciler{Client: c, Scheme: s, Recorder: rec} + res, err := r.cleanupOnDelete(context.Background(), backup) + if err != nil { + t.Fatalf("cleanupOnDelete: %v", err) + } + if res.RequeueAfter <= 0 || res.RequeueAfter > altinityCleanupRetryBase { + t.Errorf("RequeueAfter = %s, want what is left of the wait already set", res.RequeueAfter) + } + got := &backupsv1alpha1.Backup{} + if err := c.Get(context.Background(), client.ObjectKeyFromObject(backup), got); err != nil { + t.Fatalf("get Backup: %v", err) + } + if f := got.Annotations[altinityCleanupFailuresAnnotation]; f != "1" { + t.Errorf("failures = %q, want the one failure counted once", f) + } + if at := got.Annotations[altinityCleanupRetryAfterAnnotation]; at != retryAt { + t.Errorf("retry-after = %q, want the one already set, %q", at, retryAt) + } + if len(rec.Events) != 0 { + t.Errorf("unexpected event %q for a failure already reported", <-rec.Events) + } + if left := listJobs(t, c); len(left) != 0 { + t.Errorf("expected the failed Job collected, %d left", len(left)) + } +} + +// A counted Job seen again after its wait has run out still holds the Backup: +// releasing it there would drop the finalizer with the archive undeleted. +func TestCleanupAltinityBackup_KeepsTheBackupWhenACountedJobOutlivesItsWait(t *testing.T) { + backup := altinityCleanupBackup("ch-bk") + backup.Annotations = map[string]string{ + altinityCleanupFailuresAnnotation: "1", + altinityCleanupRetryAfterAnnotation: time.Now().Add(-time.Minute).UTC().Format(time.RFC3339), + altinityCleanupFailedJobAnnotation: "failed-job-uid", + } + failed := ownCleanupJob(backup) + failed.UID = "failed-job-uid" + failed.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobFailed, Status: corev1.ConditionTrue, Reason: "BackoffLimitExceeded"}} + c, s := newAltinityCleanupClient(append(altinityCleanupFixtures(backup), failed)...) + r := &BackupReconciler{Client: c, Scheme: s, Recorder: record.NewFakeRecorder(10)} + res, err := r.cleanupOnDelete(context.Background(), backup) + if err != nil { + t.Fatalf("cleanupOnDelete: %v", err) + } + if res.RequeueAfter <= 0 { + t.Errorf("RequeueAfter = %s: the Backup must stay until a delete succeeds", res.RequeueAfter) + } +} + +// A new failed Job, under the same name, is counted even when an earlier one +// was: its UID is not the one recorded. +func TestCleanupAltinityBackup_CountsANewFailedJob(t *testing.T) { + backup := altinityCleanupBackup("ch-bk") + backup.Annotations = map[string]string{ + altinityCleanupFailuresAnnotation: "1", + altinityCleanupFailedJobAnnotation: "earlier-job-uid", + } + failed := ownCleanupJob(backup) + failed.UID = "new-job-uid" + failed.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobFailed, Status: corev1.ConditionTrue, Reason: "BackoffLimitExceeded"}} + c, s := newAltinityCleanupClient(append(altinityCleanupFixtures(backup), failed)...) + rec := record.NewFakeRecorder(10) + r := &BackupReconciler{Client: c, Scheme: s, Recorder: rec} + if _, err := r.cleanupOnDelete(context.Background(), backup); err != nil { + t.Fatalf("cleanupOnDelete: %v", err) + } + got := &backupsv1alpha1.Backup{} + if err := c.Get(context.Background(), client.ObjectKeyFromObject(backup), got); err != nil { + t.Fatalf("get Backup: %v", err) + } + if f, j := got.Annotations[altinityCleanupFailuresAnnotation], got.Annotations[altinityCleanupFailedJobAnnotation]; f != "2" || j != "new-job-uid" { + t.Errorf("failures = %q, counted Job = %q; want 2 and new-job-uid", f, j) + } + if len(rec.Events) != 1 { + t.Errorf("events = %d, want one for the new failure", len(rec.Events)) + } +} + // The wait is recorded before the failed Job is deleted, so a delete that // fails leaves the Backup waiting rather than free to start the next Job at // once. diff --git a/internal/backupcontroller/altinitystrategy_controller.go b/internal/backupcontroller/altinitystrategy_controller.go index 806883ed0..ff45a910a 100644 --- a/internal/backupcontroller/altinitystrategy_controller.go +++ b/internal/backupcontroller/altinitystrategy_controller.go @@ -75,9 +75,11 @@ const ( // The cleanup Job that failed is deleted to make room for the next one, // so the count of failed attempts and the time the next may start are - // kept on the Backup. + // kept on the Backup, with the UID of the Job last counted, so that one + // still served from the cache is not counted twice. altinityCleanupFailuresAnnotation = "altinity.strategy.backups.cozystack.io/cleanup-failures" altinityCleanupRetryAfterAnnotation = "altinity.strategy.backups.cozystack.io/cleanup-retry-after" + altinityCleanupFailedJobAnnotation = "altinity.strategy.backups.cozystack.io/cleanup-failed-job" // altinityCleanupRetryBase is the wait after the first failed cleanup // Job; it doubles with each further failure up to altinityCleanupRetryMax. @@ -1213,6 +1215,19 @@ func (r *BackupReconciler) cleanupAltinityBackup(ctx context.Context, backup *ba logger.Debug("Altinity archive deleted", "backup", backup.Name, "archive", archive) return ctrl.Result{}, nil case batchv1.JobFailed: + // Recording a failure updates the Backup, which starts the next + // reconcile while the Job cache may still hold the Job just deleted; + // a failure already counted is not counted or reported again. + if job.UID != "" && backup.Annotations[altinityCleanupFailedJobAnnotation] == string(job.UID) { + if derr := r.deleteAltinityCleanupJob(ctx, job); derr != nil { + return ctrl.Result{}, derr + } + wait := altinityCleanupRetryWait(ctx, backup, time.Now()) + if wait <= 0 { + wait = altinityPollInterval + } + return ctrl.Result{RequeueAfter: wait}, nil + } failures := altinityCleanupFailures(ctx, backup) + 1 delay := altinityCleanupRetryDelay(failures) logger.Debug("Altinity cleanup Job failed; retrying", "backup", backup.Name, "job", job.Name, "failures", failures, "delay", delay) @@ -1223,7 +1238,7 @@ func (r *BackupReconciler) cleanupAltinityBackup(ctx context.Context, backup *ba "cleanup Job %s did not delete archive %s (%s: %s); retrying in %s", job.Name, archive, c.Reason, c.Message, delay) } // Recorded before the Job goes: should the delete below fail, the next - // reconcile counts this failure again and waits longer, rather than + // reconcile finds this Job counted and keeps waiting, rather than // starting the next Job at once. base := backup.DeepCopy() if backup.Annotations == nil { @@ -1231,6 +1246,7 @@ func (r *BackupReconciler) cleanupAltinityBackup(ctx context.Context, backup *ba } backup.Annotations[altinityCleanupFailuresAnnotation] = strconv.Itoa(failures) backup.Annotations[altinityCleanupRetryAfterAnnotation] = time.Now().Add(delay).UTC().Format(time.RFC3339) + backup.Annotations[altinityCleanupFailedJobAnnotation] = string(job.UID) if perr := r.Patch(ctx, backup, client.MergeFrom(base)); perr != nil { return ctrl.Result{}, perr }