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 2022/12/09 14:46:02 UTC
[GitHub] [pulsar] Demogorgon314 opened a new pull request, #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Demogorgon314 opened a new pull request, #18858:
URL: https://github.com/apache/pulsar/pull/18858
Master Issue: https://github.com/apache/pulsar/issues/16691
### Motivation
When the channel handles the split event, we should write the split bundle to zookeeper instead of `ServiceUnitStateChannel`, since we are using the `ServiceUnitStateChannel` to store the bundle ownership.
### Modifications
* Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split.
* Add unit test to cover this change.
### Documentation
<!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
- [ ] `doc` <!-- Your PR contains doc changes. Please attach the local preview screenshots (run `sh start.sh` at `pulsar/site2/website`) to your PR description, or else your PR might not get merged. -->
- [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
- [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-complete` <!-- Docs have been already added -->
### Matching PR in forked repository
PR in forked repository: https://github.com/Demogorgon314/pulsar/pull/8
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] eolivelli commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "eolivelli (via GitHub)" <gi...@apache.org>.
eolivelli commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1099773970
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
Review Comment:
what about logging the error here ?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
Review Comment:
what are the consequences of this failure ?
can you please point me out to where we handle this inconsistent state?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
+ && (counter.incrementAndGet() < NamespaceService.BUNDLE_SPLIT_RETRY_LIMIT)) {
+ pulsar.getExecutor().schedule(() -> splitServiceUnitOnceAndRetry(namespaceService, bundleFactory,
+ bundle, serviceUnit, data, counter, startTime, completionFuture), 100, MILLISECONDS);
+ } else if (ex instanceof IllegalArgumentException) {
Review Comment:
why we are doing the test on "ex" here and on "ex.getCause()" above ?
and why this case is so special ? we should at least log that we are passing from this branch
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050502207
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +526,109 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
+ && (counter.incrementAndGet() < NamespaceService.BUNDLE_SPLIT_RETRY_LIMIT)) {
+ pulsar.getExecutor().schedule(() -> pulsar.getOrderedExecutor()
Review Comment:
Good catch! It copied from https://github.com/apache/pulsar/pull/16612/files#diff-99867fd2e328e0a689daa8f60d174aa96d87dd0d0fd29b9e68fe2e1f377b55a1R903
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codelipenghui commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1104149144
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
Review Comment:
Yes, that actually work. Usually, we are using the thenCompose to interact with an async operation. So that we don't need to complete the `completionFuture` in many places.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codecov-commenter commented on pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#issuecomment-1370454609
# [Codecov](https://codecov.io/gh/apache/pulsar/pull/18858?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#18858](https://codecov.io/gh/apache/pulsar/pull/18858?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (e97ca14) into [master](https://codecov.io/gh/apache/pulsar/commit/9ec1d071c7188a2db694e9d7b359faaf33cb076e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (9ec1d07) will **decrease** coverage by `10.02%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/pulsar/pull/18858?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
```diff
@@ Coverage Diff @@
## master #18858 +/- ##
=============================================
- Coverage 47.43% 37.41% -10.03%
+ Complexity 9535 1998 -7537
=============================================
Files 632 209 -423
Lines 59839 14452 -45387
Branches 6234 1577 -4657
=============================================
- Hits 28384 5407 -22977
+ Misses 28375 8455 -19920
+ Partials 3080 590 -2490
```
| Flag | Coverage Δ | |
|---|---|---|
| unittests | `37.41% <ø> (-10.03%)` | :arrow_down: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Impacted Files](https://codecov.io/gh/apache/pulsar/pull/18858?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [...xtensions/channel/ServiceUnitStateChannelImpl.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9sb2FkYmFsYW5jZS9leHRlbnNpb25zL2NoYW5uZWwvU2VydmljZVVuaXRTdGF0ZUNoYW5uZWxJbXBsLmphdmE=) | | |
| [...ache/pulsar/broker/namespace/NamespaceService.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9uYW1lc3BhY2UvTmFtZXNwYWNlU2VydmljZS5qYXZh) | | |
| [...sar/proxy/extensions/ProxyExtensionDefinition.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLXByb3h5L3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvcHJveHkvZXh0ZW5zaW9ucy9Qcm94eUV4dGVuc2lvbkRlZmluaXRpb24uamF2YQ==) | | |
| [.../SchemaRegistryServiceWithSchemaDataValidator.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL3NjaGVtYS92YWxpZGF0b3IvU2NoZW1hUmVnaXN0cnlTZXJ2aWNlV2l0aFNjaGVtYURhdGFWYWxpZGF0b3IuamF2YQ==) | | |
| [.../pulsar/broker/service/persistent/SystemTopic.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL3BlcnNpc3RlbnQvU3lzdGVtVG9waWMuamF2YQ==) | | |
| [...java/org/apache/pulsar/broker/admin/v2/Worker.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9hZG1pbi92Mi9Xb3JrZXIuamF2YQ==) | | |
| [...g/apache/pulsar/broker/admin/impl/TenantsBase.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9hZG1pbi9pbXBsL1RlbmFudHNCYXNlLmphdmE=) | | |
| [...n/java/org/apache/pulsar/client/api/RawReader.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9hcGkvUmF3UmVhZGVyLmphdmE=) | | |
| [...a/org/apache/pulsar/proxy/server/ProxyService.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLXByb3h5L3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvcHJveHkvc2VydmVyL1Byb3h5U2VydmljZS5qYXZh) | | |
| [...e/pulsar/broker/protocol/ProtocolHandlerUtils.java](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9wcm90b2NvbC9Qcm90b2NvbEhhbmRsZXJVdGlscy5qYXZh) | | |
| ... and [416 more](https://codecov.io/gh/apache/pulsar/pull/18858/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] gaoran10 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "gaoran10 (via GitHub)" <gi...@apache.org>.
gaoran10 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1096927990
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java:
##########
@@ -957,6 +957,44 @@ void splitAndOwnBundleOnceAndRetry(NamespaceBundle bundle,
});
}
+ /**
+ * Get the split boundary's.
+ *
+ * @param bundle The bundle to split.
+ * @param boundaries The specified positions,
+ * use for {@link org.apache.pulsar.common.naming.SpecifiedPositionsBundleSplitAlgorithm}.
+ * @return A pair, left is target namespace bundle, right is split bundles.
+ */
+ public CompletableFuture<Pair<NamespaceBundles, List<NamespaceBundle>>> getSplitBoundary(
+ NamespaceBundle bundle, List<Long> boundaries) {
+ ServiceConfiguration config = pulsar.getConfig();
+ BundleSplitOption bundleSplitOption;
+ if (config.getDefaultNamespaceBundleSplitAlgorithm()
Review Comment:
This part is the same with the method NamespaceService#splitAndOwnBundleOnceAndRetry, we can add a method to unify the logic.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java:
##########
@@ -957,6 +957,44 @@ void splitAndOwnBundleOnceAndRetry(NamespaceBundle bundle,
});
}
+ /**
+ * Get the split boundary's.
+ *
+ * @param bundle The bundle to split.
+ * @param boundaries The specified positions,
+ * use for {@link org.apache.pulsar.common.naming.SpecifiedPositionsBundleSplitAlgorithm}.
+ * @return A pair, left is target namespace bundle, right is split bundles.
+ */
+ public CompletableFuture<Pair<NamespaceBundles, List<NamespaceBundle>>> getSplitBoundary(
+ NamespaceBundle bundle, List<Long> boundaries) {
+ ServiceConfiguration config = pulsar.getConfig();
+ BundleSplitOption bundleSplitOption;
+ if (config.getDefaultNamespaceBundleSplitAlgorithm()
+ .equals(NamespaceBundleSplitAlgorithm.FLOW_OR_QPS_EQUALLY_DIVIDE)) {
+ Map<String, TopicStatsImpl> topicStatsMap = pulsar.getBrokerService().getTopicStats(bundle);
+ bundleSplitOption = new FlowOrQpsEquallyDivideBundleSplitOption(this, bundle, boundaries,
+ topicStatsMap,
+ config.getLoadBalancerNamespaceBundleMaxMsgRate(),
+ config.getLoadBalancerNamespaceBundleMaxBandwidthMbytes(),
+ config.getFlowOrQpsDifferenceThresholdPercentage());
+ } else {
+ bundleSplitOption = new BundleSplitOption(this, bundle, boundaries);
+ }
+ NamespaceBundleSplitAlgorithm nsBundleSplitAlgorithm =
Review Comment:
Please check the method `NamespacesBase#getNamespaceBundleSplitAlgorithmByName`, it has a null check.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -512,25 +521,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
Review Comment:
Does this mean all new split bundles will be owned by split broker?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#issuecomment-1420297123
> Hmmm, I think the right solution is to improve the NamespaceService to decide to write to Zookeeper or service unit channel? We also have Admin API to split the bundle, it will be a problem if the load manager write the bundle split to service unit state channel, but the Admin API can't write to the service unit channel?
@codelipenghui I've had a second thought, Actually, we should keep this method in `ServiceUnitStateChannel `. The split operation should call by channel when receiving the `Splitting` state, like the unload operation. We can support the admin API later in other PRs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1100221257
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
Review Comment:
There are two cases to entry this failure.
1. When `pubAsync` has an exception.
2. When `updateNamespaceBundles`, `updateNamespaceBundlesForPolicies` has exception.
In these two cases, we won’t write the new bundle to ZK, so when the client tries to lookup, it will still find the old bundle, but some of the new bundle will be left in table-view, and when the broker is down, the unused bundle will be deleted in table-view.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048316914
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
Review Comment:
Yes, when has exception, we should change the bundle state back to `Owned`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#issuecomment-1384829226
> I think it's incorrect in the PR's description, right?
@codelipenghui Thanks for reminding me, I have fixed it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050316454
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
Sorry, I think we need to revert this next back to `ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());`
`Free->Assigned(Non-Transfer)` is for the state when broker A trying to assign bundle ownership to broker B when there is no ownership, `Free`. (Since broker A assumes the broker B is online, `Assigned-> Owned` change requires broker B to to acknowledge `Assigned` by `Owned` broadcast.
As we defined in the `ServiceUnitState` state diagram, I think the children's state can be `Owned` initially (does not need to be `Assigned`, as the parent broker(the same broker) always owns the children).
My initial concern was possible racing conditions if the broker does not wait until it receives its `Owned` message in its table-view(case 2 below). I think this waiting seems redundant but still safe to have it.
Let's review the racing conditions here.
case 1: when lookup parent(before updating zk bundle partitions) bundle before/after the broker receives children `Owned` in its table-view
=> happy case: the broker immediately returns the parent bundle lookup result(the same broker).
case 2: when lookup children(after updating zk bundle partitions) bundles before the broker receives children `Owned` in its table-view
=> unhappy case: the broker runs the bundle assignment logic and probably tries to assign the ownership to another broker. However, this `Owned->Assigned(Non-Transfer)` transition will be filtered out as invalid. Eventually, the children bundle lookup will either timeout or correctly return the original owner broker when the first `Owned` msg arrives. Still, it will be safe to avoid this case if we can delay zk bundle partition updates til `getOwner(children bundles).join(timeout ).get() == this.broker.`
case 3: when lookup children(after updating zk bundle partitions) after the broker receives children `Owned` in its table-view
=> happy case: the broker immediately returns the lookup result.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] eolivelli commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
eolivelli commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050449602
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +526,109 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
+ && (counter.incrementAndGet() < NamespaceService.BUNDLE_SPLIT_RETRY_LIMIT)) {
+ pulsar.getExecutor().schedule(() -> pulsar.getOrderedExecutor()
Review Comment:
Why do we chain two executors call?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1049241602
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
So if we set the bundle status as `Assigned`, then we don't need to block the call until `getOwner(newBundles).join(timeout ).get() == this.broker`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1104139700
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
Review Comment:
The `successPublishedBundles` list will be called concurrently in `pubAsync(sBundle.toString(), next).thenAccept(__ -> successPublishedBundles.add(sBundle))`, so we need to use a synchronized list.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048229100
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
+ namespaceService.updateNamespaceBundlesForPolicies(nsname, splitBundles.getLeft()))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles.getRight());
+ }).exceptionally(ex -> {
+ log.warn("Failed to update namespace policies [{}], NamespaceBundle: {} due to {}",
+ nsname, bundle.getBundleRange(), ex.getMessage());
+ updateFuture.completeExceptionally(ex);
+ return null;
+ })).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
Review Comment:
Good point. Updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048317208
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelTest.java:
##########
@@ -566,7 +602,7 @@ public void handleBrokerDeletionEventTest()
assertEquals(0, followerCleanupJobs.size());
assertEquals(1, getCleanupMetric(leaderChannel, "totalCleanupCnt"));
assertEquals(1, getCleanupMetric(leaderChannel, "totalBrokerCleanupTombstoneCnt"));
- assertEquals(2, getCleanupMetric(leaderChannel, "totalServiceUnitCleanupTombstoneCnt"));
+ assertEquals(4, getCleanupMetric(leaderChannel, "totalServiceUnitCleanupTombstoneCnt"));
Review Comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1097316112
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java:
##########
@@ -957,6 +957,44 @@ void splitAndOwnBundleOnceAndRetry(NamespaceBundle bundle,
});
}
+ /**
+ * Get the split boundary's.
+ *
+ * @param bundle The bundle to split.
+ * @param boundaries The specified positions,
+ * use for {@link org.apache.pulsar.common.naming.SpecifiedPositionsBundleSplitAlgorithm}.
+ * @return A pair, left is target namespace bundle, right is split bundles.
+ */
+ public CompletableFuture<Pair<NamespaceBundles, List<NamespaceBundle>>> getSplitBoundary(
+ NamespaceBundle bundle, List<Long> boundaries) {
+ ServiceConfiguration config = pulsar.getConfig();
+ BundleSplitOption bundleSplitOption;
+ if (config.getDefaultNamespaceBundleSplitAlgorithm()
+ .equals(NamespaceBundleSplitAlgorithm.FLOW_OR_QPS_EQUALLY_DIVIDE)) {
+ Map<String, TopicStatsImpl> topicStatsMap = pulsar.getBrokerService().getTopicStats(bundle);
+ bundleSplitOption = new FlowOrQpsEquallyDivideBundleSplitOption(this, bundle, boundaries,
+ topicStatsMap,
+ config.getLoadBalancerNamespaceBundleMaxMsgRate(),
+ config.getLoadBalancerNamespaceBundleMaxBandwidthMbytes(),
+ config.getFlowOrQpsDifferenceThresholdPercentage());
+ } else {
+ bundleSplitOption = new BundleSplitOption(this, bundle, boundaries);
+ }
+ NamespaceBundleSplitAlgorithm nsBundleSplitAlgorithm =
Review Comment:
Good catch, fixed.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java:
##########
@@ -957,6 +957,44 @@ void splitAndOwnBundleOnceAndRetry(NamespaceBundle bundle,
});
}
+ /**
+ * Get the split boundary's.
+ *
+ * @param bundle The bundle to split.
+ * @param boundaries The specified positions,
+ * use for {@link org.apache.pulsar.common.naming.SpecifiedPositionsBundleSplitAlgorithm}.
+ * @return A pair, left is target namespace bundle, right is split bundles.
+ */
+ public CompletableFuture<Pair<NamespaceBundles, List<NamespaceBundle>>> getSplitBoundary(
+ NamespaceBundle bundle, List<Long> boundaries) {
+ ServiceConfiguration config = pulsar.getConfig();
+ BundleSplitOption bundleSplitOption;
+ if (config.getDefaultNamespaceBundleSplitAlgorithm()
Review Comment:
Updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1100229327
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
Review Comment:
When the `updateFuture.completeExceptionally(e);` is called, the log will print in this place. https://github.com/apache/pulsar/pull/18858/files#diff-191ed1df4f5804c8fd6cdaf909cca01d071a110c3e701bdc98352f99e7a92e8eR534
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050316454
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
Sorry, I think we need to revert this next back to `ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());`
`Free->Assigned(Non-Transfer)` is for the state when broker A trying to assign bundle ownership to broker B when there is no ownership, `Free`. (Since broker A assumes the broker B is online, `Assigned-> Owned` change requires broker B to to acknowledge `Assigned` by `Owned` broadcast.
As we defined in the `ServiceUnitState` state diagram, I think the children's state can be `Owned` initially (does not need to be `Assigned`, as the parent broker(the same broker) always owns the children).
My initial concern was possible racing conditions if the broker does not wait until it receives its `Owned` message in its table-view, before updating the bundle partitions in ZK(case 2 below). I think this waiting seems redundant but still safe to have it.
Let's review the racing conditions here.
case 1: when lookup parent(before updating zk bundle partitions) bundle before/after the broker receives children `Owned` in its table-view
=> happy case: the broker immediately returns the parent bundle lookup result(the same broker).
case 2: when lookup children(after updating zk bundle partitions) bundles before the broker receives children `Owned` in its table-view
=> unhappy case: the broker runs the bundle assignment logic and probably tries to assign the ownership to another broker. However, this `Owned->Assigned(Non-Transfer)` transition will be filtered out as invalid. Eventually, the children bundle lookup will either timeout or correctly return the original owner broker when the first `Owned` msg arrives. Still, it will be safe to avoid this case if we can delay zk bundle partition updates til `getOwner(children bundles).join(timeout ).get() == this.broker.`
case 3: when lookup children(after updating zk bundle partitions) after the broker receives children `Owned` in its table-view
=> happy case: the broker immediately returns the lookup result.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1060800691
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -411,9 +421,15 @@ private void handleReleaseEvent(String serviceUnit, ServiceUnitStateData data) {
private void handleSplitEvent(String serviceUnit, ServiceUnitStateData data) {
if (isTargetBroker(data.broker())) {
- splitServiceUnit(serviceUnit)
- .thenCompose(__ -> tombstoneAsync(serviceUnit))
- .whenComplete((__, e) -> log(e, serviceUnit, data, null));
+ splitServiceUnit(serviceUnit, data)
+ .whenComplete((__, e) -> {
+ if (e != null) {
+ // When has exception, change the bundle state back to Splitting -> Owned .
+ pubAsync(serviceUnit, new ServiceUnitStateData(Owned, data.broker(), data.sourceBroker()));
Review Comment:
I am sorry. I thought about this again, and in fact, this revert `Splitting -> Owned` can cause an inconsistent state if the exception is thrown after the children partitions are updated in the metadata store or children bundles are created.
Let's keep the exception-handling logic simple (do not revert the state here). Currently, the ServiceUnitStateChannel recovery logic will automatically tombstone the ownerships of any in-flight state service units, which I think is enough.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] gaoran10 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "gaoran10 (via GitHub)" <gi...@apache.org>.
gaoran10 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1097337427
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -512,25 +521,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
Review Comment:
Ok, thanks for your explanation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#issuecomment-1384843710
> Hmmm, I think the right solution is to improve the NamespaceService to decide to write to Zookeeper or service unit channel? We also have Admin API to split the bundle, it will be a problem if the load manager write the bundle split to service unit state channel, but the Admin API can't write to the service unit channel?
Yes, then we need to wait for this PR merge first. https://github.com/apache/pulsar/pull/19102
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048194596
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
+ namespaceService.updateNamespaceBundlesForPolicies(nsname, splitBundles.getLeft()))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles.getRight());
+ }).exceptionally(ex -> {
+ log.warn("Failed to update namespace policies [{}], NamespaceBundle: {} due to {}",
+ nsname, bundle.getBundleRange(), ex.getMessage());
+ updateFuture.completeExceptionally(ex);
+ return null;
+ })).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
Review Comment:
The `namespaceService.updateNamespaceBundles(nsname, targetNsBundle)` and `namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle)` might throw the `MetadataStoreException.BadVersionException`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1049184960
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
Yes, `Owned -> Assigned` is an invalid transition(without source broker, non-transfer), so the later `Assigned` will be ignored (conflict resolution).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1049172701
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
If we make the new children's bundle status as `assigned`, then we can make sure get the right result when doing a lookup request, right? Of course, it needs a compaction strategy.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050353049
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
How about we treat the state as `Assigned(Transfer)` -> `new ServiceUnitStateData(Assigned, data.broker(), data.broker());`?
I am unsure why we need to wait `getOwner(children bundles).join(timeout ).get() == this.broker`, because the wait can only ensure the current broker state, but if the client does lookup to another broker, it will still have a chance to assign the ownership to another broker, since the broker not received the `Owned ` message yet. If I understand it correctly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1100227854
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
+ && (counter.incrementAndGet() < NamespaceService.BUNDLE_SPLIT_RETRY_LIMIT)) {
+ pulsar.getExecutor().schedule(() -> splitServiceUnitOnceAndRetry(namespaceService, bundleFactory,
+ bundle, serviceUnit, data, counter, startTime, completionFuture), 100, MILLISECONDS);
+ } else if (ex instanceof IllegalArgumentException) {
Review Comment:
Good catch. It should use `FutureUtil.unwrapCompletionException(ex)` here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codelipenghui commented on pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
codelipenghui commented on PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#issuecomment-1384826597
> When the channel handles the split event, we should write the split bundle to zookeeper instead of ServiceUnitStateChannel,
@Demogorgon314 I think it's incorrect in the PR's description, right?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codelipenghui commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1104113538
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
Review Comment:
```suggestion
List<CompletableFuture<Void>> futures = new ArrayList<>(splitBundles.size());
```
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
Review Comment:
Is it better to introduce the new immutable lists to avoid making it safe to pass to the async method? It's a little confused, some are ArrayList, some are CopyOnWriteArrayList.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
Review Comment:
is it intentional to use thenAccept? It looks like a problem to call an async method in `thenAccept`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1104156984
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
Review Comment:
Because it has retry logic, so I have to use `completionFuture ` here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1049110098
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
I am not sure why we need a separate map here.
We can make a blocking call until getOwner(newBundles).join(timeout ).get() == this.broker -- this will ensure that this broker's table-view owns the new bundles for the lookup requests.
If timeout, I assume we can retry on this, and this retry should be idempotent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048277796
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
I have been thinking, should we maintain a map instead of a table view in `ServiceUnitStateChannelImpl`, so we can put the latest value to the local map when sending success the state message to the topic?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050377776
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
> I am unsure why we need to wait getOwner(children bundles).join(timeout ).get() == this.broker, because the wait can only ensure the current broker state, but if the client does lookup to another broker, it will still have a chance to assign the ownership to another broker, since the broker not received the Owned message yet. If I understand it correctly.
Your understanding is correct here. `getOwner(children bundles).join(timeout ).get() == this.broker,` cannot fully prevent the global racing condition here.
Even with `new ServiceUnitStateData(Assigned, data.broker(), data.broker())`, some of the brokers might not see this `Assigned` upon child bundle lookup requests.
We should rely on the conflict resolution here to fully resolve this race condition.
So, I am fine with the original below `next` without waiting, `getOwner(children bundles).join(timeout ).get() == this.broker`
`ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1050380220
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
Sure, in this case, using `Owned` will be better. We can use `Assigned` when splitting the bundle to a specific broker. I will push other PR to implement it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1097315839
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -512,25 +521,107 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
Review Comment:
Yes, we can also specify the new bundles and the destination broker `org.apache.pulsar.broker.loadbalance.extensions.models.Split#splitServiceUnitToDestBroker`. This feature will be implemented in another PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 closed pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 closed pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
URL: https://github.com/apache/pulsar/pull/18858
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048153388
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
Review Comment:
Good point!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1048228745
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
+ namespaceService.updateNamespaceBundlesForPolicies(nsname, splitBundles.getLeft()))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles.getRight());
+ }).exceptionally(ex -> {
+ log.warn("Failed to update namespace policies [{}], NamespaceBundle: {} due to {}",
+ nsname, bundle.getBundleRange(), ex.getMessage());
+ updateFuture.completeExceptionally(ex);
+ return null;
+ })).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
+ && (counter.incrementAndGet() < NamespaceService.BUNDLE_SPLIT_RETRY_LIMIT)) {
+ pulsar.getExecutor().schedule(() -> pulsar.getOrderedExecutor()
+ .execute(() -> splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle,
+ serviceUnit, data, counter, startTime, completionFuture)),
+ 100, MILLISECONDS);
+ } else if (ex instanceof IllegalArgumentException) {
+ completionFuture.completeExceptionally(ex);
+ } else {
+ // Retry enough, or meet other exception
+ String msg = format("Bundle: %s not success update nsBundles, counter %d, reason %s",
+ bundle.toString(), counter.get(), ex.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ }
+ return null;
+ });
+ }
+ private CompletableFuture<Pair<NamespaceBundles, List<NamespaceBundle>>> getSplitBoundary(NamespaceBundle bundle) {
Review Comment:
Yes, updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] heesung-sn commented on a diff in pull request #18858: [improve][broker] Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by GitBox <gi...@apache.org>.
heesung-sn commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1047958331
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
Review Comment:
In this case, do we need to change the bundle state back to `Splitting -> Owned`?
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelTest.java:
##########
@@ -566,7 +602,7 @@ public void handleBrokerDeletionEventTest()
assertEquals(0, followerCleanupJobs.size());
assertEquals(1, getCleanupMetric(leaderChannel, "totalCleanupCnt"));
assertEquals(1, getCleanupMetric(leaderChannel, "totalBrokerCleanupTombstoneCnt"));
- assertEquals(2, getCleanupMetric(leaderChannel, "totalServiceUnitCleanupTombstoneCnt"));
+ assertEquals(4, getCleanupMetric(leaderChannel, "totalServiceUnitCleanupTombstoneCnt"));
Review Comment:
why do we see +2 here?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
Review Comment:
splitBundles.left and right are not very clear. Can we assign vars with meaningful names for left and right for better readability?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
Review Comment:
Should we wait til getOwner(newBundles) == this.broker?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
+ namespaceService.updateNamespaceBundlesForPolicies(nsname, splitBundles.getLeft()))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles.getRight());
+ }).exceptionally(ex -> {
+ log.warn("Failed to update namespace policies [{}], NamespaceBundle: {} due to {}",
+ nsname, bundle.getBundleRange(), ex.getMessage());
+ updateFuture.completeExceptionally(ex);
+ return null;
+ })).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
Review Comment:
Where do we get this `MetadataStoreException.BadVersionException` from? it seems like we are not accessing the metadata store in this block.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
+ namespaceService.updateNamespaceBundlesForPolicies(nsname, splitBundles.getLeft()))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles.getRight());
+ }).exceptionally(ex -> {
+ log.warn("Failed to update namespace policies [{}], NamespaceBundle: {} due to {}",
+ nsname, bundle.getBundleRange(), ex.getMessage());
+ updateFuture.completeExceptionally(ex);
+ return null;
+ })).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
+ // Free the old bundle
+ tombstoneAsync(serviceUnit).thenRun(() -> {
+ // Update bundled_topic cache for load-report-generation
+ pulsar.getBrokerService().refreshTopicToStatsMaps(bundle);
+ // TODO: Update the load data immediately if needed.
+ completionFuture.complete(null);
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ log.info("Successfully split {} parent namespace-bundle to {} in {} ms", serviceUnit, r,
+ splitBundleTime);
+ }).exceptionally(e -> {
+ double splitBundleTime = TimeUnit.NANOSECONDS.toMillis((System.nanoTime() - startTime));
+ String msg = format("Failed to free bundle %s in %s ms, under namespace [%s] with error %s",
+ bundle.getNamespaceObject().toString(), splitBundleTime, bundle, e.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return null;
+ });
+ }).exceptionally(ex -> {
+ // Retry several times on BadVersion
+ if ((ex.getCause() instanceof MetadataStoreException.BadVersionException)
+ && (counter.incrementAndGet() < NamespaceService.BUNDLE_SPLIT_RETRY_LIMIT)) {
+ pulsar.getExecutor().schedule(() -> pulsar.getOrderedExecutor()
+ .execute(() -> splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle,
+ serviceUnit, data, counter, startTime, completionFuture)),
+ 100, MILLISECONDS);
+ } else if (ex instanceof IllegalArgumentException) {
+ completionFuture.completeExceptionally(ex);
+ } else {
+ // Retry enough, or meet other exception
+ String msg = format("Bundle: %s not success update nsBundles, counter %d, reason %s",
+ bundle.toString(), counter.get(), ex.getMessage());
+ completionFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ }
+ return null;
+ });
+ }
+ private CompletableFuture<Pair<NamespaceBundles, List<NamespaceBundle>>> getSplitBoundary(NamespaceBundle bundle) {
Review Comment:
Can we define this `getSplitBoundary` in NamespaceBundleFactory?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
Review Comment:
Can't we flatten these nested futures?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -510,25 +524,120 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ getSplitBoundary(bundle).thenAccept(splitBundles -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundles == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ for (NamespaceBundle sBundle : splitBundles.getRight()) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {}));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures).thenRun(() ->
+ namespaceService.updateNamespaceBundles(nsname, splitBundles.getLeft()).thenCompose(__ ->
+ namespaceService.updateNamespaceBundlesForPolicies(nsname, splitBundles.getLeft()))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles.getRight());
+ }).exceptionally(ex -> {
+ log.warn("Failed to update namespace policies [{}], NamespaceBundle: {} due to {}",
+ nsname, bundle.getBundleRange(), ex.getMessage());
+ updateFuture.completeExceptionally(ex);
+ return null;
+ })).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
Review Comment:
After flattening the futures, I think we can simplify these exceptionally blocks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] eolivelli merged pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "eolivelli (via GitHub)" <gi...@apache.org>.
eolivelli merged PR #18858:
URL: https://github.com/apache/pulsar/pull/18858
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "Demogorgon314 (via GitHub)" <gi...@apache.org>.
Demogorgon314 commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1104137493
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
+ for (NamespaceBundle sBundle : splitBundles) {
+ futures.add(pubAsync(sBundle.toString(), next).thenAccept(__ -> {
+ successPublishedBundles.add(sBundle);
+ }));
+ }
+ NamespaceName nsname = bundle.getNamespaceObject();
+ FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> namespaceService.updateNamespaceBundles(nsname, targetNsBundle))
+ .thenCompose(__ -> namespaceService.updateNamespaceBundlesForPolicies(nsname, targetNsBundle))
+ .thenRun(() -> {
+ bundleFactory.invalidateBundleCache(bundle.getNamespaceObject());
+ updateFuture.complete(splitBundles);
+ }).exceptionally(e -> {
+ // Clean the new bundle when has exception.
+ List<CompletableFuture<Void>> futureList = new ArrayList<>();
+ for (NamespaceBundle sBundle : successPublishedBundles) {
+ futureList.add(tombstoneAsync(sBundle.toString()).thenAccept(__ -> {}));
+ }
+ FutureUtil.waitForAll(futureList)
+ .whenComplete((__, ex) -> {
+ if (ex != null) {
+ log.warn("Clean new bundles failed,", ex);
+ }
+ updateFuture.completeExceptionally(e);
+ });
+ return null;
+ });
+ }).exceptionally(e -> {
+ updateFuture.completeExceptionally(e);
+ return null;
+ });
+
+ updateFuture.thenAccept(r -> {
Review Comment:
Yes, you can see when this method is completed, it will call `completionFuture.complete(null);`. Why is it a problem to call an async method in `thenAccept`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codelipenghui commented on a diff in pull request #18858: [improve][broker] PIP-192: Write the child ownership to `ServiceUnitStateChannel` instead of ZK when handling bundle split
Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on code in PR #18858:
URL: https://github.com/apache/pulsar/pull/18858#discussion_r1104153099
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -621,25 +631,108 @@ private CompletableFuture<Integer> closeServiceUnit(String serviceUnit) {
});
}
- private CompletableFuture<Void> splitServiceUnit(String serviceUnit) {
- // TODO: after the split we need to write the child ownerships to BSC instead of ZK.
+ private CompletableFuture<Void> splitServiceUnit(String serviceUnit, ServiceUnitStateData data) {
+ // Write the child ownerships to BSC.
long startTime = System.nanoTime();
- return pulsar.getNamespaceService()
- .splitAndOwnBundle(getNamespaceBundle(serviceUnit),
- false,
- NamespaceBundleSplitAlgorithm.of(pulsar.getConfig().getDefaultNamespaceBundleSplitAlgorithm()),
- null)
- .whenComplete((__, ex) -> {
- double splitBundleTime = TimeUnit.NANOSECONDS
- .toMillis((System.nanoTime() - startTime));
- if (ex == null) {
- log.info("Successfully split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime);
- } else {
- log.error("Failed to split {} namespace-bundle in {} ms",
- serviceUnit, splitBundleTime, ex);
- }
- });
+ NamespaceService namespaceService = pulsar.getNamespaceService();
+ NamespaceBundleFactory bundleFactory = namespaceService.getNamespaceBundleFactory();
+ NamespaceBundle bundle = getNamespaceBundle(serviceUnit);
+ CompletableFuture<Void> completionFuture = new CompletableFuture<>();
+ final AtomicInteger counter = new AtomicInteger(0);
+ this.splitServiceUnitOnceAndRetry(namespaceService, bundleFactory, bundle, serviceUnit, data,
+ counter, startTime, completionFuture);
+ return completionFuture;
+ }
+
+ @VisibleForTesting
+ protected void splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
+ NamespaceBundleFactory bundleFactory,
+ NamespaceBundle bundle,
+ String serviceUnit,
+ ServiceUnitStateData data,
+ AtomicInteger counter,
+ long startTime,
+ CompletableFuture<Void> completionFuture) {
+ CompletableFuture<List<NamespaceBundle>> updateFuture = new CompletableFuture<>();
+
+ pulsar.getNamespaceService().getSplitBoundary(bundle, null).thenAccept(splitBundlesPair -> {
+ // Split and updateNamespaceBundles. Update may fail because of concurrent write to Zookeeper.
+ if (splitBundlesPair == null) {
+ String msg = format("Bundle %s not found under namespace", serviceUnit);
+ updateFuture.completeExceptionally(new BrokerServiceException.ServiceUnitNotReadyException(msg));
+ return;
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ ServiceUnitStateData next = new ServiceUnitStateData(Owned, data.broker());
+ NamespaceBundles targetNsBundle = splitBundlesPair.getLeft();
+ List<NamespaceBundle> splitBundles = splitBundlesPair.getRight();
+ List<NamespaceBundle> successPublishedBundles = new CopyOnWriteArrayList<>();
Review Comment:
Ah, got it.
For the splitBundles, I think we can change to `List<NamespaceBundle> splitBundles = Collections.unmodifiableList(splitBundlesPair.getRight());` to avoid any chance to modify it in the async callback(another thread).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org