Repository navigation
feat(failover): detect and contain a replica that diverged from the primary's timeline - #11251
Conversation
|
❗ By default, the pull request is configured to backport to all release branches.
|
|
/test |
|
@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/30120462476 |
f0dd588 to
084bd39
Compare
|
/test |
|
@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/30370404304 |
084bd39 to
7b10bee
Compare
|
/test |
|
@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/30461553743 |
|
/test |
|
@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/30463334195 |
8ecf4fe to
b23aa9b
Compare
|
/test |
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/45232402516 |
1ecacf7 to
372463c
Compare
|
/test |
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/45289873373 |
021bf75 to
4fdcb91
Compare
When a replica that is not the most advanced one is promoted, a replica that already replayed WAL past the fork point of the new timeline can never follow the new primary. PostgreSQL logs "new timeline N forked off current database system timeline N-1 before current recovery point" and the replica waits forever. The operator still reports it healthy, keeps it in the read services, can pick it for the next promotion, and keeps its slot on the primary, which retains WAL. Each standby now runs the same check from its own pg_wal and reports the result in its status. Its readiness probe fails, so it leaves the read services, and it always sorts after a replica that has not diverged, so it is never promoted. The operator lists it under the new `divergedInstances` status field, sets the new `ReplicasHealthy` condition to `False`, raises a `ReplicaDiverged` event and exports the `cnpg_collector_replicas_diverged` metric. It also fences the instance and drops its slot on the primary, unless fencing it would leave too few replicas for synchronous replication. The fence is lifted once the instance's PGDATA PVC is replaced, as `kubectl cnpg destroy` does. Rebuilding the instance is still a manual step. Related: #11557, #11114 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]>
A replica that diverged onto a discarded timeline can never follow the new primary, so it cannot acknowledge commits and does not count towards the synchronous replication quorum. Fencing it therefore never reduces the number of replicas able to satisfy the quorum. Until now, on clusters with synchronous replication, the operator skipped fencing a diverged replica when the remaining ready replicas were fewer than `synchronous.number`. The replica was only reported, and kept running with its data diverged. The operator now fences every diverged replica, whatever the synchronous replication configuration. Signed-off-by: Leonardo Cecchi <[email protected]>
A replica that diverged onto a discarded timeline holds writes the primary never received and can't follow the current primary. Until now it was only sorted after the healthy replicas, so it was still promoted whenever it was the only candidate left, or the only one with a status. The operator now takes diverged replicas out of the candidates before electing a new primary, both in regular and in replica clusters. If every remaining replica has diverged, no failover happens and the cluster waits for a healthy replica instead of promoting data that was discarded. A diverged replica that fails its readiness probe no longer delays the reconciliation loop either, so it gets fenced as soon as it is detected. Signed-off-by: Leonardo Cecchi <[email protected]>
With the failover quorum check enabled, the operator only starts a failover when a replica surely holds every acknowledged transaction. A replica diverged onto a discarded timeline holds writes the primary discarded instead, and is never promoted, but it could still be counted as a promotable replica while its pod was ready. The quorum check now ignores diverged replicas, so a failover is no longer allowed on the strength of a replica that can't take over. Signed-off-by: Leonardo Cecchi <[email protected]>
With a fence on the whole cluster set, rebuilding a diverged replica made the lift fail on every pass, which stopped the reconcile loop until the fence was cleared. A fence lifted by hand, or wiped by a cluster-wide fence that was later lifted, was also never set again. The entry in the status is now the source of truth, and the operator asserts the fence on every pass. The lift ignores a fence on the whole cluster, which is not the operator's to remove. It also runs when the PGDATA PVC and its Pod are both gone, so the entry no longer outlives a removed instance. Detection keeps a parked entry, and any entry of an instance that might be unavailable, when no divergence is reported: right after a fence is lifted PostgreSQL has not started yet and reports nothing, and deleting the entry then would leave the replica running. Signed-off-by: Marco Nenciarini <[email protected]>
Their instances still fail the readiness probe when they diverge, so they leave the read services, but the operator neither reports nor fences them. Signed-off-by: Marco Nenciarini <[email protected]>
The readiness probe checks for a timeline divergence on every instance, including the primary, which has no replay position. The check listed pg_wal before finding that out, and the listing grows with the retained WAL. It now returns first when the replay position is empty. Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
The readiness check for a diverged standby was only exercised by the e2e. It now has unit tests for a standby past the fork point, one that did not replay past it while holding the segments it wrote there (a rewound former primary or a lagging standby), a primary, and a failure to read the replay position or to list pg_wal. The replay position is read through an optional field so the tests do not need a database. A history line that is empty, has no LSN or does not start with the standby's timeline gives no fork point, which counts as a divergence, as for a sibling branch. A table pins that down. Signed-off-by: Marco Nenciarini <[email protected]>
The diverged replica e2e resumed the stopped WAL receiver as soon as the former primary was not ready. If the operator had not started the failover yet, the former primary was still in the services, and the receiver could reach it and catch up, which breaks the divergence the test needs. The test now waits for the pending failover marker. The failover waits for the stopped receiver, so the marker stays set until it is resumed. Signed-off-by: Marco Nenciarini <[email protected]>
4fdcb91 to
ddbe634
Compare
Signed-off-by: Gabriele Bartolini <[email protected]>
…rimary's timeline (cloudnative-pg/cloudnative-pg#11251) When a replica that is not the most advanced one is promoted, for example after the primary is lost abruptly, a replica that already replayed WAL past the fork point of the new timeline can never follow the new primary. PostgreSQL logs "new timeline N forked off current database system timeline N-1 before current recovery point" and the replica waits forever. The operator still reports it healthy, keeps it in the read services, can pick it for the next promotion, and keeps its slot on the primary, which retains WAL. Each standby now checks its own pg_wal and reports when it has learned about the primary's newer timeline but already replayed past the point where that timeline forked away from its own. A replica that is merely behind is not diverged, since PostgreSQL switches it to the new timeline on its own. A diverged standby fails its readiness probe, so it leaves the read services. It is excluded from the election and from the failover quorum count, so it is never promoted, and it no longer holds back the readiness guard that precedes the election. The operator lists it in the new `divergedInstances` status field, sets the new `ReplicasHealthy` condition to `False`, raises a `ReplicaDiverged` event and exports the `cnpg_collector_replicas_diverged` metric. It also fences the instance and then drops its slot on the primary. The fence is set again on every pass, so lifting it by hand, or removing a fence on the whole cluster, does not release the instance; a fence on the whole cluster is left untouched. The containment is lifted only when the instance's PGDATA PVC is replaced, or when both the PVC and the Pod are gone. Rebuilding is manual: `kubectl cnpg destroy CLUSTER INSTANCE`. Not covered: the wrong promotion is not prevented in general, only for replicas already known to be diverged. A manual `kubectl cnpg promote` onto a diverged replica that is not fenced is not blocked. A replica that has not yet fetched the new history file is not detected. In a replica cluster the diverged instance still fails its readiness probe, but it is not reported or fenced. When every instance that still reports has diverged and the primary is gone, the election is skipped. Closes cloudnative-pg/cloudnative-pg#12007 Refs cloudnative-pg/cloudnative-pg#11557 Refs cloudnative-pg/cloudnative-pg#11715 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Signed-off-by: Gabriele Bartolini <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> Co-authored-by: Gabriele Bartolini <[email protected]> (cherry picked from commit cloudnative-pg/cloudnative-pg@f4f0f33)
When a replica that is not the most advanced one is promoted, for example after the primary is lost abruptly, a replica that already replayed WAL past the fork point of the new timeline can never follow the new primary. PostgreSQL logs "new timeline N forked off current database system timeline N-1 before current recovery point" and the replica waits forever. The operator still reports it healthy, keeps it in the read services, can pick it for the next promotion, and keeps its slot on the primary, which retains WAL.
Each standby now checks its own pg_wal and reports when it has learned about the primary's newer timeline but already replayed past the point where that timeline forked away from its own. A replica that is merely behind is not diverged, since PostgreSQL switches it to the new timeline on its own.
A diverged standby fails its readiness probe, so it leaves the read services. It is excluded from the election and from the failover quorum count, so it is never promoted, and it no longer holds back the readiness guard that precedes the election. The operator lists it in the new
divergedInstancesstatus field, sets the newReplicasHealthycondition toFalse, raises aReplicaDivergedevent and exports thecnpg_collector_replicas_divergedmetric. It also fences the instance and then drops its slot on the primary. The fence is set again on every pass, so lifting it by hand, or removing a fence on the whole cluster, does not release the instance; a fence on the whole cluster is left untouched. The containment is lifted only when the instance's PGDATA PVC is replaced, or when both the PVC and the Pod are gone. Rebuilding is manual:kubectl cnpg destroy CLUSTER INSTANCE.Not covered: the wrong promotion is not prevented in general, only for replicas already known to be diverged. A manual
kubectl cnpg promoteonto a diverged replica that is not fenced is not blocked. A replica that has not yet fetched the new history file is not detected. In a replica cluster the diverged instance still fails its readiness probe, but it is not reported or fenced. When every instance that still reports has diverged and the primary is gone, the election is skipped.Closes #12007
Refs #11557
Refs #11715