Search in sources :

Example 26 with PulsarAdminException

use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.

the class MessageIdTest method partitionedProducerSendAsync.

@Test(timeOut = 10000)
public void partitionedProducerSendAsync() throws PulsarClientException, PulsarAdminException {
    // 1. Basic Config
    String key = "partitionedProducerSendAsync";
    final String topicName = "persistent://prop/cluster/namespace/topic-" + key;
    final String subscriptionName = "my-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int numberOfMessages = 30;
    int numberOfPartitions = 3;
    admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
    // 2. Create Producer
    Producer producer = pulsarClient.createProducer(topicName);
    // 3. Create Consumer
    Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
    // 4. Publish message and get message id
    Set<MessageId> messageIds = new HashSet();
    Set<Future<MessageId>> futures = new HashSet();
    for (int i = 0; i < numberOfMessages; i++) {
        String message = messagePredicate + i;
        futures.add(producer.sendAsync(message.getBytes()));
    }
    futures.forEach(f -> {
        try {
            messageIds.add(f.get());
        } catch (Exception e) {
            Assert.fail("Failed to publish message, Exception: " + e.getMessage());
        }
    });
    // 4. Check if message Ids are correct
    log.info("Message IDs = " + messageIds);
    Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully");
    for (int i = 0; i < numberOfMessages; i++) {
        MessageId messageId = consumer.receive().getMessageId();
        log.info("Message ID Received = " + messageId);
        Assert.assertTrue(messageIds.remove(messageId), "Failed to receive Message");
    }
    log.info("Message IDs = " + messageIds);
    Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully");
    consumer.unsubscribe();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Consumer(com.yahoo.pulsar.client.api.Consumer) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) HashSet(java.util.HashSet) MessageId(com.yahoo.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 27 with PulsarAdminException

use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.

the class PulsarStandaloneStarter method start.

void start() throws Exception {
    if (config == null) {
        System.exit(1);
    }
    log.debug("--- setup PulsarStandaloneStarter ---");
    if (!onlyBroker) {
        // Start LocalBookKeeper
        bkEnsemble = new LocalBookkeeperEnsemble(numOfBk, zkPort, bkPort, zkDir, bkDir, wipeData);
        bkEnsemble.start();
    }
    if (noBroker) {
        return;
    }
    // Start Broker
    broker = new PulsarService(config);
    broker.start();
    // Create a sample namespace
    URL webServiceUrl = new URL(String.format("http://%s:%d", config.getAdvertisedAddress(), config.getWebServicePort()));
    String brokerServiceUrl = String.format("pulsar://%s:%d", config.getAdvertisedAddress(), config.getBrokerServicePort());
    admin = new PulsarAdmin(webServiceUrl, config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters());
    String property = "sample";
    String cluster = config.getClusterName();
    String namespace = property + "/" + cluster + "/ns1";
    try {
        ClusterData clusterData = new ClusterData(webServiceUrl.toString(), null, /* serviceUrlTls */
        brokerServiceUrl, null);
        if (!admin.clusters().getClusters().contains(cluster)) {
            admin.clusters().createCluster(cluster, clusterData);
        } else {
            admin.clusters().updateCluster(cluster, clusterData);
        }
        if (!admin.properties().getProperties().contains(property)) {
            admin.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList(config.getSuperUserRoles()), Sets.newHashSet(cluster)));
        }
        if (!admin.namespaces().getNamespaces(property).contains(namespace)) {
            admin.namespaces().createNamespace(namespace);
        }
    } catch (PulsarAdminException e) {
        log.info(e.getMessage());
    }
    log.debug("--- setup completed ---");
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PulsarService(com.yahoo.pulsar.broker.PulsarService) PulsarAdmin(com.yahoo.pulsar.client.admin.PulsarAdmin) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) LocalBookkeeperEnsemble(com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble) URL(java.net.URL)

Example 28 with PulsarAdminException

use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.

the class AdminApiTest method clusterNamespaceIsolationPolicies.

@Test
public void clusterNamespaceIsolationPolicies() throws PulsarAdminException {
    try {
        // create
        String policyName1 = "policy-1";
        NamespaceIsolationData nsPolicyData1 = new NamespaceIsolationData();
        nsPolicyData1.namespaces = new ArrayList<String>();
        nsPolicyData1.namespaces.add("other/use/other.*");
        nsPolicyData1.primary = new ArrayList<String>();
        nsPolicyData1.primary.add("prod1-broker[4-6].messaging.use.example.com");
        nsPolicyData1.secondary = new ArrayList<String>();
        nsPolicyData1.secondary.add("prod1-broker.*.messaging.use.example.com");
        nsPolicyData1.auto_failover_policy = new AutoFailoverPolicyData();
        nsPolicyData1.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
        nsPolicyData1.auto_failover_policy.parameters = new HashMap<String, String>();
        nsPolicyData1.auto_failover_policy.parameters.put("min_limit", "1");
        nsPolicyData1.auto_failover_policy.parameters.put("usage_threshold", "100");
        admin.clusters().createNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        String policyName2 = "policy-2";
        NamespaceIsolationData nsPolicyData2 = new NamespaceIsolationData();
        nsPolicyData2.namespaces = new ArrayList<String>();
        nsPolicyData2.namespaces.add("other/use/other.*");
        nsPolicyData2.primary = new ArrayList<String>();
        nsPolicyData2.primary.add("prod1-broker[4-6].messaging.use.example.com");
        nsPolicyData2.secondary = new ArrayList<String>();
        nsPolicyData2.secondary.add("prod1-broker.*.messaging.use.example.com");
        nsPolicyData2.auto_failover_policy = new AutoFailoverPolicyData();
        nsPolicyData2.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
        nsPolicyData2.auto_failover_policy.parameters = new HashMap<String, String>();
        nsPolicyData2.auto_failover_policy.parameters.put("min_limit", "1");
        nsPolicyData2.auto_failover_policy.parameters.put("usage_threshold", "100");
        admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData2);
        // verify create indirectly with get
        Map<String, NamespaceIsolationData> policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        assertEquals(policiesMap.get(policyName2), nsPolicyData2);
        // verify update of primary
        nsPolicyData1.primary.remove(0);
        nsPolicyData1.primary.add("prod1-broker[1-2].messaging.use.example.com");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify primary change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of secondary
        nsPolicyData1.secondary.remove(0);
        nsPolicyData1.secondary.add("prod1-broker[3-4].messaging.use.example.com");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify secondary change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of failover policy limit
        nsPolicyData1.auto_failover_policy.parameters.put("min_limit", "10");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify min_limit change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of failover usage_threshold limit
        nsPolicyData1.auto_failover_policy.parameters.put("usage_threshold", "80");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify usage_threshold change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify single get
        NamespaceIsolationData policy1Data = admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
        assertEquals(policy1Data, nsPolicyData1);
        // verify creation of more than one policy
        admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData1);
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", "no-such-policy");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        // verify delete cluster failed
        try {
            admin.clusters().deleteCluster("use");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        // verify delete
        admin.clusters().deleteNamespaceIsolationPolicy("use", policyName1);
        admin.clusters().deleteNamespaceIsolationPolicy("use", policyName2);
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", policyName2);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicies("usc");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicy("usc", "no-such-cluster");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        try {
            admin.clusters().createNamespaceIsolationPolicy("usc", "no-such-cluster", nsPolicyData1);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        try {
            admin.clusters().updateNamespaceIsolationPolicy("usc", "no-such-cluster", policy1Data);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
    } catch (PulsarAdminException e) {
        LOG.warn("TEST FAILED [{}]", e.getMessage());
        throw e;
    }
}
Also used : AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 29 with PulsarAdminException

use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.

the class AdminApiTest method testNamespaceUnloadBundle.

@Test
public void testNamespaceUnloadBundle() throws Exception {
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
    // Force to create a destination
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/ds2", 0);
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/ds2"));
    // create consumer and subscription
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe("persistent://prop-xyz/use/ns1/ds2", "my-sub", conf);
    assertEquals(admin.persistentTopics().getSubscriptions("persistent://prop-xyz/use/ns1/ds2"), Lists.newArrayList("my-sub"));
    // Create producer
    Producer producer = pulsarClient.createProducer("persistent://prop-xyz/use/ns1/ds2");
    for (int i = 0; i < 10; i++) {
        String message = "message-" + i;
        producer.send(message.getBytes());
    }
    consumer.close();
    producer.close();
    try {
        admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
    } catch (Exception e) {
        fail("Unload shouldn't have throw exception");
    }
    // check that no one owns the namespace
    NamespaceBundle bundle = bundleFactory.getBundle(new NamespaceName("prop-xyz/use/ns1"), Range.range(0L, BoundType.CLOSED, 0xffffffffL, BoundType.CLOSED));
    assertFalse(pulsar.getNamespaceService().isServiceUnitOwned(bundle));
    assertFalse(otherPulsar.getNamespaceService().isServiceUnitOwned(bundle));
    pulsarClient.shutdown();
    LOG.info("--- RELOAD ---");
    // Force reload of namespace and wait for topic to be ready
    for (int i = 0; i < 30; i++) {
        try {
            admin.persistentTopics().getStats("persistent://prop-xyz/use/ns1/ds2");
            break;
        } catch (PulsarAdminException e) {
            LOG.warn("Failed to get topic stats.. {}", e.getMessage());
            Thread.sleep(1000);
        }
    }
    admin.persistentTopics().deleteSubscription("persistent://prop-xyz/use/ns1/ds2", "my-sub");
    admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/ds2");
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) ConflictException(com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 30 with PulsarAdminException

use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.

the class AdminApiTest method properties.

@Test(enabled = true)
public void properties() throws PulsarAdminException {
    Set<String> allowedClusters = Sets.newHashSet("use");
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), allowedClusters);
    admin.properties().updateProperty("prop-xyz", propertyAdmin);
    assertEquals(admin.properties().getProperties(), Lists.newArrayList("prop-xyz"));
    assertEquals(admin.properties().getPropertyAdmin("prop-xyz"), propertyAdmin);
    PropertyAdmin newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role3", "role4"), allowedClusters);
    admin.properties().updateProperty("prop-xyz", newPropertyAdmin);
    assertEquals(admin.properties().getPropertyAdmin("prop-xyz"), newPropertyAdmin);
    admin.namespaces().deleteNamespace("prop-xyz/use/ns1");
    admin.properties().deleteProperty("prop-xyz");
    assertEquals(admin.properties().getProperties(), Lists.newArrayList());
    // Check name validation
    try {
        admin.properties().createProperty("prop-xyz&", propertyAdmin);
        fail("should have failed");
    } catch (PulsarAdminException e) {
        assertTrue(e instanceof PreconditionFailedException);
    }
}
Also used : PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)45 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)30 Test (org.testng.annotations.Test)12 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)7 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)7 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)7 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)7 ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)6 RestException (com.yahoo.pulsar.broker.web.RestException)5 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 Path (javax.ws.rs.Path)5 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)4 Consumer (com.yahoo.pulsar.client.api.Consumer)4 Producer (com.yahoo.pulsar.client.api.Producer)4 NamespaceBundles (com.yahoo.pulsar.common.naming.NamespaceBundles)4 PropertyAdmin (com.yahoo.pulsar.common.policies.data.PropertyAdmin)4 ClientErrorException (javax.ws.rs.ClientErrorException)4