Skip to content

fix(inkless:switch): rebuild a follower below the seal when the leader's local log starts past it [KC-872] - #905

Merged
jeqo merged 2 commits into
mainfrom
giuseppelillo/phase2-follower-rebuild-past-seal
Nov 30, 2026
Merged

jeqo merged 2 commits into
mainfrom
giuseppelillo/phase2-follower-rebuild-past-seal

Conversation

@giuseppelillo

@giuseppelillo giuseppelillo commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.buildRemoteLogAuxState uses the lastClassicLeaderEpoch lookup only if the leader's local log starts at or below the seal. Otherwise it takes the upstream path and sends OffsetsForLeaderEpoch for the epoch before disklessLeaderEpoch to the leader. The record that starts phase 2 bumps the leader epoch, so that epoch is at or above initialDisklessLeaderEpoch. Since #896, ReplicaManager.lastOffsetForLeaderEpoch treats 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 carry disklessLeaderEpoch, so no segment matches. The lookup throws RemoteStorageException, 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.ms below 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 main with #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, disklessLeaderEpoch is 2, and initialDisklessLeaderEpoch is 1. A reassignment then adds broker 2, and two consumers read through broker 2 for 20 seconds each:

Leader's local log start Broker 2, 60 seconds after the reassignment Consumer from offset 0 Consumer from offset 310
300, the seal Reached the seal after 1.5 seconds 4300 records 3990 records
3690, after local.retention.ms=1000 Log end offset 0 0 records 0 records

In the second run, broker 2 logs this once per second:

Couldn't build the state from remote store for partition: probe-newreplica-5d161f77-0, currentLeaderEpoch: 2, leaderLocalLogStartOffset: 2370, leaderLogStartOffset: 0, epoch: 1 as the previous remote log segment metadata was not found

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 send OffsetsForLeaderEpoch.

ReplicaManager.lastOffsetForLeaderEpoch doesn'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.fetchMessages 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. Each retry sends two ListOffsets requests 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:

Before the second change With it
Added replica on the classic fetcher 5, 10, 15, and 20 seconds after the reassignment Yes No
Consumer through the added replica 2800 of 2800 records 2800 of 2800 records
After one new record Hands off within 5 seconds Already handed off

CLASSIC_TO_DISKLESS_SWITCH.md said that the tier-state machine uses the lastClassicLeaderEpoch lookup 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

  • ReplicaManagerInklessTest has two new tests. The class passes with 322 tests, and ReplicaManagerTest passes with 129.
    • testTierStateMachineRebuildsFromSourceWhoseLocalLogStartsPastTheSeal. Without the change to TierStateMachine it fails with Couldn't build the state from remote store ... epoch: 4 as the previous remote log segment metadata was not found.
    • testFollowerFetchPastSealSucceedsWhenLeaderLocalLogStartsPastSeal. Without the change to ReplicaManager it fails with expected: <NONE> but was: <OFFSET_OUT_OF_RANGE>.
  • InklessTopicTypeSwitcherClusterTest has 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.
    • Without the change to TierStateMachine, both variants fail with its log end offset is 0, and the classic fetcher still holds the partition.
    • Without the change to ReplicaManager, the idle variant fails with its log end offset is 2800, and the classic fetcher still holds the partition, and the other variant passes.
  • The cluster test class now starts its clusters with log.retention.check.interval.ms=500, so local retention runs within a test.
  • InklessConsolidatedDisklessReassignmentTest, TierStateMachineTest, AbstractFetcherThreadTest, and the tests in io.aiven.inkless.consolidation pass.
  • :core:checkstyleMain, :core:checkstyleTest, :core:spotlessCheck, and :core:spotbugsMain pass. make vale reports no alerts on the added lines.

🤖 Generated with Claude Code

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 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

@giuseppelillo
giuseppelillo requested a review from jeqo November 26, 2026 09:46
…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
@giuseppelillo
giuseppelillo force-pushed the giuseppelillo/phase2-follower-rebuild-past-seal branch from 8abeafe to 57167c3 Compare November 27, 2026 10:49

@jeqo jeqo 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.

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
@giuseppelillo

Copy link
Copy Markdown
Contributor Author

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 max(seal, localLogStartOffset), it leaves the classic fetcher within 5 seconds of the reassignment.

The retention path is as you describe: UnifiedLog.deletableSegments rolls the last segment once it breaches local retention, the remote log manager uploads it, and the next retention check deletes it.

Tests:

  • testFollowerFetchPastSealSucceedsWhenLeaderLocalLogStartsPastSeal in ReplicaManagerInklessTest. Without the ReplicaManager change it fails with OFFSET_OUT_OF_RANGE.
  • testAddedReplicaRebuildsWhenLeaderLocalLogStartsPastSeal now also runs with an idle leader, and it waits until the added replica leaves the classic fetcher. That's the check the first version lacked, as you pointed out. Without the ReplicaManager change the idle variant fails, with the partition still on the classic fetcher at log end offset 2800.

The description has a new "Idle leader" section with the details.

@jeqo
jeqo merged commit 3c784a5 into main Nov 30, 2026
6 checks passed
@jeqo
jeqo deleted the giuseppelillo/phase2-follower-rebuild-past-seal branch November 30, 2026 08:48
gqmelo added a commit that referenced this pull request Nov 30, 2026
#899 and #905 merged into main after the sync branched. Their tests
call incrementalAlterConfig(s) and legacyAlterConfigs without the
`forwarded` argument that upstream 4.4 adds.

Assisted-by: Claude Opus 5.5
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