DongWook LEE created KAFKA-21617:
------------------------------------

             Summary: ConfigurationsDelta.replay(RemoveTopicRecord) leaves 
pending topic configs in delta
                 Key: KAFKA-21617
                 URL: https://issues.apache.org/jira/browse/KAFKA-21617
             Project: Kafka
          Issue Type: Bug
          Components: config, kraft
    Affects Versions: 4.4.0, 3.8.0
            Reporter: DongWook LEE
            Assignee: DongWook LEE


If {{ConfigRecord}}s and {{RemoveTopicRecord}} for a topic are replayed within 
the same {{MetadataDelta}}, and that topic has no entry in the base 
{{ConfigurationsImage}} (either newly created in this delta or existing without 
dynamic configs), {{ConfigurationsDelta.replay(RemoveTopicRecord, String)}} 
skips cleanup and leaves the pending {{ConfigurationDelta}} in {{changes}}:

{code:java}
public void replay(RemoveTopicRecord record, String topicName) {
    ConfigResource resource =
        new ConfigResource(Type.TOPIC, topicName);
    if (image.resourceData().containsKey(resource)) {
        ConfigurationImage configImage = image.resourceData().get(resource);
        ConfigurationDelta delta = changes.computeIfAbsent(resource,
            __ -> new ConfigurationDelta(configImage, supportedConfigChecker));
        delta.deleteAll();
    }
}
{code}

When {{ConfigurationsDelta.apply()}} runs, those configs are materialized into 
the new {{ConfigurationsImage}} even though the topic was deleted.

This is a regression from KAFKA-16206 (3.8.0), which guarded {{deleteAll()}} 
with {{image.resourceData().containsKey(resource)}} to avoid redundant ZK 
config deletions. Before that change, {{deleteAll()}} ran unconditionally and 
cleared pending changes as well.

In practice, multiple batches are replayed into a single {{MetadataDelta}} 
because {{MetadataLoader.handleCommit}} feeds every batch from a 
{{BatchReader}} into {{MetadataBatchLoader}} before flushing once at the end 
(for instance, when a follower or observer catches up via {{KafkaRaftClient}} 
reading multiple batches from the log).

Minimal reproduction:
{code:java}
MetadataDelta delta = new MetadataDelta.Builder()
    .setImage(MetadataImage.EMPTY)
    .setSupportedConfigChecker(SupportedConfigChecker.TRUE)
    .build();
delta.replay(new TopicRecord().setName("foo").setTopicId(id));
delta.replay(new ConfigRecord().setResourceType(ConfigResource.Type.TOPIC.id())
    .setResourceName("foo").setName("retention.ms").setValue("1000"));
delta.replay(new RemoveTopicRecord().setTopicId(id));
MetadataImage image = delta.apply(MetadataProvenance.EMPTY);
// image.topics() is empty, while image.configs() still has TOPIC:foo -> 
{retention.ms=1000}
{code}

Consequences:
* Orphaned configs for the deleted topic stay in {{ConfigurationsImage}} and 
get written out to metadata snapshots (a controller that loads such a snapshot 
also picks up the stale entry in {{ConfigurationControlManager}}).
* Recreating a topic with the same name (in the same delta or a later one) 
causes it to inherit the old topic's configs from the image via 
{{KRaftMetadataCache}}.
* {{DynamicConfigPublisher}} passes the deleted topic's stale configs to 
{{TopicConfigHandler}}.

Removing the pending entry when the topic is absent from the base image fixes 
the issue while keeping the KAFKA-16206 behavior intact:
{code:java}
} else {
    changes.remove(resource);
}
{code}
{{AclsDelta}} handles same-delta add/remove the same way, and KAFKA-19497 fixed 
the corresponding case in {{TopicsDelta}}.

I'd like to pick this up and will open a PR with unit tests.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to