Skip to content

fix(controller): relabel the old primary as unhealthy only during a failover - #11950

Open
clain23 wants to merge 1 commit into
cloudnative-pg:mainfrom
clain23:dev/11715
Open

clain23 wants to merge 1 commit into
cloudnative-pg:mainfrom
clain23:dev/11715

Conversation

@clain23

@clain23 clain23 commented Nov 18, 2026

Copy link
Copy Markdown

Closes #11715

Problem

Since #10409 the reconcile loop relabels the current primary cnpg.io/instanceRole=unhealthy on every pass where status.currentPrimary != status.targetPrimary (the "switchover or failover in progress" guard in internal/controller/cluster_controller.go, calling markOldPrimaryAsUnhealthy in internal/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 -rw Service 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 with bpf-lb-sock-hostns-only=true), their established connections stop passing packets. The walsenders get no replies and exit on wal_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:

cluster.Status.Phase == apiv1.PhaseFailOver ||
	cluster.Status.TargetPrimary == apiv1.PendingFailoverMarker

The immediate relabel in reconcileTargetPrimaryForNonReplicaCluster and markOldPrimaryAsUnhealthy itself are unchanged.

Why this condition, from reading every writer of status.targetPrimary:

  • Every failover path registers PhaseFailOver before it changes the target primary: "Initiating a failover" and "Failing over from X to Y" in reconcileTargetPrimaryForNonReplicaCluster, and "Failing over to X" in reconcileTargetPrimaryForReplicaCluster.
  • Every switchover path registers another phase: PhaseSwitchover (kubectl cnpg promote, unschedulable node) or PhaseUpgrade (switchPrimary in a rolling update).
  • The phase is stored in the status, so an operator restart in the middle of a failover does not lose it.
  • The phase can be overwritten while the reconciler keeps running with the pending marker set (for example the "Instance Status Extraction Error" phase, or a plugin failure). The pending marker is only ever set by a failover, so it is checked as well.

Known limit: once the failover target is chosen (targetPrimary is 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 is PhaseSwitchover or PhaseUpgrade), I can change it.

During a switchover updateRoleLabels already leaves the label of the current primary alone until currentPrimary changes, so the old primary keeps primary until the promotion and then becomes replica, 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 with PhaseSwitchover and PhaseUpgrade, relabel with PhaseFailOver, relabel with the pending marker and an unrelated phase. go test ./internal/controller/, make fmt, make vet and make 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_timeout 5 s, 2026-11-17. The patch tested was the same change on top of v1.30.1, with the phase check only.

1.30.1 1.30.1 + this change
planned switchovers 24 18 (16 kubectl cnpg promote, 2 rolling updates)
runs where the walsenders were killed by wal_sender_timeout 24 (all three walsenders) 0
runs where both standbys received the shutdown checkpoint over streaming 0 18
runs where a standby forked 8 0
lost or duplicated CDC events not measured 0

On 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 -rw EndpointSlice 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 trailer Assisted-by). The diagnosis, the measurements and the production rollout come from our own clusters.

…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]>
@clain23
clain23 requested a review from a team as a code owner November 18, 2026 12:09
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Nov 18, 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 release-1.28 release-1.30

Projects

None yet

2 participants