Skip to content

[fix][broker][branch-4.0] Avoid namespace scan for topic policy validation - #27372

Merged
shibd merged 1 commit into
apache:branch-4.0from
shibd:codex/optimize-topic-policy-existence-branch-4.0
Nov 6, 2026
Merged

shibd merged 1 commit into
apache:branch-4.0from
shibd:codex/optimize-topic-policy-existence-branch-4.0

Conversation

@shibd

@shibd shibd commented Nov 5, 2026

Copy link
Copy Markdown
Member

Motivation

On branch-4.0, every persistent-topic policy operation runs PersistentTopicsBase.preValidation, which calls checkTopicExistsAsync. For persistent topics, that method lists every topic in the namespace and normalizes every topic name before answering a single-topic existence question.

This makes each policy request O(number of topics in the namespace). The cost is amplified when a control-plane inventory reads policies for many topics concurrently.

We reproduced the issue with an inventory of 3,956 topics and policy concurrency 20. On Pulsar 4.0, one Broker received 4,929 policy requests across 451 topics in five minutes, and the repeated namespace scans drove all Broker web workers and Brokers to an 88-100% CPU plateau. With the same inventory on Pulsar 4.2, where the direct existence-check path from #24225 is present, the saturation pattern disappeared: the maximum sampled Broker CPU was 74.2%, and the inventory completed 17.3% faster.

The complete #24225 change also modifies partitioned-topic recreation semantics. This PR intentionally backports only the safe existence-check optimization needed by topic-policy validation.

Modifications

  • Make topic-policy pre-validation call internalCheckTopicExists, which uses NamespaceService.checkTopicExistsAsync for a direct metadata/resource existence check and recycles the returned TopicExistsInfo.
  • Preserve the existing 404 response, partition metadata lookup, and ownership validation behavior.
  • Do not change partitioned-topic creation or recreation semantics on branch-4.0.
  • Add a regression test that makes namespace topic listing fail and verifies that a persistent-topic policy read still succeeds.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

  • ./mvnw -pl pulsar-broker -Dtest=TopicPoliciesTest#testTopicPolicyValidationDoesNotListNamespaceTopics test
  • ./mvnw -pl pulsar-broker -Dtest=TopicPoliciesTest#testNonPersistentTopicAppliesTopicPolicyOnLoad test
  • ./mvnw -pl pulsar-broker -DskipTests install (includes compilation and Checkstyle)

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

The implementation of topic-policy REST endpoint pre-validation changes, but its API and response semantics are preserved.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for tracking down the namespace-scan cost and for keeping the backport scoped to the existence check rather than the whole of #24225.

I compared the new path with the old one for the cases that matter: non-persistent topics already went through NamespaceService.checkTopicExistsAsync, so nothing changes there; missing topics still get the same 404 and message; partitioned topics are covered by the metadata lookup; system topics are not filtered by either path; and the TopicExistsInfo is recycled. The ordering of the existence check, the partitioned-metadata lookup and the ownership validation is unchanged and nothing blocks. This also matches what master does in preValidation today. The regression test fails when the production change is reverted and passes with it.

One non-blocking observation is in the inline comment about an edge case where partition ledgers exist without metadata.

.thenCompose(exist -> {
if (!exist) {
throw new RestException(Status.NOT_FOUND, getTopicNotFoundErrorMessage(topicName.toString()));
.thenCompose(__ -> internalCheckTopicExists(topicName))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[QUALITY] A base topic whose only trace is orphan partition ledgers now gets 404 from topic-policy calls

Non-blocking: the old AdminResource.checkTopicExistsAsync list scan treated t as existing when any listed topic normalized to t (for example a t-partition-0 managed ledger) even without partitioned metadata for t. NamespaceService.checkTopicExistsAsync instead looks at the partitioned metadata and then at the managed-ledger node of t itself. If the metadata was lost but the partition ledgers remain, policy get/set for t used to pass the existence check and now returns 404 Topic ... not found.

This is the same behaviour master has, and that state is already broken for most other operations, so I think it is acceptable. It would be worth a line in the PR description, since it says the 404 behaviour is preserved.

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review approval for commit 8e9bec2.

Summary:

No actionable findings identified within the reviewed scope.

The narrow backport removes namespace scans while preserving normal validation, authorization and ownership behavior, with correct TopicExistsInfo recycling. Recommend approval based on source review, noting the orphan-partition compatibility caveat. Local validation did not reach any selected tests.

@shibd
shibd merged commit 6189a82 into apache:branch-4.0 Nov 6, 2026
51 of 53 checks passed
codelipenghui added a commit that referenced this pull request Nov 9, 2026
…g in topic policy validation

#27372 made the topic policy validation check the topic existence with NamespaceService#checkTopicExistsAsync,
which on branch-4.0 doesn't treat the topics of the heartbeat namespace as existing like it does since #24225.
PartitionedSystemTopicTest.testHeartbeatTopicNotAllowedToSendEvent then gets a 404 instead of the 409 with which
the topic policies service refuses the policy updates of heartbeat namespace topics.
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.

4 participants