Search in sources :

Example 26 with PulsarClientException

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

the class ProxyWithAuthorizationTest method testTlsHostVerificationProxyToBroker.

/**
 * It verifies hostname verification at proxy when proxy tries to connect with broker. Proxy performs hostname
 * verification when broker sends its certs over tls .
 *
 * <pre>
 * 1. Broker sends certs back to proxy with CN="Broker" however, proxy tries to connect with hostname=localhost
 * 2. so, client fails to create consumer if proxy is enabled with hostname verification
 * </pre>
 *
 * @param hostnameVerificationEnabled
 * @throws Exception
 */
@Test(dataProvider = "hostnameVerification")
public void testTlsHostVerificationProxyToBroker(boolean hostnameVerificationEnabled) throws Exception {
    log.info("-- Starting {} test --", methodName);
    proxyConfig.setTlsHostnameVerificationEnabled(hostnameVerificationEnabled);
    startProxy();
    createAdminClient();
    final String proxyServiceUrl = "pulsar://localhost:" + proxyConfig.getServicePortTls();
    // create a client which connects to proxy over tls and pass authData
    PulsarClient proxyClient = createPulsarClient(proxyServiceUrl, PulsarClient.builder().operationTimeout(1, TimeUnit.SECONDS));
    String namespaceName = "my-property/proxy-authorization/my-ns";
    admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("proxy-authorization")));
    admin.namespaces().createNamespace(namespaceName);
    admin.namespaces().grantPermissionOnNamespace(namespaceName, "Proxy", Sets.newHashSet(AuthAction.consume, AuthAction.produce));
    admin.namespaces().grantPermissionOnNamespace(namespaceName, "Client", Sets.newHashSet(AuthAction.consume, AuthAction.produce));
    try {
        proxyClient.newConsumer().topic("persistent://my-property/proxy-authorization/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
        if (hostnameVerificationEnabled) {
            Assert.fail("Connection should be failed due to hostnameVerification enabled");
        }
    } catch (PulsarClientException e) {
        if (!hostnameVerificationEnabled) {
            Assert.fail("Consumer should be created because hostnameverification is disabled");
        }
    }
    log.info("-- Exiting {} test --", methodName);
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 27 with PulsarClientException

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

the class ProxyWithAuthorizationTest method testTlsHostVerificationProxyToClient.

@Test(dataProvider = "hostnameVerification")
public void testTlsHostVerificationProxyToClient(boolean hostnameVerificationEnabled) throws Exception {
    log.info("-- Starting {} test --", methodName);
    startProxy();
    createAdminClient();
    final String proxyServiceUrl = "pulsar://localhost:" + proxyConfig.getServicePortTls();
    // create a client which connects to proxy over tls and pass authData
    PulsarClient proxyClient = createPulsarClient(proxyServiceUrl, PulsarClient.builder().enableTlsHostnameVerification(hostnameVerificationEnabled));
    String namespaceName = "my-property/proxy-authorization/my-ns";
    admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("proxy-authorization")));
    admin.namespaces().createNamespace(namespaceName);
    admin.namespaces().grantPermissionOnNamespace(namespaceName, "Proxy", Sets.newHashSet(AuthAction.consume, AuthAction.produce));
    admin.namespaces().grantPermissionOnNamespace(namespaceName, "Client", Sets.newHashSet(AuthAction.consume, AuthAction.produce));
    try {
        proxyClient.newConsumer().topic("persistent://my-property/proxy-authorization/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
        if (hostnameVerificationEnabled) {
            Assert.fail("Connection should be failed due to hostnameVerification enabled");
        }
    } catch (PulsarClientException e) {
        if (!hostnameVerificationEnabled) {
            Assert.fail("Consumer should be created because hostnameverification is disabled");
        }
    }
    log.info("-- Exiting {} test --", methodName);
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 28 with PulsarClientException

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

the class PersistentQueueE2ETest method testSimpleConsumerEvents.

@Test
public void testSimpleConsumerEvents() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/shared-topic1";
    final String subName = "sub1";
    final int numMsgs = 100;
    ConsumerBuilder<byte[]> consumerBuilder = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Shared);
    // 1. two consumers on the same subscription
    Consumer<byte[]> consumer1 = consumerBuilder.subscribe();
    Consumer<byte[]> consumer2 = consumerBuilder.subscribe();
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    PersistentSubscription subRef = topicRef.getSubscription(subName);
    assertNotNull(topicRef);
    assertNotNull(subRef);
    // 2. validate basic dispatcher state
    assertTrue(subRef.getDispatcher().isConsumerConnected());
    assertEquals(subRef.getDispatcher().getType(), SubType.Shared);
    List<CompletableFuture<MessageId>> futures = Lists.newArrayListWithCapacity(numMsgs * 2);
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    for (int i = 0; i < numMsgs * 2; i++) {
        String message = "my-message-" + i;
        futures.add(producer.sendAsync(message.getBytes()));
    }
    FutureUtil.waitForAll(futures).get();
    rolloverPerIntervalStats();
    assertEquals(subRef.getNumberOfEntriesInBacklog(), numMsgs * 2);
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    // both consumers will together consumer all messages
    Message<byte[]> msg;
    Consumer<byte[]> c = consumer1;
    while (true) {
        try {
            msg = c.receive(1, TimeUnit.SECONDS);
            c.acknowledge(msg);
        } catch (PulsarClientException e) {
            if (c.equals(consumer1)) {
                consumer1.close();
                c = consumer2;
            } else {
                break;
            }
        }
    }
    rolloverPerIntervalStats();
    // 3. messages deleted on individual acks
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(subRef.getNumberOfEntriesInBacklog(), 0);
    // 4. shared consumer unsubscribe not allowed
    try {
        consumer1.unsubscribe();
        fail("should fail");
    } catch (PulsarClientException e) {
    // ok
    }
    // 5. cumulative acks disabled
    consumer1.close();
    producer.send("message".getBytes());
    msg = consumer2.receive();
    try {
        consumer2.acknowledgeCumulative(msg);
        fail("Should fail");
    } catch (PulsarClientException e) {
        assertTrue(e instanceof PulsarClientException.InvalidConfigurationException);
    }
    // 6. unsubscribe allowed if this is the lone consumer
    try {
        consumer2.unsubscribe();
    } catch (PulsarClientException e) {
        fail("Should not fail");
    }
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    subRef = topicRef.getSubscription(subName);
    assertNull(subRef);
    producer.close();
    consumer2.close();
    admin.persistentTopics().delete(topicName);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 29 with PulsarClientException

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

the class PersistentTopicE2ETest method testMultipleClientsMultipleSubscriptions.

@Test
public void testMultipleClientsMultipleSubscriptions() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic7";
    final String subName = "sub7";
    PulsarClient client1 = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    PulsarClient client2 = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    try {
        client1.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
        client1.newProducer().topic(topicName).create();
        client2.newProducer().topic(topicName).create();
        client2.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
        fail("Should have thrown an exception since one consumer is already connected");
    } catch (PulsarClientException cce) {
        Assert.assertTrue(cce.getMessage().contains("Exclusive consumer is already connected"));
    } finally {
        client2.shutdown();
        client1.shutdown();
    }
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 30 with PulsarClientException

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

the class V1_ProducerConsumerTest method testBlockUnackConsumerAckByDifferentConsumer.

/**
 * Verify: Consumer2 sends ack of Consumer1 and consumer1 should be unblock if it is blocked due to unack-messages
 *
 * @param batchMessageDelayMs
 * @throws Exception
 */
@Test
public void testBlockUnackConsumerAckByDifferentConsumer() throws Exception {
    log.info("-- Starting {} test --", methodName);
    int unAckedMessages = pulsar.getConfiguration().getMaxUnackedMessagesPerConsumer();
    try {
        final int maxUnackedMessages = 20;
        final int receiverQueueSize = 10;
        final int totalProducedMsgs = 100;
        int totalReceiveMessages = 0;
        pulsar.getConfiguration().setMaxUnackedMessagesPerConsumer(maxUnackedMessages);
        ConsumerConfiguration conf = new ConsumerConfiguration();
        conf.setReceiverQueueSize(receiverQueueSize);
        conf.setSubscriptionType(SubscriptionType.Shared);
        Consumer consumer1 = pulsarClient.subscribe("persistent://my-property/use/my-ns/unacked-topic", "subscriber-1", conf);
        Consumer consumer2 = pulsarClient.subscribe("persistent://my-property/use/my-ns/unacked-topic", "subscriber-1", conf);
        ProducerConfiguration producerConf = new ProducerConfiguration();
        Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/unacked-topic", producerConf);
        // (1) Produced Messages
        for (int i = 0; i < totalProducedMsgs; i++) {
            String message = "my-message-" + i;
            producer.send(message.getBytes());
        }
        // (2) Consumer1: consume without ack:
        // try to consume messages: but will be able to consume number of messages = maxUnackedMessages
        Message msg = null;
        List<Message> messages = Lists.newArrayList();
        for (int i = 0; i < totalProducedMsgs; i++) {
            msg = consumer1.receive(1, TimeUnit.SECONDS);
            if (msg != null) {
                messages.add(msg);
                totalReceiveMessages++;
                log.info("Received message: " + new String(msg.getData()));
            } else {
                break;
            }
        }
        // consumer1
        assertEquals(messages.size(), maxUnackedMessages);
        // (3) ack for all UnackedMessages from consumer2
        messages.forEach(m -> {
            try {
                consumer2.acknowledge(m);
            } catch (PulsarClientException e) {
                fail("shouldn't have failed ", e);
            }
        });
        // (4) consumer1 will consumer remaining msgs and consumer2 will ack those messages
        for (int i = 0; i < totalProducedMsgs; i++) {
            msg = consumer1.receive(1, TimeUnit.SECONDS);
            if (msg != null) {
                totalReceiveMessages++;
                consumer2.acknowledge(msg);
                log.info("Received message: " + new String(msg.getData()));
            } else {
                break;
            }
        }
        for (int i = 0; i < totalProducedMsgs; i++) {
            msg = consumer2.receive(1, TimeUnit.SECONDS);
            if (msg != null) {
                totalReceiveMessages++;
                log.info("Received message: " + new String(msg.getData()));
            } else {
                break;
            }
        }
        // verify total-consumer messages = total-produce messages
        assertEquals(totalProducedMsgs, totalReceiveMessages);
        producer.close();
        consumer1.close();
        consumer2.close();
        log.info("-- Exiting {} test --", methodName);
    } catch (Exception e) {
        fail();
    } finally {
        pulsar.getConfiguration().setMaxUnackedMessagesPerConsumer(unAckedMessages);
    }
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) Message(org.apache.pulsar.client.api.Message) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

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