Repository navigation
fix(inkless:switch): answer epoch 0 from the diskless log in OffsetsForLeaderEpoch [KC-852] - #888
giuseppelillo wants to merge 1 commit into
Conversation
…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
There was a problem hiding this comment.
🟢 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 && |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
For completeness here is the timeline:
- 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).
- 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.)
- 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). - What happens next:
- Before PR fix(inkless:switch): answer epoch 0 from the diskless log in OffsetsForLeaderEpoch [KC-852] #888: The broker asks the classic log for epoch 0, which ends at 80. Seeing 80 < 95, the consumer detects truncation, rewinds to 80, and reads the new records 80..94.
- With PR fix(inkless:switch): answer epoch 0 from the diskless log in OffsetsForLeaderEpoch [KC-852] #888: Because the requested epoch is 0, the broker skips the classic log and asks the control plane, which returns end offset 100. Seeing 100 >= 95, the consumer thinks its position 95 is still valid, fetches from 95 onward, and silently skips the new records 80..94.
There was a problem hiding this comment.
Right, this is properly addressed by #888 now. Closing this one.
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), andFetchHandlerreturns the batches with that epoch. A consumer that tails a switched partition therefore holds a position past the seal withoffsetEpoch=0.The consumer validates a position with
OffsetsForLeaderEpochin 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.lastOffsetForLeaderEpochasks the local classic log first. If the classic end offset is at or belowclassicToDisklessStartOffset, 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
mainThe setup matches
testLastOffsetForLeaderEpochKeepsPlaceholderEpochPositionPastSeal:classicToDisklessStartOffset)[(epoch 0, start 0), (epoch 5, start 10)]offsetEpoch=0The validation runs in this order:
OffsetsForLeaderEpochwithleaderEpoch=0.LeaderEpochFileCache.endOffsetFor(0)returns the start offset of the next cached epoch, which is 10.endOffset=10.Truncation detectedand seeks to offset 10.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.atLeastCommittedSealapplies 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
OffsetsForLeaderEpochwithleaderEpoch=0.endOffset=200.Trade-offs
FetchHandlerreturns still carry epoch 0.Test plan
testLastOffsetForLeaderEpochKeepsPlaceholderEpochPositionPastSealbuilds the example and expects end offset 200 for epoch 0. Onmainit fails withexpected: <200> but was: <10>.testLastOffsetForLeaderEpochHybridFloorsDisklessEndOffsetAtSealhas the control plane report end offset 0 for a partition sealed at 101 and expects end offset 101.testLastOffsetForLeaderEpochHybridAtSwitchBoundaryUsesLocalLogandtestLastOffsetForLeaderEpochHybridAtSwitchBoundaryUsesFollowerLocalLogasserted 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) andInklessDisklessListOffsetsTest(5 tests) pass.InklessTopicTypeSwitcherClusterTestsetup, a consumer tails a switched partition sealed at offset 20 and commits offset 50 withleaderEpoch=0. A second consumer in the same group starts at offset 20 onmainand 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, commitsleaderEpoch=2, and resumes at its committed offset onmain.make valereports no inkless-owned prose to lint in this change.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 onmainlocally.🤖 Generated with Claude Code