Search in sources :

Example 1 with CommandSubscribe

use of org.apache.pulsar.common.api.proto.CommandSubscribe in project pulsar by apache.

the class PersistentTopicConcurrentTest method testConcurrentTopicDeleteAndUnsubscribe.

@Test(enabled = false)
public void testConcurrentTopicDeleteAndUnsubscribe() throws Exception {
    // create topic
    final PersistentTopic topic = (PersistentTopic) brokerService.getOrCreateTopic(successTopicName).get();
    CommandSubscribe cmd = new CommandSubscribe().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(CommandSubscribe.SubType.Exclusive);
    SubscriptionOption subscriptionOption = getSubscriptionOption(cmd);
    Future<Consumer> f1 = topic.subscribe(subscriptionOption);
    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();
                Thread.sleep(4, 700);
                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();
                // Thread.sleep(2,0);
                // assertTrue(topic.unsubscribe(successSubName).isDone());
                ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
                PersistentSubscription ps = subscriptions.get(successSubName);
                log.info("unsubscribe result : {}", topic.unsubscribe(successSubName).get());
                log.info("closing consumer..");
                ps.getConsumers().get(0).close();
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    unsubscriber.start();
    counter.await();
    assertFalse(gotException.get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) CyclicBarrier(java.util.concurrent.CyclicBarrier) CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 2 with CommandSubscribe

use of org.apache.pulsar.common.api.proto.CommandSubscribe in project pulsar by apache.

the class PersistentTopicConcurrentTest method testConcurrentTopicDeleteAndSubsUnsubscribe.

@Test(enabled = false)
public void testConcurrentTopicDeleteAndSubsUnsubscribe() throws Exception {
    // create topic
    final PersistentTopic topic = (PersistentTopic) brokerService.getOrCreateTopic(successTopicName).get();
    CommandSubscribe cmd = new CommandSubscribe().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(CommandSubscribe.SubType.Exclusive);
    SubscriptionOption subscriptionOption = getSubscriptionOption(cmd);
    Future<Consumer> f1 = topic.subscribe(subscriptionOption);
    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();
                Thread.sleep(4, 730);
                log.info("@@@@@@@@ DELETER TH");
                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();
                log.info("&&&&&&&&& UNSUBSCRIBER TH");
                // Thread.sleep(2,0);
                // assertTrue(topic.unsubscribe(successSubName).isDone());
                ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
                PersistentSubscription ps = subscriptions.get(successSubName);
                log.info("unsubscribe result : " + ps.doUnsubscribe(ps.getConsumers().get(0)).get());
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    unsubscriber.start();
    counter.await();
    assertFalse(gotException.get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) CyclicBarrier(java.util.concurrent.CyclicBarrier) CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 3 with CommandSubscribe

use of org.apache.pulsar.common.api.proto.CommandSubscribe in project pulsar by apache.

the class PersistentTopicTest method testConcurrentTopicAndSubscriptionDelete.

@Test(enabled = false)
public void testConcurrentTopicAndSubscriptionDelete() throws Exception {
    // create topic
    final PersistentTopic topic = (PersistentTopic) brokerService.getOrCreateTopic(successTopicName).get();
    CommandSubscribe cmd = new CommandSubscribe().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Exclusive);
    Future<Consumer> f1 = topic.subscribe(getSubscriptionOption(cmd));
    f1.get();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        @Override
        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() {

        @Override
        public void run() {
            try {
                barrier.await();
                // do subscription delete
                ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
                PersistentSubscription ps = subscriptions.get(successSubName);
                // Thread.sleep(5,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();
    assertFalse(gotException.get());
}
Also used : CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Mockito.anyString(org.mockito.Mockito.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 4 with CommandSubscribe

use of org.apache.pulsar.common.api.proto.CommandSubscribe in project pulsar by apache.

the class PersistentTopicTest method testFailoverSubscription.

@Test
public void testFailoverSubscription() throws Exception {
    PersistentTopic topic1 = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    CommandSubscribe cmd1 = new CommandSubscribe().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setConsumerName("consumer-name").setReadCompacted(false).setRequestId(1).setSubType(SubType.Failover);
    // 1. Subscribe with non partition topic
    Future<Consumer> f1 = topic1.subscribe(getSubscriptionOption(cmd1));
    f1.get();
    // 2. Subscribe with partition topic
    PersistentTopic topic2 = new PersistentTopic(successPartitionTopicName, ledgerMock, brokerService);
    CommandSubscribe cmd2 = new CommandSubscribe().setConsumerId(1).setConsumerName("C1").setTopic(successPartitionTopicName).setSubscription(successSubName).setReadCompacted(false).setRequestId(1).setSubType(SubType.Failover);
    Future<Consumer> f2 = topic2.subscribe(getSubscriptionOption(cmd2));
    f2.get();
    // 3. Subscribe and create second consumer
    CommandSubscribe cmd3 = new CommandSubscribe().setConsumerId(2).setConsumerName("C2").setTopic(successPartitionTopicName).setSubscription(successSubName).setReadCompacted(false).setRequestId(1).setSubType(SubType.Failover);
    Future<Consumer> f3 = topic2.subscribe(getSubscriptionOption(cmd3));
    f3.get();
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 1);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C1");
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerId(), 2);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerName(), "C2");
    // 4. Subscribe and create third duplicate consumer
    CommandSubscribe cmd4 = new CommandSubscribe().setConsumerId(3).setConsumerName("C1").setTopic(successPartitionTopicName).setSubscription(successSubName).setReadCompacted(false).setRequestId(1).setSubType(SubType.Failover);
    Future<Consumer> f4 = topic2.subscribe(getSubscriptionOption(cmd4));
    f4.get();
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 1);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C1");
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerId(), 3);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerName(), "C1");
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(2).consumerId(), 2);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(2).consumerName(), "C2");
    // 5. Subscribe on partition topic with existing consumer id and different sub type
    CommandSubscribe cmd5 = new CommandSubscribe().setConsumerId(2).setConsumerName("C1").setTopic(successPartitionTopicName).setSubscription(successSubName).setReadCompacted(false).setRequestId(1).setSubType(SubType.Exclusive);
    Future<Consumer> f5 = topic2.subscribe(getSubscriptionOption(cmd5));
    try {
        f5.get();
        fail("should fail with exception");
    } catch (ExecutionException ee) {
        // Expected
        assertTrue(ee.getCause() instanceof BrokerServiceException.SubscriptionBusyException);
    }
    // 6. Subscribe on partition topic with different sub name, type and different consumer id
    CommandSubscribe cmd6 = new CommandSubscribe().setConsumerId(4).setConsumerName("C3").setTopic(successPartitionTopicName).setSubscription(successSubName2).setReadCompacted(false).setRequestId(1).setSubType(SubType.Exclusive);
    Future<Consumer> f6 = topic2.subscribe(getSubscriptionOption(cmd6));
    f6.get();
    // 7. unsubscribe exclusive sub
    Future<Void> f7 = topic2.unsubscribe(successSubName2);
    f7.get();
    assertNull(topic2.getSubscription(successSubName2));
    // 8. unsubscribe active consumer from shared sub.
    PersistentSubscription sub = topic2.getSubscription(successSubName);
    Consumer cons = sub.getDispatcher().getConsumers().get(0);
    sub.removeConsumer(cons);
    // Verify second consumer become active
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 3);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C1");
    // 9. unsubscribe active consumer from shared sub.
    cons = sub.getDispatcher().getConsumers().get(0);
    sub.removeConsumer(cons);
    // Verify second consumer become active
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 2);
    assertEquals(topic2.getSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C2");
    // 10. unsubscribe shared sub
    Future<Void> f8 = topic2.unsubscribe(successSubName);
    f8.get();
    assertNull(topic2.getSubscription(successSubName));
}
Also used : CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ExecutionException(java.util.concurrent.ExecutionException) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 5 with CommandSubscribe

use of org.apache.pulsar.common.api.proto.CommandSubscribe in project pulsar by apache.

the class PersistentTopicTest method testDeleteTopic.

@Test
public void testDeleteTopic() throws Exception {
    // create topic
    PersistentTopic topic = (PersistentTopic) brokerService.getOrCreateTopic(successTopicName).get();
    Field isFencedField = AbstractTopic.class.getDeclaredField("isFenced");
    isFencedField.setAccessible(true);
    Field isClosingOrDeletingField = PersistentTopic.class.getDeclaredField("isClosingOrDeleting");
    isClosingOrDeletingField.setAccessible(true);
    assertFalse((boolean) isFencedField.get(topic));
    assertFalse((boolean) isClosingOrDeletingField.get(topic));
    String role = "appid1";
    // 1. delete inactive topic
    topic.delete().get();
    assertFalse(brokerService.getTopicReference(successTopicName).isPresent());
    assertTrue((boolean) isFencedField.get(topic));
    assertTrue((boolean) isClosingOrDeletingField.get(topic));
    // 2. publish message to deleted topic
    ByteBuf payload = Unpooled.wrappedBuffer("content".getBytes());
    final CountDownLatch latch = new CountDownLatch(1);
    topic.publishMessage(payload, (exception, ledgerId, entryId) -> {
        assertTrue(exception instanceof BrokerServiceException.TopicFencedException);
        latch.countDown();
    });
    assertTrue(latch.await(1, TimeUnit.SECONDS));
    assertTrue((boolean) isFencedField.get(topic));
    assertTrue((boolean) isClosingOrDeletingField.get(topic));
    // 3. delete topic with producer
    topic = (PersistentTopic) brokerService.getOrCreateTopic(successTopicName).get();
    Producer producer = new Producer(topic, serverCnx, 1, /* producer id */
    "prod-name", role, false, null, SchemaVersion.Latest, 0, false, ProducerAccessMode.Shared, Optional.empty(), true);
    topic.addProducer(producer, new CompletableFuture<>()).join();
    assertTrue(topic.delete().isCompletedExceptionally());
    assertFalse((boolean) isFencedField.get(topic));
    assertFalse((boolean) isClosingOrDeletingField.get(topic));
    topic.removeProducer(producer);
    // 4. delete topic with subscriber
    CommandSubscribe cmd = new CommandSubscribe().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setConsumerName("consumer-name").setRequestId(1).setSubType(SubType.Exclusive);
    Future<Consumer> f1 = topic.subscribe(getSubscriptionOption(cmd));
    f1.get();
    assertTrue(topic.delete().isCompletedExceptionally());
    assertFalse((boolean) isFencedField.get(topic));
    assertFalse((boolean) isClosingOrDeletingField.get(topic));
    topic.unsubscribe(successSubName);
}
Also used : CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) Field(java.lang.reflect.Field) CompletableFuture(java.util.concurrent.CompletableFuture) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Mockito.anyString(org.mockito.Mockito.anyString) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

CommandSubscribe (org.apache.pulsar.common.api.proto.CommandSubscribe)15 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)13 Test (org.testng.annotations.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)8 PersistentDispatcherSingleActiveConsumer (org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)8 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)7 ExecutionException (java.util.concurrent.ExecutionException)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 TimeoutException (java.util.concurrent.TimeoutException)4 ByteBuf (io.netty.buffer.ByteBuf)3 Map (java.util.Map)3 KeySharedMeta (org.apache.pulsar.common.api.proto.KeySharedMeta)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 InetSocketAddress (java.net.InetSocketAddress)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Set (java.util.Set)2