[fix][broker] Reject topic names with leading or trailing whitespace on creation - #26281
Open
crossoverJie wants to merge 8 commits into
Open
[fix][broker] Reject topic names with leading or trailing whitespace on creation#26281crossoverJie wants to merge 8 commits into
crossoverJie wants to merge 8 commits into
Conversation
…espace Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
…alableTopicsListByPropertyTest Signed-off-by: crossoverJie <crossoverJie@gmail.com>
…Test Signed-off-by: crossoverJie <crossoverJie@gmail.com>
dao-jun
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Creating a topic whose local name has leading or trailing whitespace (e.g.
persistent://public/default/test-topic-x) succeeds via the admin API, but producing to / consuming from it fails withTopicDoesNotExistExceptionwhenallowAutoTopicCreation=false.Root cause: Pulsar clients (Java
ConsumerBuilder/ProducerBuilder/ReaderBuilder) trim topic names, so the client resolves to the trimmed name while the created znode keeps the whitespace (Codecencodes the space as+). The orphaned topic can never be reached — and can't even be deleted, because the delete path also trims the name and returns 404.Rejecting on creation is preferred over server-side trimming: trimming would silently reroute non-Java clients (Go/C++/Python, which do not trim) to a different name after an upgrade, and would make already-broken whitespace topics un-deletable.
Modifications
TopicName: addedisValidForCreation(TopicName)andvalidateTopicNameForCreation(TopicName)— reject leading/trailing whitespace in the local name; internal whitespace (e.g.my topic) stays allowed.Broker admin creation paths now reject with HTTP 412 (
PreconditionFailedException) instead of persisting an unreachable topic:Centralized create-time validation in
AdminResource.validateCreateTopic(TopicName)(moved fromPersistentTopicsBase) so persistent, non-persistent and scalable topics share one source of truth.BrokerService.isAllowAutoTopicCreationAsyncreturnsfalsefor such names, so auto-creation is also refused (covers clients that do not trim).Client admin fail-fast:
TopicsImpl(partitioned + non-partitioned),NonPersistentTopicsImpl, andScalableTopicsImplnow reject whitespace names locally and fail the future withPreconditionFailedException(412), consistent with the server response — so callers catchingPreconditionFailedExceptionwork for both the client fast-fail and the server path.Verifying this change
This change added tests and can be verified.
Does this pull request potentially affect one of the following parts:
(
PreconditionFailedException) for names with leading/trailingwhitespace instead of persisting an unreachable topic.
leading/trailing whitespace that were previously accepted; this is a
behavioral correction of a bug. Callers that never sent whitespace
names are unaffected.