Skip to content

feat(inkless:control-plane): add standby circuit breaker with primary fallback [KC-858] - #910

Draft
jeqo wants to merge 1 commit into
jeqo/kc-859from
jeqo/kc-858-standby-circuit-breaker
Draft

jeqo wants to merge 1 commit into
jeqo/kc-859from
jeqo/kc-858-standby-circuit-breaker

Conversation

@jeqo

@jeqo jeqo commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Builds on #906.

When a PostgreSQL read replica restarts or becomes unreachable during a switchover, findBatches calls against readDataSource stall up to the pool or socket timeout and fail with UNKNOWN_SERVER_ERROR until DNS failover completes.

Add StandbyCircuitBreaker when both read.* and read.primary.* are configured:

  • Classify standby connectivity and timeout failures across the cause, nextException, and suppressed chains (08*, 57P01, 57P02, 57P03, SQLTransientConnectionException, SocketTimeoutException, ConnectException, UnknownHostException), excluding query and serialization errors (40001, 40P01).
  • Fall back immediately to primaryReadJooqCtx through the fail-fast concurrency limiter on every standby connectivity failure (including failures before reaching the trip threshold) and while OPEN, returning per-partition retriable KAFKA_STORAGE_ERROR when the limiter rejects or the primary query throws.
  • Trip CLOSED -> OPEN after standby.circuit.breaker.failure.threshold (default 3) consecutive failures, soft-evict idle standby pool connections so recovery resolves fresh DNS, and probe the standby with SELECT 1 on a daemon thread every standby.circuit.breaker.probe.interval.ms (default 10000) using fixed-delay scheduling. Close on the first successful probe and bump a generation counter so late in-flight failures from an earlier outage can't re-trip the recovered circuit.
  • Publish StandbyCircuitOpen (CAS-detached on close() so rebuilt control planes don't clobber each other's gauge), StandbyFallbackRate, and StandbyFallbackFailureRate on PostgresControlPlaneMetrics, reusing FindBatchesPrimaryQueryTime and PrimaryReadLimitRejectedRate, and regenerate the config and metrics reference docs.

Testing

  • StandbyCircuitBreakerTest verifies failure classification, cycle safety, CLOSED -> OPEN -> CLOSED transitions, soft eviction, stale-generation protection, rebuild-safe gauge detachment, and non-blocking close().
  • PostgresControlPlaneStandbyProtectionTest exercises a local TCP proxy in front of Testcontainers PostgreSQL to verify pre-trip fallback, tripping open, bypassing the dead standby while open, concurrency-limit rejection and primary failure returning per-partition KAFKA_STORAGE_ERROR, and automatic recovery via the background SELECT 1 probe.
  • PostgresControlPlaneConfigTest and PostgresControlPlaneMetricsTest verify the new config defaults, ranges, and metrics.

… fallback [KC-858]

When a PostgreSQL read replica restarts or becomes unreachable during a
switchover, `findBatches` calls against `readDataSource` stall up to the
pool or socket timeout and fail with `UNKNOWN_SERVER_ERROR` until DNS
failover completes.

Add `StandbyCircuitBreaker` when both `read.*` and `read.primary.*` are
configured:

- Classify standby connectivity and timeout failures across the cause,
  `nextException`, and suppressed chains (`08*`, `57P01`, `57P02`,
  `57P03`, `SQLTransientConnectionException`, `SocketTimeoutException`,
  `ConnectException`, `UnknownHostException`), excluding query and
  serialization errors (`40001`, `40P01`).
- Fall back immediately to `primaryReadJooqCtx` through the fail-fast
  concurrency limiter on every standby connectivity failure (including
  failures before reaching the trip threshold) and while `OPEN`,
  returning per-partition retriable `KAFKA_STORAGE_ERROR` when the
  limiter rejects or the primary query throws.
- Trip `CLOSED -> OPEN` after `standby.circuit.breaker.failure.threshold`
  (default `3`) consecutive failures, soft-evict idle standby pool
  connections so recovery resolves fresh DNS, and probe the standby with
  `SELECT 1` on a daemon thread every
  `standby.circuit.breaker.probe.interval.ms` (default `10000`) using
  fixed-delay scheduling. Close on the first successful probe and bump a
  generation counter so late in-flight failures from an earlier outage
  cannot re-trip the recovered circuit.
- Publish `StandbyCircuitOpen` (CAS-detached on `close()` so rebuilt
  control planes do not clobber each other's gauge),
  `StandbyFallbackRate`, and `StandbyFallbackFailureRate` on
  `PostgresControlPlaneMetrics`, reusing `FindBatchesPrimaryQueryTime`
  and `PrimaryReadLimitRejectedRate`, and regenerate the config and
  metrics reference docs.

Testing

- `StandbyCircuitBreakerTest` verifies failure classification, cycle
  safety, `CLOSED -> OPEN -> CLOSED` transitions, soft eviction,
  stale-generation protection, rebuild-safe gauge detachment, and
  non-blocking `close()`.
- `PostgresControlPlaneStandbyProtectionTest` exercises a local TCP
  proxy in front of Testcontainers PostgreSQL to verify pre-trip
  fallback, tripping open, bypassing the dead standby while open,
  concurrency-limit rejection and primary failure returning
  per-partition `KAFKA_STORAGE_ERROR`, and automatic recovery via the
  background `SELECT 1` probe.
- `PostgresControlPlaneConfigTest` and `PostgresControlPlaneMetricsTest`
  verify the new config defaults, ranges, and metrics.

Assisted-by: Gemini 4 Neon
Co-authored-by: Cursor <cursoragent@cursor.com>
@jeqo
jeqo added this pull request to stack #912 November 27, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant