Skip to content

[fix][broker] Delete scalable topics when deleting a namespace or a tenant - #26869

Merged
merlimat merged 1 commit into
apache:masterfrom
merlimat:mmerli/namespace-delete-scalable-topics
Oct 12, 2026
Merged

merlimat merged 1 commit into
apache:masterfrom
merlimat:mmerli/namespace-delete-scalable-topics

Conversation

@merlimat

Copy link
Copy Markdown
Contributor

Motivation

Namespace deletion only lists regular topics: the persistent topics under /managed-ledgers/<ns>/persistent,
the loaded non-persistent topics and the partitioned topics. A scalable topic (topic://) lives elsewhere:
its record under /topics/<ns>, the managed ledgers of its segment topics under /managed-ledgers/<ns>/segment,
and its schema. So namespace deletion never sees scalable topics:

  • A delete without force is not refused while the namespace still has scalable topics.
  • On ZooKeeper and Oxia the delete fails at the end, when it deletes /managed-ledgers/<ns> (not recursively)
    while the segment domain is still under it. It fails even after every scalable topic was deleted first: the
    scalable-topic delete leaves the empty parents /managed-ledgers/<ns>/segment/<topic> and
    /managed-ledgers/<ns>/segment behind (Oxia never removes them; ZooKeeper removes its empty container nodes
    eventually). On ZooKeeper this ends in a 409 after the 5 retries. On Oxia the not-empty error is a
    MetadataStoreException.NotEmptyException, which the retry does not recognize, so it is a 500 right away.
    Either way the namespace is left marked deleted, so lookups and topic creation in it are refused.
  • On a store that does not check children, the delete succeeds but leaves the records, the segment ledgers and
    the schema behind, and a namespace re-created with the same name has the old scalable topics again.

Tenant deletion deletes each namespace the same way, so it fails the same way, and it never deleted
/topics/<tenant>.

Example

admin.scalableTopics().createScalableTopic("topic://t/ns/orders", 1);
admin.namespaces().deleteNamespace("t/ns", true);
// Oxia:      500 Key '/managed-ledgers/t/ns' has children
// ZooKeeper: 409 The broker still have in-flight topics created during namespace deletion
//            (path /managed-ledgers/t/ns) is not empty on metadata store, please try again.

NamespacesBase.internalRetryableDeleteNamespaceAsync0 lists and deletes only the regular topics:

return topicsFuture.thenCompose(allTopics ->
        pulsar().getNamespaceService().getFullListOfPartitionedTopic(namespaceName)
                .thenCompose(allPartitionedTopics -> { /* regular and partitioned topics only */ }))

and internalClearZkSources then deletes /managed-ledgers/<ns>/persistent and /managed-ledgers/<ns>,
neither recursively, with nothing for /managed-ledgers/<ns>/segment or /topics/<ns>.

Modifications

  • Namespace deletion lists the scalable topics too (ScalableTopicResources.listScalableTopicsAsync). Any of them
    makes a delete without force fail with 409 "Cannot delete non empty namespace", before the namespace is marked
    deleted. A forced delete deletes them through the scalable-topic delete (internal admin), which also deletes
    their segment topics and their schema, after the regular user topics and before the system topics (deleting a
    segment topic deletes its topic policies). Then it deletes the segment topics still in the namespace
    (TopicResources.listSegmentTopicsAsync, from /managed-ledgers/<ns>/segment/<topic>/<descriptor>): the
    scalable-topic delete makes a single attempt at each of its segment topics, and one left behind would keep the
    namespace from being deleted.

    private CompletableFuture<Void> internalDeleteScalableTopicsAsync(List<String> scalableTopics) {
        return deleteWithAdminAsync(scalableTopics,
                        (admin, topic) -> admin.scalableTopics().deleteScalableTopicAsync(topic, true))
                .thenCompose(__ -> pulsar().getPulsarResources().getTopicResources()
                        .listSegmentTopicsAsync(namespaceName))
                .thenCompose(segmentTopics -> deleteWithAdminAsync(segmentTopics,
                        (admin, segmentTopic) -> admin.scalableTopics().deleteSegmentAsync(segmentTopic, true)));
    }
  • internalClearZkSources also deletes /topics/<ns> and the segment domain (each
    /managed-ledgers/<ns>/segment/<topic>, then /managed-ledgers/<ns>/segment), not recursively: a node that
    still has children is a topic created during the deletion, which the retry deletes.

  • The retry also recognizes Oxia's not-empty error; MetadataStoreException.NotEmptyException now exposes the
    path, like ZooKeeper's KeeperException.NotEmptyException.

  • Tenant deletion deletes /topics/<tenant>, recursively like /managed-ledgers/<tenant>.

A regular topic with the name of a scalable topic (the persistent:// topic it was migrated from, or a
non-persistent topic) shares its schema. It is deleted before the scalable topic, whose delete deletes the schema.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows. Each new integration test fails without the change:

  • ScalableTopicNamespaceDeleteTest (in-memory store): a delete without force is refused; a forced namespace or
    tenant delete deletes the records, the segment ledgers and the schema, also for a migrated topic and with a
    same-named non-persistent topic; segment topics left behind by a scalable-topic delete are deleted; a re-created
    namespace starts empty.
  • ScalableTopicNamespaceDeleteZooKeeperTest (MockZooKeeper) and V5MultiBrokerNamespaceDeleteTest (Oxia): forced
    delete, delete after the scalable topics were deleted, segment topics left behind, refused delete without force
    that leaves the namespace usable, tenant delete, and a node left under the namespace now failing with the 409
    on Oxia.
  • TopicResourcesTest#testListSegmentTopics.

The tests don't elect controllers: until the scalable-topic delete stops them (a separate fix), a controller that
is still running puts its lock back under the deleted record, which on Oxia re-creates the record node and fails
the /topics/<ns> cleanup.

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

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema: deleting a namespace or a tenant deletes the schema of its scalable topics
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints: deleting a namespace without force fails with 409 while it has scalable topics
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

…enant

Namespace deletion only listed regular topics, so it never saw the scalable topics of a
namespace: a delete without force was not refused while scalable topics existed, and a forced
delete left their records, their segment managed ledgers and their schema behind. On ZooKeeper
and Oxia it failed at the end instead, when deleting /managed-ledgers/<ns> while the segment
domain was still under it, even after every scalable topic was deleted (their empty parent
nodes stay): a 409 after the retries on ZooKeeper, a 500 on Oxia, whose not-empty error was
not retried, and either way the namespace stayed marked deleted.

- List the scalable topics with the regular ones. Any of them makes a delete without force
  fail with 409. A forced delete deletes them through the scalable-topic delete, after the
  regular user topics, then deletes the segment topics still in the namespace.
- Clear /topics/<ns> and the segment domain of /managed-ledgers/<ns> with the other namespace
  metadata, not recursively, and /topics/<tenant> when deleting a tenant.
- Retry the namespace delete on Oxia's NotEmptyException too, which now exposes its path.

@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

@merlimat
merlimat merged commit 35cabeb into apache:master Oct 12, 2026
44 checks passed
lhotari pushed a commit that referenced this pull request Oct 12, 2026
@merlimat
merlimat deleted the mmerli/namespace-delete-scalable-topics branch October 24, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants