Skip to content

Fixes Issue 7793: Stuck reconciliation fixes - #7794

Closed
jmealo wants to merge 1 commit into
cloudnative-pg:mainfrom
jmealo:feat/fix-issue-7793
Closed

jmealo wants to merge 1 commit into
cloudnative-pg:mainfrom
jmealo:feat/fix-issue-7793

Conversation

@jmealo

@jmealo jmealo commented Jun 10, 2025 •

Copy link
Copy Markdown
Contributor

Fix stuck reconciliation loop for failed snapshot recovery jobs #7793

⚠️ This PR is superseded by:

This PR is superseded by:

#10004

#10005

⚠️

Problem

CloudNativePG clusters get stuck in "Creating a new replica" phase when snapshot recovery jobs fail, even when the cluster already has the correct number of replicas. Currently requires manual intervention to recover.

Solution

Adds automatic detection and recovery:

  • Detects stuck/failed jobs (10min timeout)
  • Validates if cluster already has correct replica count
  • Clears stuck scaling phase and deletes failed jobs
  • Returns cluster to normal operation

Key Changes

New Files

  • pkg/utils/jobs.go - Job state detection utilities
  • pkg/utils/jobs_test.go - Unit tests

Modified

  • internal/controller/cluster_controller.go - Added stuck detection/recovery logic
  • api/v1/cluster_types.go - Added scaling phase constants

Example Scenario

1. Cluster scaling 9→10 replicas
2. Snapshot recovery job fails (missing PVC)
3. Instance created via streaming replication (now has 10 replicas)
4. Cluster stuck in "Creating a new replica" despite having 10 instances

Fix automatically:
✅ Detects stuck state
✅ Verifies has 10 instances (correct count)
✅ Clears phase → Healthy
✅ Deletes failed job

Testing Required

Before Merging

  • Test normal scaling still works
  • Verify stuck job detection (10min timeout)
  • Confirm phase clears when replica count correct
  • Test recovery with missing PVCs
  • Check no impact on healthy clusters

Test Instructions

  1. Create cluster, scale up
  2. Delete PVC to cause job failure
  3. Verify automatic recovery

Configuration

  • Stuck job timeout: 10 minutes (hardcoded)
  • Equilibrium timeout: 15 minutes (hardcoded)
  • Future: Make configurable

Risks

  • 10min timeout might be aggressive for large restores
  • Need to ensure no race conditions with legitimate scaling

Future Work

  • Configurable timeouts
  • Prometheus metrics
  • Root cause fixes for job failures

Questions for Reviewers:

  1. Are the timeout values (10/15min) appropriate?
  2. Should timeouts be configurable now vs later?
  3. Any additional test scenarios needed?

⚠️ This PR is superseded by:

This PR is superseded by:

#10004

#10005

⚠️

@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 labels Jun 10, 2025
@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

@jmealo
jmealo force-pushed the feat/fix-issue-7793 branch from 65e4ebc to fedbf8c Compare June 10, 2025 01:47
@armru
armru self-requested a review June 28, 2025 19:24
@armru armru added this to the 1.26.1 milestone Jun 28, 2025
@armru
armru requested review from leonardoce and mnencia June 28, 2025 19:24
@armru

armru commented Jun 28, 2025

Copy link
Copy Markdown
Member

/test limit=local

@github-actions

Copy link
Copy Markdown
Contributor

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Jun 28, 2025
@mnencia

mnencia commented Jul 21, 2025 •

Copy link
Copy Markdown
Member

@jmealo What is missing to move this in ready for review?

@mnencia mnencia modified the milestones: 1.26.1, 1.28.0 Jul 21, 2025
@jmealo

jmealo commented Jul 21, 2025 •

Copy link
Copy Markdown
Contributor Author

@mnencia I haven't been able to test it on a cluster yet. I've only run the automated tests.

@jmealo

jmealo commented Sep 8, 2025 •

Copy link
Copy Markdown
Contributor Author

@mnencia I still haven't had a chance to test this in a cluster. I likely won't have time for another month.

@jmealo
jmealo marked this pull request as ready for review September 8, 2025 16:56
@jmealo
jmealo requested a review from a team September 8, 2025 16:56
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Sep 8, 2025
@jmealo

jmealo commented Sep 8, 2025

Copy link
Copy Markdown
Contributor Author

@armru @mnencia I'm marking this as ready-for-review, but I could use a hand testing it. The changes made make sense, their implementation may need some work.

@dosubot dosubot Bot added the bug 🐛 Something isn't working label Sep 8, 2025
@jmealo

jmealo commented Sep 8, 2025

Copy link
Copy Markdown
Contributor Author

Issues I see:

  • Magic values for timeouts / stuck job criteria
  • We should extract the isInScalingPhase to its own function, and instead of hard coding the strings, get them from a constant if possible

@jmealo
jmealo force-pushed the feat/fix-issue-7793 branch from 3e7779e to ae614ef Compare September 8, 2025 17:07
@jmealo

jmealo commented Sep 8, 2025 •

Copy link
Copy Markdown
Contributor Author

I'm working on fixing the issues I flagged here. Let me know if there's anything else that needs to be cleaned up.

I'm trying to find the best way to surface this condition to folks since cleaning up stuck pods might make it difficult to troubleshoot the issue if it persists.

For example, kubectl cnpg status should provide helpful information.

@jmealo
jmealo force-pushed the feat/fix-issue-7793 branch from ae614ef to 46b614b Compare September 8, 2025 18:33
@jmealo
jmealo requested a review from jsilvela as a code owner September 8, 2025 18:33
// defaultStuckJobTimeout is the duration after which a job with no progress is considered stuck
// This should be long enough to allow for normal PostgreSQL operations like backup restoration
// but short enough to prevent indefinite blocking of cluster operations
defaultStuckJobTimeout = 10 * time.Minute

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what's a reasonable value here. Thoughts?

Comment thread api/v1/cluster_types.go
@@ -673,6 +673,12 @@ const (

// PhaseCannotCreateClusterObjects is set by the operator when is unable to create cluster resources
PhaseCannotCreateClusterObjects = "Unable to create required cluster objects"

// PhaseScalingUp when scaling up the cluster
PhaseScalingUp = "Scaling up"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't have these in a const, so I added them here, not sure if that's the way to go?

@armru

armru commented Sep 9, 2025

Copy link
Copy Markdown
Member

/test

@github-actions

github-actions Bot commented Sep 9, 2025

Copy link
Copy Markdown
Contributor

@jmealo
jmealo force-pushed the feat/fix-issue-7793 branch from 46b614b to 5708665 Compare February 17, 2026 01:17
@jmealo

jmealo commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@armru @mnencia I'm closing this PR because it's unwieldy and tries to do too much. Now that I've had some time to think about it, a more iterative approach would be best.

This PR is superseded by:
#10004
#10005

I've implemented the approach below:

Proposed Approach

PR 1: Handle failed/deleted jobs (core fix)

  • When a job enters a Failed state, ensure it doesn't block reconciliation (runningJobNames() excluding failed jobs)
  • Clean up failed jobs in cleanupCompletedJobs
  • Add a safety net: when the cluster is in a scaling phase but currentInstances == desiredInstances with zero running jobs, clear the phase to Healthy. This handles the "manually delete the job" workaround and any other edge cases where the phase gets stuck despite the cluster being fine.

All signals are deterministic (no timeouts).

PR 2: Fallback from snapshot to pg_basebackup

  • When a snapshot-recovery job for a replica fails, fall back to pg_basebackup instead of retrying the broken snapshot
  • Self-healing rather than just unblocking

Future: Stuck job detection
Timeout-based detection of jobs stuck in Pending is useful but tricky to get right. Probably best to start narrow (job age > N minutes with Active == 0 && Succeeded == 0 && Failed == 0) and make timeouts configurable. Separate PR to keep scope manageable. I think this is the most difficult to "get right", so let's punt on it and get the concrete fixes out sooner rather than later.

@jmealo jmealo closed this 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

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 bug 🐛 Something isn't working ok to merge 👌 This PR can be merged release-1.28 size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

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

6 participants