Skip to content

fix(inkless:switch): return a diskless leader epoch with a diskless offset in ListOffsets [KC-865] - #900

Merged
viktorsomogyi merged 1 commit into
mainfrom
giuseppelillo/list-offsets-diskless-leader-epoch
Nov 27, 2026
Merged

viktorsomogyi merged 1 commit into
mainfrom
giuseppelillo/list-offsets-diskless-leader-epoch

Conversation

@giuseppelillo

Copy link
Copy Markdown
Contributor

A consumer that takes its position and leader epoch from ListOffsets on a switched partition seeks back to the seal the next time it validates that position. With this change, ListOffsets returns an epoch that keeps the position.

Problem

ListOffsets returns leader epoch 0 with every diskless offset, because FetchOffsetHandler sets a placeholder epoch on each control-plane result. On a switched partition, ReplicaManager.lastOffsetForLeaderEpoch treats an epoch below initialDisklessLeaderEpoch as a classic epoch and answers with an end offset at or below the seal. A client that keeps the epoch of the ListOffsets response then sees a truncation when it validates its position.

A consumer fetch doesn't have this problem: ReplicaManager.stampDisklessLeaderEpoch sets disklessLeaderEpoch on the batches that a switched partition serves from diskless storage. The Java consumer drops the epoch of a reset position (OffsetFetcherUtils.resetPositionIfNeeded), so it isn't affected on a reset. Other clients keep the epoch:

  • librdkafka passes the epoch of the ListOffsets response to the fetch position (rd_kafka_toppar_handle_Offset), and rd_kafka_offset_validate sends it when metadata shows a higher leader epoch.
  • The Java consumer, if the application calls seek(TopicPartition, OffsetAndMetadata) with the epoch from offsetsForTimes or Admin.listOffsets.

Example before this change

On a 3-broker cluster, a partition is sealed at offset 20 and has 30 diskless records:

  • Admin.listOffsets(latest) returns offset 50 with leaderEpoch=0. A Java consumer that seeks to offset 50 with that epoch reads its first record at offset 20.
  • kcat 1.7.0 (librdkafka 2.11.1) starts at the log end while the partition is in phase 1 and receives no record. After the leader epoch bump that starts phase 2, with no record produced, kcat prints offsets 20 to 49.

Fix

DisklessFetchOffsetRouter replaces the placeholder on a switched partition:

  • An offset at or past the seal gets disklessLeaderEpoch, the epoch that a consumer fetch carries for the same offsets. OffsetsForLeaderEpoch answers that epoch with the diskless log end offset, so the client keeps its position.
  • An offset below the seal gets no leader epoch, because the broker doesn't know its classic epoch. The client skips the validation.

Born-diskless partitions keep the placeholder, and a result from the local log keeps the epoch of the local record.

Test plan

  • DisklessFetchOffsetRouterTest covers LATEST, MAX_TIMESTAMP, EARLIEST, and a timestamp lookup on both sides of the seal, in phase 1 and phase 2, and a partition with a seal of 0.
  • ReplicaManagerInklessTest.testFetchOffsetLatestOnSwitchedPartitionReturnsAnEpochThatKeepsThePosition sends ListOffsets and validates the returned epoch with OffsetsForLeaderEpoch.
  • InklessTopicTypeSwitcherClusterTest.testConsumerKeepsAPositionFromListOffsetsOnASwitchedPartition runs on 3 brokers. A consumer seeks to the offset and epoch that ListOffsets returns, in phase 1 and in phase 2, and starts at that offset.
  • Without the change to DisklessFetchOffsetRouter, the new router tests, the ReplicaManagerInklessTest test, and the cluster test fail.
  • DisklessFetchOffsetRouterTest passes with 65 tests. :core:checkstyleMain, :core:checkstyleTest, and :core:spotbugsMain pass. make vale reports no alerts on the added lines.

🤖 Generated with Claude Code

…ffset in ListOffsets [KC-865]

`ListOffsets` returns leader epoch 0 with every diskless offset, because
`FetchOffsetHandler` sets a placeholder epoch on each control-plane
result. On a switched partition, `ReplicaManager.lastOffsetForLeaderEpoch`
treats an epoch below `initialDisklessLeaderEpoch` as a classic epoch and
answers with an end offset at or below the seal. A client that keeps the
epoch of the `ListOffsets` response then sees a truncation when it
validates its position, and seeks back to the seal. librdkafka keeps that
epoch, and the Java consumer does if the application seeks with it.

On a 3-broker cluster, kcat (librdkafka 2.11.1) that starts at the log
end of a partition sealed at 20, and receives no record, reads offsets 20
to 49 again after the leader epoch bump that starts phase 2.

`DisklessFetchOffsetRouter` now replaces the placeholder on a switched
partition:

- An offset at or past the seal gets `disklessLeaderEpoch`, the epoch
  that a consumer fetch carries for the same offsets.
  `OffsetsForLeaderEpoch` answers that epoch with the diskless log end
  offset, so the client keeps its position.
- An offset below the seal gets no leader epoch, because the broker
  doesn't know its classic epoch. The client skips the validation.

Born-diskless partitions keep the placeholder, and a result from the
local log keeps the epoch of the local record.

## Testing

- `DisklessFetchOffsetRouterTest` covers LATEST, MAX_TIMESTAMP, EARLIEST,
  and a timestamp lookup on both sides of the seal, in phase 1 and
  phase 2, and a partition with a seal of 0.
- `ReplicaManagerInklessTest.testFetchOffsetLatestOnSwitchedPartitionReturnsAnEpochThatKeepsThePosition`
  sends `ListOffsets` and validates the returned epoch with
  `OffsetsForLeaderEpoch`.
- `InklessTopicTypeSwitcherClusterTest.testConsumerKeepsAPositionFromListOffsetsOnASwitchedPartition`
  runs on 3 brokers. A consumer seeks to the offset and epoch that
  `ListOffsets` returns, in phase 1 and in phase 2, and starts at that
  offset.
- Without the change to `DisklessFetchOffsetRouter`, the new router
  tests, the `ReplicaManagerInklessTest` test, and the cluster test fail.
- `DisklessFetchOffsetRouterTest` passes with 65 tests.
  `:core:checkstyleMain`, `:core:checkstyleTest`, and
  `:core:spotbugsMain` pass.

Assisted-by: Claude Opus 5.5
Assisted-by: Claude Fable 5.2

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

There are no active review issues blocking the ListOffsets leader-epoch fix.

No open issues 🎉

What changed in this PR

This PR fixes ListOffsets leader epochs for partitions switched from classic to diskless storage, preventing clients from incorrectly rewinding to the seal when validating their positions.

Changes:

  • Return the diskless leader epoch for control-plane offsets at or past the seal, and omit the epoch for offsets below it.
  • Add router, replica-manager, and three-broker consumer regression tests.
  • Document epoch handling while preserving local-log and born-diskless behavior.
File Description
docs/​inkless/​DISKLESS_CONSOLIDATION.md Documents ListOffsets epoch selection and client validation behavior.
core/​src/​test/​scala/​unit/​kafka/​server/​ReplicaManagerInklessTest.scala Checks that epoch validation preserves the returned latest position.
core/​src/​test/​scala/​unit/​kafka/​server/​DisklessFetchOffsetRouterTest.scala Covers epoch selection around the seal and born-diskless behavior.
core/​src/​test/​java/​kafka/​server/​InklessTopicTypeSwitcherClusterTest.java Tests consumer positioning in both switch phases on three brokers.
core/​src/​main/​scala/​kafka/​server/​DisklessFetchOffsetRouter.scala Replaces placeholder epochs on switched-partition control-plane results.

Review effort: Balanced

@viktorsomogyi
viktorsomogyi merged commit 55409b0 into main Nov 27, 2026
9 checks passed
@viktorsomogyi
viktorsomogyi deleted the giuseppelillo/list-offsets-diskless-leader-epoch branch November 27, 2026 10:38
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.

3 participants