Skip to content

feat: fallback to pg_basebackup on snapshot recovery failure (#7793) - #10005

Open
jmealo wants to merge 8 commits into
cloudnative-pg:mainfrom
jmealo:dev/7793-phase-2
Open

jmealo wants to merge 8 commits into
cloudnative-pg:mainfrom
jmealo:dev/7793-phase-2

Conversation

@jmealo

@jmealo jmealo commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a snapshot recovery job fails during replica creation, the operator now automatically falls back to using pg_basebackup instead of remaining stuck or repeatedly retrying broken snapshot recovery.

This provides self-healing behavior: instead of just unblocking (#10004), the cluster recovers by trying an alternative join method.

Changes

File Change
internal/controller/cluster_create.go Add getFailedSnapshotDataSources(), integrate fallback logic in joinReplicaInstance, and annotate snapshot-recovery jobs with their data source
internal/controller/cluster_create_test.go Unit tests for getFailedSnapshotDataSources() and fallback behavior
pkg/utils/labels_annotations.go Add SnapshotDataSourceAnnotationName constant
tests/e2e/failed_job_handling_test.go E2E test: snapshot-based replica creation and graceful fallback

How It Works

  1. When joinReplicaInstance is called and a snapshot source is available:
  2. Check if the specific snapshot's recovery job has previously failed
  3. If that snapshot failed:
    • Emit a warning event
    • Fall back to pg_basebackup instead of attempting snapshot recovery
  4. If the snapshot hasn't failed, annotate the job with the snapshot name for tracking

This design means:

  • Only the specific failed snapshot is skipped; new snapshots are still attempted
  • The failed job is preserved as both troubleshooting evidence and a marker to prevent retrying that snapshot
  • To re-enable snapshot recovery for a specific snapshot, a user can manually delete the failed job (kubectl delete job <job-name>)

Why Create PVCs Before Job

The order of operations was changed from "create job, then PVCs" to "create PVCs, then job" because:

  • If the job is created first and PVC creation fails, we have an orphaned job
  • Creating PVCs first is safer: if it fails, no job is created
  • This matches the pattern in createPrimaryInstance

Note: The ctrl.Result from CreateInstancePVCs is intentionally ignored in both createPrimaryInstance and joinReplicaInstance. PVC creation returns a requeue request, but the init/join job must be created in the same reconciliation loop. Returning early would leave dangling PVCs without pods, causing the operator to get stuck.

Test Plan

  • Unit tests for getFailedSnapshotDataSources()
  • Unit tests for snapshot fallback logic
  • All existing tests pass
  • E2E (AKS): Snapshot-based replica creation succeeds
  • E2E (AKS): Phase 1 tests still pass on Phase 2 branch (backward compatible)

Related

When a job fails or is manually deleted during replica creation, the
reconciler could get stuck. This fix ensures failed jobs don't block
reconciliation by excluding them from runningJobNames().

Note: Failed jobs are intentionally NOT deleted by cleanupCompletedJobs.
They are kept for troubleshooting and serve as markers for fallback
logic (e.g., snapshot recovery fallback in phase 2).

Fixes: cloudnative-pg#7793

Assisted-by: Claude Opus 4.5
Signed-off-by: Jeff Mealo <[email protected]>
Add E2E tests to verify:

1. Cluster scaling resilience:
   - Create cluster, scale up, attempt to disrupt join job
   - Verify cluster reaches healthy state regardless of job outcome
   - Verify scaling back down works correctly

2. Safety net clears stuck phase:
   - Create healthy cluster
   - Manually set phase to "Creating replica" to simulate stuck state
   - Verify safety net clears phase when instances match desired count

These tests validate the fix for the reconciliation loop getting stuck
when jobs fail during scaling operations.

Assisted-by: Claude Opus 4.5
Signed-off-by: Jeff Mealo <[email protected]>
Use the typed constant corev1.ConditionTrue instead of the string
literal "True" for checking job failure conditions. This matches the
project convention used in pod_conditions.go and other condition checks.

Assisted-by: Claude Opus 4.5
Signed-off-by: Jeff Mealo <[email protected]>
When a snapshot-recovery job fails, the operator now tracks which specific
VolumeSnapshot was attempted using the cnpg.io/snapshotDataSource annotation.
This allows the operator to:

1. Skip only the specific snapshot that has failed, not all snapshots
2. Allow new snapshots taken after the failure to be attempted
3. Keep failed jobs for troubleshooting evidence

To re-enable snapshot recovery for a specific snapshot, delete its failed job.

This change builds on phase 1 which prevents failed jobs from blocking the
reconciliation loop by excluding them from runningJobNames().

Assisted-by: Claude Opus 4.5
Signed-off-by: Jeff Mealo <[email protected]>
Add E2E test to verify snapshot-based replica creation and graceful
fallback when scaling up a cluster with a completed volume snapshot:

1. Create single-instance cluster with WAL archiving (MinIO) and snapshot support
2. Take volume snapshot backup and wait for completion
3. Scale up to 3 instances to trigger snapshot-based replica creation
4. Verify cluster reaches healthy state with all replicas ready

This validates that the operator uses snapshots for replica creation
when available and can fall back to pg_basebackup if needed.

Assisted-by: Claude Opus 4.6
Signed-off-by: Jeff Mealo <[email protected]>
CreateInstancePVCs returns a requeue request when PVCs are newly created.
Previously, we returned early on this requeue, which prevented the init/join
job from being created in the same reconciliation loop. This left dangling
PVCs without pods, causing the operator to get stuck in "PVC not ready" loop.

Fix by intentionally ignoring the ctrl.Result from CreateInstancePVCs,
matching the upstream pattern where callers only check the error return.

Also fix E2E test assertion type mismatch (ReadyInstances is int, not int32)
and use the correct fixture for snapshot fallback test.

Assisted-by: Claude Opus 4.6

Signed-off-by: Jeff Mealo <[email protected]>
@jmealo
jmealo requested a review from a team February 17, 2026 21:44
@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Feb 17, 2026
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.25 release-1.28 labels Feb 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@gbartolini

Copy link
Copy Markdown
Contributor

For maintainers' attention to evaluate inclusion in 1.29.0.

@armru armru self-assigned this Feb 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-requested ◀️ This pull request should be backported to all supported releases enhancement 🪄 New feature or request release-1.28 release-1.30 size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

Status: Waiting for First Review

Development

Successfully merging this pull request may close these issues.

[Bug]: Cluster reconciliation loop gets stuck when scaling replicas due to failed snapshot recovery jobs

5 participants