feat(cloud-backups): rotation-interval-days for audit-rotate (cadence-independent rotation) - #8
Merged
Conversation
…-independent rotation) Decouple archive rotation from the cron cadence so the audit-rotate CronJob can run at ANY frequency (per-minute..daily) while the number of coexisting audit_archive_* tables stays bounded by an INTERVAL, not by how often the cron fires. rotation-interval-days=N cuts a new archive only when the newest existing one is >= N days old (0 = OFF = rotate every run, unchanged). Set = retention for a single archive at a time (one monthly bucket object for easy audit restore). The cron still reconciles every run (recover interrupted backups, drop aged archives); only Pass 2 (rotate) is gated. The gate is stateless -- derived from the newest archive's name-timestamp, the same oracle the drop gate trusts -- so any run/replay/missed-then-resumed run computes the same decision. Guardrails (from a 5-lens adversarial design review): - Concurrency: the rotate txn is self-guarding -- pg_try_advisory_xact_lock + a supersession check (a newer archive already exists) BEFORE the rename, so two overlapping runs (a manual job racing the cron) can't both cut an archive; the loser aborts with a benign skip. Without this the second run would rotate the fresh EMPTY table into a stray archive squatting a full retention window. - DROP TABLE IF EXISTS makes a concurrent Pass-1 double-drop a no-op, not a spurious quarantine + non-zero exit. - Observability: audit_rotate_summary now emits rotated_this_run, rotation_interval_days, newest_archive_age_days, rotation_skipped_reason so a monitor can alert on 'was due but did not rotate' (the stall that, under interval rotation, the old oldest>retention signal can't see). - Validation: reject interval<0, interval>retention (degenerate), and cap both retention and interval at 100y so the now-Nd cutoff can't overflow int64. Live-validated on PG17 + MinIO: bootstrap rotate, skip-when-not-due, single- archive drop+rotate cycle (interval=retention), and two overlapping runs producing exactly one archive (the guard aborts one, both exit 0). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> ReARM-Agent: 1420896f-adf5-4843-896f-d863cfcc6528 ReARM-Agentic-Session: 7fa98c2d-b192-4b4a-b1a8-ac64349a89b2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
--rotation-interval-days/ROTATION_INTERVAL_DAYS(default 0 = OFF = today's rotate-every-run) topg audit-rotate. When set, it decouples archive rotation from the cron cadence: the cron reconciles every run (recover interrupted backups, drop aged archives) but cuts a new archive only when the newest existing one is>= Ndays old. So the CronJob can run at any frequency (per-minute … daily) while the number of coexistingaudit_archive_*tables stays bounded by the interval, not by how often the cron fires. Set= audit-retention-daysfor a single archive at a time (one bucket object per interval → simplest month-granular audit restore).Why
Customer wants a single audit archive table at a time (optics + easier bucket restore) at 30-day retention, with the freedom to run a fast reconcile cron. Only a monthly cron gives ~1 archive today, and it can't retry a failed backup for a month; this knob gives ~1 archive with a frequent reconcile/backup-retry cron. Full reasoning + the R+C cadence law in
rearm-core/ai-plans/audit-rotate-backup-drop-design.md.How
rotationDecision); Pass 1 (reconcile/drop) runs every run, unchanged. The gate is stateless — derived from the newest archive's name-timestamp, the same oracle the drop gate trusts — so any run/replay/missed-then-resumed run computes the same decision.interval == retentionholds exactly one archive (no transient second).Guardrails (from a 5-lens adversarial design review)
pg_try_advisory_xact_lock+ a supersession check (a newer archive already exists) before the rename, so two overlapping runs (a manual job racing the cron) can't both cut an archive; the loser aborts with a benign skip. The client shells out a freshpsqlper call, so a session lock can't span the run — the txn-scoped guard is the robust equivalent.DROP TABLE IF EXISTSmakes a concurrent Pass-1 double-drop a no-op, not a spurious quarantine + non-zero exit.audit_rotate_summarynow emitsrotated_this_run,rotation_interval_days,newest_archive_age_days,rotation_skipped_reason— so alerting can shift to "was due but did not rotate" (the stall that, under interval rotation, the oldoldest>retentionsignal can't see).interval < 0andinterval > retention(degenerate); retention/interval day counts capped so thenow - N dayscutoff can't overflow int64.Testing
TestRotationDecision(off/drain/bootstrap/under/over boundaries),TestNewestArchive,TestAdvisoryLockKey,TestIsRotateSkip, guard-before-rename ordering,DROP … IF EXISTS, config validation.go test ./...green;gofmtclean.Rollout
Merge + build the cloud-backup image before the helm knob (rearm-saas #325) does anything. Default 0 is a no-op for existing deployers, and an old image simply ignores the new env.
🤖 Generated with Claude Code