← Back to Pull requests

docs(inkless:switch): describe the last-known-leader election in phase 1 [KC-759] #871

Closed
unknown closed this pull request without merging on Nov 13, 2026 16:01 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 14:19 UTC
Author

With eligible leader replicas (ELR) enabled, a partition in phase 1 of the classic-to-diskless switch can still elect a leader from outside its ISR. CLASSIC_TO_DISKLESS_SWITCH.md says that a partition that loses every ISR member has no leader until one of them returns. The last-known-leader election of KIP-966 contradicts that, and the docs don't describe it.

Problem

The phase 1 guards in ReplicationControlManager block unclean leader election by not calling setElection(Election.UNCLEAN). PartitionChangeBuilder.electAnyLeader and electPreferredLeader call canElectLastKnownLeader() before they check the election type. So the last-known-leader election runs for every election type, and unclean.leader.election.enable doesn't control it.

The following example uses replicas [2, 0, 1], min.insync.replicas=2, and a seal at 20:

| Step | Event | ISR | ELR | lastKnownElr | Leader |
| --- | --- | --- | --- | --- | --- |
| 1 | Phase 1 starts | [2, 0, 1] | [] | [] | 2 |
| 2 | Brokers 0 and 1 stop | [2] | [0] | [] | 2 |
| 3 | Broker 2 stops after its log loses offsets 15 to 19 | [] | [0, 2] | [2] | none |
| 4 | Brokers 0 and 1 return after an unclean shutdown | [] | [2] | [2] | none |
| 5 | Broker 2 returns after an unclean shutdown | [2] | [] | [] | 2, RECOVERING |

The partition loses offsets 15 to 19, even though the high watermark covered them. KIP-966 accepts the same loss for a classic topic. In phase 1, the loss shows up differently:

  • The leader's log ends below the seal, so the broker marks the partition offline. The controller isn't told, and keeps broker 2 as the leader with an ISR of one.

  • No other replica can lead. None is in the ISR or the ELR, and phase 1 blocks unclean leader election.

  • The remote log manager can't upload the prefix, so phase 2 doesn't start.
  • Change

    The switch doc gains a "Last-known-leader election" subsection. It describes when the controller runs the election, what happens to a partition in phase 1, and how to recover one that's offline:

    1. Lower the seal to the leader's log end offset with kafka-topic-switch.sh seal.
    2. Restart the leader. The broker doesn't apply metadata changes to a partition that it marked offline.
    3. Run kafka-topic-switch.sh repair.

    The recovery loses the records between the leader's log end offset and the old seal. The "Unclean leader election" section and the phase table now point to the new subsection.

    This change doesn't modify controller behavior. Blocking the election in phase 1 would leave the partition without a leader in both outcomes: the one where the last known leader holds the whole prefix, and the one where it doesn't.

    Testing

    ClassicToDisklessSwitchLastKnownLeaderClusterTest runs the sequence in the example on a 3-broker cluster with ELR enabled. A gated remote storage manager fails segment copies until the test releases it. Without the gate, the leader rolls its active segment at the seal and the partition leaves phase 1 within one task interval. The test deletes the clean-shutdown markers, so the controller sees unclean shutdowns.

    The test is parametrized on loseTail, so it covers both outcomes in the doc.

    With loseTail=true, the test truncates the leader's log to 15 before the leader stops. It then checks the following:

  • The controller elects the last known leader with an ISR of one and the RECOVERING state, and the leader marks the partition offline.

  • 15 falls in the offset range that kafka-topic-switch.sh seal validates against. After the seal is lowered, the partition stays offline until the leader restarts.

  • After the restart and the repair, the ISR recovers and every replica's log ends at 15 while the partition is still in phase 1. After the gate opens, phase 2 starts.

  • A consumer with auto.offset.reset=none reads offsets 0 to 14 and the diskless records at 20 to 24.
  • With loseTail=false, the test checks the following:

  • The leader keeps the partition online and finishes recovery.

  • The ISR recovers and every replica's log ends at 20 in phase 1. After the gate opens, phase 2 starts.

  • A consumer reads offsets 0 to 24.
  • The test doesn't run the recovery without the repair step, or the election while the switch is pending. The doc describes the pending case from the code.

    KC-759

    🤖 Generated with Claude Code

    Assisted-by: Claude Opus 5.5

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

    Diff: 576 lines (+432 / -144) across 7 files Reviewer: giuseppelillo Review duration: 1541m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 576 lines diff across 7 files
    0.50
    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.380
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #871"
    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: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    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: 19.7% · Safe to merge
    19.7%
    Repository