Skip to content

fix(controller): ensure old primary is shutdown before picking new primary in unschedulable node switchovers - #11294

Draft
chunngaiau wants to merge 9 commits into
cloudnative-pg:mainfrom
chunngaiau:chunngaiau/fix-unschedulable-node-switchover-bug
Draft

chunngaiau wants to merge 9 commits into
cloudnative-pg:mainfrom
chunngaiau:chunngaiau/fix-unschedulable-node-switchover-bug

Conversation

@chunngaiau

Copy link
Copy Markdown

Fixes #11293.

There is a race condition in the current switch over logic when the operator detects the primary is running on an unschedulable node. The operator picks the new primary before waiting for the old primary to fully shutdown. The old primary can still commit transactions and replicate them to any replicas. These transactions may not be replicated to the chosen new primary, especially when quorum-based synchronous replication is configured (i.e. method: any), and are "lost" when it is promoted. The more advanced replica enters a crash-loop because it is ahead of the new primary's fork point of the new timeline.

This PR fixes it by reusing the code path for failovers. The failover code path sets the target primary to a pending marker, which triggers the shutdown of the old primary, but waits until the primary is fully shutdown before picking the new primary. Reusing the failover code path also ensures that we perform the failover quorum check. This is only required for non-replica clusters. The designated primary of a replica cluster does not need to be shut down before a switchover.

This fix was inspired by #11185, but I've chosen not to introduce a separate pending switchover marker. I don't think we need to distinguish a failover from a switchover as their mechanics should be the same. The only difference is in how they are triggered, a failover is unintentional and a switchover is intentional. In fact, I think one of the causes of this bug and #11114 is that there are separate code paths for each. Happy to change it if this is wrong.

As mentioned in this comment, the failover primary selection can be further improved using the primary lease. The operator can try to acquire the primary lease before selecting the new primary, which should give better guarantees that the old primary is fully shutdown before picking the new primary. I've decided not to implement that improvement in this PR.

Appreciate any feedback as this is my first contribution to the project.

@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.29 release-1.30 labels Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

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

@ardentperf

ardentperf commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

The operator picks the new primary before waiting for the old primary to fully shutdown.

there is not technically any requirement to wait for cleanup of the old/dead primary to complete. Also note there is not any race condition leading to data loss or split brain here. #11293 (comment)

This PR is interesting, but it needs a different framework for evaluation. It may extend RTO/unavailability - need to reason through this carefully.

@gbartolini

Copy link
Copy Markdown
Contributor

Thanks for the analysis and the draft, @chunngaiau. The diagnosis in #11293 is right, and unifying the switchover and failover code paths is the direction we want to go.

We're mid-way through a broader rework of failover/switchover coordination built on the primary lease. See the status update on a related report for the failover side: #10591 (comment). That covers reconcileTargetPrimaryForNonReplicaCluster; it doesn't yet include the switchover paths this PR touches, but rather than land a third, separate implementation of the pending-marker pattern, we'd like to extend the same lease-gated mechanism to updatePrimaryPod and setPrimaryOnSchedulableNode once #11367 is in.

Given that, I'd suggest keeping this as a draft for now rather than pushing it toward review. We'll probably fold the unschedulable-node case into the lease-based rework and loop you in when that follow-up starts. Also worth weighing @ardentperf's RTO comment above, it applies to any version of this fix, not just this one.

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 release-1.28 release-1.30

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Race condition in unschedulable node auto-switchover

5 participants