Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[improve] [client] improve the class GetTopicsResult #22766

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove the method lookupService.getExistsPartitions
  • Loading branch information
poorbarcode committed May 29, 2024
commit 473348e04bb41bed9ad0d011c8f7b850d60ca325

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@
package org.apache.pulsar.client.impl;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode;
import org.apache.pulsar.common.lookup.GetTopicsResult;
import org.apache.pulsar.common.naming.NamespaceName;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
import org.apache.pulsar.common.schema.SchemaInfo;
import org.apache.pulsar.common.util.FutureUtil;

/**
* Provides lookup service to find broker which serves given topic. It helps to
Expand Down Expand Up @@ -111,51 +105,4 @@ public interface LookupService extends AutoCloseable {
*/
CompletableFuture<GetTopicsResult> getTopicsUnderNamespace(NamespaceName namespace, Mode mode,
String topicPattern, String topicsHash);

/**
* Get the exists partitions of a partitioned topic, the result does not contain the partitions which has not been
* created yet(in other words, the partitions that do not exist in the response of "pulsar-admin topics list").
* @return sorted partitions list if it is a partitioned topic; @return an empty list if it is a non-partitioned
* topic.
*/
default CompletableFuture<List<Integer>> getExistsPartitions(String topic) {
TopicName topicName = TopicName.get(topic);
if (!topicName.isPersistent()) {
return FutureUtil.failedFuture(new IllegalArgumentException("The API LookupService.getExistsPartitions does"
+ " not support non-persistent topic yet."));
}
return getTopicsUnderNamespace(topicName.getNamespaceObject(),
topicName.isPersistent() ? Mode.PERSISTENT : Mode.NON_PERSISTENT,
"^" + topicName.getPartitionedTopicName() + "$",
null).thenApply(getTopicsResult -> {
if (getTopicsResult.getNonPartitionedOrPartitionTopics() == null
|| getTopicsResult.getNonPartitionedOrPartitionTopics().isEmpty()) {
return Collections.emptyList();
}
// If broker version is less than "2.11.x", it does not support broker-side pattern check, so append
// a client-side pattern check.
// If lookup service is typed HttpLookupService, the HTTP API does not support broker-side pattern
// check yet, so append a client-side pattern check.
Predicate<String> clientSideFilter;
if (getTopicsResult.isFiltered()) {
clientSideFilter = __ -> true;
} else {
clientSideFilter =
tp -> Pattern.compile(TopicName.getPartitionPattern(topic)).matcher(tp).matches();
}
ArrayList<Integer> list = new ArrayList<>(getTopicsResult.getNonPartitionedOrPartitionTopics().size());
for (String partition : getTopicsResult.getNonPartitionedOrPartitionTopics()) {
int partitionIndex = TopicName.get(partition).getPartitionIndex();
if (partitionIndex < 0) {
// It is not a partition.
continue;
}
if (clientSideFilter.test(partition)) {
list.add(partitionIndex);
}
}
Collections.sort(list);
return list;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public static boolean isValid(String topic) {
}
}

public static String getPartitionPattern(String topic) {
return "^" + get(topic).getPartitionedTopicName().toString() + "-partition-[0-9]+$";
}

@SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
private TopicName(String completeTopicName) {
try {
Expand Down
Loading