Skip to content

Commit 59a11c1

Browse files
committed
refactor(postgres): judge an attach record's clock drift in one place
The job that attached the plugin and a job waiting on another job's attach each parsed the Cluster's attach record and applied the clock-drift margin on their own, so the two could drift apart on the next change. Both now take the record from one parser that also refuses one too far ahead, and differ only in what they do with a record they cannot use. The margin is now pinned for the waiting job from both sides: a record a minute ahead still holds it for the rollout. Assisted-by: LLM Signed-off-by: Alexey Artamonov <[email protected]>
1 parent 5635d9d commit 59a11c1

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

‎internal/backupcontroller/cnpgstrategy_controller.go‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,6 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp
505505
// The Cluster's record covers an attach whose status write was lost.
506506
attachedAt := rollout.LastTransitionTime
507507
recorded, ok, err := cnpgRecordedAttach(attachment.attachedAt)
508-
if err == nil && ok && time.Until(recorded) > cnpgAttachClockSkewTolerance {
509-
// Every instance would predate it, and the deadline would count from it.
510-
err = fmt.Errorf("%s is in the future", attachment.attachedAt)
511-
}
512508
if err != nil {
513509
return r.markBackupJobFailed(ctx, j, fmt.Sprintf(
514510
"cannot tell when the barman-cloud plugin was attached: the %s annotation of cnpg.io/Cluster %s/%s: %v",
@@ -553,15 +549,12 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp
553549
rolledOut = true
554550
} else if existingBackup == nil && attachment.attachedAt != "" {
555551
// Another backup job attached the plugin. Its rollout can still be
556-
// running while the attach is recent, and later than that the
557-
// attaching job has failed it already. The record is that job's:
558-
// one this job cannot read does not fail it, and neither does one
559-
// further ahead than clocks drift, which every instance would
560-
// predate for as long as it lies ahead.
552+
// running while the attach is recent; past the rollout deadline
553+
// this job stops waiting, as the attaching job normally has judged
554+
// the rollout by then unless a wait of its own moved its deadline
555+
// on. The record is that job's:
556+
// one this job cannot use does not fail it.
561557
recorded, _, err := cnpgRecordedAttach(attachment.attachedAt)
562-
if err == nil && time.Until(recorded) > cnpgAttachClockSkewTolerance {
563-
err = fmt.Errorf("%s is in the future", attachment.attachedAt)
564-
}
565558
if err != nil {
566559
logger.Info("ignoring an unusable attach record another backup job left",
567560
"cluster", clusterName, "annotation", cnpgPluginAttachedAtAnnotation, "error", err)
@@ -924,7 +917,10 @@ func (r *BackupJobReconciler) cnpgPluginRolloutPending(ctx context.Context, name
924917
}
925918

926919
// cnpgRecordedAttach parses a cnpgPluginAttachedAtAnnotation value; ok is
927-
// false for "", a Cluster that carries no record.
920+
// false for "", a Cluster that carries no record. A record further ahead of
921+
// this controller's clock than cnpgAttachClockSkewTolerance is an error:
922+
// every instance would predate it, and a deadline counted from it would not
923+
// run out for as long as it lies ahead.
928924
func cnpgRecordedAttach(raw string) (recorded time.Time, ok bool, err error) {
929925
if raw == "" {
930926
return time.Time{}, false, nil
@@ -933,6 +929,9 @@ func cnpgRecordedAttach(raw string) (recorded time.Time, ok bool, err error) {
933929
if err != nil {
934930
return time.Time{}, false, err
935931
}
932+
if time.Until(recorded) > cnpgAttachClockSkewTolerance {
933+
return time.Time{}, false, fmt.Errorf("%s is in the future", raw)
934+
}
936935
return recorded, true, nil
937936
}
938937

‎internal/backupcontroller/cnpgstrategy_controller_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,6 +3227,7 @@ func TestReconcileCNPG_JobWithoutLatchWaitsForAnotherJobsRollout(t *testing.T) {
32273227
{name: "attach older than the rollout deadline", record: time.Now().Add(-2 * cnpgDefaultBackupDeadline).UTC().Format(time.RFC3339), podCreated: time.Now().Add(-3 * cnpgDefaultBackupDeadline), wantBackup: true},
32283228
{name: "unreadable record", record: "yesterday", podCreated: time.Now().Add(-time.Hour), wantBackup: true},
32293229
{name: "record further ahead than clocks drift", record: time.Now().Add(time.Hour).UTC().Format(time.RFC3339), podCreated: time.Now().Add(-time.Hour), wantBackup: true},
3230+
{name: "record ahead within clock drift", record: time.Now().Add(time.Minute).UTC().Format(time.RFC3339), podCreated: time.Now().Add(-time.Hour), wantBackup: false},
32303231
}
32313232
for _, tc := range cases {
32323233
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)