GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][broker] Don't lose the topic changes made while a topic list watcher lists its initial topics by merlimat · Pull Request #27525 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Motivation

A pattern consumer's topic list watcher can miss topics created (or deleted) while the broker sets it up, and then
only picks them up at its next reconciliation, patternAutoDiscoveryPeriod later (1 minute by default).

TopicListService.initializeTopicsListWatcher() lists the namespace's topics and registers the watcher as a topic
listener only once the listing completes. A topic created after the listing read the namespace, but before the
watcher registered, is in neither: the watch request's success response has the stale list, whose hash matches the
client's, so the broker sends no topics, and no update follows for that topic.

This happens when topics change right after a pattern consumer subscribes, which is what these tests do and what fails
them in CI (TopicsConsumerImplTest.testPartitionsUpdatesForMultipleTopics 14 times in 83 runs since 6 Nov, also
PatternTopicsConsumerImplTest.testAutoSubscribePatterConsumerFromBrokerWatcher and testPreciseRegexpSubscribe):

TopicsConsumerImplTest.testPartitionsUpdatesForMultipleTopics: ConditionTimeoutException:
  ... expected [5] but found [2] within 10 seconds.
PatternTopicsConsumerImplTest.testAutoSubscribePatterConsumerFromBrokerWatcher[2](false): ConditionTimeoutException:
  ... expected [4] but found [0] within 10 seconds.

The variants that wait for the watcher before creating topics (createTopicAfterWatcherStarted = true) don't fail.
In a local run, the broker listed the namespace at .125, registered the watcher at .135, and the test's new partitions
were created at .169: a 10 ms window that a slower CI runner can hit.

// TopicListService.initializeTopicsListWatcher()
Consumer<List<String>> afterListing = topics -> {
    // register watcher immediately so that we don't lose events
    TopicListWatcher watcher = new TopicListWatcher(this, watcherId, namespace, topicsPattern, topics, ...);
    watcherRef.set(watcher);
    topicResources.registerPersistentTopicListener(watcher);   // after the listing read the namespace
};
getTopics(namespace, afterListing, cancelled)...

Modifications

  • The watcher is registered as a topic listener right before the listing reads the namespace (once the topic list
    memory permits are acquired, so that permit retries don't register watchers), and its matching topics are
    initialized when the listing completes.
  • Until then, the watcher keeps the last change of each matching topic, and applies them to the listed topics: the
    listing may or may not include a change, and the last change of a topic is its current state. The watch request's
    success response, sent after that, includes the changes, so they aren't sent as updates.
  • Metadata store session events that the watcher receives while listing are handled once the topics are listed: an
    update they start would otherwise be overwritten by the older initial listing. Before this change, the watcher
    didn't receive them at all.
Runnable beforeListing = () -> {
    // Register the watcher before the listing reads the namespace, so that it receives the topic changes that
    // the listing misses. It applies them to the listed topics.
    TopicListWatcher watcher = new TopicListWatcher(this, watcherId, namespace, topicsPattern, ...);
    watcherRef.set(watcher);
    topicResources.registerPersistentTopicListener(watcher);
};
...
w.initializeMatchingTopics(topics);

// TopicListWatcher.onTopicEvent(), for a matching topic
if (topicChangesWhileListing != null) {
    // the success response of the watch request sends the topics, including this change
    topicChangesWhileListing.put(topicName, notificationType);
    return;
}

Known residual, not changed here: NamespaceService.getListOfUserTopics() coalesces concurrent listings of the same
namespace (#23049), so a watcher can still join an in-flight listing that read the namespace just before the watcher
registered.

Verifying this change

  • New TopicListServiceTest.testTopicChangesWhileListingTheInitialTopicsAreNotLost holds the namespace listing,
    creates topic2 and deletes topic3 meanwhile, then completes the listing with the stale topics. Without the fix, the
    success response has the client's own hash and no topics; with it, it has topic1 and topic2.
  • New TopicListWatcherTest.testSessionEventWhileListingIsHandledOnceTheTopicsAreListed.
  • Existing tests that asserted the watcher was registered only after the listing now assert it's registered before it
    and deregistered when the listing fails.
  • TopicListServiceTest, TopicListWatcherTest, TopicsConsumerImplTest, PatternTopicsConsumerImplTest,
    ServerCnxTest, PatternConsumerTopicWatcherBackPressureMultipleConsumersTest,
    AutoCloseUselessClientConTopicsPatternConsumerTest, CustomizedPulsarResourcesExtendedTest pass locally;
    ./gradlew quickCheck passes.

testGetLastMessageId, which also failed in this job, is a different cause, fixed by #27492 (merged).

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
merlimat added 2 commits November 9, 2026 08:53
@merlimat
[fix][broker] Don't lose the topic changes made while a topic list wa…
f3e9e44
…tcher lists its initial topics

TopicListService registered a new topic list watcher as a topic listener only
after listing the namespace's topics. A topic created or deleted after the
listing read the namespace, but before the registration, was in neither: the
watch request's success response had the stale topics, whose hash matched the
client's, and no update followed, so the pattern consumer only noticed at its
next reconciliation, patternAutoDiscoveryPeriod later. This fails
TopicsConsumerImplTest.testPartitionsUpdatesForMultipleTopics and
PatternTopicsConsumerImplTest's watcher tests when topics change right after
the consumer subscribes.

The watcher is now registered right before the listing reads the namespace.
Until the listing completes, it keeps the last change of each matching topic
and applies them to the listed topics, which the success response then sends.
Session events received meanwhile are handled once the topics are listed, so
that an update they start isn't overwritten by the older initial listing.
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/topics-consu…
430ce61
…mer-shared-broker-flakes
codelipenghui
codelipenghui approved these changes Nov 9, 2026
View reviewed changes
@merlimat
merlimat merged commit 97f3ecb into apache:master Nov 9, 2026
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@merlimat
merlimat deleted the mmerli/topics-consumer-shared-broker-flakes branch November 9, 2026 22:27
lhotari pushed a commit that referenced this pull request Nov 11, 2026
@merlimat @lhotari
[fix][broker] Don't lose the topic changes made while a topic list wa…
c702e67
…tcher lists its initial topics (#27525)

(cherry picked from commit 97f3ecb)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 11, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][broker] Don't lose the topic changes made while a topic list wa…
ae4a403
…tcher lists its initial topics (apache#27525)

(cherry picked from commit 97f3ecb)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][broker] Don't lose the topic changes made while a topic list wa…
bfb6d4d
…tcher lists its initial topics (apache#27525)

(cherry picked from commit 97f3ecb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@merlimat @codelipenghui @lhotari
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.