Search in sources :

Example 1 with NotFoundException

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);
            }
        }
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Subscription(org.apache.pulsar.broker.service.Subscription) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 2 with NotFoundException

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());
}
Also used : Message(org.apache.pulsar.client.api.Message) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with NotFoundException

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");
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) NamespaceEphemeralData(org.apache.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with NotFoundException

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));
    }
}
Also used : NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata)

Example 5 with NotFoundException

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);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) TopicName(org.apache.pulsar.common.naming.TopicName) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)8 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)7 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)6 Test (org.testng.annotations.Test)5 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)4 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)4 URL (java.net.URL)3 ConflictException (org.apache.pulsar.client.admin.PulsarAdminException.ConflictException)3 NotAuthorizedException (org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException)3 PartitionedTopicMetadata (org.apache.pulsar.common.partition.PartitionedTopicMetadata)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)2 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)2 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)2 RestException (org.apache.pulsar.broker.web.RestException)2 PulsarClient (org.apache.pulsar.client.api.PulsarClient)2 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)2