Skip to content

fix(inkless:control-plane): guard against stale PostgreSQL standby reads [KC-859] - #906

Open
jeqo wants to merge 1 commit into
mainfrom
jeqo/kc-859
Open

jeqo wants to merge 1 commit into
mainfrom
jeqo/kc-859

Conversation

@jeqo

@jeqo jeqo commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

When PostgresControlPlane routes reads to a PostgreSQL standby, a restarted or lagging standby can serve an older snapshot during node replacement or switchover. Consumers fetching an offset valid on the primary receive a false OFFSET_OUT_OF_RANGE and reset their offsets, and consolidating fetchers call listOffsets(LATEST) against the same stale standby and truncate their local log and high watermark backward.

Add an optional inkless.control.plane.read.primary.* pool and activate standby protection when both read.* and read.primary.* are configured:

  • Route listOffsets, getCrossTierLogStart, and getProducerState directly to readPrimaryDataSource so staleness that list_offsets_v1 can't signal never reaches callers. Keep jobsDataSource isolated when read.primary.* is absent.
  • When findBatches receives above-high-watermark OFFSET_OUT_OF_RANGE from the standby, re-run the fetch against readPrimaryDataSource behind a fail-fast concurrency limiter (standby.fallback.max.concurrent, defaulting to 90% of read.primary.max.connections, at least 1) so confirmation and fallback bursts don't starve listOffsets, getCrossTierLogStart, and getProducerState sharing the same pool. Re-running the full fetch on primary lets find_batches_v2 enforce fetchMaxBytes, per-partition limits, and the request-wide minimum-batch allowance in one pass with fresh watermarks. Skip below-LSO errors, which are monotonically expired, and return retriable KAFKA_STORAGE_ERROR for only the unconfirmed partitions when the limiter rejects or primary fails while preserving healthy standby responses for other partitions in the same fetch.
  • Add inkless.control.plane.read.primary.connection.string to InklessControlPlaneConfigs.RECONFIGURABLE_CONFIGS and DynamicInklessControlPlaneConfig with the same runtime takedown semantics as the existing connection strings.
  • Publish FindBatchesPrimaryQueryTime, FindBatchesPrimaryConfirmRate, FindBatchesPrimaryOverrideRate, FindBatchesPrimaryConfirmFailureRate, and PrimaryReadLimitRejectedRate on PostgresControlPlaneMetrics, and regenerate the config and metrics reference docs.

Testing

  • PostgresControlPlaneStandbyProtectionTest exercises two PostgreSQL databases where the read database lags behind the primary, covering above-HWM override, genuine out-of-range confirmation, below-LSO skip, request-order fetchMaxBytes and zero-limit enforcement across both orderings, mixed-partition concurrency-limit rejection and primary failure, primary routing for listOffsets, getCrossTierLogStart, and getProducerState, and read.*-only non-fallback.
  • ConsolidationFetcherThreadTest verifies with a real UnifiedLog that consolidation doesn't truncate or regress local highWatermark on KAFKA_STORAGE_ERROR or when listOffsets reads ahead from primary.
  • Unit tests cover read.primary.* parsing, prefix validation, dynamic reconfiguration, FindBatchResponse.kafkaStorageError(), and the new metrics.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The primary query resets the minimum-batch allowance after standby results exhaust fetchMaxBytes, allowing the combined response to exceed the requested byte budget.

1 open issue
What changed in this PR

Adds opt-in PostgreSQL standby protection to Inkless’s control plane to reduce false offset errors and protect consolidation from stale offset lookups.

Changes:

  • Adds a primary-read pool, configuration validation, and runtime connection-string updates.
  • Confirms above-watermark standby errors on primary with bounded concurrency and retriable errors when confirmation fails.
  • Adds protection metrics, regression tests, and generated configuration and metrics documentation.
File Description
storage/​inkless/​src/​test/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlaneStandbyProtectionTest.java Tests lagging reads, primary routing, confirmation failures, and concurrency limits.
storage/​inkless/​src/​test/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlaneMetricsTest.java Tests protection metrics and their documentation registration.
storage/​inkless/​src/​test/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlaneConfigureTest.java Tests primary-read connection-string requirements.
storage/​inkless/​src/​test/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlaneConfigTest.java Tests pool configuration isolation and concurrency defaults.
storage/​inkless/​src/​test/​java/​io/​aiven/​inkless/​control_plane/​FindBatchResponseTest.java Tests the storage-error response factory.
storage/​inkless/​src/​test/​java/​io/​aiven/​inkless/​config/​InklessControlPlaneConfigsConsistencyTest.java Checks consistency of the new reconfigurable key.
storage/​inkless/​src/​main/​java/​io/​aiven/​inkless/​doc/​ConfigsDocs.java Generates primary-read pool documentation.
storage/​inkless/​src/​main/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlaneMetrics.java Defines confirmation, override, failure, timing, and rejection metrics.
storage/​inkless/​src/​main/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlaneConfig.java Adds primary-read settings and the confirmation concurrency cap.
storage/​inkless/​src/​main/​java/​io/​aiven/​inkless/​control_plane/​postgres/​PostgresControlPlane.java Implements primary routing, standby confirmation, and pool lifecycle management.
storage/​inkless/​src/​main/​java/​io/​aiven/​inkless/​control_plane/​FindBatchResponse.java Adds a retriable storage-error response factory.
server-common/​src/​main/​java/​org/​apache/​kafka/​server/​config/​InklessControlPlaneConfigs.java Registers the primary-read connection string for reconfiguration.
docs/​inkless/​metrics.rst Documents standby-protection metrics.
docs/​inkless/​configs.rst Documents primary-read settings and concurrency limits.
core/​src/​test/​scala/​unit/​kafka/​server/​InklessControlPlaneConnectionStringValidatorTest.scala Tests runtime scope and credential restrictions.
core/​src/​test/​scala/​unit/​kafka/​server/​DynamicConfigChangeTest.scala Tests reconfiguration registration and empty-string takedown.
core/​src/​test/​scala/​io/​aiven/​inkless/​consolidation/​ConsolidationFetcherThreadTest.scala Tests log and watermark preservation during error recovery.
core/​src/​main/​scala/​kafka/​server/​InklessControlPlaneConnectionStringValidator.scala Updates comments describing runtime configuration restrictions.
core/​src/​main/​scala/​kafka/​server/​DynamicBrokerConfig.scala Extends takedown handling to the primary-read connection string.

Review effort: Balanced

@jeqo
jeqo force-pushed the jeqo/kc-859 branch 3 times, most recently from 3812dd0 to 025f228 Compare November 27, 2026 11:26
@jeqo
jeqo requested a balanced review from Copilot November 27, 2026 11:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The merge can retain an extra batch for a zero-limit partition after another partition returns data, violating fetch byte limits and requiring a shared request-wide minimum-batch allowance with regression coverage for both query orderings.

1 open issue
1 resolved since last review

Review effort: Balanced

…ads [KC-859]

When `PostgresControlPlane` routes reads to a PostgreSQL standby, a
restarted or lagging standby can serve an older snapshot during node
replacement or switchover. Consumers fetching an offset valid on the
primary receive a false `OFFSET_OUT_OF_RANGE` and reset their offsets,
and consolidating fetchers call `listOffsets(LATEST)` against the same
stale standby and truncate their local log and high watermark backward.

Add an optional `inkless.control.plane.read.primary.*` pool and activate
standby protection when both `read.*` and `read.primary.*` are
configured:

- Route `listOffsets`, `getCrossTierLogStart`, and `getProducerState`
  directly to `readPrimaryDataSource` so staleness that `list_offsets_v1`
  can't signal never reaches callers. Keep `jobsDataSource` isolated
  when `read.primary.*` is absent.
- When `findBatches` receives above-high-watermark `OFFSET_OUT_OF_RANGE`
  from the standby, re-run the fetch against `readPrimaryDataSource`
  behind a fail-fast concurrency limiter
  (`standby.fallback.max.concurrent`, defaulting to 90% of
  `read.primary.max.connections`, at least 1) so confirmation and
  fallback bursts don't starve `listOffsets`, `getCrossTierLogStart`,
  and `getProducerState` sharing the same pool. Re-running the full
  fetch on primary lets `find_batches_v2` enforce `fetchMaxBytes`,
  per-partition limits, and the request-wide minimum-batch allowance in
  one pass with fresh watermarks. Skip below-LSO errors, which are
  monotonically expired, and return retriable `KAFKA_STORAGE_ERROR` for
  only the unconfirmed partitions when the limiter rejects or primary
  fails while preserving healthy standby responses for other partitions
  in the same fetch.
- Add `inkless.control.plane.read.primary.connection.string` to
  `InklessControlPlaneConfigs.RECONFIGURABLE_CONFIGS` and
  `DynamicInklessControlPlaneConfig` with the same runtime takedown
  semantics as the existing connection strings.
- Publish `FindBatchesPrimaryQueryTime`, `FindBatchesPrimaryConfirmRate`,
  `FindBatchesPrimaryOverrideRate`,
  `FindBatchesPrimaryConfirmFailureRate`, and
  `PrimaryReadLimitRejectedRate` on `PostgresControlPlaneMetrics`, and
  regenerate the config and metrics reference docs.

Testing

- `PostgresControlPlaneStandbyProtectionTest` exercises two PostgreSQL
  databases where the read database lags behind the primary, covering
  above-HWM override, genuine out-of-range confirmation, below-LSO skip,
  request-order `fetchMaxBytes` and zero-limit enforcement across both
  orderings, mixed-partition concurrency-limit rejection and primary
  failure, primary routing for `listOffsets`, `getCrossTierLogStart`,
  and `getProducerState`, and `read.*`-only non-fallback.
- `ConsolidationFetcherThreadTest` verifies with a real `UnifiedLog` that
  consolidation doesn't truncate or regress local `highWatermark` on
  `KAFKA_STORAGE_ERROR` or when `listOffsets` reads ahead from primary.
- Unit tests cover `read.primary.*` parsing, prefix validation, dynamic
  reconfiguration, `FindBatchResponse.kafkaStorageError()`, and the new
  metrics.

Assisted-by: Gemini 4 Neon
Co-authored-by: Cursor <cursoragent@cursor.com>
@jeqo
jeqo marked this pull request as ready for review November 27, 2026 12:54
@jeqo
jeqo requested a balanced review from Copilot November 27, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the optional primary-read protection change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

2 participants