Repository navigation
fix(inkless:switch): rebuild a follower below the seal when the leader's local log starts past it [KC-872] - #905
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the replica-recovery fix during classic-to-diskless switching.
No open issues 🎉
What changed in this PR
Fixes phase 2 replica recovery during a classic-to-diskless switch when local retention moves the leader’s local log start past the seal. The change lets a follower below the seal rebuild from consolidated remote storage without changing consumer epoch validation.
Changes:
- Select the consolidated segment’s epoch directly when the source’s local log starts past the seal.
- Add unit and cluster regression tests for replica recovery and consumer reads.
- Clarify how remote rebuilds choose segments on either side of the seal.
| File | Description |
|---|---|
docs/inkless/CLASSIC_TO_DISKLESS_SWITCH.md |
Clarifies segment lookup and rebuild behavior relative to the seal. |
core/src/test/scala/unit/kafka/server/ReplicaManagerInklessTest.scala |
Tests recovery from a source whose local log starts past the seal. |
core/src/test/java/kafka/server/InklessTopicTypeSwitcherClusterTest.java |
Tests replica reassignment after local retention and subsequent consumer reads; shortens retention checks. |
core/src/main/java/kafka/server/TierStateMachine.java |
Uses the source’s local-start epoch to locate consolidated remote segments past the seal. |
Review effort: Balanced
…r's local log starts past it [KC-872] In phase 2 of the classic-to-diskless switch, a follower whose log ends below the seal stays on the classic fetcher and rebuilds from remote storage. If local retention moved the leader's local log start past the seal, the rebuild failed on every retry. The replica's log stayed empty, the controller kept the replica in the ISR, and a consumer that the metadata routing sent to it read no records. `TierStateMachine` used the `lastClassicLeaderEpoch` lookup only if the leader's local log starts at or below the seal. Otherwise it sent `OffsetsForLeaderEpoch` for the epoch before `disklessLeaderEpoch` to the leader. The leader treats that epoch as a diskless epoch and answers with the requested epoch and the diskless end offset, which a consumer needs to validate its position. No remote segment holds that epoch, so the segment lookup threw `RemoteStorageException`. If the source's local log starts past the seal, the record before it is a consolidated record. Consolidation writes every record from the seal on in `disklessLeaderEpoch`, which is the epoch the source reports for its local log start. The tier-state machine now looks up the remote segment in that epoch and doesn't send the request. - `ReplicaManagerInklessTest`: `testTierStateMachineRebuildsFromSourceWhoseLocalLogStartsPastTheSeal`. - `InklessTopicTypeSwitcherClusterTest`: `testAddedReplicaRebuildsWhenLeaderLocalLogStartsPastSeal`. The class now runs with `log.retention.check.interval.ms=500`, so local retention runs within a test. - Both tests fail without the change to `TierStateMachine`. Assisted-by: Claude Fable 5.2
8abeafe to
57167c3
Compare
jeqo
left a comment
There was a problem hiding this comment.
LGTM. Though I found an edge-case on ReplicaManager related to this change worth having a look:
There is an edge case right after this rebuild finishes on an idle phase-2 partition with local.retention.ms configured. When a tiered topic stops receiving writes and local.retention.ms elapses, UnifiedLog.deletableSegments (UnifiedLog.java:1928) rolls the active consolidated segment, RemoteLogManager uploads it, and the next retention check deletes it. That leaves the leader with only an empty active segment at localLogStartOffset == logEndOffset > seal.
If a reassignment adds a replica (or a replica loses its local log) in that state, this PR lets it rebuild cleanly to leader.localLogStartOffset. On its very next classic fetch at that offset, however, the leader sees fetchOffset < readableFrontier is false and enters the at-seal validation block (ReplicaManager.scala:2933). Because that block clamps fetchAtSeal down to classicToDisklessStartOffset, LocalLog.read(seal) throws OffsetOutOfRangeException (the leader's oldest local segment starts at localLogStartOffset > seal). The follower gets OFFSET_OUT_OF_RANGE, resets to its own LEO, and loops once per second without evicting to ConsolidationFetcherThread until a new record is produced.
The cluster test didn't hit it because waitForCondition(() -> leaderLog.localLogStartOffset() > seal) fired as soon as the first closed segment expired, while the leader still held a partial local segment up to 2790, so the follower fetched a record and evicted.
I'd suggest clamping the validation offset at ReplicaManager.scala:2940 to math.max(classicToDisklessStartOffset, log.localLogStartOffset) (either here or as a quick follow-up). That keeps phase-1 and leader-below-seal behavior unchanged while letting an idle phase-2 leader return Errors.NONE so the rebuilt follower hands off to consolidation right away.
Current:
// ReplicaManager.scala:2936-2945
try {
// Use the classic follower-read validation without returning any records.
val fetchAtSeal = new PartitionData(
fetchPartitionData.topicId,
classicToDisklessStartOffset,
fetchPartitionData.logStartOffset,
0,
fetchPartitionData.currentLeaderEpoch,
fetchPartitionData.lastFetchedEpoch
)Proposed:
try {
// Use the classic follower-read validation without returning any records.
// In phase 2, local retention can move the leader's local log start past the seal,
// so validating at the seal would hit OffsetOutOfRangeException on an idle leader.
val validationOffset = partition.log
.map(log => math.max(classicToDisklessStartOffset, log.localLogStartOffset))
.getOrElse(classicToDisklessStartOffset)
val fetchAtSeal = new PartitionData(
fetchPartitionData.topicId,
validationOffset,
fetchPartitionData.logStartOffset,
0,
fetchPartitionData.currentLeaderEpoch,
fetchPartitionData.lastFetchedEpoch
)…log start when it's past the seal A follower that rebuilds its log from a leader whose local log starts past the seal fetches next at the leader's local log start. If the leader has local records from there on, it returns them, and the follower hands off to consolidation. An idle leader has none. Local retention rolls its last segment, the remote log manager uploads it, and the next retention check deletes it. The local log is then an empty active segment past the seal. The follower's fetch is at the leader's log end offset, so the leader validates it as a fetch at the seal. The read at the seal throws `OffsetOutOfRangeException`, because no local segment holds that offset. The follower gets `OFFSET_OUT_OF_RANGE`, keeps its offset, and retries once per second on the classic fetcher until the partition gets a new record. The leader now validates the fetch at its local log start if that offset is past the seal. In phase 1 and on a leader whose log ends below the seal, the local log starts at or below the seal, so the validation offset stays the seal. A reviewer found the case on the pull request. ## Testing - `ReplicaManagerInklessTest`: `testFollowerFetchPastSealSucceedsWhenLeaderLocalLogStartsPastSeal`. - `InklessTopicTypeSwitcherClusterTest`: `testAddedReplicaRebuildsWhenLeaderLocalLogStartsPastSeal` now runs with an idle leader too, and it checks that the added replica leaves the classic fetcher. - Without the change to `ReplicaManager`, the unit test fails with `OFFSET_OUT_OF_RANGE`, and the idle variant of the cluster test fails because the classic fetcher still holds the partition. Assisted-by: Claude Fable 5.2
|
Good catch, thanks. I reproduced it and took your suggestion in fc30e33. On a 3-broker cluster with the seal at 300 and an idle leader whose local log starts and ends at 2800, the added replica rebuilds to 2800 and then stays on the classic fetcher. Reads through it work, and it hands off within 5 seconds of the next record. With the validation offset at The retention path is as you describe: Tests:
The description has a new "Idle leader" section with the details. |
In phase 2 of the classic-to-diskless switch, a replica that starts below the seal never rebuilds its log once local retention moves the leader's local log start past the seal. The replica stays in the ISR with an empty log, and a consumer that the metadata routing sends to it reads no records. With this change the replica rebuilds from the consolidated segment in remote storage and hands off to consolidation, also on an idle leader.
Problem
A follower whose log ends below the seal stays on the classic fetcher in phase 2. The leader answers its fetch with
OFFSET_MOVED_TO_TIERED_STORAGE, and the follower runs the tier-state rebuild with the leader's local log start and its leader epoch,disklessLeaderEpoch.TierStateMachine.buildRemoteLogAuxStateuses thelastClassicLeaderEpochlookup only if the leader's local log starts at or below the seal. Otherwise it takes the upstream path and sendsOffsetsForLeaderEpochfor the epoch beforedisklessLeaderEpochto the leader. The record that starts phase 2 bumps the leader epoch, so that epoch is at or aboveinitialDisklessLeaderEpoch. Since #896,ReplicaManager.lastOffsetForLeaderEpochtreats such an epoch as a diskless epoch and answers with the requested epoch and the diskless end offset, which a consumer needs to validate its position. The tier-state machine then looks up a remote segment in that epoch. Classic segments end below the seal, and consolidated segments carrydisklessLeaderEpoch, so no segment matches. The lookup throwsRemoteStorageException, and the fetcher retries once per second.The leader's local log starts past the seal after local retention deletes the first consolidated segment. That is the state of a switched partition with
local.retention.msbelow its retention, one local retention period after phase 2 starts. A replica starts below the seal if a reassignment adds it, if the broker lost its local log, or if the broker recreated the replica.Example before this change
On a 3-broker cluster in three zones, on
mainwith #900, #901, #903, and #904 merged. A topic has one partition on brokers 0 and 1, with 300 classic records and 4000 records produced in phase 2. The seal is 300,disklessLeaderEpochis 2, andinitialDisklessLeaderEpochis 1. A reassignment then adds broker 2, and two consumers read through broker 2 for 20 seconds each:local.retention.ms=1000In the second run, broker 2 logs this once per second:
Fix
If the source's local log starts past the seal, the record before the local log start is a consolidated record. Consolidation writes every record from the seal on in
disklessLeaderEpoch, which is the epoch that the source reports for its local log start. The tier-state machine looks up the remote segment that holds the record in that epoch, and it doesn't sendOffsetsForLeaderEpoch.ReplicaManager.lastOffsetForLeaderEpochdoesn't change, so a consumer past the seal still validates its position as #896 intends. The consolidation fetcher takes the same branch: its endpoint already answered that request with the seal, which led to the same epoch.Idle leader
A review of this pull request found a second case. After the rebuild, the follower fetches at the leader's local log start. A leader with local records from there on returns them, and the follower hands off to consolidation. An idle leader has none: local retention rolls its last segment, the remote log manager uploads it, and the next retention check deletes it. Its local log is then an empty active segment past the seal.
The follower's fetch is then at the leader's log end offset, and
ReplicaManager.fetchMessagesvalidates it as a fetch at the seal. The read at the seal throwsOffsetOutOfRangeException, because no local segment holds that offset. The follower getsOFFSET_OUT_OF_RANGE, keeps its offset, and retries once per second on the classic fetcher until the partition gets a new record. Each retry sends twoListOffsetsrequests from the fetcher thread. Reads through the replica work in that state.The leader now validates the fetch at its local log start if that offset is past the seal. In phase 1 and on a leader whose log ends below the seal, the local log starts at or below the seal, so the validation offset stays the seal.
On the same cluster as in the example, with a leader whose local log starts and ends at 2800:
CLASSIC_TO_DISKLESS_SWITCH.mdsaid that the tier-state machine uses thelastClassicLeaderEpochlookup whatever the local log start of the source is. It now states the condition and describes the lookup and the idle leader case for a source whose local log starts past the seal.Test plan
ReplicaManagerInklessTesthas two new tests. The class passes with 322 tests, andReplicaManagerTestpasses with 129.testTierStateMachineRebuildsFromSourceWhoseLocalLogStartsPastTheSeal. Without the change toTierStateMachineit fails withCouldn't build the state from remote store ... epoch: 4 as the previous remote log segment metadata was not found.testFollowerFetchPastSealSucceedsWhenLeaderLocalLogStartsPastSeal. Without the change toReplicaManagerit fails withexpected: <NONE> but was: <OFFSET_OUT_OF_RANGE>.InklessTopicTypeSwitcherClusterTesthas a new test,testAddedReplicaRebuildsWhenLeaderLocalLogStartsPastSeal, with two variants: a leader that still has local records past its local log start, and an idle leader. It moves the leader's local log start past the seal with local retention and adds a replica. It then waits until the added replica leaves the classic fetcher, and it reads every record through the zone of that replica. The class passes with 13 tests.TierStateMachine, both variants fail withits log end offset is 0, and the classic fetcher still holds the partition.ReplicaManager, the idle variant fails withits log end offset is 2800, and the classic fetcher still holds the partition, and the other variant passes.log.retention.check.interval.ms=500, so local retention runs within a test.InklessConsolidatedDisklessReassignmentTest,TierStateMachineTest,AbstractFetcherThreadTest, and the tests inio.aiven.inkless.consolidationpass.:core:checkstyleMain,:core:checkstyleTest,:core:spotlessCheck, and:core:spotbugsMainpass.make valereports no alerts on the added lines.🤖 Generated with Claude Code
Assisted-by: Claude Fable 5.2