Repository navigation
feat(instance-manager): turn the liveness probe into a watchdog - #11358
Merged
Merged
Conversation
Contributor
|
❗ By default, the pull request is configured to backport to all release branches.
|
Contributor
Author
Contributor
Author
|
/test |
Contributor
|
@leonardoce, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/32492547872 |
leonardoce
marked this pull request as ready for review
August 24, 2026 07:21
This was referenced Sep 14, 2026
Closed
mnencia
approved these changes
Oct 21, 2026
Contributor
|
Related to #11721 |
Member
|
/test |
Contributor
|
@armru, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/39752319947 |
armru
approved these changes
Oct 22, 2026
The liveness probe used to always report healthy, so a Pod could never be restarted when it truly got stuck. It now fails, so the kubelet can kill and recreate the Pod, in two cases the primary lease and fencing logic can't recover from on their own: - the primary-lease loop stops attempting to renew or take over the lease altogether (e.g. a deadlock), as opposed to merely failing to renew, which is an expected condition handled separately by the lease/failsafe step-down check; - PostgreSQL was asked to shut down immediately (e.g. because this primary had to step down) but didn't honor it within a new configurable `.spec.immediateShutdownTimeout` (default 30s). Signed-off-by: Leonardo Cecchi <[email protected]>
The liveness probe fails again, when the primary lease loop stops attempting lock operations and when PostgreSQL ignores an immediate shutdown for longer than the configured timeout. The probe description still said that it only checks that the status web server answers. Assisted-by: Claude Signed-off-by: Marco Nenciarini <[email protected]>
A lock call blocked on an unreachable API server can last well beyond the renew deadline, and the heartbeat was recorded only when a call started. The liveness probe then failed and asked PostgreSQL for an immediate shutdown, stopping a healthy primary during a plain API outage. Report the watchdog healthy while a lock call is running, and beat when a call returns as well as when it starts, so the limit only has to cover the pause between two calls. Derive the limit from the lease timings once they are known. Assisted-by: Claude Signed-off-by: Marco Nenciarini <[email protected]>
Add the liveness watchdog of the lease loop to the primary lease model. A leader's lease loop may get stuck (LeaseLoopStuck); once its silence exceeds WatchdogMaxSilence the probe handler asks for the soft stop without a peer check, and the kubelet kills the node ProbeDelay ticks after that. MonitorWorks switches the lease-age monitor off to check what the watchdog covers alone. TLC shows the relation RenewDeadline + StepDownDelay < LeaseDuration is unchanged by the watchdog. With the monitor off the watchdog bound WatchdogMaxSilence + ProbeDelay < LeaseDuration holds only for a loop stuck right after its last confirmed write, and it is false at the production defaults (14s + 30s against 15s), so the watchdog is an availability backstop, not part of the fencing bound. The README documents the runs and the mapping to the code. Signed-off-by: Marco Nenciarini <[email protected]>
The watchdog stored the last lock call as Unix nanoseconds and rebuilt a time.Time from them, which drops the monotonic reading, so the silence it measured followed the wall clock. A forward step of the clock by more than the silence limit, as an NTP correction or a VM resume can make, failed the liveness probe of a healthy primary and requested an immediate shutdown of its PostgreSQL. A backward step hid a wedged loop for as long as the step. Keep the time.Time as time.Now returns it, as the step-down monitor already does for the lease age. Signed-off-by: Armando Ruocco <[email protected]>
The comment on the watchdog silence limit said that client-go jitters the retry period by up to 20%. Its acquire loop waits up to 2.2 times RetryPeriod between attempts, and the limit holds because client-go refuses a RenewDeadline that is not above 1.2 times RetryPeriod. Signed-off-by: Armando Ruocco <[email protected]>
The watchdog was only tested on its own, so nothing failed if the lease runnable stopped wrapping its lock, stopped marking the lease acquired or kept the default silence limit. These specs run the loop through Start and Acquire against a fake API server. The watchdog must stay healthy while the lease is renewed, while every call fails and while calls take longer than the limit to fail, and report the loop stuck once it stops. Signed-off-by: Armando Ruocco <[email protected]>
An unreachable former primary asks for an immediate shutdown on every reconcile, and each request ran pg_ctl again for up to immediateShutdownTimeout even after PostgreSQL had ignored the first one. The lifecycle manager reads the kubelet's termination signal only between two commands, so the restart that the failing liveness probe asks for waited for the attempt in progress, up to 30 seconds by default. Once PostgreSQL has been found unresponsive, answer the request with an error instead of running pg_ctl. Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
4 tasks done
cnpg-bot
pushed a commit
to cloudnative-pg/api
that referenced
this pull request
Oct 23, 2026
…dnative-pg/cloudnative-pg#11358) The liveness probe always reported healthy, so a Pod could never be restarted when it was truly stuck. It now fails, and the kubelet kills and recreates the Pod, in two cases the primary lease and fencing logic cannot recover from on their own. The first is a primary-lease loop that stops attempting lock calls altogether, for example a deadlock. Every Get, Create and Update on the lease lock beats a watchdog, whether or not the call succeeds, and a call still running counts as activity. A loop that is alive but cannot renew is not flagged: the step-down monitor handles it. The probe fails once the lease was held at least once and the silence exceeds `renewDeadline + 2 * retryPeriod`. It also asks PostgreSQL for an immediate shutdown, because the lifecycle manager usually still works when only the lease loop is stuck. The second is PostgreSQL ignoring an immediate shutdown, for example a backend stuck in an uninterruptible state, for longer than the new `.spec.immediateShutdownTimeout` (default 30s). The probe then fails, no further shutdown is attempted, and the lifecycle manager exits on the kubelet's termination signal without waiting. The TLA+ model of the primary lease now covers the watchdog. It does not set the fencing bound: at the defaults it is 14s + 30s against a 15s lease, so it is an availability backstop. Fencing stays with the lease-age monitor and its hard stop. Related to cloudnative-pg/cloudnative-pg#11721 Signed-off-by: Leonardo Cecchi <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Signed-off-by: Armando Ruocco <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> Co-authored-by: Armando Ruocco <[email protected]> (cherry picked from commit cloudnative-pg/cloudnative-pg@90dda4f)
mnencia
added a commit
that referenced
this pull request
Oct 23, 2026
…#11718) The superuser and metrics exporter pools connect to PostgreSQL with no timeout, and every query opens a new connection. When the postmaster is stopped or stuck, its socket still accepts connections but never answers the startup message, so every caller blocks for good. The instance status endpoint polled by the operator is one of them. On a KIND cluster with the primary's postmaster stopped with SIGSTOP, a goroutine dump shows the `pgStatus` handlers blocked in `pgconn.connectOne`. At shutdown the `:8000` web server waits for them until the controller manager gives up 30 seconds later, which delays the release of the primary lease by as much. The check that runs before an in-tree backup opens its own local connection and has the same problem, holding the backup request forever. This sets `connect_timeout=5` on these connections, as the connections to the primary already do. With #11358 applied, the same scenario releases the lease 5 seconds after the instance manager decides to exit, instead of 30. A TCP user timeout would not help: these connections go through the Unix socket, and even over TCP the kernel keeps acknowledging the data sent to a stopped postmaster. Closes #11722 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
cnpg-bot
pushed a commit
that referenced
this pull request
Oct 23, 2026
…#11718) The superuser and metrics exporter pools connect to PostgreSQL with no timeout, and every query opens a new connection. When the postmaster is stopped or stuck, its socket still accepts connections but never answers the startup message, so every caller blocks for good. The instance status endpoint polled by the operator is one of them. On a KIND cluster with the primary's postmaster stopped with SIGSTOP, a goroutine dump shows the `pgStatus` handlers blocked in `pgconn.connectOne`. At shutdown the `:8000` web server waits for them until the controller manager gives up 30 seconds later, which delays the release of the primary lease by as much. The check that runs before an in-tree backup opens its own local connection and has the same problem, holding the backup request forever. This sets `connect_timeout=5` on these connections, as the connections to the primary already do. With #11358 applied, the same scenario releases the lease 5 seconds after the instance manager decides to exit, instead of 30. A TCP user timeout would not help: these connections go through the Unix socket, and even over TCP the kernel keeps acknowledging the data sent to a stopped postmaster. Closes #11722 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> (cherry picked from commit 21e24f6)
cnpg-bot
pushed a commit
that referenced
this pull request
Oct 23, 2026
…#11718) The superuser and metrics exporter pools connect to PostgreSQL with no timeout, and every query opens a new connection. When the postmaster is stopped or stuck, its socket still accepts connections but never answers the startup message, so every caller blocks for good. The instance status endpoint polled by the operator is one of them. On a KIND cluster with the primary's postmaster stopped with SIGSTOP, a goroutine dump shows the `pgStatus` handlers blocked in `pgconn.connectOne`. At shutdown the `:8000` web server waits for them until the controller manager gives up 30 seconds later, which delays the release of the primary lease by as much. The check that runs before an in-tree backup opens its own local connection and has the same problem, holding the backup request forever. This sets `connect_timeout=5` on these connections, as the connections to the primary already do. With #11358 applied, the same scenario releases the lease 5 seconds after the instance manager decides to exit, instead of 30. A TCP user timeout would not help: these connections go through the Unix socket, and even over TCP the kernel keeps acknowledging the data sent to a stopped postmaster. Closes #11722 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> (cherry picked from commit 21e24f6)
gbartolini
pushed a commit
that referenced
this pull request
Nov 10, 2026
When the kubelet and the instance manager of the primary both hang, the walsenders keep the replicas' WAL receivers connected and the failover waits for them. The code already stops the old primary when either one still works (#11347, #11357, #11358). Document the recovery when neither does: the kubelet systemd watchdog plus a short liveness probe grace period. Otherwise the container kill waits for the pod grace period, 30 minutes by default. Closes #10591 Signed-off-by: Marco Nenciarini <[email protected]>
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.
The liveness probe always reported healthy, so a Pod could never be restarted when it was truly stuck. It now fails, and the kubelet kills and recreates the Pod, in two cases the primary lease and fencing logic cannot recover from on their own.
The first is a primary-lease loop that stops attempting lock calls altogether, for example a deadlock. Every Get, Create and Update on the lease lock beats a watchdog, whether or not the call succeeds, and a call still running counts as activity. A loop that is alive but cannot renew is not flagged: the step-down monitor handles it. The probe fails once the lease was held at least once and the silence exceeds
renewDeadline + 2 * retryPeriod. It also asks PostgreSQL for an immediate shutdown, because the lifecycle manager usually still works when only the lease loop is stuck.The second is PostgreSQL ignoring an immediate shutdown, for example a backend stuck in an uninterruptible state, for longer than the new
.spec.immediateShutdownTimeout(default 30s). The probe then fails, no further shutdown is attempted, and the lifecycle manager exits on the kubelet's termination signal without waiting.The TLA+ model of the primary lease now covers the watchdog. It does not set the fencing bound: at the defaults it is 14s + 30s against a 15s lease, so it is an availability backstop. Fencing stays with the lease-age monitor and its hard stop.
Related to #11721