Search in sources :

Example 61 with PersistentTopic

use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.

the class PersistentTopicE2ETest method testSubscriptionTypeTransitions.

@Test
public void testSubscriptionTypeTransitions() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/shared-topic2";
    final String subName = "sub2";
    ConsumerConfiguration conf1 = new ConsumerConfiguration();
    conf1.setSubscriptionType(SubscriptionType.Exclusive);
    ConsumerConfiguration conf2 = new ConsumerConfiguration();
    conf2.setSubscriptionType(SubscriptionType.Shared);
    ConsumerConfiguration conf3 = new ConsumerConfiguration();
    conf3.setSubscriptionType(SubscriptionType.Failover);
    Consumer consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
    Consumer consumer2 = null;
    Consumer consumer3 = null;
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    // 1. shared consumer on an exclusive sub fails
    try {
        consumer2 = pulsarClient.subscribe(topicName, subName, conf2);
        fail("should have failed");
    } catch (PulsarClientException e) {
        assertTrue(e.getMessage().contains("Subscription is of different type"));
    }
    // 2. failover consumer on an exclusive sub fails
    try {
        consumer3 = pulsarClient.subscribe(topicName, subName, conf3);
        fail("should have failed");
    } catch (PulsarClientException e) {
        assertTrue(e.getMessage().contains("Subscription is of different type"));
    }
    // 3. disconnected sub can be converted in shared
    consumer1.close();
    try {
        consumer2 = pulsarClient.subscribe(topicName, subName, conf2);
        assertEquals(subRef.getDispatcher().getType(), SubType.Shared);
    } catch (PulsarClientException e) {
        fail("should not fail");
    }
    // 4. exclusive fails on shared sub
    try {
        consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
        fail("should have failed");
    } catch (PulsarClientException e) {
        assertTrue(e.getMessage().contains("Subscription is of different type"));
    }
    // 5. disconnected sub can be converted in failover
    consumer2.close();
    try {
        consumer3 = pulsarClient.subscribe(topicName, subName, conf3);
        assertEquals(subRef.getDispatcher().getType(), SubType.Failover);
    } catch (PulsarClientException e) {
        fail("should not fail");
    }
    // 5. exclusive consumer can connect after failover disconnects
    consumer3.close();
    try {
        consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
        assertEquals(subRef.getDispatcher().getType(), SubType.Exclusive);
    } catch (PulsarClientException e) {
        fail("should not fail");
    }
    consumer1.close();
    admin.persistentTopics().delete(topicName);
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 62 with PersistentTopic

use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.

the class PersistentTopicE2ETest method testSimpleProducerEvents.

@Test
public void testSimpleProducerEvents() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic0";
    // 1. producer connect
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    // 2. producer publish messages
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    rolloverPerIntervalStats();
    assertTrue(topicRef.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
    // 3. producer disconnect
    producer.close();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(topicRef.getProducers().size(), 0);
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 63 with PersistentTopic

use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.

the class PersistentTopicE2ETest method testMessageExpiry.

@Test
public void testMessageExpiry() throws Exception {
    int messageTTLSecs = 1;
    String namespaceName = "prop/use/expiry-check";
    admin.namespaces().createNamespace(namespaceName);
    admin.namespaces().setNamespaceMessageTTL(namespaceName, messageTTLSecs);
    final String topicName = "persistent://prop/use/expiry-check/topic1";
    final String subName = "sub1";
    final int numMsgs = 10;
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    consumer.close();
    assertFalse(subRef.getDispatcher().isConsumerConnected());
    Producer producer = pulsarClient.createProducer(topicName);
    for (int i = 0; i < numMsgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    rolloverPerIntervalStats();
    assertEquals(subRef.getNumberOfEntriesInBacklog(), numMsgs);
    Thread.sleep(TimeUnit.SECONDS.toMillis(messageTTLSecs));
    runMessageExpiryCheck();
    // 1. check all messages expired for this unconnected subscription
    assertEquals(subRef.getNumberOfEntriesInBacklog(), 0);
    // clean-up
    producer.close();
    consumer.close();
    admin.persistentTopics().deleteSubscription(topicName, subName);
    admin.persistentTopics().delete(topicName);
    admin.namespaces().deleteNamespace(namespaceName);
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 64 with PersistentTopic

use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.

the class PersistentTopicE2ETest method testTopicDeleteWithDisconnectedSubscription.

@Test
public void testTopicDeleteWithDisconnectedSubscription() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic8";
    final String subName = "sub1";
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    // 1. client connect
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(topicRef);
    assertNotNull(subRef);
    assertTrue(subRef.getDispatcher().isConsumerConnected());
    // 2. client disconnect
    consumer.close();
    assertFalse(subRef.getDispatcher().isConsumerConnected());
    // 3. delete topic
    admin.persistentTopics().delete(topicName);
    try {
        admin.persistentTopics().getStats(topicName);
    } catch (PulsarAdminException e) {
    // ok
    }
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 65 with PersistentTopic

use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.

the class PersistentTopicE2ETest method testConsumerFlowControl.

@Test
public void testConsumerFlowControl() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic2";
    final String subName = "sub2";
    Message msg;
    int recvQueueSize = 4;
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    conf.setReceiverQueueSize(recvQueueSize);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(subRef);
    // 1. initial receive queue size recorded
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(getAvailablePermits(subRef), recvQueueSize);
    for (int i = 0; i < recvQueueSize / 2; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    // 2. queue size re-adjusted after successful receive of half of window size
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(getAvailablePermits(subRef), recvQueueSize);
    consumer.close();
    assertFalse(subRef.getDispatcher().isConsumerConnected());
}
Also used : Message(com.yahoo.pulsar.client.api.Message) Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Aggregations

PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)92 Test (org.testng.annotations.Test)67 Producer (com.yahoo.pulsar.client.api.Producer)35 Consumer (com.yahoo.pulsar.client.api.Consumer)33 Message (com.yahoo.pulsar.client.api.Message)31 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)29 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)24 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)23 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)21 CompletableFuture (java.util.concurrent.CompletableFuture)17 KeeperException (org.apache.zookeeper.KeeperException)15 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)14 PersistentReplicator (com.yahoo.pulsar.broker.service.persistent.PersistentReplicator)13 IOException (java.io.IOException)13 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)12 PersistentDispatcherSingleActiveConsumer (com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)12 RestException (com.yahoo.pulsar.broker.web.RestException)12 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)12 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)12 CountDownLatch (java.util.concurrent.CountDownLatch)12