use of org.apache.pulsar.common.policies.data.TopicHashPositions in project pulsar by yahoo.
the class AdminApiTest method testNamespacesGetTopicHashPositions.
@Test
public void testNamespacesGetTopicHashPositions() throws Exception {
// Force to create a namespace with only one bundle and create a topic
final String namespace = "prop-xyz/ns-one-bundle";
final String topic = "persistent://" + namespace + "/topic";
final int topicPartitionNumber = 4;
Policies policies = new Policies();
policies.bundles = PoliciesUtil.getBundles(1);
admin.namespaces().createNamespace(namespace, policies);
admin.topics().createPartitionedTopic(topic, topicPartitionNumber);
admin.lookups().lookupPartitionedTopic(topic);
// check bundles and bundle boundaries
BundlesData bundleData = admin.namespaces().getBundles(namespace);
assertEquals(bundleData.getNumBundles(), 1);
assertEquals(bundleData.getBoundaries().size(), 2);
assertEquals(bundleData.getBoundaries().get(0), "0x00000000");
assertEquals(bundleData.getBoundaries().get(1), "0xffffffff");
// test get topic position for partitioned-topic name
String bundleRange = "0x00000000_0xffffffff";
TopicHashPositions topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, Collections.singletonList(topic));
assertEquals(topicHashPositions.getNamespace(), "prop-xyz/ns-one-bundle");
assertEquals(topicHashPositions.getBundle(), "0x00000000_0xffffffff");
assertEquals(topicHashPositions.getTopicHashPositions().size(), topicPartitionNumber);
final HashFunction hashFunction = Hashing.crc32();
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
// test get hash position for topic partition
List<String> partitions = new ArrayList<>();
partitions.add(topic + "-partition-0");
partitions.add(topic + "-partition-1");
partitions.add(topic + "-partition-2");
partitions.add(topic + "-partition-3");
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, partitions);
assertEquals(topicHashPositions.getNamespace(), "prop-xyz/ns-one-bundle");
assertEquals(topicHashPositions.getBundle(), "0x00000000_0xffffffff");
assertEquals(topicHashPositions.getTopicHashPositions().size(), topicPartitionNumber);
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
// test non-exist topic
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, Collections.singletonList(topic + "no-exist"));
assertEquals(topicHashPositions.getTopicHashPositions().size(), 0);
// test topics is null
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, null);
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
// test topics is empty
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, new ArrayList<>());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
}
use of org.apache.pulsar.common.policies.data.TopicHashPositions in project pulsar by yahoo.
the class NamespacesImpl method getTopicHashPositionsAsync.
@Override
public CompletableFuture<TopicHashPositions> getTopicHashPositionsAsync(String namespace, String bundle, List<String> topics) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, bundle, "topicHashPositions");
if (topics != null && topics.size() > 0) {
path = path.queryParam("topics", topics.stream().map(Codec::encode).toArray());
}
final CompletableFuture<TopicHashPositions> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<TopicHashPositions>() {
@Override
public void completed(TopicHashPositions topicHashPositions) {
future.complete(topicHashPositions);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.TopicHashPositions in project incubator-pulsar by apache.
the class NamespacesBase method internalGetTopicHashPositions.
protected void internalGetTopicHashPositions(AsyncResponse asyncResponse, String bundleRange, List<String> topics) {
if (log.isDebugEnabled()) {
log.debug("[{}] Getting hash position for topic list {}, bundle {}", clientAppId(), topics, bundleRange);
}
validateNamespacePolicyOperation(namespaceName, PolicyName.PERSISTENCE, PolicyOperation.READ);
Policies policies = getNamespacePolicies(namespaceName);
NamespaceBundle bundle = validateNamespaceBundleOwnership(namespaceName, policies.bundles, bundleRange, false, true);
pulsar().getNamespaceService().getOwnedTopicListForNamespaceBundle(bundle).whenComplete((allTopicsInThisBundle, throwable) -> {
if (throwable != null) {
log.error("[{}] {} Failed to get topic list for bundle {}.", clientAppId(), namespaceName, bundle);
asyncResponse.resume(new RestException(throwable));
}
// if topics is empty, return all topics' hash position in this bundle
Map<String, Long> topicHashPositions = new HashMap<>();
if (topics == null || topics.size() == 0) {
allTopicsInThisBundle.forEach(t -> {
topicHashPositions.put(t, pulsar().getNamespaceService().getNamespaceBundleFactory().getLongHashCode(t));
});
} else {
for (String topic : topics.stream().map(Codec::decode).collect(Collectors.toList())) {
TopicName topicName = TopicName.get(topic);
// partitioned topic
if (topicName.getPartitionIndex() == -1) {
allTopicsInThisBundle.stream().filter(t -> TopicName.get(t).getPartitionedTopicName().equals(TopicName.get(topic).getPartitionedTopicName())).forEach(partition -> {
topicHashPositions.put(partition, pulsar().getNamespaceService().getNamespaceBundleFactory().getLongHashCode(partition));
});
} else {
// topic partition
if (allTopicsInThisBundle.contains(topicName.toString())) {
topicHashPositions.put(topic, pulsar().getNamespaceService().getNamespaceBundleFactory().getLongHashCode(topic));
}
}
}
}
asyncResponse.resume(new TopicHashPositions(namespaceName.toString(), bundleRange, topicHashPositions));
});
}
use of org.apache.pulsar.common.policies.data.TopicHashPositions in project incubator-pulsar by apache.
the class NamespacesImpl method getTopicHashPositionsAsync.
@Override
public CompletableFuture<TopicHashPositions> getTopicHashPositionsAsync(String namespace, String bundle, List<String> topics) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, bundle, "topicHashPositions");
if (topics != null && topics.size() > 0) {
path = path.queryParam("topics", topics.stream().map(Codec::encode).toArray());
}
final CompletableFuture<TopicHashPositions> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<TopicHashPositions>() {
@Override
public void completed(TopicHashPositions topicHashPositions) {
future.complete(topicHashPositions);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.TopicHashPositions in project incubator-pulsar by apache.
the class AdminApiTest method testNamespacesGetTopicHashPositions.
@Test
public void testNamespacesGetTopicHashPositions() throws Exception {
// Force to create a namespace with only one bundle and create a topic
final String namespace = "prop-xyz/ns-one-bundle";
final String topic = "persistent://" + namespace + "/topic";
final int topicPartitionNumber = 4;
Policies policies = new Policies();
policies.bundles = PoliciesUtil.getBundles(1);
admin.namespaces().createNamespace(namespace, policies);
admin.topics().createPartitionedTopic(topic, topicPartitionNumber);
admin.lookups().lookupPartitionedTopic(topic);
// check bundles and bundle boundaries
BundlesData bundleData = admin.namespaces().getBundles(namespace);
assertEquals(bundleData.getNumBundles(), 1);
assertEquals(bundleData.getBoundaries().size(), 2);
assertEquals(bundleData.getBoundaries().get(0), "0x00000000");
assertEquals(bundleData.getBoundaries().get(1), "0xffffffff");
// test get topic position for partitioned-topic name
String bundleRange = "0x00000000_0xffffffff";
TopicHashPositions topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, Collections.singletonList(topic));
assertEquals(topicHashPositions.getNamespace(), "prop-xyz/ns-one-bundle");
assertEquals(topicHashPositions.getBundle(), "0x00000000_0xffffffff");
assertEquals(topicHashPositions.getTopicHashPositions().size(), topicPartitionNumber);
final HashFunction hashFunction = Hashing.crc32();
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
// test get hash position for topic partition
List<String> partitions = new ArrayList<>();
partitions.add(topic + "-partition-0");
partitions.add(topic + "-partition-1");
partitions.add(topic + "-partition-2");
partitions.add(topic + "-partition-3");
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, partitions);
assertEquals(topicHashPositions.getNamespace(), "prop-xyz/ns-one-bundle");
assertEquals(topicHashPositions.getBundle(), "0x00000000_0xffffffff");
assertEquals(topicHashPositions.getTopicHashPositions().size(), topicPartitionNumber);
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
// test non-exist topic
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, Collections.singletonList(topic + "no-exist"));
assertEquals(topicHashPositions.getTopicHashPositions().size(), 0);
// test topics is null
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, null);
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
// test topics is empty
topicHashPositions = admin.namespaces().getTopicHashPositions(namespace, bundleRange, new ArrayList<>());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-0"), hashFunction.hashString(topic + "-partition-0", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-1"), hashFunction.hashString(topic + "-partition-1", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-2"), hashFunction.hashString(topic + "-partition-2", Charsets.UTF_8).padToLong());
assertEquals((long) topicHashPositions.getTopicHashPositions().get(topic + "-partition-3"), hashFunction.hashString(topic + "-partition-3", Charsets.UTF_8).padToLong());
}
Aggregations