← Back to Pull requests

feat(inkless:switch): elect a chosen replica in phase 1 of the classic-to-diskless switch [KC-717] #882

Closed
unknown closed this pull request without merging on Nov 23, 2026 10:17 UTC
View on GitHub
unknown unknown commented on Nov 19, 2026 10:41 UTC
Author

In phase 1 of the classic-to-diskless switch, only the replicas' local disks store the classic prefix, and the controller blocks unclean leader election. If the only ISR member loses its disk, the partition goes offline and no command brings it back:

  • If the log directory of the replica is offline, the controller elects no leader, because a phase 1 leader needs an online log directory.

  • If the broker returns with a log that ends below a committed seal, it leads the partition and marks it offline.

  • If the broker returns without a log for the partition after the seal commits, the controller keeps it as the leader, but ReplicaManager.applyLocalLeadersDelta doesn't create the partition. No broker serves it.
  • rearm re-sets the current leader and lower-seal keeps it, so neither command moves leadership to a replica that still has the classic prefix. This PR adds a command that does.

    Add the elect subcommand

    kafka-topic-switch.sh elect --topic <topic> --partition <partition> --replica <broker-id> elects the chosen replica as the leader of a partition in phase 1, even if the replica is outside the ISR. The command accepts --bootstrap-server or --bootstrap-controller.

    The command sends AlterDisklessSwitch with a new LeaderId field and a seal offset of -1, which keeps the seal. The field joins version 0 of the request. The switch isn't used in production yet, so the request doesn't need a new version. AlterDisklessSwitchOptions.leaderId(int) sets the field, so the Admin interface keeps its methods.

    On the controller, ReplicationControlManager.electPhaseOneLeader runs PartitionChangeBuilder with an unclean election and a leader predicate that accepts only the chosen replica:

  • If the replica is outside the ISR, the controller makes it the leader, resets the ISR to it, and sets the leader recovery state to RECOVERING.

  • If the replica is in the ISR, the controller makes it the leader and keeps the ISR.

  • The election runs whether or not the partition has a leader, with a pending switch or a committed seal.
  • The controller rejects the request with INVALID_REQUEST in these cases:

  • The partition isn't in phase 1.

  • The broker isn't a replica of the partition, is fenced, or has an offline log directory for its replica.

  • The broker already leads the partition.

  • A reassignment is adding the broker, and the broker is outside the ISR. With an ISR of only that replica, the next partition change completes the staged reassignment and drops the removing replicas, which can store the only copy of the classic prefix. The election of any other replica stays possible during a reassignment.

  • The election would complete a reassignment. PartitionChangeBuilder completes a reassignment whose adding replicas are all in the ISR before it elects a leader, and it then drops the removing replicas.

  • The request has a seal offset other than -1, or a leader ID below -1.
  • The phase 1 guards on unclean leader election don't change. ElectLeaders with type UNCLEAN, the config check, and the automatic election paths still reject or skip the election.

    The broker code doesn't change. A leader whose log ends at a committed seal serves the partition. A leader whose log ends below it marks the partition offline, and lower-seal then applies as it does after a last-known-leader election. A leader without a log for the partition doesn't create it, and the operator elects another replica.

    CLASSIC_TO_DISKLESS_SWITCH.md has a new section, "Elect a chosen replica in phase 1", with the command, the rejections, the outcomes on the broker, and the limitations.

    Limitations

  • The controller doesn't know replica log end offsets, so it doesn't check the log of the chosen replica.

  • With a pending switch, the elected leader seals at its own log end offset. A replica with a shorter log costs the partition the difference at the election.

  • The partition keeps only the records of the elected replica. This PR doesn't rebuild the part of the classic prefix that only remote storage has, which KC-717 also asks for.

  • A topic with one replica per partition has no other replica to elect.

  • No test in this PR takes a partition with a lowered seal into phase 2. KC-837 covers that path.
  • Test plan

  • ./gradlew :clients:checkstyleMain :metadata:checkstyleMain :metadata:checkstyleTest :tools:checkstyleMain :tools:checkstyleTest :core:checkstyleTest :core:spotlessJavaCheck :metadata:spotlessJavaCheck passes, and make vale reports no alerts.

  • These test classes pass: ReplicationControlManagerInklessTest, TopicSwitchCommandTest, RequestResponseTest, MessageTest, and InklessTopicTypeSwitcherClusterTest.

  • Controller: ReplicationControlManagerInklessTest elects a replica outside the ISR with a committed seal, with and without a live leader, and with a pending switch. It checks the leader, the ISR, the RECOVERING state, the leader epoch bump, and that the seal stays. It also elects an ISR member and checks that the ISR stays.

  • Controller rejections: the same class covers phase 2, a broker that isn't a replica, the current leader, a fenced replica, a replica with an offline log directory, an adding replica outside the ISR, an election that would complete a reassignment, a seal offset sent with the leader ID, and a negative leader ID. The fenced-replica test checks that a rejected election keeps the current leader.

  • Reassignment: with a staged reassignment, the same class elects the removing replica and a replica that stays, each from outside the ISR, and checks that the replicas, the adding replicas, and the removing replicas don't change.

  • Tool: TopicSwitchCommandTest checks that elect sends the replica with a seal offset of -1, rejects a negative replica before it calls the controller, accepts --bootstrap-controller, and requires --partition and --replica.

  • End to end: InklessTopicTypeSwitcherClusterTest.testElectedReplicaOutsideTheIsrRecoversAPhaseOnePartition switches a topic with two replicas and a committed seal. It shuts down the follower, then the leader, and restarts the follower. The partition has no leader. After elect, the follower leads with an ISR of one, a consumer reads the whole classic prefix, the partition takes diskless records, and the old leader rejoins the ISR after its restart. The test then creates the bucket of the tiered storage plugin, and the partition enters phase 2.

  • The cluster test needs the partition to stay in phase 1 until the election. It carries the tag blocks-remote-uploads, for which the setup configures the tiered storage plugin with a bucket that doesn't exist. Segment uploads fail until the test creates the bucket, which it does after the recovery. The test asserts that classicPrefixInRemoteStorage is unset before the election. In a local run with a temporary 15-second wait before that assertion, the test fails with the default bucket and passes with the missing one.

  • The barrier adds no class file on purpose. Gradle assigns the test classes of :core:test to its two forks in turn, by class file. An earlier version of this test added a nested class, which moved both 256-second cluster test classes to the same fork. :core:test then took 17 minutes instead of 11 in CI, and the test step reached its 30-minute limit.

  • checkstyle/suppressions.xml exempts InklessTopicTypeSwitcherClusterTest from ClassFanOutComplexity. The new test takes the class over the limit of 55.

  • No cluster test elects a replica while the switch is pending, a replica whose log ends below the seal, or a replica without a log.
  • 🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: giuseppelillo Review duration: 5735m
    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 #882"
    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