use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class TopicNameTest method topic.
@Test
void topic() {
try {
assertEquals(TopicName.get("property.namespace:topic").getNamespace(), "property.namespace");
fail("Should have thrown exception");
} catch (IllegalArgumentException e) {
// Expected
}
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getNamespace(), "property/cluster/namespace");
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getNamespace(), "property/cluster/namespace");
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic"), TopicName.get("persistent", "property", "cluster", "namespace", "topic"));
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").hashCode(), TopicName.get("persistent", "property", "cluster", "namespace", "topic").hashCode());
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").toString(), "persistent://property/cluster/namespace/topic");
assertFalse(TopicName.get("persistent://property/cluster/namespace/topic").equals("persistent://property/cluster/namespace/topic"));
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getDomain(), TopicDomain.persistent);
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getProperty(), "property");
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getCluster(), "cluster");
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getNamespacePortion(), "namespace");
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getNamespace(), "property/cluster/namespace");
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getLocalName(), "topic");
try {
TopicName.get("property.namespace:my-topic").getDomain();
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property.namespace:my-topic").getProperty();
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property.namespace:my-topic").getCluster();
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property.namespace:my-topic").getNamespacePortion();
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property.namespace:my-topic").getLocalName();
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property.namespace");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("invalid://property/cluster/namespace/topic");
fail("Should have raied exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property/cluster/namespace/topic");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("persistent:///cluster/namespace/mydest-1");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("persistent://pulsar//namespace/mydest-1");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("persistent://pulsar/cluster//mydest-1");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("persistent://pulsar/cluster/namespace/");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("://pulsar/cluster/namespace/");
fail("Should have raised exception");
} catch (IllegalArgumentException e) {
// Ok
}
assertEquals(TopicName.get("persistent://property/cluster/namespace/topic").getPersistenceNamingEncoding(), "property/cluster/namespace/persistent/topic");
try {
TopicName.get("property.namespace");
fail("Should have raied exception");
} catch (IllegalArgumentException e) {
// Ok
}
try {
TopicName.get("property/cluster/namespace");
fail("Should have raied exception");
} catch (IllegalArgumentException e) {
// Ok
}
TopicName nameWithSlash = TopicName.get("persistent://property/cluster/namespace/ns-abc/table/1");
assertEquals(nameWithSlash.getEncodedLocalName(), Codec.encode("ns-abc/table/1"));
TopicName nameEndingInSlash = TopicName.get("persistent://property/cluster/namespace/ns-abc/table/1/");
assertEquals(nameEndingInSlash.getEncodedLocalName(), Codec.encode("ns-abc/table/1/"));
TopicName nameWithTwoSlashes = TopicName.get("persistent://property/cluster/namespace//ns-abc//table//1//");
assertEquals(nameWithTwoSlashes.getEncodedLocalName(), Codec.encode("/ns-abc//table//1//"));
TopicName nameWithRandomCharacters = TopicName.get("persistent://property/cluster/namespace/$#3rpa/table/1");
assertEquals(nameWithRandomCharacters.getEncodedLocalName(), Codec.encode("$#3rpa/table/1"));
TopicName topicName = TopicName.get("persistent://myprop/mycolo/myns/mytopic");
assertEquals(topicName.getPartition(0).toString(), "persistent://myprop/mycolo/myns/mytopic-partition-0");
TopicName partitionedDn = TopicName.get("persistent://myprop/mycolo/myns/mytopic").getPartition(2);
assertEquals(partitionedDn.getPartitionIndex(), 2);
assertEquals(topicName.getPartitionIndex(), -1);
assertEquals(TopicName.getPartitionIndex("persistent://myprop/mycolo/myns/mytopic-partition-4"), 4);
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class TopicNameTest method testDecodeEncode.
@Test
public void testDecodeEncode() throws Exception {
String encodedName = "a%3Aen-in_in_business_content_item_20150312173022_https%5C%3A%2F%2Fin.news.example.com%2Fr";
String rawName = "a:en-in_in_business_content_item_20150312173022_https\\://in.news.example.com/r";
assertEquals(Codec.decode(encodedName), rawName);
assertEquals(Codec.encode(rawName), encodedName);
String topicName = "persistent://prop/colo/ns/" + rawName;
TopicName name = TopicName.get(topicName);
assertEquals(name.getLocalName(), rawName);
assertEquals(name.getEncodedLocalName(), encodedName);
assertEquals(name.getPersistenceNamingEncoding(), "prop/colo/ns/persistent/" + encodedName);
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class PersistentTopicsBase method internalDeletePartitionedTopic.
protected void internalDeletePartitionedTopic(boolean authoritative) {
validateAdminAccessOnProperty(topicName.getProperty());
PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
int numPartitions = partitionMetadata.partitions;
if (numPartitions > 0) {
final CompletableFuture<Void> future = new CompletableFuture<>();
final AtomicInteger count = new AtomicInteger(numPartitions);
try {
for (int i = 0; i < numPartitions; i++) {
TopicName topicNamePartition = topicName.getPartition(i);
pulsar().getAdminClient().persistentTopics().deleteAsync(topicNamePartition.toString()).whenComplete((r, ex) -> {
if (ex != null) {
if (ex instanceof NotFoundException) {
// partition is failed to be deleted
if (log.isDebugEnabled()) {
log.debug("[{}] Partition not found: {}", clientAppId(), topicNamePartition);
}
} else {
future.completeExceptionally(ex);
log.error("[{}] Failed to delete partition {}", clientAppId(), topicNamePartition, ex);
return;
}
} else {
log.info("[{}] Deleted partition {}", clientAppId(), topicNamePartition);
}
if (count.decrementAndGet() == 0) {
future.complete(null);
}
});
}
future.get();
} catch (Exception e) {
Throwable t = e.getCause();
if (t instanceof PreconditionFailedException) {
throw new RestException(Status.PRECONDITION_FAILED, "Topic has active producers/subscriptions");
} else {
throw new RestException(t);
}
}
}
// Only tries to delete the znode for partitioned topic when all its partitions are successfully deleted
String path = path(PARTITIONED_TOPIC_PATH_ZNODE, namespaceName.toString(), domain(), topicName.getEncodedLocalName());
try {
globalZk().delete(path, -1);
globalZkCache().invalidate(path);
// we wait for the data to be synced in all quorums and the observers
Thread.sleep(PARTITIONED_TOPIC_WAIT_SYNC_TIME_MS);
log.info("[{}] Deleted partitioned topic {}", clientAppId(), topicName);
} catch (KeeperException.NoNodeException nne) {
throw new RestException(Status.NOT_FOUND, "Partitioned topic does not exist");
} catch (Exception e) {
log.error("[{}] Failed to delete partitioned topic {}", clientAppId(), topicName, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class PulsarService method loadNamespaceTopics.
/**
* Load all the topics contained in a namespace
*
* @param bundle
* <code>NamespaceBundle</code> to identify the service unit
* @throws Exception
*/
public void loadNamespaceTopics(NamespaceBundle bundle) {
executor.submit(() -> {
LOG.info("Loading all topics on bundle: {}", bundle);
NamespaceName nsName = bundle.getNamespaceObject();
List<CompletableFuture<Topic>> persistentTopics = Lists.newArrayList();
long topicLoadStart = System.nanoTime();
for (String topic : getNamespaceService().getListOfTopics(nsName)) {
try {
TopicName topicName = TopicName.get(topic);
if (bundle.includes(topicName)) {
CompletableFuture<Topic> future = brokerService.getTopic(topic);
if (future != null) {
persistentTopics.add(future);
}
}
} catch (Throwable t) {
LOG.warn("Failed to preload topic {}", topic, t);
}
}
if (!persistentTopics.isEmpty()) {
FutureUtil.waitForAll(persistentTopics).thenRun(() -> {
double topicLoadTimeSeconds = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - topicLoadStart) / 1000.0;
LOG.info("Loaded {} topics on {} -- time taken: {} seconds", persistentTopics.size(), bundle, topicLoadTimeSeconds);
});
}
return null;
});
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class NonPersistentTopics method getListFromBundle.
@GET
@Path("/{property}/{cluster}/{namespace}/{bundle}")
@ApiOperation(value = "Get the list of non-persistent topics under a namespace bundle.", response = String.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Namespace doesn't exist") })
public List<String> getListFromBundle(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("bundle") String bundleRange) {
log.info("[{}] list of topics on namespace bundle {}/{}/{}/{}", clientAppId(), property, cluster, namespace, bundleRange);
validateAdminAccessOnProperty(property);
Policies policies = getNamespacePolicies(property, cluster, namespace);
if (!cluster.equals(Constants.GLOBAL_CLUSTER)) {
validateClusterOwnership(cluster);
validateClusterForProperty(property, cluster);
} else {
// check cluster ownership for a given global namespace: redirect if peer-cluster owns it
validateGlobalNamespaceOwnership(NamespaceName.get(property, cluster, namespace));
}
NamespaceName fqnn = NamespaceName.get(property, cluster, namespace);
if (!isBundleOwnedByAnyBroker(fqnn, policies.bundles, bundleRange)) {
log.info("[{}] Namespace bundle is not owned by any broker {}/{}/{}/{}", clientAppId(), property, cluster, namespace, bundleRange);
return null;
}
NamespaceBundle nsBundle = validateNamespaceBundleOwnership(fqnn, policies.bundles, bundleRange, true, true);
try {
final List<String> topicList = Lists.newArrayList();
pulsar().getBrokerService().getTopics().forEach((name, topicFuture) -> {
TopicName topicName = TopicName.get(name);
if (nsBundle.includes(topicName)) {
topicList.add(name);
}
});
return topicList;
} catch (Exception e) {
log.error("[{}] Failed to unload namespace bundle {}/{}", clientAppId(), fqnn.toString(), bundleRange, e);
throw new RestException(e);
}
}
Aggregations