Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][broker] Avoid using blocking calls for the async method checkTopicOwnership #15023

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ private CompletableFuture<Boolean> isTopicOwnedAsync(TopicName topic) {

public CompletableFuture<Boolean> checkTopicOwnership(TopicName topicName) {
return getBundleAsync(topicName)
.thenApply(ownershipCache::checkOwnership);
.thenCompose(ownershipCache::checkOwnershipAsync);
}

public void removeOwnedServiceUnit(NamespaceBundle nsBundle) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ public OwnershipCache(PulsarService pulsar, NamespaceBundleFactory bundleFactory
* @param bundle namespace bundle
* @return future that will complete with check result
*/
public boolean checkOwnership(NamespaceBundle bundle) {
return getOwnedBundle(bundle) != null;
public CompletableFuture<Boolean> checkOwnershipAsync(NamespaceBundle bundle) {
Optional<CompletableFuture<OwnedBundle>> ownedBundleFuture = getOwnedBundleAsync(bundle);
if (!ownedBundleFuture.isPresent()) {
return CompletableFuture.completedFuture(false);
}
return ownedBundleFuture.get()
.thenApply(bd -> bd != null && bd.isActive());
}

/**
Expand Down Expand Up @@ -279,6 +284,10 @@ public OwnedBundle getOwnedBundle(NamespaceBundle bundle) {
}
}

public Optional<CompletableFuture<OwnedBundle>> getOwnedBundleAsync(NamespaceBundle bundle) {
return Optional.ofNullable(ownedBundlesCache.getIfPresent(bundle));
}

/**
* Disable bundle in local cache and on zk.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public void testReestablishOwnership() throws Exception {
assertFalse(data3.isDisabled());
assertNotNull(cache.getOwnedBundle(testFullBundle));

assertTrue(cache.checkOwnership(testFullBundle));
assertTrue(cache.checkOwnershipAsync(testFullBundle).get());
assertEquals(data2.getNativeUrl(), selfBrokerUrl);
assertFalse(data2.isDisabled());
assertNotNull(cache.getOwnedBundle(testFullBundle));
Expand Down