use of org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException in project incubator-pulsar by apache.
the class PersistentTopicsBase method internalDeleteSubscription.
protected void internalDeleteSubscription(String subName, boolean authoritative) {
if (topicName.isGlobal()) {
validateGlobalNamespaceOwnership(namespaceName);
}
PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
if (partitionMetadata.partitions > 0) {
try {
for (int i = 0; i < partitionMetadata.partitions; i++) {
pulsar().getAdminClient().persistentTopics().deleteSubscription(topicName.getPartition(i).toString(), subName);
}
} catch (Exception e) {
if (e instanceof NotFoundException) {
throw new RestException(Status.NOT_FOUND, "Subscription not found");
} else if (e instanceof PreconditionFailedException) {
throw new RestException(Status.PRECONDITION_FAILED, "Subscription has active connected consumers");
} else {
log.error("[{}] Failed to delete subscription {} {}", clientAppId(), topicName, subName, e);
throw new RestException(e);
}
}
} else {
validateAdminOperationOnTopic(authoritative);
Topic topic = getTopicReference(topicName);
try {
Subscription sub = topic.getSubscription(subName);
checkNotNull(sub);
sub.delete().get();
log.info("[{}][{}] Deleted subscription {}", clientAppId(), topicName, subName);
} catch (Exception e) {
Throwable t = e.getCause();
if (e instanceof NullPointerException) {
throw new RestException(Status.NOT_FOUND, "Subscription not found");
} else if (t instanceof SubscriptionBusyException) {
throw new RestException(Status.PRECONDITION_FAILED, "Subscription has active connected consumers");
} else {
log.error("[{}] Failed to delete subscription {} {}", clientAppId(), topicName, subName, e);
throw new RestException(t);
}
}
}
}
use of org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException in project incubator-pulsar by apache.
the class AdminApiTest method persistentTopics.
@Test(dataProvider = "topicName")
public void persistentTopics(String topicName) throws Exception {
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
final String persistentTopicName = "persistent://prop-xyz/use/ns1/" + topicName;
// Force to create a topic
publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/" + topicName, 0);
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/" + topicName));
// create consumer and subscription
URL pulsarUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
PulsarClient client = PulsarClient.builder().serviceUrl(pulsarUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
Consumer<byte[]> consumer = client.newConsumer().topic(persistentTopicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
assertEquals(admin.persistentTopics().getSubscriptions(persistentTopicName), Lists.newArrayList("my-sub"));
publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/" + topicName, 10);
PersistentTopicStats topicStats = admin.persistentTopics().getStats(persistentTopicName);
assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
assertEquals(topicStats.subscriptions.get("my-sub").consumers.size(), 1);
assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10);
assertEquals(topicStats.publishers.size(), 0);
PersistentTopicInternalStats internalStats = admin.persistentTopics().getInternalStats(persistentTopicName);
assertEquals(internalStats.cursors.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(persistentTopicName, "my-sub", 3);
assertEquals(messages.size(), 3);
for (int i = 0; i < 3; i++) {
String expectedMessage = "message-" + i;
assertEquals(messages.get(i).getData(), expectedMessage.getBytes());
}
messages = admin.persistentTopics().peekMessages(persistentTopicName, "my-sub", 15);
assertEquals(messages.size(), 10);
for (int i = 0; i < 10; i++) {
String expectedMessage = "message-" + i;
assertEquals(messages.get(i).getData(), expectedMessage.getBytes());
}
admin.persistentTopics().skipMessages(persistentTopicName, "my-sub", 5);
topicStats = admin.persistentTopics().getStats(persistentTopicName);
assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 5);
admin.persistentTopics().skipAllMessages(persistentTopicName, "my-sub");
topicStats = admin.persistentTopics().getStats(persistentTopicName);
assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 0);
consumer.close();
client.close();
admin.persistentTopics().deleteSubscription(persistentTopicName, "my-sub");
assertEquals(admin.persistentTopics().getSubscriptions(persistentTopicName), Lists.newArrayList());
topicStats = admin.persistentTopics().getStats(persistentTopicName);
assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet());
assertEquals(topicStats.publishers.size(), 0);
try {
admin.persistentTopics().skipAllMessages(persistentTopicName, "my-sub");
} catch (NotFoundException e) {
}
admin.persistentTopics().delete(persistentTopicName);
try {
admin.persistentTopics().delete(persistentTopicName);
fail("Should have received 404");
} catch (NotFoundException e) {
}
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
}
use of org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException in project incubator-pulsar by apache.
the class AdminApiTest method namespaces.
@Test(invocationCount = 1)
public void namespaces() throws PulsarAdminException, PulsarServerException, Exception {
admin.clusters().createCluster("usw", new ClusterData());
PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use", "usw"));
admin.properties().updateProperty("prop-xyz", propertyAdmin);
assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns1").bundles, Policies.defaultBundle());
admin.namespaces().createNamespace("prop-xyz/use/ns2");
admin.namespaces().createNamespace("prop-xyz/use/ns3", 4);
assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns3").bundles.numBundles, 4);
assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns3").bundles.boundaries.size(), 5);
admin.namespaces().deleteNamespace("prop-xyz/use/ns3");
try {
admin.namespaces().createNamespace("non-existing/usw/ns1");
fail("Should not have passed");
} catch (NotFoundException e) {
// Ok
}
assertEquals(admin.namespaces().getNamespaces("prop-xyz"), Lists.newArrayList("prop-xyz/use/ns1", "prop-xyz/use/ns2"));
assertEquals(admin.namespaces().getNamespaces("prop-xyz", "use"), Lists.newArrayList("prop-xyz/use/ns1", "prop-xyz/use/ns2"));
try {
admin.namespaces().createNamespace("prop-xyz/usc/ns1");
fail("Should not have passed");
} catch (NotAuthorizedException e) {
// Ok, got the non authorized exception since usc cluster is not in the allowed clusters list.
}
admin.namespaces().grantPermissionOnNamespace("prop-xyz/use/ns1", "my-role", EnumSet.allOf(AuthAction.class));
Policies policies = new Policies();
policies.auth_policies.namespace_auth.put("my-role", EnumSet.allOf(AuthAction.class));
assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns1"), policies);
assertEquals(admin.namespaces().getPermissions("prop-xyz/use/ns1"), policies.auth_policies.namespace_auth);
assertEquals(admin.namespaces().getTopics("prop-xyz/use/ns1"), Lists.newArrayList());
admin.namespaces().revokePermissionsOnNamespace("prop-xyz/use/ns1", "my-role");
policies.auth_policies.namespace_auth.remove("my-role");
assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns1"), policies);
assertEquals(admin.namespaces().getPersistence("prop-xyz/use/ns1"), new PersistencePolicies(1, 1, 1, 0.0));
admin.namespaces().setPersistence("prop-xyz/use/ns1", new PersistencePolicies(3, 2, 1, 10.0));
assertEquals(admin.namespaces().getPersistence("prop-xyz/use/ns1"), new PersistencePolicies(3, 2, 1, 10.0));
// Force topic creation and namespace being loaded
Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://prop-xyz/use/ns1/my-topic").create();
producer.close();
admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/my-topic");
admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
NamespaceName ns = NamespaceName.get("prop-xyz/use/ns1");
// Now, w/ bundle policies, we will use default bundle
NamespaceBundle defaultBundle = bundleFactory.getFullBundle(ns);
int i = 0;
for (; i < 10; i++) {
Optional<NamespaceEphemeralData> data1 = pulsar.getNamespaceService().getOwnershipCache().getOwnerAsync(defaultBundle).get();
if (!data1.isPresent()) {
// Already unloaded
break;
}
LOG.info("Waiting for unload namespace {} to complete. Current service unit isDisabled: {}", defaultBundle, data1.get().isDisabled());
Thread.sleep(1000);
}
assertTrue(i < 10);
admin.namespaces().deleteNamespace("prop-xyz/use/ns1");
assertEquals(admin.namespaces().getNamespaces("prop-xyz", "use"), Lists.newArrayList("prop-xyz/use/ns2"));
try {
admin.namespaces().unload("prop-xyz/use/ns1");
fail("should have raised exception");
} catch (Exception e) {
// OK excepted
}
// Force topic creation and namespace being loaded
producer = pulsarClient.newProducer().topic("persistent://prop-xyz/use/ns2/my-topic").create();
producer.close();
admin.persistentTopics().delete("persistent://prop-xyz/use/ns2/my-topic");
// both unload and delete should succeed for ns2 on other broker with a redirect
// otheradmin.namespaces().unload("prop-xyz/use/ns2");
}
use of org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException in project flink by apache.
the class PulsarRuntimeOperator method deleteTopic.
/**
* Delete a Pulsar topic.
*
* @param topic The topic name.
*/
public void deleteTopic(String topic) {
String topicName = topicName(topic);
PartitionedTopicMetadata metadata;
try {
metadata = admin().topics().getPartitionedTopicMetadata(topicName);
} catch (NotFoundException e) {
// This topic doesn't exist. Just skip deletion.
return;
} catch (PulsarAdminException e) {
sneakyThrow(e);
return;
}
removeConsumers(topic);
removeProducers(topic);
if (metadata.partitions <= 0) {
sneakyAdmin(() -> admin().topics().delete(topicName));
} else {
sneakyAdmin(() -> admin().topics().deletePartitionedTopic(topicName));
}
}
use of org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException 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);
}
}
Aggregations