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/01/21 03:05:55 UTC
[GitHub] [pulsar] codelipenghui commented on a change in pull request #13846: [Broker] Fix call sync method in async rest api for internalGetSubscriptions
codelipenghui commented on a change in pull request #13846:
URL: https://github.com/apache/pulsar/pull/13846#discussion_r789316379
##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
##########
@@ -1044,72 +1045,88 @@ protected void internalDeleteTopic(boolean authoritative, boolean deleteSchema)
}
protected void internalGetSubscriptions(AsyncResponse asyncResponse, boolean authoritative) {
+ CompletableFuture<Void> future;
if (topicName.isGlobal()) {
- try {
- validateGlobalNamespaceOwnership(namespaceName);
- } catch (Exception e) {
- log.error("[{}] Failed to get subscriptions for topic {}", clientAppId(), topicName, e);
- resumeAsyncResponseExceptionally(asyncResponse, e);
- return;
- }
- }
-
- validateTopicOwnership(topicName, authoritative);
-
- // If the topic name is a partition name, no need to get partition topic metadata again
- if (topicName.isPartitioned()) {
- internalGetSubscriptionsForNonPartitionedTopic(asyncResponse, authoritative);
+ future = validateGlobalNamespaceOwnershipAsync(namespaceName);
} else {
- getPartitionedTopicMetadataAsync(topicName, authoritative,
- false).thenAccept(partitionMetadata -> {
- if (partitionMetadata.partitions > 0) {
- try {
- final Set<String> subscriptions = Sets.newConcurrentHashSet();
- final List<CompletableFuture<Object>> subscriptionFutures = Lists.newArrayList();
- if (topicName.getDomain() == TopicDomain.persistent) {
- final Map<Integer, CompletableFuture<Boolean>> existsFutures = Maps.newConcurrentMap();
- for (int i = 0; i < partitionMetadata.partitions; i++) {
- existsFutures.put(i, topicResources().persistentTopicExists(topicName.getPartition(i)));
- }
- FutureUtil.waitForAll(Lists.newArrayList(existsFutures.values())).thenApply(__ ->
- existsFutures.entrySet().stream().filter(e -> e.getValue().join())
- .map(item -> topicName.getPartition(item.getKey()).toString())
- .collect(Collectors.toList())
- ).thenAccept(topics -> {
- if (log.isDebugEnabled()) {
- log.debug("activeTopics : {}", topics);
- }
- topics.forEach(topic -> {
- try {
- CompletableFuture<List<String>> subscriptionsAsync = pulsar().getAdminClient()
- .topics().getSubscriptionsAsync(topic);
- subscriptionFutures.add(subscriptionsAsync.thenApply(subscriptions::addAll));
- } catch (PulsarServerException e) {
- throw new RestException(e);
+ future = CompletableFuture.completedFuture(null);
+ }
+ future.thenAccept(__ -> validateTopicOwnershipAsync(topicName, authoritative))
+ .thenAccept(unused -> {
+ // If the topic name is a partition name, no need to get partition topic metadata again
+ if (topicName.isPartitioned()) {
+ internalGetSubscriptionsForNonPartitionedTopic(asyncResponse, authoritative);
+ } else {
+ getPartitionedTopicMetadataAsync(topicName, authoritative, false)
+ .thenAccept(partitionMetadata -> {
+ if (partitionMetadata.partitions > 0) {
+ try {
+ final Set<String> subscriptions =
+ Collections.newSetFromMap(
+ new ConcurrentHashMap<>(partitionMetadata.partitions));
+ final List<CompletableFuture<Object>> subscriptionFutures = Lists.newArrayList();
+ if (topicName.getDomain() == TopicDomain.persistent) {
+ final Map<Integer, CompletableFuture<Boolean>> existsFutures =
+ new ConcurrentHashMap<>(partitionMetadata.partitions);
+ for (int i = 0; i < partitionMetadata.partitions; i++) {
+ existsFutures.put(i,
+ topicResources().persistentTopicExists(topicName.getPartition(i)));
+ }
+ FutureUtil.waitForAll(Lists.newArrayList(existsFutures.values()))
+ .thenApply(__ ->
+ existsFutures.entrySet().stream().filter(e -> e.getValue().join())
+ .map(item -> topicName.getPartition(item.getKey()).toString())
+ .collect(Collectors.toList())
+ ).thenAccept(topics -> {
+ if (log.isDebugEnabled()) {
+ log.debug("activeTopics : {}", topics);
+ }
+ topics.forEach(topic -> {
+ try {
+ CompletableFuture<List<String>> subscriptionsAsync = pulsar()
+ .getAdminClient()
+ .topics().getSubscriptionsAsync(topic);
+ subscriptionFutures.add(subscriptionsAsync
+ .thenApply(subscriptions::addAll));
+ } catch (PulsarServerException e) {
+ throw new RestException(e);
+ }
+ });
+ }).thenAccept(__ -> resumeAsyncResponse(asyncResponse,
+ subscriptions, subscriptionFutures));
+ } else {
+ for (int i = 0; i < partitionMetadata.partitions; i++) {
+ CompletableFuture<List<String>> subscriptionsAsync = pulsar()
+ .getAdminClient().topics()
+ .getSubscriptionsAsync(topicName.getPartition(i).toString());
+ subscriptionFutures.add(subscriptionsAsync
+ .thenApply(subscriptions::addAll));
+ }
+ resumeAsyncResponse(asyncResponse, subscriptions, subscriptionFutures);
Review comment:
Interesting, we have a `resumeAsyncResponse` method but looks like can only be used by list subscriptions.
--
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