Search in sources :

Example 16 with PersistentSubscription

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

the class PersistentTopicConcurrentTest method testConcurrentTopicGCAndSubscriptionDelete.

// @Test
public void testConcurrentTopicGCAndSubscriptionDelete() throws Exception {
    // create topic
    final PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
    PulsarApi.CommandSubscribe cmd = PulsarApi.CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(PulsarApi.CommandSubscribe.SubType.Exclusive).build();
    Future<Consumer> f1 = topic.subscribe(serverCnx, cmd.getSubscription(), cmd.getConsumerId(), cmd.getSubType(), 0, cmd.getConsumerName());
    f1.get();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                // assertTrue(topic.unsubscribe(successSubName).isDone());
                // Thread.sleep(5,0);
                log.info("{} forcing topic GC ", Thread.currentThread());
                for (int i = 0; i < 2000; i++) {
                    topic.checkGC(0);
                }
                log.info("GC done..");
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread unsubscriber = new Thread() {

        public void run() {
            try {
                barrier.await();
                // do subscription delete
                ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
                PersistentSubscription ps = subscriptions.get(successSubName);
                // Thread.sleep(2,0);
                log.info("unsubscriber outcome is {}", ps.doUnsubscribe(ps.getConsumers().get(0)).get());
            // assertFalse(ps.delete().isCompletedExceptionally());
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    unsubscriber.start();
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : PulsarApi(com.yahoo.pulsar.common.api.proto.PulsarApi) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(com.yahoo.pulsar.broker.service.Consumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic)

Example 17 with PersistentSubscription

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

the class PersistentTopicConcurrentTest method testConcurrentTopicAndSubscriptionDelete.

// @Test
public void testConcurrentTopicAndSubscriptionDelete() throws Exception {
    // create topic
    final PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
    PulsarApi.CommandSubscribe cmd = PulsarApi.CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(PulsarApi.CommandSubscribe.SubType.Exclusive).build();
    Future<Consumer> f1 = topic.subscribe(serverCnx, cmd.getSubscription(), cmd.getConsumerId(), cmd.getSubType(), 0, cmd.getConsumerName());
    f1.get();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                // assertTrue(topic.unsubscribe(successSubName).isDone());
                Thread.sleep(5, 0);
                log.info("deleter outcome is {}", topic.delete().get());
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread unsubscriber = new Thread() {

        public void run() {
            try {
                barrier.await();
                // do subscription delete
                ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
                PersistentSubscription ps = subscriptions.get(successSubName);
                // Thread.sleep(2,0);
                log.info("unsubscriber outcome is {}", ps.doUnsubscribe(ps.getConsumers().get(0)).get());
            // assertFalse(ps.delete().isCompletedExceptionally());
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    unsubscriber.start();
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : PulsarApi(com.yahoo.pulsar.common.api.proto.PulsarApi) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(com.yahoo.pulsar.broker.service.Consumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic)

Example 18 with PersistentSubscription

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

the class PersistentTopicE2ETest method testGracefulClose.

@Test(enabled = false)
public // TODO: enable this after java client supports graceful close
void testGracefulClose() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic4";
    final String subName = "sub4";
    Producer producer = pulsarClient.createProducer(topicName);
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    ExecutorService executor = Executors.newCachedThreadPool();
    CountDownLatch latch = new CountDownLatch(1);
    executor.submit(() -> {
        for (int i = 0; i < 10; i++) {
            String message = "my-message-" + i;
            producer.send(message.getBytes());
        }
        latch.countDown();
        return null;
    });
    producer.close();
    // 1. verify there are no pending publish acks once the producer close
    // is completed on client
    assertEquals(topicRef.getProducers().values().iterator().next().getPendingPublishAcks(), 0);
    // safety latch in case of failure,
    // wait for the spawned thread to complete
    latch.await();
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(subRef);
    Message msg = null;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive();
    }
    // message acks
    try {
        consumer.close();
        fail("should have failed");
    } catch (IllegalStateException e) {
    // Expected - messages not acked
    }
    consumer.acknowledgeCumulative(msg);
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    // 3. verify consumer close succeeds once all messages are ack'ed
    consumer.close();
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertTrue(subRef.getDispatcher().isConsumerConnected());
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) Consumer(com.yahoo.pulsar.client.api.Consumer) Message(com.yahoo.pulsar.client.api.Message) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 19 with PersistentSubscription

use of com.yahoo.pulsar.broker.service.persistent.PersistentSubscription 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 20 with PersistentSubscription

use of com.yahoo.pulsar.broker.service.persistent.PersistentSubscription 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)

Aggregations

PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)29 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)29 Test (org.testng.annotations.Test)16 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)12 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)12 Consumer (com.yahoo.pulsar.client.api.Consumer)11 Producer (com.yahoo.pulsar.client.api.Producer)10 Message (com.yahoo.pulsar.client.api.Message)8 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)7 RestException (com.yahoo.pulsar.broker.web.RestException)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 KeeperException (org.apache.zookeeper.KeeperException)7 NotAllowedException (com.yahoo.pulsar.broker.service.BrokerServiceException.NotAllowedException)6 TopicBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException)6 PersistentDispatcherSingleActiveConsumer (com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)6 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)6 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)6 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)6 PartitionedTopicMetadata (com.yahoo.pulsar.common.partition.PartitionedTopicMetadata)6 IOException (java.io.IOException)6