← Back to Pull requests

fix(inkless:switch): recreate phase 2 replicas that don't hold the committed classic prefix [KC-702] #857

Closed
unknown closed this pull request without merging on Nov 11, 2026 13:26 UTC
View on GitHub
unknown unknown commented on Nov 05, 2026 12:02 UTC
Author

In phase 2 of the classic-to-diskless switch, the controller adds every live replica to the ISR without checking its local copy of the classic prefix, and any replica can lead. A replica can hold records below the seal that the partition doesn't hold. It led an older leader epoch and wrote records that the next leader didn't have, or an unclean leader election before the switch discarded records below its high watermark. Forced deletion keeps the leader epoch cache of that replica, and the remote log manager uses it to compute the highest offset in remote storage and to pick remote segments. On that replica, a tiered read below the seal fails with OFFSET_OUT_OF_RANGE, and the consumer resets its offset. On an idle topic, the highest offset in remote storage also stays below the seal, so forced deletion never completes.

Example

Broker B leads epoch 1 and appends [80, 100), but no follower fetches those records before B goes offline. The controller elects A in epoch 2, and A appends its own [80, 100). A seals the partition at 100 and uploads the classic prefix while B stays offline.

offset                    0           80          100 = seal
                          |           |           |
A, C, and remote storage  [ epoch 0   | epoch 2   ]  diskless records from 100 on
B (offline)               [ epoch 0   | epoch 1   ]  B never replicated [80, 100)

A requests the phase 2 flag with lastClassicLeaderEpoch = 2, the epoch of its record at 99. In phase 2, B returns, joins the ISR, and can lead. Its log ends at the seal, so it looks complete. Without this change, B serves its epoch 1 records at [80, 100), which no other replica holds. The cache of B also maps that range to epoch 1, and no remote segment has that epoch.

With this change, B checks its log in applyDelta before it leads. Its epoch at 99 is 1, so the broker deletes the log and lets the leadership change create an empty one, which the existing recovery rebuilds from remote storage:

sequenceDiagram
    participant K as Controller
    participant B as ReplicaManager of B
    participant RLM as Remote log manager of B
    participant D as DisklessLeaderEndPoint
    participant R as Remote storage
    K-->>B: Delta: B leads epoch 5, lastClassicLeaderEpoch = 2
    B->>B: Stop the replica, move, and consolidation fetchers
    B->>B: Epoch at 99 is 1, so the log fails the check
    B->>RLM: Cancel the partition tasks, keep the metadata subscription
    B->>B: Delete the current and future logs
    B->>B: makeLeader creates an empty log in epoch 5
    B->>RLM: onLeadershipChange starts new tasks on the new log
    B->>D: Consolidation fetch at 0
    D-->>B: OFFSET_MOVED_TO_TIERED_STORAGE, earliest local offset 100
    B->>R: Segment at 99 in epoch 2, the recorded epoch
    R-->>B: Segment with epochs 0 and 2
    B->>B: Restore the epochs and producer state, start the log at 100
    B->>D: Consolidation fetch at 100

Recreate instead of truncate

The broker never truncates a log that it keeps in place. A truncation leaves state that later steps trust, and each piece needs its own guard. A recreated log starts without any of it. In this example, broker B led epoch 3, uploaded [50, 119], and still holds [100, 120) when an unclean leader election makes A the leader in epoch 4. A seals at 100 and records lastClassicLeaderEpoch = 2.

offset                     50             100 = seal           120
                           |              |                    |
log of B                   [ epoch 2      | epoch 3, discarded ]
remote progress of B                                         ^ 119
future log of B (moving)   [ epoch 2      | epoch 3, discarded ]
diskless records                          [ from 100 on ...

A truncation of B to the seal leaves this state behind:

| State | After a truncation in place | After a recreate |
| --- | --- | --- |
| Highest offset in remote storage | Stays 119, because it only grows. The WAL pruner then deletes diskless batches [100, 120) before consolidation uploads them. | -1 until the remote log manager computes it from the rebuilt log. |
| Future log of a log directory move | Keeps [100, 120) in epoch 3 and replaces the current log once both end at 120. | Deleted with the current log. |
| Remote log tasks | Can compute 119 in epoch 3 before the truncation and apply it after consolidation grows the log back past it. The copy task keeps a copied offset that skips [100, 120). | Canceled. A task that still runs updates only the deleted log. |
| Promotion of the future log | Can run between the check and the truncation. | Stopping the move fetcher waits for a running promotion. |

The epoch of B at 99 is 2, but the check still rejects B because it holds classic records past the seal. If B applied the seal while it was online, it already truncated its log to 100. The check then rejects B because its remote progress 119 isn't below its log end offset 100. A remote log task that computed 119 before that truncation can't install it after the check, because the log drops an offset that a task computed before a truncation.

Remote segment that crosses the seal

Leader X appends [100, 120) in epoch 3 and uploads a segment [50, 119] with epochs 2 and 3. An unclean leader election then makes Y the leader in epoch 4. Y's log ends at 100. The partition loses [100, 120). Y seals at 100 before any new produce and records lastClassicLeaderEpoch = 2.

offset           50             100 = seal           120
                 |              |                    |
remote segment   [ epoch 2      | epoch 3, discarded ]
diskless log                    [ diskless records from 100 on ...
upstream rebuild                                     ^ local log starts at 120
phase 2 rebuild                 ^ local log starts at 100

The segment at 99 in epoch 2 is the right one to rebuild from, but it ends at 119. Without this change, TierStateMachine starts the local log at 120. The consolidating leader then never materializes the diskless records [100, 120), and the WAL prune deletes them. With this change, a phase 2 rebuild starts the local log at the seal with only the epoch 2 entry, and the leader consolidates the diskless records from 100 on.

Source with local classic records below the seal

In phase 2, a classic leader can still hold local classic segments below the seal, and so can the current log of a log directory move. Upstream TierStateMachine rebuilds from the segment that contains the record before the local log start of the source. Broker X leads epoch 1 and uploads a segment [0, 119] with its records [80, 120). An unclean leader election then makes A the leader in epoch 2. A appends its own records from 80 and seals at 100. It records lastClassicLeaderEpoch = 2. The local log of A starts at 50.

offset             0        50       80              100 = seal      120
                   |        |        |               |               |
committed prefix   [ epoch 0         | epoch 2       ]  diskless records from 100 on
local log of A              [ epoch 0| epoch 2       ]
segment of X       [ epoch 0         | epoch 1, discarded            ]
segment of A                [ epoch 0| epoch 2       ]

A follower that rebuilds from A looks up the record at 49 in epoch 0. The segment of X holds that record in the same epoch, so remote storage can return that segment:

  • Without this change, the follower restores epoch 1 for [80, 100) and starts its local log at 120, past the diskless records [100, 120).

  • With this change, the follower looks up the record at 99 in epoch 2, which only the segment of A holds. It restores epochs 0 and 2 and starts its local log at 100. It then hands off to consolidation.
  • Changes

  • Recorded epoch: the leader that requests the phase 2 flag sends the leader epoch of its record at seal - 1 in SetClassicPrefixInRemoteStorage. The controller stores it with the flag as tag 104 (lastClassicLeaderEpoch) and drops it when the switch is undone. The value is -1 if the leader's log doesn't contain a record below the seal. The controller rejects a value below -1 or higher than the current leader epoch, which no replica can match. DescribeTopicPartitions returns the epoch with the other switch fields, and kafka-topic-switch.sh state prints it.

  • Replica check: applyDelta checks each consolidating phase 2 replica that is new to the broker or gets a higher leader epoch, before the leader and follower transitions. The flag record bumps the leader epoch, which makes live replicas run the check when phase 2 starts. The broker first stops the replica, move, and consolidation fetchers of the partition. It keeps the log only if all of these are true:

  • - The epoch at min(seal, LEO) - 1 is the recorded epoch, or the log has no record below that offset. Each epoch has one leader, so a matching epoch proves that the records below the seal are the committed ones, at any high watermark.
    - Records past the seal are consolidated diskless records (hasConsolidatedDisklessSuffixFromSeal).
    - highestOffsetInRemoteStorage is below the log end offset.
    - The future log, if any, ends at or below the current log end offset, in the same epoch as the current log at that offset. A future log that ends at or below the log start offset of the current log also passes: the current log holds none of its records, and the move fetcher drops them on its next fetch.
  • Recreate: otherwise, the broker stops the replica with stopPartitions and deleteLocalLog = true, as for a deleted partition. That removes the partition, deletes the current and future logs, and cancels the remote log tasks. A new keepRemoteLogMetadata flag keeps the remote log metadata subscription and the topic ID of the partition, because the partition stays on the broker. The leader or follower transition in the same delta then creates an empty log, and the existing recovery rebuilds the log, the leader epoch cache, and the producer state from remote storage. UnifiedLog and the remote log manager need no change for this path.

  • Failed recreate: a storage error can stop the deletion before it reaches the future log, which would then restart the move on the new log. If the broker can't delete both logs or cancel the remote log tasks, it marks the replica offline with its topic ID, as after a storage error in a transition, so later deltas skip it. stopPartitions cancels the remote log tasks even if a deletion fails, because a copy task that keeps running also keeps the copy progress of the deleted log.

  • Remote progress fence: a kept log can still have truncated before the check, to the seal in phase 1 or to the new leader after an unclean leader election. The remote log tasks compute the highest offset in remote storage without the log lock. A follower task that read the discarded tail, or a copy task that uploaded it, could install its offset after the check accepted the log. UnifiedLog now counts truncations. A truncation counts itself and changes the log under the log lock. The follower and copy tasks read the count under that lock before they read the leader epoch cache or the segments. A task never pairs the new count with the state from before the truncation. updateHighestOffsetInRemoteStorage(offset, truncationCount) installs the offset under the log lock only if the count didn't change. After a truncation, the copy task also finds its copied offset again instead of resuming after the discarded tail.

  • Rebuild: in phase 2, TierStateMachine looks up the remote segment that holds seal - 1 in the recorded epoch, whatever the local log start of the source is, and starts the local log at the seal. Each epoch has one leader, so the records of that segment below the seal are the committed prefix, even if the segment also covers discarded records past the seal. The rebuild keeps the epochs that start below the seal and skips the epoch lookup on the source. It restores the producer snapshot only if the segment ends at seal - 1. Sealing aborts undecided transactions, so the empty producer state is correct at the seal. Partitions without a recorded epoch, including partitions in phase 1, keep the upstream rebuild. TierStateMachine.java is a new INTERLEAVED entry in INKLESS_OWNERSHIP.

  • First prune: for a switched topic, the endpoint uses the seal as the lowest WAL start. fetchEarliestLocalOffset applies the same bound. The control plane keeps the WAL start at the classic log start until the first prune moves it to the seal. Without the bound, a leader that lost its local log before that prune reads the first diskless batch at offset 0, and its local log hides the remote prefix.

  • Deleted prefix: if retention or DeleteRecords moved the cross-tier earliest offset to the diskless start or past it, the endpoint returns OFFSET_OUT_OF_RANGE before it asks remote storage. The reset then starts the local log at that offset. DeleteRecords at the seal runs only the local leg, so before the first prune the WAL start stays below the seal. Without this check, the fetcher waits for coverage of seal - 1 that never returns, or rebuilds a deleted prefix from a segment that remote storage still lists.

  • Retention: while a recreated leader rebuilds, its leader epoch cache lacks the older epochs. The remote log manager runs its expiration task as soon as the broker becomes the leader. That task deletes every remote segment whose epochs are all older than the earliest cached epoch, without an offset check. For a consolidating partition, that cleanup now deletes only the segments that end below the cross-tier log start. Without this guard, the task deletes the classic prefix that the rebuild reads. A leader that lost its local log had the same exposure.

  • Docs: CLASSIC_TO_DISKLESS_SWITCH.md describes the recorded epoch, the replica check, the recreate, and the phase 2 rebuild. DISKLESS_CONSOLIDATION.md describes the first-prune bound, the deleted-prefix reset, the retention guard, and why a replica with stale remote progress is recreated.
  • A leader uploads only segments below its last stable offset, so the partition had committed every record in remote storage when its leader uploaded it. Phase 1 blocks unclean leader election. An unclean leader election before the switch, through the cluster default or an ElectLeaders request, can still discard uploaded records, and the phase 2 rebuild handles that case.

    inkless-release-0.50 has the SetClassicPrefixInRemoteStorage RPC and tag 103, but its brokers don't send the flag request. So no released partition is in phase 2 without the recorded epoch.

    Limitations

  • Only consolidating partitions run the check. A switched leader below the seal of a partition that doesn't consolidate goes offline, because nothing rebuilds its prefix from remote storage.

  • A replica whose log ends below the seal compares the epoch of its last record, so the broker can recreate it even when its records match. That costs only the rebuild.

  • The leader relies on the TierStateMachine rebuild, like a leader that lost its local log. A gap in that rebuild affects both cases.

  • For a consolidating partition, a remote segment from an epoch older than the leader's cache that doesn't end below the cross-tier log start stays until retention moves that start past it. That includes a divergent segment from before the switch. Upstream treats a divergent segment inside the cached epochs the same way.

  • After an unclean leader election, a remote segment that crosses the seal still serves tiered reads below the seal. Consumers that read through it receive the discarded records past the seal in place of the diskless records at the same offsets. Upstream tiered storage has the same gap after an unclean leader election, and closing it needs a cap on remote reads in RemoteLogManager.
  • Assisted-by: Claude Opus 5.5

    [KC-727]: https://aiven.atlassian.net/browse/KC-727?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jeqo Review duration: 8724m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #857"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository