Search in sources :

Example 1 with ConflictException

use of org.apache.pulsar.client.admin.PulsarAdminException.ConflictException in project incubator-pulsar by apache.

the class WebServiceTest method setupEnv.

private void setupEnv(boolean enableFilter, String minApiVersion, boolean allowUnversionedClients, boolean enableTls, boolean enableAuth, boolean allowInsecure) throws Exception {
    Set<String> providers = new HashSet<>();
    providers.add("org.apache.pulsar.broker.authentication.AuthenticationProviderTls");
    Set<String> roles = new HashSet<>();
    roles.add("client");
    ServiceConfiguration config = new ServiceConfiguration();
    config.setAdvertisedAddress("localhost");
    config.setWebServicePort(BROKER_WEBSERVICE_PORT);
    config.setWebServicePortTls(BROKER_WEBSERVICE_PORT_TLS);
    config.setClientLibraryVersionCheckEnabled(enableFilter);
    config.setAuthenticationEnabled(enableAuth);
    config.setAuthenticationProviders(providers);
    config.setAuthorizationEnabled(false);
    config.setSuperUserRoles(roles);
    config.setTlsEnabled(enableTls);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config.setTlsAllowInsecureConnection(allowInsecure);
    config.setTlsTrustCertsFilePath(allowInsecure ? "" : TLS_CLIENT_CERT_FILE_PATH);
    config.setClusterName("local");
    // TLS certificate expects localhost
    config.setAdvertisedAddress("localhost");
    config.setZookeeperServers("localhost:2181");
    pulsar = spy(new PulsarService(config));
    doReturn(new MockedZooKeeperClientFactoryImpl()).when(pulsar).getZooKeeperClientFactory();
    doReturn(new MockedBookKeeperClientFactory()).when(pulsar).getBookKeeperClientFactory();
    pulsar.start();
    try {
        pulsar.getZkClient().delete("/minApiVersion", -1);
    } catch (Exception ex) {
    }
    pulsar.getZkClient().create("/minApiVersion", minApiVersion.getBytes(), null, CreateMode.PERSISTENT);
    String serviceUrl = BROKER_URL_BASE;
    ClientConfiguration clientConfig = new ClientConfiguration();
    if (enableTls && enableAuth) {
        serviceUrl = BROKER_URL_BASE_TLS;
        Map<String, String> authParams = new HashMap<>();
        authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
        authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
        Authentication auth = new AuthenticationTls();
        auth.configure(authParams);
        clientConfig.setAuthentication(auth);
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(true);
    }
    PulsarAdmin pulsarAdmin = new PulsarAdmin(new URL(serviceUrl), clientConfig);
    try {
        pulsarAdmin.clusters().createCluster(config.getClusterName(), new ClusterData(pulsar.getWebServiceAddress()));
    } catch (ConflictException ce) {
    // This is OK.
    } finally {
        pulsarAdmin.close();
    }
}
Also used : MockedBookKeeperClientFactory(org.apache.pulsar.broker.MockedBookKeeperClientFactory) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) HashMap(java.util.HashMap) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) URL(java.net.URL) MockedZooKeeperClientFactoryImpl(org.apache.pulsar.zookeeper.MockedZooKeeperClientFactoryImpl) AuthenticationTls(org.apache.pulsar.client.impl.auth.AuthenticationTls) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) Authentication(org.apache.pulsar.client.api.Authentication) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration) HashSet(java.util.HashSet)

Example 2 with ConflictException

use of org.apache.pulsar.client.admin.PulsarAdminException.ConflictException in project incubator-pulsar by apache.

the class CreateSubscriptionTest method createSubscriptionSingleTopic.

@Test
public void createSubscriptionSingleTopic() throws Exception {
    String topic = "persistent://prop-xyz/use/ns1/my-topic";
    admin.persistentTopics().createSubscription(topic, "sub-1", MessageId.latest);
    // Create should fail if the subscription already exists
    try {
        admin.persistentTopics().createSubscription(topic, "sub-1", MessageId.latest);
        fail("Should have failed");
    } catch (ConflictException e) {
        assertEquals(((ClientErrorException) e.getCause()).getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    assertEquals(admin.persistentTopics().getSubscriptions(topic), Lists.newArrayList("sub-1"));
    Producer<byte[]> p1 = pulsarClient.newProducer().topic(topic).create();
    p1.send("test-1".getBytes());
    p1.send("test-2".getBytes());
    MessageId m3 = p1.send("test-3".getBytes());
    assertEquals(admin.persistentTopics().getStats(topic).subscriptions.get("sub-1").msgBacklog, 3);
    admin.persistentTopics().createSubscription(topic, "sub-2", MessageId.latest);
    assertEquals(admin.persistentTopics().getStats(topic).subscriptions.get("sub-2").msgBacklog, 0);
    admin.persistentTopics().createSubscription(topic, "sub-3", MessageId.earliest);
    assertEquals(admin.persistentTopics().getStats(topic).subscriptions.get("sub-3").msgBacklog, 3);
    admin.persistentTopics().createSubscription(topic, "sub-5", m3);
    assertEquals(admin.persistentTopics().getStats(topic).subscriptions.get("sub-5").msgBacklog, 1);
}
Also used : ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with ConflictException

use of org.apache.pulsar.client.admin.PulsarAdminException.ConflictException in project incubator-pulsar by apache.

the class PulsarBrokerStatsClientTest method testServiceException.

@Test
public void testServiceException() throws Exception {
    URL url = new URL("http://localhost:15000");
    PulsarAdmin admin = new PulsarAdmin(url, (Authentication) null);
    BrokerStatsImpl client = (BrokerStatsImpl) spy(admin.brokerStats());
    try {
        client.getLoadReport();
    } catch (PulsarAdminException e) {
    // Ok
    }
    try {
        client.getPendingBookieOpsStats();
    } catch (PulsarAdminException e) {
    // Ok
    }
    try {
        client.getBrokerResourceAvailability("prop/cluster/ns");
    } catch (PulsarAdminException e) {
    // Ok
    }
    assertTrue(client.getApiException(new ClientErrorException(403)) instanceof NotAuthorizedException);
    assertTrue(client.getApiException(new ClientErrorException(404)) instanceof NotFoundException);
    assertTrue(client.getApiException(new ClientErrorException(409)) instanceof ConflictException);
    assertTrue(client.getApiException(new ClientErrorException(412)) instanceof PreconditionFailedException);
    assertTrue(client.getApiException(new ClientErrorException(400)) instanceof PulsarAdminException);
    assertTrue(client.getApiException(new ServerErrorException(500)) instanceof ServerSideErrorException);
    assertTrue(client.getApiException(new ServerErrorException(503)) instanceof PulsarAdminException);
    log.info("Client: ", client);
    admin.close();
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) BrokerStatsImpl(org.apache.pulsar.client.admin.internal.BrokerStatsImpl) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) ServerSideErrorException(org.apache.pulsar.client.admin.PulsarAdminException.ServerSideErrorException) ClientErrorException(javax.ws.rs.ClientErrorException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) ServerErrorException(javax.ws.rs.ServerErrorException) URL(java.net.URL) Test(org.testng.annotations.Test)

Example 4 with ConflictException

use of org.apache.pulsar.client.admin.PulsarAdminException.ConflictException in project incubator-pulsar by apache.

the class AdminApiTest method partitionedTopics.

@Test(dataProvider = "topicName")
public void partitionedTopics(String topicName) throws Exception {
    assertEquals(admin.persistentTopics().getPartitionedTopicList("prop-xyz/use/ns1"), Lists.newArrayList());
    final String partitionedTopicName = "persistent://prop-xyz/use/ns1/" + topicName;
    admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 4);
    assertEquals(admin.persistentTopics().getPartitionedTopicList("prop-xyz/use/ns1"), Lists.newArrayList(partitionedTopicName));
    assertEquals(admin.persistentTopics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 4);
    // check if the virtual topic doesn't get created
    List<String> topics = admin.persistentTopics().getList("prop-xyz/use/ns1");
    assertEquals(topics.size(), 0);
    assertEquals(admin.persistentTopics().getPartitionedTopicMetadata("persistent://prop-xyz/use/ns1/ds2").partitions, 0);
    // 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(partitionedTopicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
    assertEquals(admin.persistentTopics().getSubscriptions(partitionedTopicName), Lists.newArrayList("my-sub"));
    try {
        admin.persistentTopics().deleteSubscription(partitionedTopicName, "my-sub");
        fail("should have failed");
    } catch (PulsarAdminException.PreconditionFailedException e) {
    // ok
    } catch (Exception e) {
        fail(e.getMessage());
    }
    Consumer<byte[]> consumer1 = client.newConsumer().topic(partitionedTopicName).subscriptionName("my-sub-1").subscribe();
    assertEquals(Sets.newHashSet(admin.persistentTopics().getSubscriptions(partitionedTopicName)), Sets.newHashSet("my-sub", "my-sub-1"));
    consumer1.close();
    admin.persistentTopics().deleteSubscription(partitionedTopicName, "my-sub-1");
    assertEquals(admin.persistentTopics().getSubscriptions(partitionedTopicName), Lists.newArrayList("my-sub"));
    Producer<byte[]> producer = client.newProducer().topic(partitionedTopicName).messageRoutingMode(MessageRoutingMode.RoundRobinPartition).create();
    for (int i = 0; i < 10; i++) {
        String message = "message-" + i;
        producer.send(message.getBytes());
    }
    assertEquals(Sets.newHashSet(admin.persistentTopics().getList("prop-xyz/use/ns1")), Sets.newHashSet(partitionedTopicName + "-partition-0", partitionedTopicName + "-partition-1", partitionedTopicName + "-partition-2", partitionedTopicName + "-partition-3"));
    // test cumulative stats for partitioned topic
    PartitionedTopicStats topicStats = admin.persistentTopics().getPartitionedStats(partitionedTopicName, false);
    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(), 1);
    assertEquals(topicStats.partitions, Maps.newHashMap());
    // test per partition stats for partitioned topic
    topicStats = admin.persistentTopics().getPartitionedStats(partitionedTopicName, true);
    assertEquals(topicStats.metadata.partitions, 4);
    assertEquals(topicStats.partitions.keySet(), Sets.newHashSet(partitionedTopicName + "-partition-0", partitionedTopicName + "-partition-1", partitionedTopicName + "-partition-2", partitionedTopicName + "-partition-3"));
    PersistentTopicStats partitionStats = topicStats.partitions.get(partitionedTopicName + "-partition-0");
    assertEquals(partitionStats.publishers.size(), 1);
    assertEquals(partitionStats.subscriptions.get("my-sub").consumers.size(), 1);
    assertEquals(partitionStats.subscriptions.get("my-sub").msgBacklog, 3, 1);
    try {
        admin.persistentTopics().skipMessages(partitionedTopicName, "my-sub", 5);
        fail("skip messages for partitioned topics should fail");
    } catch (Exception e) {
    // ok
    }
    admin.persistentTopics().skipAllMessages(partitionedTopicName, "my-sub");
    topicStats = admin.persistentTopics().getPartitionedStats(partitionedTopicName, false);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 0);
    producer.close();
    consumer.close();
    admin.persistentTopics().deleteSubscription(partitionedTopicName, "my-sub");
    assertEquals(admin.persistentTopics().getSubscriptions(partitionedTopicName), Lists.newArrayList());
    try {
        admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 32);
        fail("Should have failed as the partitioned topic already exists");
    } catch (ConflictException ce) {
    }
    producer = client.newProducer().topic(partitionedTopicName).create();
    topics = admin.persistentTopics().getList("prop-xyz/use/ns1");
    assertEquals(topics.size(), 4);
    try {
        admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
        fail("The topic is busy");
    } catch (PreconditionFailedException pfe) {
    // ok
    }
    producer.close();
    client.close();
    admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
    assertEquals(admin.persistentTopics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 0);
    admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 32);
    assertEquals(admin.persistentTopics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 32);
    try {
        admin.persistentTopics().deletePartitionedTopic("persistent://prop-xyz/use/ns1/ds2");
        fail("Should have failed as the partitioned topic was not created");
    } catch (NotFoundException nfe) {
    }
    admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
    // delete a partitioned topic in a global namespace
    admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 4);
    admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
}
Also used : ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) URL(java.net.URL) 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) PartitionedTopicStats(org.apache.pulsar.common.policies.data.PartitionedTopicStats) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

ConflictException (org.apache.pulsar.client.admin.PulsarAdminException.ConflictException)4 URL (java.net.URL)3 Test (org.testng.annotations.Test)3 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)2 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)2 NotAuthorizedException (org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException)2 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)2 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ClientErrorException (javax.ws.rs.ClientErrorException)1 ServerErrorException (javax.ws.rs.ServerErrorException)1 MockedBookKeeperClientFactory (org.apache.pulsar.broker.MockedBookKeeperClientFactory)1 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)1 PulsarService (org.apache.pulsar.broker.PulsarService)1 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)1 ServerSideErrorException (org.apache.pulsar.client.admin.PulsarAdminException.ServerSideErrorException)1 BrokerStatsImpl (org.apache.pulsar.client.admin.internal.BrokerStatsImpl)1 Authentication (org.apache.pulsar.client.api.Authentication)1