Skip to content

fix(status): do not retry instance status HTTP errors - #11793

Merged
mnencia merged 2 commits into
cloudnative-pg:mainfrom
kabbydriver:dev/11628
Nov 2, 2026
Merged

mnencia merged 2 commits into
cloudnative-pg:mainfrom
kabbydriver:dev/11628

Conversation

@kabbydriver

@kabbydriver kabbydriver commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

getReplicaStatusFromPodViaHTTP meant to stop retrying when an instance answers with a non-200 status, but errors.As targeted a StatusError value while rawInstanceStatusRequest returns a *StatusError. It never matched, so every HTTP error used all 5 attempts (about 1.6 seconds). Pods are queried one after the other, so one failing instance delayed the whole reconcile.

This showed after a switchover. The old primary answers HTTP 500 while its PostgreSQL is stopped, and each reconcile waited about 1.6 seconds on it. That delayed the primary role label on the new primary, and the -rw Service had no endpoint until the label moved. On kind with two instances, when the old primary answers HTTP 500, the label now follows currentPrimary after about 0.4 seconds instead of 3.9 (four switchovers each), and the -rw Service has no endpoint for about 3.1 seconds instead of 6.4 (four fixed, two baseline).

The target is now *StatusError. HTTP errors stop after one request and keep their status code and body. Transport errors still retry and timeouts still do not, so a switchover where the old primary refuses the connection is unchanged (about 3.9 seconds before and after).

A transient failure of the status probe is no longer hidden by the retry. The instance is reported as not answering right away. If it is the Ready primary, the operator logs a PrimaryStatusCheckFailed warning event and requeues after 10 seconds. Failover is still deferred, never triggered.

Closes #11628
Closes #11637


Original PR message:

Summary

Fix the errors.As destination from StatusError to *StatusError, matching the error returned by rawInstanceStatusRequest. Non-200 responses now stop after one request and preserve the status code/body; transient transport errors still retry.

Fixes #11628
Fixes #11637

AI disclosure: OpenAI Codex assisted with investigation, code, tests, validation, and this description.

Background

A three-instance CNPG 1.30.1 rolling restart showed a 3.748-second gap between PostgreSQL readiness on the new primary and its primary role label, delaying RW routing. Meanwhile, the old primary returned HTTP 500 because its PostgreSQL socket was absent. Unnecessary retries plausibly contributed; no end-to-end improvement has been measured.

Upstream #1708 proposed broader retry/timeout changes and was closed as resolved. Current comments and the original timeout change (ff6dd54d) explicitly exclude HTTP errors from retries. This patch leaves timeout/backoff, failover safety, and service reconciliation unchanged.

Validation

Local validation on October 14, 2026 used Go 1.27.2. Regression tests use mocked RoundTrippers through the actual instance-status request path:

  • Before fix: HTTP 500 regression failed with 5 requests instead of 1.
  • After fix: all 5 remote specs passed. HTTP 500 makes 1 request and preserves code/body; transport EOF followed by HTTP 200 succeeds in 2 requests with decoded status and no final error. Assertions use request counts, not timing.
  • Passed: main-module unit tests; formatting, vet, and golangci-lint 2.14.0 in both modules.
  • Rerun passed: make test, plus uncached remote (5 specs) and PostgreSQL helper (3 specs) suites, including the previously blocked Docker image pull.
  • E2E passed: BUILD_IMAGE=true OPERATOR=local FEATURE_TYPE=smoke,basic make e2e-test-kind on local Kubernetes v1.37.0: 50 passed, 0 failed, 207 skipped (205 outside the label filter; architecture and pod-patch tests skipped by default test depth). The deployed operator reported build commit c6c7b0ae. The disposable cluster was removed afterward.
  • Limits: full suite, higher-depth tests, upgrades, and the complete version matrix were not validated. No end-to-end routing improvement was measured.

@kabbydriver
kabbydriver requested a review from a team as a code owner October 28, 2026 21:53
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Oct 28, 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

/test

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@mnencia mnencia changed the title fix: stop retrying instance-status HTTP errors fix(status): do not retry instance status HTTP errors Nov 2, 2026
@leonardoce

Copy link
Copy Markdown
Contributor

I believe the E2e tests are failing because they didn't manage to download an image, and this is unrelated to this PR.

@mnencia

mnencia commented Nov 2, 2026

Copy link
Copy Markdown
Member

/ok-to-merge E2E green (failures are unrelated)

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Nov 2, 2026
kabbydriver and others added 2 commits November 2, 2026 15:06
Match the *StatusError returned by rawInstanceStatusRequest so HTTP errors stop immediately. Cover HTTP 500 response preservation and transient transport-error recovery through the instance-status request path.

Assisted-by: OpenAI Codex
Signed-off-by: Kabir Gogia <[email protected]>
@mnencia
mnencia merged commit 6e5c680 into cloudnative-pg:main Nov 2, 2026
cnpg-bot pushed a commit that referenced this pull request Nov 2, 2026
`getReplicaStatusFromPodViaHTTP` meant to stop retrying when an instance
answers with a non-200 status, but `errors.As` targeted a `StatusError`
value while `rawInstanceStatusRequest` returns a `*StatusError`. It
never matched, so every HTTP error used all 5 attempts (about 1.6
seconds). Pods are queried one after the other, so one failing instance
delayed the whole reconcile.

This showed after a switchover. The old primary answers HTTP 500 while
its PostgreSQL is stopped, and each reconcile waited about 1.6 seconds
on it. That delayed the primary role label on the new primary, and the
`-rw` Service had no endpoint until the label moved. On kind with two
instances, when the old primary answers HTTP 500, the label now follows
`currentPrimary` after about 0.4 seconds instead of 3.9 (four
switchovers each), and the `-rw` Service has no endpoint for about 3.1
seconds instead of 6.4 (four fixed, two baseline).

The target is now `*StatusError`. HTTP errors stop after one request and
keep their status code and body. Transport errors still retry and
timeouts still do not, so a switchover where the old primary refuses the
connection is unchanged (about 3.9 seconds before and after).

A transient failure of the status probe is no longer hidden by the
retry. The instance is reported as not answering right away. If it is
the Ready primary, the operator logs a `PrimaryStatusCheckFailed`
warning event and requeues after 10 seconds. Failover is still deferred,
never triggered.

Closes #11628
Closes #11637

Signed-off-by: Kabir Gogia <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit 6e5c680)
mnencia pushed a commit that referenced this pull request Nov 2, 2026
`getReplicaStatusFromPodViaHTTP` meant to stop retrying when an instance
answers with a non-200 status, but `errors.As` targeted a `StatusError`
value while `rawInstanceStatusRequest` returns a `*StatusError`. It
never matched, so every HTTP error used all 5 attempts (about 1.6
seconds). Pods are queried one after the other, so one failing instance
delayed the whole reconcile.

This showed after a switchover. The old primary answers HTTP 500 while
its PostgreSQL is stopped, and each reconcile waited about 1.6 seconds
on it. That delayed the primary role label on the new primary, and the
`-rw` Service had no endpoint until the label moved. On kind with two
instances, when the old primary answers HTTP 500, the label now follows
`currentPrimary` after about 0.4 seconds instead of 3.9 (four
switchovers each), and the `-rw` Service has no endpoint for about 3.1
seconds instead of 6.4 (four fixed, two baseline).

The target is now `*StatusError`. HTTP errors stop after one request and
keep their status code and body. Transport errors still retry and
timeouts still do not, so a switchover where the old primary refuses the
connection is unchanged (about 3.9 seconds before and after).

A transient failure of the status probe is no longer hidden by the
retry. The instance is reported as not answering right away. If it is
the Ready primary, the operator logs a `PrimaryStatusCheckFailed`
warning event and requeues after 10 seconds. Failover is still deferred,
never triggered.

Closes #11628
Closes #11637

Signed-off-by: Kabir Gogia <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit 6e5c680)
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

backport-requested ◀️ This pull request should be backported to all supported releases ok to merge 👌 This PR can be merged release-1.28 release-1.30

Projects

None yet

4 participants