Skip to content

fix: demote a primary that stopped reporting its status - #11268

Closed
armru wants to merge 4 commits into
mainfrom
fix/demote-silent-primary
Closed

armru wants to merge 4 commits into
mainfrom
fix/demote-silent-primary

Conversation

@armru

@armru armru commented Jul 29, 2026

Copy link
Copy Markdown
Member

Based on #11260, whose commits are included here: the fallback below reads
.status.instancesReportedState, which only becomes trustworthy once a failed
probe stops overwriting an instance's entry with zeros.

When a cluster is switched to replica cluster mode, replicaclusterswitch
looks for a primary among the instances that reported their status and, when it
finds one, fences the cluster to demote it. An instance whose /pg/status
probe failed reports no role at all, IsPrimary included, so a single failing
probe made containsPrimaryInstance return false and the reconciler take its
"no primary instance present means that we have no work to do" branch, skipping
the transition while that primary kept accepting writes.

Nothing else in that pass fences it either. AllReadyInstancesStatusUnreachable
only trips when every ready instance is silent, and the IsComplete gate that
does stop the reconcile sits further down, past the point where the demotion
would have started. The pass is requeued about ten seconds later by
evaluatePodReadinessGuards, so the demotion does eventually happen, but only
once the probe answers again: for as long as the operator could not reach an
instance that clients could, the cluster was a replica cluster on paper with a
writable primary behind it.

The decision now falls back on the last state observed for the instance
recorded as the current primary. Fencing is a metadata write that goes through
the API server, so stopping that primary never required reaching it in the
first place, and the transition completes on the instance manager's own watch of
the Cluster object rather than on an operator probe.

The fallback is deliberately narrow, so that it fires only where a primary
really is at stake:

  • it asks for an instance that was actually observed as a primary, which a
    designated primary never is since it runs with a standby.signal file, so a
    replica cluster created from scratch is left alone when its designated primary
    goes quiet;
  • it requires the instance to still be among the probed ones, because one that
    has left the status list has no PostgreSQL left to stop;
  • it steps aside once a demotion token exists, so a probe failing right after a
    completed transition cannot start a second one and fence a healthy replica
    cluster.

The package had no coverage of Reconcile at all, so the tests come with it:
the silent-primary case, the reporting-primary case that must keep working, and
one case per carve-out above.

There is no issue tracking this. It came out of reviewing #11260, which fixes
the fabricated data this fallback relies on not being fabricated.

armru added 4 commits July 29, 2026 14:47
…tate

Nothing stopped decision code from reading an observed field (e.g.
IsWalReceiverActive, IsPrimary, TimeLineID) off an instance whose
/pg/status probe failed and getting back a Go zero value instead of a
real observation. Add InstanceSet, which splits a PostgresqlStatusList
into the instances that actually reported their status and the ones
that didn't, the latter further split by whether kubelet still
considers the pod Ready. Only Pod and Node, which are always populated
regardless of the probe outcome, are exposed for a silent instance.

updateClusterStatusThatRequiresInstancesState is the first consumer:
it rebuilt InstancesReportedState from scratch every reconciliation
and wrote an entry for every item in the status list unconditionally,
so an instance whose status probe failed had its IsPrimary, TimeLineID
and IP published as zero values into the CRD as though they had been
observed.

Keep the map across reconciliations instead of rebuilding it, and only
write or overwrite an entry for an instance that is actually reporting;
a silent instance keeps whatever entry it already had, and gets no
entry at all if this is its first-ever observation. An instance is
only removed from the map once it stops appearing in the status list
altogether (scaled down, or replaced under the same name), which is
also what keeps ensureInstancesAreReachable in the isolation check from
pinging a pod that is actually gone rather than merely unreachable
this pass.

Signed-off-by: Armando Ruocco <[email protected]>
The comment claimed that a pod goes missing from the probed status list
only when it has been scaled down or replaced, and that every instance
still belonging to the cluster shows up in that list on every pass. The
first half is incomplete and the second one is wrong: GetStatusFromInstances
probes only the pods accepted by utils.IsPodActive, so a pod that is
pending or terminating never enters the list at all, and its entry is
pruned while it is being recreated.

That pruning is the behaviour we want, because the entry goes away before
the replacement pod exists and so cannot survive to hand the isolation
check an address that has meanwhile moved to a different pod. Say that,
instead of resting the explanation on a guarantee the code does not give.

Signed-off-by: Armando Ruocco <[email protected]>
The existing cases all pair a failed probe with a zero-valued status, so
they cannot tell a decision taken on the error apart from one taken on the
values happening to look empty. Feed a failed probe carrying a full status
that disagrees with the last known observation on every field, and assert
that both the reported state and the timeline on the status root keep the
values last actually observed.

Signed-off-by: Armando Ruocco <[email protected]>
When a cluster is switched to replica cluster mode, the operator looks for
a primary among the instances that reported their status and fences the
cluster to demote it. An instance whose status probe failed reports no
role at all, so a single failing probe made the operator conclude that
there was no primary to demote and skip the transition entirely, while
that primary kept accepting writes. The pass ends up requeued a few
seconds later, so the demotion did eventually happen, but only once the
probe answered again: for as long as the operator could not reach an
instance that clients could, the cluster was a replica cluster on paper
with a writable primary behind it.

The decision now falls back on the last state observed for the instance
recorded as the current primary, kept in .status.instancesReportedState.
Fencing is a metadata write that goes through the API server, so stopping
that primary never required reaching it in the first place.

The fallback is deliberately narrow. It asks for an instance that was
actually observed as a primary, which a designated primary never is since
it runs with a standby.signal file, so a replica cluster created from
scratch is left alone when its designated primary goes quiet. It also
requires the instance to still be among the probed ones, because one that
has left the status list has no PostgreSQL left to stop, and it steps
aside once a demotion token exists, so a probe failing right after a
completed transition cannot start a second one.

Signed-off-by: Armando Ruocco <[email protected]>
@armru
armru requested a review from a team July 29, 2026 08:12
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug 🐛 Something isn't working labels Jul 29, 2026
@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 Jul 29, 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

@github-actions

Copy link
Copy Markdown
Contributor

Warning

Linked issue missing.

Reference one in the PR body using a keyword (Closes / Fixes / Resolves / Refs / See) followed by #<issue_id> or owner/repo#<issue_id>.

Alternatively, apply the no-issue label to skip this check.

@armru
armru marked this pull request as draft July 29, 2026 08:13
@armru

armru commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

merged on 11260

@armru armru closed this Jul 29, 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 release-1.28 release-1.30 size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants