Repository navigation
Conversation
…ailover Since cloudnative-pg#10409 the reconcile loop relabels the current primary as unhealthy whenever the current primary differs from the target primary, which is also true during a planned switchover. The old primary then leaves the -rw service while it is still running its fast shutdown. Standbys and logical replication clients connect through -rw, and where the CNI translates the service address per packet their established connections stop passing packets. The walsenders exit on wal_sender_timeout, the shutdown checkpoint is written after the streams are gone, the target promotes without the last segment, and another standby can restore that segment from the archive and continue on the old timeline. Run the reconcile loop relabel only while a failover is in progress: the phase is PhaseFailOver, or the target primary is the pending failover marker. Every failover path registers PhaseFailOver before it changes the target primary, and the pending marker is only set by a failover, which covers a phase overwritten while the operator waits for the WAL receivers to stop. Closes cloudnative-pg#11715 Assisted-by: Claude Fable 5.2 Signed-off-by: Igor Kudrin <[email protected]>
Contributor
|
❗ By default, the pull request is configured to backport to all release branches.
|
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.
Closes #11715
Problem
Since #10409 the reconcile loop relabels the current primary
cnpg.io/instanceRole=unhealthyon every pass wherestatus.currentPrimary != status.targetPrimary(the "switchover or failover in progress" guard ininternal/controller/cluster_controller.go, callingmarkOldPrimaryAsUnhealthyininternal/controller/replicas.go). That is also true for a planned switchover:kubectl cnpg promote, the primary switchover of a rolling update, and a primary on an unschedulable node.In a switchover the old primary is healthy and is running a fast shutdown. Taking the label away empties the
-rwService at that moment. Standbys (primary_conninfo host=<cluster>-rw) and logical replication clients connect through-rw. Where the CNI translates the Service address per packet for pod traffic (Cilium withbpf-lb-sock-hostns-only=true), their established connections stop passing packets. The walsenders get no replies and exit onwal_sender_timeout, the shutdown checkpoint is written after the streams are gone, and the target promotes without the last segment. Another standby can then restore that segment from the archive and continue on the old timeline, which is the fork described in this issue.The relabel was added for #10403, which is about failover: the old primary may still be alive and must not receive traffic or satisfy the synchronous quorum. A switchover does not have that risk. The old primary is told to shut down, the target promotes only after its WAL receiver has stopped, and the standbys need the old primary reachable until they have received its shutdown checkpoint.
Change
The relabel in the reconcile loop now runs only while a failover is in progress:
The immediate relabel in
reconcileTargetPrimaryForNonReplicaClusterandmarkOldPrimaryAsUnhealthyitself are unchanged.Why this condition, from reading every writer of
status.targetPrimary:PhaseFailOverbefore it changes the target primary: "Initiating a failover" and "Failing over from X to Y" inreconcileTargetPrimaryForNonReplicaCluster, and "Failing over to X" inreconcileTargetPrimaryForReplicaCluster.PhaseSwitchover(kubectl cnpg promote, unschedulable node) orPhaseUpgrade(switchPrimaryin a rolling update).Known limit: once the failover target is chosen (
targetPrimaryis a pod name), only the phase identifies a failover. If an error phase overwrites it at that point and no earlier attempt has succeeded, the retry does not run. For a non-replica cluster the label has been set by then, during the pending stage. I did not find a status field other than the phase that identifies this stage. If you prefer the opposite default (relabel unless the phase isPhaseSwitchoverorPhaseUpgrade), I can change it.During a switchover
updateRoleLabelsalready leaves the label of the current primary alone untilcurrentPrimarychanges, so the old primary keepsprimaryuntil the promotion and then becomesreplica, as before #10409.This does not replace the promotion gate from #11884. It removes the trigger we observed.
Tests
Unit tests in
internal/controller/replicas_test.go: no relabel withPhaseSwitchoverandPhaseUpgrade, relabel withPhaseFailOver, relabel with the pending marker and an unrelated phase.go test ./internal/controller/,make fmt,make vetandmake lint(golangci-lint v2.15.0) pass locally. I have not run the e2e suite.Measurements
3 instances, PostgreSQL 18.7, one Debezium logical slot, constant write load, Cilium with
bpf-lb-sock-hostns-only=true,wal_sender_timeout5 s, 2026-11-17. The patch tested was the same change on top of v1.30.1, with the phase check only.kubectl cnpg promote, 2 rolling updates)wal_sender_timeoutOn 1.30.1 the write stall was 4.2 to 5.3 s. The operator logged "Setting primary label to unhealthy in the old primary during failover" 13 ms before the old primary logged
received fast shutdown request, and the-rwEndpointSlice was empty 0.25 s later.With the change, a real failover (postgres frozen with SIGSTOP) still logs "Current primary isn't healthy, initiating a failover" and relabels the old primary.
We run the v1.30.1 based build on a production fleet of 79 clusters and 237 instances. The operator was replaced in place with no PostgreSQL restart.
AI disclosure
As asked by the AI policy: Claude was used to port the change to
main, to write the unit tests and to draft this description (commit trailerAssisted-by). The diagnosis, the measurements and the production rollout come from our own clusters.