Repository navigation
feat: Add automatic pooler pause/resume during switchover or failover - #9873
jakubhajek wants to merge 1 commit into
Conversation
|
❗ By default, the pull request is configured to backport to all release branches.
|
205e18c to
af36813
Compare
|
nb. this feature is referenced in docs as something we want to do - https://cloudnative-pg.io/docs/1.28/connection_pooling#pausing-connections Would be good to add some E2E tests around this Would be good to check types (things added on cluster, should they be on pooler?) For future direction, we don't want to close the door to having multiple cnpg clusters behind single poolers. So pooler should know about clusters, versus other way around. Probably want to move logic from cluster controller to pooler controller. Should be possible to "watch" cluster and it will dispatch notices when it does switchovers. Having pooler know more about clusters, it also knows more about pods, might enable other useful features in the future. It should also simplify E2E tests since there's a section dedicated to pooler. Completely separate comment - there is interest in integrating sysbench this into cnpg plugin as another testing tool. |
af36813 to
39671f7
Compare
39671f7 to
f8f88cb
Compare
d343fc0 to
38e38dc
Compare
d89c57e to
4052b29
Compare
|
@ardentperf @sxd Jeremy, Jonathan - thanks for the feedback - it seems that I have addressed all your points:
Manually tested: planned switchover, pod kill failover, multiple poolers (RW + RO), timeout force-resume, and manual pause not being overwritten. Ready for review when you get a chance. Thank you for the opportunity to contribute to the CNPG! |
fe93e34 to
6b9dc5a
Compare
6b9dc5a to
05c3746
Compare
05c3746 to
bf542e2
Compare
bf542e2 to
dada8c2
Compare
Just curious if the longer term goal for CNPG is to do the PAUSE before the switchover is initiated to prevent disconnects - to me this would be the expected behaviour. This PR is definitely a step in the right direction though. Thank you 😄 |
|
@charlesomerThe goal is to automate the process of upgrading Kubernetes worker nodes. Currently, we manually pause and resume the nodes using an additional Ansible playbook to minimise disruption to our customers. With this feature, we can automate the process, eliminating the need for any extra steps. Simply move the primary role to a replica, and the operator will handle the entire task, reducing the number of disruptions for our customers. |
|
i'm concerned with this approach. i do think that the finally merged code needs to pause connections before starting a switchover, making it seamless, and i'm worried that this code would have to be completely refactored to accomplish that also - what is the use case for |
dada8c2 to
81f362e
Compare
81f362e to
0321d1d
Compare
0321d1d to
845d49b
Compare
e32d369 to
b1826a2
Compare
|
@ardentperf thank you for the feedback. I reworked the design. Here is what the recent changes include.
|
b1826a2 to
5321607
Compare
5321607 to
81ff3b1
Compare
|
/test |
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/38752526531 |
81ff3b1 to
5321607
Compare
Signed-off-by: Jakub Hajek <[email protected]>
5321607 to
64e7bc6
Compare
Summary
Add automatic Pgbouncer pooler pause/resume during switchover or failover operations to minimize client connection failures. The operator drives the pause entirely through operator-owned state (status + an annotation) and never touches the user-owned Pooler
spec, so it is safe to use with GitOps.Fix: #8299
Disclaimer
This feature was developed with assistance from Claude Code. Significant effort was invested in manual testing to validate the behavior across multiple scenarios including manual switchover, automatic failover, multiple poolers, and edge cases.
Changes
API Changes
Add the following fields to
spec.pgbouncerin the Pooler CRD, since each pooler independently manages itsown pause/resume lifecycle:
pauseDuringSwitchover- opt-in boolean to enable the feature (default:false)pauseDuringSwitchoverTimeout- safety timeout (default:120s)Add status fields to Pooler:
status.pausedForSwitchover- tracks if this pooler is currently paused for a switchoverstatus.pausedForSwitchoverTimestamp- when this pooler was paused (RFC3339Micro)Add
cnpg.io/pausedDuringSwitchoverannotation to track auto-paused poolers.Controller Changes
CurrentPrimaryorTargetPrimarychangescurrentPrimary != targetPrimary), the pooler is marked paused viastatus.pausedForSwitchoverand thecnpg.io/pausedDuringSwitchoverannotation; the operator never writes tospec.pgbouncer.pausedPAUSE/RESUMEcommand when either the user requested it via spec or the operator paused it for switchover via status, so a manual pause and an automatic switchover pause coexistcurrentPrimary == targetPrimary)Documentation Changes
docs/src/connection_pooling.md- new "Automatic pause during switchover/failover" section explaining the feature, configuration, and behavior, including the GitOps-safe designdocs/src/labels_annotations.md- addedcnpg.io/pausedDuringSwitchoverannotation documentationdocs/src/cloudnative-pg.v1.md- updated PgBouncerSpec and PoolerStatus API reference tablesDefault Behaviour
status.pausedForSwitchoverand the annotation). It never changes the user-ownedspec.pgbouncer.paused, so Argo CD or Flux see no drift and do not fight the operator during a switchoverspec.pgbouncer.pausedare not modifiedConfiguration example
Events
Events
PoolerPausedPoolerResumedPoolerPauseTimeoutHow it Works
Manual Switchover (via
cnpg.io/targetPrimaryannotation orkubectl cnpg promote):currentPrimary != targetPrimary)status.pausedForSwitchover+ the annotation (neverspec); the pooler instance manager reads that status and issues the PgBouncerPAUSEcommandcurrentPrimary == targetPrimary), the status and annotation are cleared and the instance manager issuesRESUMEAutomatic Failover (primary pod failure):
targetPrimaryTimeout Protection:
Limitations
spec.clusteris a single reference). Users needing poolers for multiple clusters must create separate Pooler resources.currentPrimary != targetPrimary), not before the primary change is initiated. Pausing before the switchover starts (seamless for planned switchover) is possible future work; it is not achievable for failover, where the old primary is already gone by the time the operator reacts.authQuery,authQuerySecretorserverTLSSecretare skipped.Testing
Automated tests
synchronizePausepauses from status without touching spec, resumes when the status flag is cleared, and still honors a user pause via spectests/e2e/pgbouncer_pause_switchover_test.go):pauseDuringSwitchover: true, triggers a switchover, proves the pause through the persisted PoolerPaused event, and verifies the pooler resumes after completionservice-connectivitysuite passedPoolerPaused,PoolerResumed,PoolerPauseTimeout)make generate manifests- CRDs updated, no leftover Cluster CRD fieldsManual testing scenario
Apply the following manifests:
PGPASSWORDenvironment variable.Additional tests
On the same pod install Sysbench and run the following testing scenario:
Initialize a test table:
sysbench oltp_read_write \ --db-driver=pgsql \ --pgsql-host=pooler-switchover-pause-rw \ --pgsql-port=5432 \ --pgsql-user=app \ --pgsql-password=$PGPASSWORD \ --pgsql-db=app \ --tables=4 \ --table-size=10000 \ prepareRun benchmark that tolerates some errors rate:
sysbench oltp_read_write \ --db-driver=pgsql \ --pgsql-host=pooler-switchover-pause-rw \ --pgsql-port=5432 \ --pgsql-user=app \ --pgsql-password=$PGPASSWORD \ --pgsql-db=app \ --tables=4 \ --table-size=10000 \ --threads=10 \ --time=120 \ --report-interval=5 \ run