8000 fix(inkless:switch): answer epoch 0 from the diskless log in OffsetsForLeaderEpoch [KC-852] by giuseppelillo · Pull Request #888 · aiven/inkless · GitHub
[go: up one dir, main page]

Skip to content

fix(inkless:switch): answer epoch 0 from the diskless log in OffsetsForLeaderEpoch [KC-852] - #888

Closed
giuseppelillo wants to merge 1 commit into
mainfrom
claude/kc-852-bug-ec7403
Closed

giuseppelillo wants to merge 1 commit into
mainfrom
claude/kc-852-bug-ec7403

Conversation

@giuseppelillo
@giuseppelillo giuseppelillo commented Nov 20, 2026 •
Copy link
Copy Markdown
Contributor

A consumer that reads past the seal of a switched partition resets to the seal the next time it validates its position. It then re-reads every record from the seal to that position.

Problem

Diskless produce stamps every batch with leader epoch 0 (UnifiedLog.LEADER_EPOCH), and FetchHandler returns the batches with that epoch. A consumer that tails a switched partition therefore holds a position past the seal with offsetEpoch=0.

The consumer validates a position with OffsetsForLeaderEpoch in two cases: when it resumes from a committed offset that carries a leader epoch, and when a metadata update shows a different leader or leader epoch for the partition. The request carries the epoch of the position and no offset.

For a switched partition, ReplicaManager.lastOffsetForLeaderEpoch asks the local classic log first. If the classic end offset is at or below classicToDisklessStartOffset, the broker returns it. In the classic log, epoch 0 is a real epoch that ends at or below the seal, so the broker answers the diskless placeholder with a classic end offset. The consumer treats an end offset below its position as a truncation.

Example on main

The setup matches testLastOffsetForLeaderEpochKeepsPlaceholderEpochPositionPastSeal:

Item Value
Classic prefix Offsets 0 to 9, leader epoch 0
Seal (classicToDisklessStartOffset) 10
Diskless leader epoch 5
Local log of the answering broker Offsets 0 to 12, epoch cache [(epoch 0, start 0), (epoch 5, start 10)]
Diskless end offset 200
Consumer position Offset 200, offsetEpoch=0

The validation runs in this order:

  1. The consumer sends OffsetsForLeaderEpoch with leaderEpoch=0.
  2. The broker asks the local classic log. LeaderEpochFileCache.endOffsetFor(0) returns the start offset of the next cached epoch, which is 10.
  3. The classic answer, 10, is at or below the seal, so the broker returns endOffset=10.
  4. The consumer compares the end offset with its position. 10 is less than 200, so the consumer logs Truncation detected and seeks to offset 10.
  5. The consumer reads offsets 10 to 199 a second time.

Fix

For a switched partition, the broker answers a request for epoch 0 with the diskless end offset. The classic log still answers every other epoch that ends at or below the seal.

The diskless answer for a switched partition is at least the committed seal. A control-plane placeholder row reports end offset 0, and without the floor a consumer in the classic prefix would truncate to 0. DisklessLeaderEndPoint.atLeastCommittedSeal applies the same floor for consolidating followers.

The local lookup still runs first, so its errors, such as NOT_LEADER_OR_FOLLOWER, reach the client as before.

The rule keys on the requested epoch, not on the classic answer. If the classic log had leader changes before the switch, classic epoch 0 ends at the sta 10000 rt of classic epoch 1, which is below the seal. A check that compares the classic answer with the seal doesn't catch that case.

The same example with the fix

  1. The consumer sends OffsetsForLeaderEpoch with leaderEpoch=0.
  2. The requested epoch is 0, so the broker doesn't use the classic answer. It asks the control plane for the end offset, which is 200.
  3. 200 is above the seal, 10, so the floor doesn't apply. The broker returns endOffset=200.
  4. The consumer compares the end offset with its position. 200 isn't less than 200, so the validation completes and the consumer keeps position 200.

Trade-offs

  • A consumer whose position is in the classic prefix with a real epoch 0 gets the diskless end offset, which is never below its position. That consumer doesn't detect a truncation of the classic log.
  • Validating a position with epoch 0 needs the control plane. If the control plane lookup fails, the broker returns the error for that partition instead of a local answer.
  • Batches that FetchHandler returns still carry epoch 0.

Test plan

  • testLastOffsetForLeaderEpochKeepsPlaceholderEpochPositionPastSeal builds the example and expects end offset 200 for epoch 0. On main it fails with expected: <200> but was: <10>.
  • testLastOffsetForLeaderEpochHybridFloorsDisklessEndOffsetAtSeal has the control plane report end offset 0 for a partition sealed at 101 and expects end offset 101.
  • testLastOffsetForLeaderEpochHybridAtSwitchBoundaryUsesLocalLog and testLastOffsetForLeaderEpochHybridAtSwitchBoundaryUsesFollowerLocalLog asserted that the classic log answers epoch 0. They now build the classic prefix with epoch 1 and request epoch 1, so they still cover the classic answer at the seal.
  • ReplicaManagerInklessTest (309 tests) and InklessDisklessListOffsetsTest (5 tests) pass.
  • On a 3-broker cluster with the InklessTopicTypeSwitcherClusterTest setup, a consumer tails a switched partition sealed at offset 20 and commits offset 50 with leaderEpoch=0. A second consumer in the same group starts at offset 20 on main and at offset 50 with this change. That test isn't part of this PR, because its precondition depends on timing: a consumer that starts after consolidation catches up reads the local log, commits leaderEpoch=2, and resumes at its committed offset on main.
  • make vale reports no inkless-owned prose to lint in this change.
  • The system test test_switch_succeeds_with_degraded_network, where the failure first showed, passes in 3 local runs on this change. In 2 of the runs, the consumer validates a position with epoch 0 about 3 seconds after phase 2 starts, on 3 partitions in total. Each answer is above the partition's seal: 1836 for seal 1674, 1908 for seal 1742, and 1757 for seal 1625. In the third run, the consumer validates only at the seal. The test wasn't run on main locally.

🤖 Generated with Claude Code

…orLeaderEpoch [KC-852]

Diskless produce stamps every batch with leader epoch 0, so a consumer that
reads past the seal of a switched partition through FetchHandler holds a
position with epoch 0. When the consumer validates that position,
ReplicaManager.lastOffsetForLeaderEpoch asks the local classic log, where
epoch 0 is a real epoch that ends at or below the seal. The broker returns
that end offset, the consumer sees it below its position, and it resets to
the seal and re-reads every record from there.

For a switched partition, the broker now answers an epoch 0 request with the
diskless end offset. The answer is at least the committed seal, because a
control-plane placeholder row reports end offset 0.

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

No active review issues were identified for the change correcting diskless placeholder-epoch offset responses.

No open issues 🎉

What changed in this PR

This PR prevents consumers from unnecessarily rereading records after a classic-to-diskless topic switch by correcting OffsetsForLeaderEpoch responses for the diskless placeholder epoch.

Changes:

  • Route epoch 0 requests for switched partitions to the diskless end offset while preserving local lookup errors.
  • Floor diskless end offsets at the committed seal to prevent placeholder values from triggering false truncation.
  • Add regression tests and update boundary tests to retain coverage of nonzero classic epochs.
File Description
core/​src/​test/​scala/​unit/​kafka/​server/​ReplicaManagerInklessTest.scala Tests epoch 0 validation and the seal floor; adjusts classic-boundary tests and parameterizes the setup helper’s epoch.
core/​src/​main/​scala/​kafka/​server/​ReplicaManager.scala Routes switched-partition epoch 0 lookups to diskless storage and applies the committed-seal floor.

Review effort: Balanced

if (localError != Errors.NONE) {
() => localResult
} else if (localResult.endOffset != OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET && localResult.endOffset <= classicToDisklessStartOffset) {
} else if (offsetForLeaderPartition.leaderEpoch != LeaderAndIsr.INITIAL_LEADER_EPOCH &&
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.

When electing a shorter replica while the switch is pending (or lowering the seal before control-plane init) could cause the following: suppose a consumer is at offset 95 with epoch 0, and the partition seals and initializes the diskless log at offset 80. Once diskless writes advance past 80 (or past 95), OffsetsForLeaderEpoch(0) returns the diskless end offset instead of 80, so the consumer skips new diskless records in [80, 95) without noticing.
The other case which has similar problems is lowering the seal after control-plane init. In this case the control plane keeps the old start offset, so no new records land in the lost range, though a consumer with auto.offset.reset=none no longer gets LogTruncationException.

@viktorsomogyi viktorsomogyi Nov 23, 2026 •
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.

For completeness here is the timeline:

  1. Before the switch (classic topic, epoch 0): The leader has offsets 0..99 in epoch 0. The consumer reads 0..94, so its position is (offset=95, offsetEpoch=0).
  2. Switch starts (-2), then data-loss recovery: The ISR loses its disk while the switch is pending. An operator elects a lagging out-of-ISR replica that only has offsets 0..79 (still in epoch 0). That replica seals at 80, and the control plane initializes the diskless log starting at 80. (Offsets 80..99 from the old classic log are gone.)
  3. New writes arrive: Producers write new diskless records at offsets 80, 81, ..., 99 (diskless end offset is now 100).
    The consumer wakes up / sees the new leader epoch: It still has position (offset=95, offsetEpoch=0). Because the leader epoch changed (or because it's resuming from a committed offset with epoch 0), Kafka's consumer validates its position by sending OffsetsForLeaderEpoch(leaderEpoch=0).
  4. What happens next:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, this is properly addressed by #888 now. Closing this one.

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

0