You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/04/17 05:27:52 UTC
[GitHub] merlimat closed pull request #1527: Reduce un-necessary work when
doing bundle lookup for partitioned topic
merlimat closed pull request #1527: Reduce un-necessary work when doing bundle lookup for partitioned topic
URL: https://github.com/apache/incubator-pulsar/pull/1527
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
index a712aec39..bf44519e9 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
@@ -69,6 +69,7 @@
import org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies;
import org.apache.pulsar.common.util.Codec;
import org.apache.pulsar.common.util.ObjectMapperFactory;
+import org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap;
import org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException;
@@ -119,7 +120,7 @@
public static final Pattern SLA_NAMESPACE_PATTERN = Pattern.compile(SLA_NAMESPACE_PROPERTY + "/[^/]+/([^:]+:\\d+)");
public static final String HEARTBEAT_NAMESPACE_FMT = "pulsar/%s/%s:%s";
public static final String SLA_NAMESPACE_FMT = SLA_NAMESPACE_PROPERTY + "/%s/%s:%s";
-
+
public static final String NAMESPACE_ISOLATION_POLICIES = "namespaceIsolationPolicies";
/**
@@ -275,6 +276,11 @@ private boolean registerNamespace(String namespace, boolean ensureOwned) throws
}
}
+ static ConcurrentOpenHashMap<NamespaceBundle, CompletableFuture<Optional<LookupResult>>> findingBundlesAuthoritative
+ = new ConcurrentOpenHashMap<>();
+ static ConcurrentOpenHashMap<NamespaceBundle, CompletableFuture<Optional<LookupResult>>> findingBundlesNotAuthoritative
+ = new ConcurrentOpenHashMap<>();
+
/**
* Main internal method to lookup and setup ownership of service unit to a broker
*
@@ -290,38 +296,51 @@ private boolean registerNamespace(String namespace, boolean ensureOwned) throws
LOG.debug("findBrokerServiceUrl: {} - read-only: {}", bundle, readOnly);
}
- CompletableFuture<Optional<LookupResult>> future = new CompletableFuture<>();
+ ConcurrentOpenHashMap<NamespaceBundle, CompletableFuture<Optional<LookupResult>>> targetMap;
+ if (authoritative) {
+ targetMap = findingBundlesAuthoritative;
+ } else {
+ targetMap = findingBundlesNotAuthoritative;
+ }
- // First check if we or someone else already owns the bundle
- ownershipCache.getOwnerAsync(bundle).thenAccept(nsData -> {
- if (!nsData.isPresent()) {
- // No one owns this bundle
+ return targetMap.computeIfAbsent(bundle, (k) -> {
+ CompletableFuture<Optional<LookupResult>> future = new CompletableFuture<>();
- if (readOnly) {
- // Do not attempt to acquire ownership
- future.complete(Optional.empty());
- } else {
- // Now, no one owns the namespace yet. Hence, we will try to dynamically assign it
- pulsar.getExecutor().execute(() -> {
- searchForCandidateBroker(bundle, future, authoritative);
- });
- }
- } else if (nsData.get().isDisabled()) {
- future.completeExceptionally(
+ // First check if we or someone else already owns the bundle
+ ownershipCache.getOwnerAsync(bundle).thenAccept(nsData -> {
+ if (!nsData.isPresent()) {
+ // No one owns this bundle
+
+ if (readOnly) {
+ // Do not attempt to acquire ownership
+ future.complete(Optional.empty());
+ } else {
+ // Now, no one owns the namespace yet. Hence, we will try to dynamically assign it
+ pulsar.getExecutor().execute(() -> {
+ searchForCandidateBroker(bundle, future, authoritative);
+ });
+ }
+ } else if (nsData.get().isDisabled()) {
+ future.completeExceptionally(
new IllegalStateException(String.format("Namespace bundle %s is being unloaded", bundle)));
- } else {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Namespace bundle {} already owned by {} ", bundle, nsData);
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Namespace bundle {} already owned by {} ", bundle, nsData);
+ }
+ future.complete(Optional.of(new LookupResult(nsData.get())));
}
- future.complete(Optional.of(new LookupResult(nsData.get())));
- }
- }).exceptionally(exception -> {
- LOG.warn("Failed to check owner for bundle {}: {}", bundle, exception.getMessage(), exception);
- future.completeExceptionally(exception);
- return null;
- });
+ }).exceptionally(exception -> {
+ LOG.warn("Failed to check owner for bundle {}: {}", bundle, exception.getMessage(), exception);
+ future.completeExceptionally(exception);
+ return null;
+ });
- return future;
+ future.whenComplete((r, t) -> pulsar.getExecutor().execute(
+ () -> targetMap.remove(bundle)
+ ));
+
+ return future;
+ });
}
private void searchForCandidateBroker(NamespaceBundle bundle,
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services