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 let the leader's bundle-data write recreate the data of a bundle it just removed by merlimat · Pull Request #27521 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Motivation

NamespaceServiceTest.testModularLoadManagerRemoveInactiveBundleFromLoadData is in the flaky group, so it runs in "Flaky tests suite". It sometimes fails its first attempt after about 10.5 s and passes on retry. Since 2026-11-07 it needed a retry in 4 CI runs (42365724164, 42371640296, 42573898627, 42648663625):

org.awaitility.core.ConditionTimeoutException: Assertion condition defined as a Lambda expression in
org.apache.pulsar.broker.namespace.NamespaceServiceTest expected [false] but found [true] within 10 seconds.

The second assertion is the one that fails. The split bundle is gone from the leader's loadData, but its /loadbalance/bundle-data/... node still exists, and nothing deletes it again.

With ModularLoadManagerImpl, two threads change the leader's bundle data without any synchronization:

  • updateAll() runs on the load manager's own thread whenever a broker's load report changes. Its updateBundleData() removes from loadData every bundle that no broker reports any more, and deletes the bundle's data from the metadata store.
  • writeBundleDataOnZooKeeper() runs from LoadResourceQuotaUpdaterTask on the load manager executor. The test calls it from its own thread. It also calls updateBundleData(), then writes the data of the bundles it finds in loadData:
public void writeBundleDataOnZooKeeper() {
    ...
    updateBundleData();
    ...
    synchronized (bundleArr) {
        int updateBundleCount = selectTopKBundle();   // collects the bundles from loadData.getBundleData()
        for (Map.Entry<String, ? extends Comparable> entry : bundleArr.subList(0, updateBundleCount)) {
            ...
            futures.add(pulsarResources.getLoadBalanceResources().getBundleDataResources().updateBundleData(
                    entry.getKey(), (BundleData) entry.getValue()));
        }
    }
    ...
    FutureUtil.waitForAll(futures).join();
}

private void updateBundleData() {
    ...
    // Remove not active bundle from loadData
    for (String bundle : bundleData.keySet()) {
        if (!activeBundles.contains(bundle)){
            bundleData.remove(bundle);
            if (isLeader()) {
                deleteBundleDataFromMetadataStore(bundle);
            }
        }
    }
}

Suppose a write collects a bundle before the leader learns that the bundle is gone. If updateAll() then removes and deletes the bundle before the write lands, the write recreates the node after the delete. The bundle is no longer in loadData, so no later updateBundleData() deletes the node again. The node stays until the bundle reappears and goes away again.

The test splits a bundle. Then, one right after the other, it writes the new load report and calls writeResourceQuotasToZooKeeper() on its own thread. The report's notification runs updateAll() on the load manager's thread:

pulsar.getNamespaceService().splitAndOwnBundle(oldBundle, false,
        NamespaceBundleSplitAlgorithm.RANGE_EQUALLY_DIVIDE_ALGO, null).get();

// update broker bundle report to zk
pulsar.getBrokerService().updateRates();
loadManager.writeLoadReportOnZookeeper();       // -> notification -> updateAll() on pulsar-modular-load-manager
loadManager.writeResourceQuotasToZooKeeper();   // -> writeBundleDataOnZooKeeper() on the test thread

The same race exists in a running broker, between the leader's updateAll() and LoadResourceQuotaUpdaterTask. It also exists with the auto-split in checkNamespaceBundleSplit(), which removes the split bundle from loadData and deletes its data the same way. Each time the race hits, a bundle-data node leaks.

A local variant of the test forces this order. It spies BundleDataResources so that the write of the old bundle's data waits until the delete completes. It starts writeResourceQuotasToZooKeeper() before the leader has seen the post-split load report, and then writes that report. It fails every time with the same expected [false] but found [true] within 10 seconds, and the log shows the delete on pulsar-modular-load-manager before the write.

Modifications

ModularLoadManagerImpl gets a bundleDataLock. It serializes writeBundleDataOnZooKeeper() with the removals of bundles from loadData and the deletes of their data. writeBundleDataOnZooKeeper() holds the lock until its writes complete:

// Serializes the removals of bundles from loadData, with the deletions of their data from the metadata store, and
// writeBundleDataOnZooKeeper(). Otherwise a write that collected a bundle before its removal can recreate the
// bundle's data after the deletion, and nothing deletes it again.
private final Object bundleDataLock = new Object();

public void updateAll() {
    ...
    updateAllBrokerData();
    synchronized (bundleDataLock) {
        updateBundleData();
    }
    ...
}

public void writeBundleDataOnZooKeeper() {
    if (!isLeader()) {
        return;
    }
    // Hold the lock until the writes complete, so that the bundles removed meanwhile are deleted after them
    synchronized (bundleDataLock) {
        updateBundleData();
        ... // unchanged: collect, write, wait for the writes
    }
}

checkNamespaceBundleSplit() also removes the split bundle and deletes its data under the same lock. Now a bundle that a write collected is either written before the leader removes it, and then deleted after the write, or it's already removed when the write collects the bundles. Two threads no longer run updateBundleData() at the same time. That also stops a stale updateBundleData() from putting a just-removed bundle back into loadData.

writeBundleDataOnZooKeeper() already waited for its writes. It now does so while it holds bundleDataLock. An updateAll() that starts meanwhile on the pulsar-modular-load-manager thread waits at updateBundleData() until those writes complete. That briefly delays the rest of that update, including the bundle-split check. The write covers at most loadBalancerMaxNumberOfBundlesInBundleLoadReport (10) bundles per broker, every loadBalancerResourceQuotaUpdateIntervalMinutes (15). In the other direction, a write waits for an in-progress updateBundleData() and its deletes. No metadata-store callback takes the lock, so it can't deadlock with the operations it waits for.

The test is unchanged.

Verifying this change

  • The forced local variant of the test fails without the change. With it, the variant passes: the write lands first and the delete follows.
  • ./gradlew :pulsar-broker:test --tests 'org.apache.pulsar.broker.namespace.NamespaceServiceTest' -PexcludedTestGroups=quarantine -PtestRetryCount=0: 28 tests pass.
  • ModularLoadManagerImplTest (26 tests) and LoadBalancerTest (7 tests) pass.
  • ./gradlew quickCheck passes.

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
  • 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:43
@merlimat
[fix][broker] Don't let the leader's bundle-data write recreate the d…
47d2f6e
…ata of a bundle it just removed

With ModularLoadManagerImpl, updateAll() (on the load manager's executor, after
each load report change) and writeBundleDataOnZooKeeper() (on the load manager
executor, from LoadResourceQuotaUpdaterTask) both change the leader's bundle
data without synchronization. When a write collects a bundle before the leader
learns it's gone, and updateAll() removes it from loadData and deletes its data
before the write lands, the write recreates the bundle-data node. The bundle is
no longer in loadData, so nothing deletes the node again. The auto-split in
checkNamespaceBundleSplit() removes and deletes the split bundle the same way.

This made NamespaceServiceTest.testModularLoadManagerRemoveInactiveBundleFromLoadData
flaky: it writes the post-split load report and then the bundle data right after.

Serialize the removals of bundles from loadData, with the deletions of their
data, and writeBundleDataOnZooKeeper(), which holds the lock until its writes
complete.
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/ns-service-i…
a9605b1
…nactive-bundle-flake
codelipenghui
codelipenghui approved these changes Nov 9, 2026
View reviewed changes
@merlimat
merlimat merged commit 8063b6a into apache:master Nov 9, 2026
42 checks passed

Uh oh!

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

@merlimat
merlimat deleted the mmerli/ns-service-inactive-bundle-flake branch November 9, 2026 20:52
lhotari pushed a commit that referenced this pull request Nov 11, 2026
@merlimat @lhotari
[fix][broker] Don't let the leader's bundle-data write recreate the d…
060228c
…ata of a bundle it just removed (#27521)

(cherry picked from commit 8063b6a)
@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 let the leader's bundle-data write recreate the d…
90282e0
…ata of a bundle it just removed (apache#27521)

(cherry picked from commit 8063b6a)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][broker] Don't let the leader's bundle-data write recreate the d…
f243214
…ata of a bundle it just removed (apache#27521)

(cherry picked from commit 8063b6a)
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.