Skip to content

Make Kafka AutoProvision actually create missing topics (GH-2537)#2540

Merged
jeremydmiller merged 1 commit intomainfrom
fix/kafka-autoprovision-creates-topics-2537
Apr 20, 2026
Merged

Make Kafka AutoProvision actually create missing topics (GH-2537)#2540
jeremydmiller merged 1 commit intomainfrom
fix/kafka-autoprovision-creates-topics-2537

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

  • Fixes Calling the "AutoProvision" method does not automatically create topics in kafka. #2537: AutoProvision() on the Kafka transport is documented as "Create newly used Kafka topics on endpoint activation if the topic is missing", but on its own it had no effect — nothing in the Kafka transport ever read the flag during startup. The only workaround today is to additionally call AddResourceSetupOnStartup(), which runs for every broker and defeats per-transport granularity.
  • KafkaTopic and KafkaTopicGroup now each override InitializeAsync and call their existing SetupAsync when Parent.AutoProvision is true — the same wiring RabbitMqQueue.InitializeAsync already uses.

Root cause

BrokerTransport.InitializeAsync iterates endpoints and calls endpoint.InitializeAsync(logger), but neither Kafka endpoint class overrode that hook, so the default no-op ran and the flag was never consulted. Both classes already had correct SetupAsync methods (create topic, swallow "already exists") — they just weren't being invoked on the AutoProvision path.

KafkaTopicGroup shadows SetupAsync with a multi-topic version via new, so it needs its own override — otherwise the base KafkaTopic.SetupAsync would create a single topic named after the sanitized composite group name instead of each topic in the group.

Reproducer

Test: src/Transports/Kafka/Wolverine.Kafka.Tests/Bugs/Bug_2537_autoprovision_creates_missing_topics.cs

Targets the user's reported path (ListenToKafkaTopics(…), group listener). Two deliberate design choices to avoid Confluent-local's defaults masking the bug:

  • confluent-local:7.7.1 has auto.create.topics.enable=true, so any producer publish would silently create the topic. The test never produces — it only asserts on broker metadata.
  • admin.GetMetadata(topicName, timeout) also triggers broker auto-create on a per-topic query. The test uses admin.GetMetadata(timeout) (full-cluster snapshot) instead.

Without the fix, the test fails with the exact error the user reported:

Confluent.Kafka.ConsumeException: Subscribed topic not available: bug2537-…: Broker: Unknown topic or partition

Test plan

  • Reproducer fails pre-fix with the user-reported error, passes post-fix
  • dotnet test src/Transports/Kafka/Wolverine.Kafka.Tests --framework net9.0137 / 137 pass (2 pre-existing skips), 8m 36s locally against confluentinc/confluent-local:7.7.1
  • CI across net8 / net9 / net10

🤖 Generated with Claude Code

AutoProvision() is documented as "Create newly used Kafka topics on
endpoint activation if the topic is missing", but on its own it had no
effect: nothing in the Kafka transport ever read the flag at startup.
The bug was invisible unless users also called AddResourceSetupOnStartup
(which runs SetupAsync for every broker unconditionally), and users
reported hitting "Subscribed topic not available" from the
KafkaTopicGroupListener consumer on fresh brokers.

Root cause: BrokerTransport.InitializeAsync iterates endpoints and
calls endpoint.InitializeAsync(logger), but neither KafkaTopic nor
KafkaTopicGroup overrode that hook. Both classes already had a correct
SetupAsync that creates topics (and swallows "already exists"), so the
fix is just to wire Parent.AutoProvision into InitializeAsync — the
same pattern RabbitMqQueue.InitializeAsync uses.

KafkaTopicGroup needs its own override (not just inheritance) because
it shadows SetupAsync with a multi-topic version — the base
KafkaTopic.SetupAsync would otherwise create a single topic with the
group's sanitized composite name.

Reproducer test covers the user's reported path (ListenToKafkaTopics
group listener) and is careful not to let confluent-local's default
auto.create.topics.enable=true mask the bug: it uses full-cluster
broker metadata (not per-topic queries, which trigger auto-creation)
and never produces to the topic.

Full Kafka test suite: 137 / 137 pass on net9.0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling the "AutoProvision" method does not automatically create topics in kafka.

1 participant