Search in sources :

Example 56 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class PeerReplicatorTest method testPeerClusterTopicLookup.

/**
 * It verifies that lookup/admin requests for global-namespace would be redirected to peer-cluster if local cluster
 * doesn't own it and peer-cluster owns it, else request will be failed.
 * <pre>
 * 1. Create global-namespace ns1 for replication cluster-r1
 * 2. Try to create producer using broker in cluster r3
 * 3. Reject lookup: "r3" receives request and doesn't find namespace in local/peer cluster
 * 4. Add "r1" as a peer-cluster into "r3"
 * 5. Try to create producer using broker in cluster r3
 * 6. Success : "r3" finds "r1" in peer cluster which owns n1 and redirects to "r1"
 * 7. call admin-api to "r3" which redirects request to "r1"
 *
 * </pre>
 *
 * @param protocol
 * @throws Exception
 */
@Test(dataProvider = "lookupType")
public void testPeerClusterTopicLookup(String protocol) throws Exception {
    final String serviceUrl = protocol.equalsIgnoreCase("http") ? pulsar3.getWebServiceAddress() : pulsar3.getBrokerServiceUrl();
    final String namespace1 = "pulsar/global/peer1-" + protocol;
    final String namespace2 = "pulsar/global/peer2-" + protocol;
    admin1.namespaces().createNamespace(namespace1);
    admin1.namespaces().createNamespace(namespace2);
    // add replication cluster
    admin1.namespaces().setNamespaceReplicationClusters(namespace1, Lists.newArrayList("r1"));
    admin1.namespaces().setNamespaceReplicationClusters(namespace2, Lists.newArrayList("r2"));
    admin1.clusters().updatePeerClusterNames("r3", null);
    // disable tls as redirection url is prepared according tls configuration
    pulsar1.getConfiguration().setTlsEnabled(false);
    pulsar2.getConfiguration().setTlsEnabled(false);
    pulsar3.getConfiguration().setTlsEnabled(false);
    final String topic1 = "persistent://" + namespace1 + "/topic1";
    final String topic2 = "persistent://" + namespace2 + "/topic2";
    PulsarClient client3 = PulsarClient.builder().serviceUrl(serviceUrl).statsInterval(0, TimeUnit.SECONDS).build();
    try {
        // try to create producer for topic1 (part of cluster: r1) by calling cluster: r3
        client3.newProducer().topic(topic1).create();
        fail("should have failed as cluster:r3 doesn't own namespace");
    } catch (PulsarClientException e) {
    // Ok
    }
    try {
        // try to create producer for topic2 (part of cluster: r2) by calling cluster: r3
        client3.newProducer().topic(topic2).create();
        fail("should have failed as cluster:r3 doesn't own namespace");
    } catch (PulsarClientException e) {
    // Ok
    }
    // set peer-clusters : r3->r1
    admin1.clusters().updatePeerClusterNames("r3", Sets.newLinkedHashSet(Lists.newArrayList("r1")));
    Producer<byte[]> producer = client3.newProducer().topic(topic1).create();
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopic(topic1).get();
    assertNotNull(topic);
    pulsar1.getBrokerService().updateRates();
    // get stats for topic1 using cluster-r3's admin3
    PersistentTopicStats stats = admin1.persistentTopics().getStats(topic1);
    assertNotNull(stats);
    assertEquals(stats.publishers.size(), 1);
    stats = admin3.persistentTopics().getStats(topic1);
    assertNotNull(stats);
    assertEquals(stats.publishers.size(), 1);
    producer.close();
    // set peer-clusters : r3->r2
    admin2.clusters().updatePeerClusterNames("r3", Sets.newLinkedHashSet(Lists.newArrayList("r2")));
    producer = client3.newProducer().topic(topic2).create();
    topic = (PersistentTopic) pulsar2.getBrokerService().getTopic(topic2).get();
    assertNotNull(topic);
    pulsar2.getBrokerService().updateRates();
    // get stats for topic1 using cluster-r3's admin3
    stats = admin3.persistentTopics().getStats(topic2);
    assertNotNull(stats);
    assertEquals(stats.publishers.size(), 1);
    stats = admin3.persistentTopics().getStats(topic2);
    assertNotNull(stats);
    assertEquals(stats.publishers.size(), 1);
    producer.close();
    client3.close();
}
Also used : PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Example 57 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

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<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    // 3. Create Consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
    // 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 : CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) MessageId(org.apache.pulsar.client.api.MessageId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 58 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class TopicsConsumerImplTest method testTopicsNameSubscribeWithBuilderFail.

@Test(timeOut = testTimeout)
public void testTopicsNameSubscribeWithBuilderFail() throws Exception {
    String key = "TopicsNameSubscribeWithBuilder";
    final String subscriptionName = "my-ex-subscription-" + key;
    final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key;
    final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key;
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // test failing builder with empty topics
    try {
        pulsarClient.newConsumer().subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe1 with no topicName should fail.");
    } catch (PulsarClientException e) {
    // expected
    }
    try {
        pulsarClient.newConsumer().topic().subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe2 with no topicName should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        pulsarClient.newConsumer().topics(null).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe3 with no topicName should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        pulsarClient.newConsumer().topics(Lists.newArrayList()).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe4 with no topicName should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Example 59 with PulsarClientException

use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.

the class BacklogQuotaManagerTest method testProducerException.

@Test
public void testProducerException() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception));
    final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/usc/quotahold/except";
    final String subName1 = "c1except";
    boolean gotException = false;
    client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    byte[] content = new byte[1024];
    Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        Assert.fail("backlog quota did not exceed");
    } catch (PulsarClientException ce) {
        Assert.assertTrue(ce instanceof PulsarClientException.ProducerBlockedQuotaExceededException || ce instanceof PulsarClientException.TimeoutException, ce.getMessage());
        gotException = true;
    }
    Assert.assertTrue(gotException, "backlog exceeded exception did not occur");
    client.close();
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) Test(org.testng.annotations.Test)

Aggregations

PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)59 Test (org.testng.annotations.Test)24 CompletableFuture (java.util.concurrent.CompletableFuture)17 Message (org.apache.pulsar.client.api.Message)13 IOException (java.io.IOException)12 ByteBuf (io.netty.buffer.ByteBuf)11 PulsarClient (org.apache.pulsar.client.api.PulsarClient)11 ExecutionException (java.util.concurrent.ExecutionException)10 Consumer (org.apache.pulsar.client.api.Consumer)10 Producer (org.apache.pulsar.client.api.Producer)10 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)8 MessageId (org.apache.pulsar.client.api.MessageId)8 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)7 List (java.util.List)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 HashSet (java.util.HashSet)5 TimeUnit (java.util.concurrent.TimeUnit)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4