From a2f7ea18eef2b7f89c094a9f502e09b8a7b9ad38 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Mon, 23 Nov 2026 12:14:57 +0300 Subject: [PATCH] fix(clickhouse): drop the predecessor wait once a run's own Job starts A BackupJob or RestoreJob that waited for the Job of an earlier owner of its name kept its Ready=False PredecessorJobPending condition after that Job was gone and its own Job had started, so for the whole run it still said it was waiting for the garbage collector. The condition is now dropped when the run's own Job is running, the way the credentials wait is. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- .../altinitystrategy_controller.go | 23 ++++++++++--- .../altinitystrategy_controller_test.go | 32 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/internal/backupcontroller/altinitystrategy_controller.go b/internal/backupcontroller/altinitystrategy_controller.go index 7e5b68373..accff2f70 100644 --- a/internal/backupcontroller/altinitystrategy_controller.go +++ b/internal/backupcontroller/altinitystrategy_controller.go @@ -180,6 +180,17 @@ func clearCredentialsWait(conditions *[]metav1.Condition) bool { return apimeta.RemoveStatusCondition(conditions, "Ready") } +// clearPredecessorWait drops the Ready condition a run set while it waited +// for the Job of an earlier owner of its name, once its own Job runs. It +// reports whether it did. +func clearPredecessorWait(conditions *[]metav1.Condition) bool { + ready := apimeta.FindStatusCondition(*conditions, "Ready") + if ready == nil || ready.Reason != altinityPredecessorJobReason { + return false + } + return apimeta.RemoveStatusCondition(conditions, "Ready") +} + const credentialsWaitReason = "WaitingForCredentials" // credentialsWaitCondition is the Ready condition of a job held back by @@ -436,7 +447,7 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv apimeta.SetStatusCondition(&j.Status.Conditions, metav1.Condition{ Type: "Ready", Status: metav1.ConditionFalse, - Reason: "PredecessorJobPending", + Reason: altinityPredecessorJobReason, Message: err.Error(), }) if uerr := r.Status().Update(ctx, j); uerr != nil { @@ -494,7 +505,7 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv return r.markBackupJobFailed(ctx, j, message) default: - if clearCredentialsWait(&j.Status.Conditions) { + if clearCredentialsWait(&j.Status.Conditions) || clearPredecessorWait(&j.Status.Conditions) { if err := r.Status().Update(ctx, j); err != nil { return ctrl.Result{}, err } @@ -547,6 +558,10 @@ func (r *BackupJobReconciler) ensureAltinityJob( return desired, nil } +// altinityPredecessorJobReason is the Ready reason of a run that waits for +// the Job of an earlier owner of its name to be removed. +const altinityPredecessorJobReason = "PredecessorJobPending" + // errAltinityPredecessorJob marks a Job left under the Job name by an earlier // BackupJob or RestoreJob of the same name. Its owner is gone, so the garbage // collector removes it, and the new owner waits for that instead of failing. @@ -926,7 +941,7 @@ func (r *RestoreJobReconciler) reconcileAltinityRestore(ctx context.Context, res apimeta.SetStatusCondition(&restoreJob.Status.Conditions, metav1.Condition{ Type: "Ready", Status: metav1.ConditionFalse, - Reason: "PredecessorJobPending", + Reason: altinityPredecessorJobReason, Message: err.Error(), }) if uerr := r.Status().Update(ctx, restoreJob); uerr != nil { @@ -962,7 +977,7 @@ func (r *RestoreJobReconciler) reconcileAltinityRestore(ctx context.Context, res return r.markRestoreJobFailed(ctx, restoreJob, message) default: - if clearCredentialsWait(&restoreJob.Status.Conditions) { + if clearCredentialsWait(&restoreJob.Status.Conditions) || clearPredecessorWait(&restoreJob.Status.Conditions) { if err := r.Status().Update(ctx, restoreJob); err != nil { return ctrl.Result{}, err } diff --git a/internal/backupcontroller/altinitystrategy_controller_test.go b/internal/backupcontroller/altinitystrategy_controller_test.go index 00dd83a52..d8547320e 100644 --- a/internal/backupcontroller/altinitystrategy_controller_test.go +++ b/internal/backupcontroller/altinitystrategy_controller_test.go @@ -1805,6 +1805,22 @@ func TestReconcileAltinity_DoesNotReuseAJobItDoesNotControl(t *testing.T) { if c := meta.FindStatusCondition(got.Status.Conditions, "Ready"); c == nil || !strings.Contains(c.Message, job.Name) { t.Errorf("Ready condition %+v does not say which Job it waits for", c) } + + // Once the garbage collector has removed the earlier Job, this + // run's own Job starts and the wait is over. + if err := r.Delete(ctx, job); err != nil { + t.Fatalf("remove the earlier Job: %v", err) + } + if _, err := r.reconcileAltinity(ctx, got, altinityResolved(strategy.Name)); err != nil { + t.Fatalf("reconcileAltinity after the earlier Job went: %v", err) + } + after := &backupsv1alpha1.BackupJob{} + if err := r.Get(ctx, client.ObjectKeyFromObject(bj), after); err != nil { + t.Fatalf("get BackupJob: %v", err) + } + if c := meta.FindStatusCondition(after.Status.Conditions, "Ready"); c != nil && c.Reason == altinityPredecessorJobReason { + t.Errorf("Ready condition %+v still says the run waits for an earlier Job while its own runs", c) + } }) } } @@ -1902,6 +1918,22 @@ func TestReconcileAltinityRestore_DoesNotReuseAJobItDoesNotControl(t *testing.T) if c := meta.FindStatusCondition(got.Status.Conditions, "Ready"); c == nil || !strings.Contains(c.Message, job.Name) { t.Errorf("Ready condition %+v does not say which Job it waits for", c) } + + // Once the garbage collector has removed the earlier Job, this + // run's own Job starts and the wait is over. + if err := rr.Delete(ctx, job); err != nil { + t.Fatalf("remove the earlier Job: %v", err) + } + if _, err := rr.reconcileAltinityRestore(ctx, got, backup); err != nil { + t.Fatalf("reconcileAltinityRestore after the earlier Job went: %v", err) + } + after := &backupsv1alpha1.RestoreJob{} + if err := rr.Get(ctx, client.ObjectKeyFromObject(restoreJob), after); err != nil { + t.Fatalf("get RestoreJob: %v", err) + } + if c := meta.FindStatusCondition(after.Status.Conditions, "Ready"); c != nil && c.Reason == altinityPredecessorJobReason { + t.Errorf("Ready condition %+v still says the run waits for an earlier Job while its own runs", c) + } }) } }