Skip to content

feat(synchronous): failure domain-aware synchronous replication - #11144

Merged
leonardoce merged 30 commits into
cloudnative-pg:mainfrom
leonardoce:failure-domain
Aug 10, 2026
Merged

leonardoce merged 30 commits into
cloudnative-pg:mainfrom
leonardoce:failure-domain

Conversation

@leonardoce

@leonardoce leonardoce commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Let CloudNativePG restrict synchronous standby selection to instances in a failure domain different from the primary's, such as another availability zone, so a single-zone outage cannot take out the primary and all its synchronous replicas at once.

  • Add mutually exclusive podFailureDomainKeys and nodeFailureDomainKeys to .spec.postgresql.synchronous: pod labels are resolved without ever consulting the node, while node labels remain available for clusters where pod-level topology labels aren't populated (Kubernetes 1.34 and older, or where the PodTopologyLabelsAdmission admission plugin isn't enabled - see KEP4742).
  • Apply the constraint only when it is fully satisfiable by the configured number of synchronous standbys (or, with preferred data durability, by a single cross-domain standby); otherwise, fall back to selecting standbys as if the keys were not configured, so write transactions never wait on acknowledgments that cannot arrive.
  • Add a SyncReplicationTopologySatisfied cluster condition reporting whether the placement preference is currently honored, evaluated only against cluster instances (not standbyNamesPre/standbyNamesPost).
  • Show a Failure Domain Topology section in kubectl cnpg status, grouping instances by domain, marking not-ready instances, and skipping the table for hibernated clusters.
  • Update the kind/k3d e2e setup scripts to wait for every node to register and label them with distinct topology.kubernetes.io/region/zone values, so the feature can be exercised locally out of the box.

@leonardoce
leonardoce requested a review from jsilvela as a code owner July 7, 2026 15:33
@leonardoce
leonardoce requested a review from a team July 7, 2026 15:33
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement 🪄 New feature or request release-1.28 release-1.29 release-1.30 labels Jul 7, 2026
@cnpg-bot cnpg-bot added the backport-requested ◀️ This pull request should be backported to all supported releases label Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 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

@leonardoce leonardoce added no-issue do not backport This PR must not be backported - it will be in the next minor release and removed do not backport This PR must not be backported - it will be in the next minor release release-1.28 release-1.29 release-1.30 labels Jul 7, 2026
@leonardoce

Copy link
Copy Markdown
Contributor Author

This work can actually be split in 3 different PR. The last one can't be backported.

@ardentperf

Copy link
Copy Markdown
Contributor

what is the fallback model, if we need N sync targets but we can't satisfy that number from available replicas based on the current topology? assuming it's just around ordering, that it prioritizes the failure domain compliant replicas above others, but still adds others if needed?

@leonardoce

Copy link
Copy Markdown
Contributor Author

what is the fallback model, if we need N sync targets but we can't satisfy that number from available replicas based on the current topology? assuming it's just around ordering, that it prioritizes the failure domain compliant replicas above others, but still adds others if needed?

It is just the same as what the "old" API was doing. Only the replicas that are not in the same failure domain of the primary are listed in synchronous_standby_names - besides of their readiness status.

@leonardoce

Copy link
Copy Markdown
Contributor Author

/test

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

@armru
armru force-pushed the failure-domain branch from 0e2216b to e45d244 Compare July 9, 2026 16:22
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jul 9, 2026
@armru

armru commented Jul 9, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

armru and others added 11 commits August 10, 2026 23:05
Add a section to the replication page describing podFailureDomainKeys
and nodeFailureDomainKeys: the single source each list reads from, the
mutual exclusions, the placement preference semantics, the
SyncReplicationTopologySatisfied condition, and the automatic topology
labels available on pods starting from Kubernetes 1.35.

Signed-off-by: Armando Ruocco <[email protected]>
…ication

The warning fired on any multi-instance cluster with default
configuration, making the webhook end-to-end test fail. The change is
independently proposed in cloudnative-pg#11148, where its design is being discussed:
this PR stays focused on failure domain-aware synchronous replication.

Signed-off-by: Armando Ruocco <[email protected]>
… fully satisfiable

With required data durability, a cross-domain standby list shorter than
the configured number left synchronous_standby_names asking for more
acknowledgments than the listed standbys could ever provide, blocking
every synchronous commit: in a three-instance cluster spread over two
zones with number set to 2, the filter produced a single name for two
required acknowledgments. Apply the constraint only when the
cross-domain standbys cover the whole configured number (one is enough
with preferred data durability, where the requirement is already capped
to the available standbys), falling back to the unfiltered list
otherwise. The SyncReplicationTopologySatisfied condition uses the same
threshold, so it keeps matching what the replication path elects.

Signed-off-by: Armando Ruocco <[email protected]>
The table groups every instance in the extracted topology, while the
topology condition only counts electable standbys: a failed cross-domain
instance made the table show multiple domains next to a condition
reporting they are insufficient. Mark instances that are not ready so
the two views cannot be read as contradicting each other.

Signed-off-by: Armando Ruocco <[email protected]>
Every other section reporting live instance state in `kubectl cnpg
status` is skipped while the cluster is hibernated, since there are no
running pods to describe. The Failure Domain Topology section was
printed unconditionally, showing the last-known topology grouping and
condition as if they were current.

Signed-off-by: Gabriele Bartolini <[email protected]>

Assisted-by: Claude Sonnet 5 <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
@gbartolini

Copy link
Copy Markdown
Contributor

/test

@github-actions

Copy link
Copy Markdown
Contributor

…lication

The warning was removed from this branch when it was still duplicating an
open pull request. That pull request has been merged in the meantime,
together with the update to the end-to-end assertion that expected no
admission warnings, so the removal now deletes a feature that exists on
the main branch.

Restore the warning next to the failure domain one, so a cluster can
report both.

Signed-off-by: Armando Ruocco <[email protected]>
@armru

armru commented Aug 10, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

@armru

armru commented Aug 10, 2026

Copy link
Copy Markdown
Member

A few commits on top of this branch change the original design, so, briefly:

The cross-domain list is now used only when it can satisfy the requested
durability, otherwise we fall back to the unfiltered one. With the hard filter, a
cluster with fewer cross-domain candidates than number gets
ANY 2 ("cluster-2") and every commit hangs, and one node failure is enough to
get there: better to lose the domain distribution than the writes. Does that
trade-off sound right, or should it be configurable?

failureDomainKey is split into podFailureDomainKeys and
nodeFailureDomainKeys, mutually exclusive: with the pod-then-node fallback the
same key meant different things on different instances, and a typo on a pod label
silently resolved to the node one. Pod labels are strict now, a missing key fails
the extraction.

The condition uses the same helper as the election, so it can't say "satisfied"
while the constraint isn't applied.

Last commit restores the "no synchronous replication configured" warning: it had
been dropped here while it duplicated #11148, which is now merged, so the removal
was deleting a feature that lives on main.

Feedback welcome, especially on the first point.

armru added 5 commits August 10, 2026 16:17
The two functions updating the cluster status decide whether the update is
worth an API call by comparing the status against a copy taken before the
changes, and that copy was shallow. meta.SetStatusCondition edits an already
existing condition through a pointer into the conditions slice, so the copy
carried the very change it was supposed to reveal, the comparison found no
difference and the update was skipped.

Every condition set there used to change together with a plain status field,
which produced a difference on its own and kept the problem hidden.
SyncReplicationTopologySatisfied breaks that: its outcome also depends on
`.spec.postgresql.synchronous.number` and on the data durability level, so
raising the number of requested synchronous standbys on an otherwise quiet
cluster leaves the condition reporting a satisfied constraint that is no
longer applied, until an unrelated field happens to move. The same applies to
any reason or message reworded across operator versions, which could never be
corrected on an existing condition.

Compare against a deep copy, so that a condition edited in place is seen as a
change to persist.

Signed-off-by: Armando Ruocco <[email protected]>
With preferred data durability the constraint is applied as soon as a single
cross-domain standby is available, and the number of required acknowledgments
follows the standbys in the list: one cross-domain standby out of two healthy
replicas means one acknowledgment instead of two. The documentation attributed
that cap to the health of the replicas, and the comment on
filterCrossDomainInstances claimed the keys never lower the durability of a
commit, which holds for required data durability only.

Signed-off-by: Armando Ruocco <[email protected]>
The groups are sorted by the rendered domain value, so two domains rendering
the same way, such as a value that is literally "<empty>" and an empty one,
were left in an order that depended on the sort implementation. Sort them
stably, keeping the order in which the instances appear, and describe the
resulting order.

Signed-off-by: Armando Ruocco <[email protected]>
wait_for_all_nodes runs on every kind and k3d cluster creation, and exiting
when the expected nodes do not all show up within two minutes makes a slow
registration the reason a whole test run never starts. The nodes that did
register are usable, and a test needing the complete set reports the missing
ones on its own, so warn and move on.

Signed-off-by: Armando Ruocco <[email protected]>
The end-to-end suite had no spec reading the topology labels the setup
scripts now write on every node. The new spec builds its own failure
domains, labelling the node of a single standby, so that the outcome does
not depend on where the scheduler put the pods: relying on the zone of
the underlying cluster would leave the interesting case untested on the
clusters whose every node sits in a distinct zone.

It then checks both directions of the constraint: the cross-domain
standby elected alone while it satisfies the requested number of
synchronous replicas, and the whole standby list coming back, with the
topology condition reporting the reason, as soon as it does not.

Signed-off-by: Armando Ruocco <[email protected]>
@armru

armru commented Aug 10, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

@ardentperf

Copy link
Copy Markdown
Contributor

How does this interact with failover quorum?

@leonardoce

Copy link
Copy Markdown
Contributor Author

How does this interact with failover quorum?

This restrict the replicas listed in synchronous_standby_names, and is reflected on the FailoverQuorum custom resource.
From there the code path is the same, influencing the quorum check directly via the cardinality of the synchronous_standby_names and of the read set.

@leonardoce
leonardoce merged commit 6b8139c into cloudnative-pg:main Aug 10, 2026
48 of 49 checks passed
cnpg-bot pushed a commit to cloudnative-pg/api that referenced this pull request Aug 10, 2026
…dnative-pg/cloudnative-pg#11144)

Let CloudNativePG restrict synchronous standby selection to instances in
a failure domain different from the primary's, such as another
availability zone, so a single-zone outage cannot take out the primary
and all its synchronous replicas at once.

- Add mutually exclusive `podFailureDomainKeys` and
`nodeFailureDomainKeys` to `.spec.postgresql.synchronous`: pod labels
are resolved without ever consulting the node, while node labels remain
available for clusters where pod-level topology labels aren't populated
(Kubernetes 1.34 and older, or where the `PodTopologyLabelsAdmission`
admission plugin isn't enabled - see KEP4742).
- Apply the constraint only when it is fully satisfiable by the
configured `number` of synchronous standbys (or, with `preferred` data
durability, by a single cross-domain standby); otherwise, fall back to
selecting standbys as if the keys were not configured, so write
transactions never wait on acknowledgments that cannot arrive.
- Add a `SyncReplicationTopologySatisfied` cluster condition reporting
whether the placement preference is currently honored, evaluated only
against cluster instances (not `standbyNamesPre`/`standbyNamesPost`).
- Show a Failure Domain Topology section in `kubectl cnpg status`,
grouping instances by domain, marking not-ready instances, and skipping
the table for hibernated clusters.
- Update the kind/k3d e2e setup scripts to wait for every node to
register and label them with distinct
`topology.kubernetes.io/region`/`zone` values, so the feature can be
exercised locally out of the box.

Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>

(cherry picked from commit cloudnative-pg/cloudnative-pg@6b8139c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not backport This PR must not be backported - it will be in the next minor release enhancement 🪄 New feature or request lgtm This PR has been approved by a maintainer no-issue ok to merge 👌 This PR can be merged size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants