Skip to content

feat(instance-manager): turn the liveness probe into a watchdog - #11358

Merged
armru merged 9 commits into
cloudnative-pg:mainfrom
leonardoce:watchdog
Oct 22, 2026
Merged

armru merged 9 commits into
cloudnative-pg:mainfrom
leonardoce:watchdog

Conversation

@leonardoce

@leonardoce leonardoce commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

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

@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 Aug 21, 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

@leonardoce

Copy link
Copy Markdown
Contributor Author

IMPORTANT

This PR depends on #11347 #11350 and #11357 that are cherry-picked here out of convenience but should be removed before merging it.

@leonardoce leonardoce added do not backport This PR must not be backported - it will be in the next minor release no-issue and removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.29 release-1.30 labels Aug 21, 2026
@leonardoce

Copy link
Copy Markdown
Contributor Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@gbartolini

Copy link
Copy Markdown
Contributor

Related to #11721

@armru

armru commented Oct 22, 2026

Copy link
Copy Markdown
Member

/test

@github-actions

Copy link
Copy Markdown
Contributor

leonardoce and others added 9 commits October 22, 2026 11:09
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]>
@armru
armru merged commit 90dda4f into cloudnative-pg:main Oct 22, 2026
@github-project-automation github-project-automation Bot moved this from In Second Review to Done in CloudNativePG operator development Oct 22, 2026
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]>
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 no-issue ok to merge 👌 This PR can be merged size:XXL This PR changes 1000+ lines, ignoring generated files.

Development

Successfully merging this pull request may close these issues.

5 participants