hechengbo created KAFKA-21748:
---------------------------------
Summary: Deleting a topic with an empty ISR leaves stale directory
mappings and can trigger controller failover
Key: KAFKA-21748
URL: https://issues.apache.org/jira/browse/KAFKA-21748
Project: Kafka
Issue Type: Bug
Components: controller
Reporter: hechengbo
Assignee: hechengbo
h1. Description
When a topic is deleted while one of its partitions has an empty ISR,
{{ReplicationControlManager.replay(RemoveTopicRecord)}} does not remove that
partition from {{{}directoriesToPartitions{}}}.
If a broker subsequently reports one of the partition's directories offline,
the controller looks up the stale partition and throws a {{RuntimeException}}
because the topic has already been removed. In the active controller's
event-handling path, this exception is classified as a fault that causes
controller failover.
h2. Preconditions and reproduction
# Enable Eligible Leader Replicas (ELR) and use a metadata version supporting
replica directory assignments.
# Register and unfence brokers 0, 1, and 2, each with two online directories.
# Create topic {{foo}} with one partition, replicas {{{}[0, 1, 2]{}}}, and
{{{}min.insync.replicas=3{}}}.
# Assign each replica to a concrete, non-reserved directory on its broker.
Keep the second directory online.
# Fence brokers 1 and 2, then process a clean shutdown for broker 0. Verify
that the partition's ISR is now empty while its directory assignments remain
present.
# Delete the topic and replay its {{{}RemoveTopicRecord{}}}.
# Process an offline-directory report for the directory previously assigned to
broker 0's replica.
A regression test added locally reproduces the failure using the existing
{{ReplicationControlTestContext}} helpers:
```java
ctx.fenceBrokers(1, 2);
ctx.handleBrokersShutdown(true, 0);
assertEquals(0, ctx.replicationControl.getPartition(topicId, 0).isr.length);
ctx.deleteTopic(anonymousContextFor(ApiKeys.DELETE_TOPICS), topicId);
assertNull(ctx.replicationControl.getPartition(topicId, 0));
List<ApiMessageAndVersion> records = new ArrayList<>();
ctx.replicationControl.handleDirectoriesOffline(
0, ctx.currentBrokerEpoch(0), List.of(replicaDirectory), records);
```
h2. Expected behavior
Deleting a topic removes all of its partitions from every directory mapping,
regardless of ISR membership. A subsequent directory-offline report should
produce the appropriate {{{}BrokerRegistrationChangeRecord{}}}, with no
partition change records for the deleted topic.
h2. Actual behavior
The directory mapping still contains a {{TopicIdPartition}} for the deleted
topic. Processing the offline-directory report fails with:
```text
java.lang.RuntimeException: Topic ID <topicId> existed in isrMembers, but not
in the topics map.
at
org.apache.kafka.controller.ReplicationControlManager.generateLeaderAndIsrUpdates(ReplicationControlManager.java:2161)
at
org.apache.kafka.controller.ReplicationControlManager.handleDirectoriesOffline(ReplicationControlManager.java:1594)
```
The message refers to {{{}isrMembers{}}}, but in this case the stale entry
comes from {{{}directoriesToPartitions{}}}.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)