Search in sources :

Example 6 with CommandSubscribe

use of com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe in project pulsar by yahoo.

the class PersistentTopicTest method testFailoverSubscription.

@Test
public void testFailoverSubscription() throws Exception {
    PersistentTopic topic1 = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    CommandSubscribe cmd1 = CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Failover).build();
    // 1. Subscribe with non partition topic
    Future<Consumer> f1 = topic1.subscribe(serverCnx, cmd1.getSubscription(), cmd1.getConsumerId(), cmd1.getSubType(), 0, cmd1.getConsumerName());
    f1.get();
    // 2. Subscribe with partition topic
    PersistentTopic topic2 = new PersistentTopic(successPartitionTopicName, ledgerMock, brokerService);
    CommandSubscribe cmd2 = CommandSubscribe.newBuilder().setConsumerId(1).setConsumerName("C1").setTopic(successPartitionTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Failover).build();
    Future<Consumer> f2 = topic2.subscribe(serverCnx, cmd2.getSubscription(), cmd2.getConsumerId(), cmd2.getSubType(), 0, cmd2.getConsumerName());
    f2.get();
    // 3. Subscribe and create second consumer
    CommandSubscribe cmd3 = CommandSubscribe.newBuilder().setConsumerId(2).setConsumerName("C2").setTopic(successPartitionTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Failover).build();
    Future<Consumer> f3 = topic2.subscribe(serverCnx, cmd3.getSubscription(), cmd3.getConsumerId(), cmd3.getSubType(), 0, cmd3.getConsumerName());
    f3.get();
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 1);
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C1");
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerId(), 2);
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerName(), "C2");
    // 4. Subscribe and create third duplicate consumer
    CommandSubscribe cmd4 = CommandSubscribe.newBuilder().setConsumerId(3).setConsumerName("C1").setTopic(successPartitionTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Failover).build();
    Future<Consumer> f4 = topic2.subscribe(serverCnx, cmd4.getSubscription(), cmd4.getConsumerId(), cmd4.getSubType(), 0, cmd4.getConsumerName());
    f4.get();
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 1);
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C1");
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerId(), 3);
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(1).consumerName(), "C1");
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(2).consumerId(), 2);
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(2).consumerName(), "C2");
    // 5. Subscribe on partition topic with existing consumer id and different sub type
    CommandSubscribe cmd5 = CommandSubscribe.newBuilder().setConsumerId(2).setConsumerName("C1").setTopic(successPartitionTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Exclusive).build();
    Future<Consumer> f5 = topic2.subscribe(serverCnx, cmd5.getSubscription(), cmd5.getConsumerId(), cmd5.getSubType(), 0, cmd5.getConsumerName());
    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 = CommandSubscribe.newBuilder().setConsumerId(4).setConsumerName("C3").setTopic(successPartitionTopicName).setSubscription(successSubName2).setRequestId(1).setSubType(SubType.Exclusive).build();
    Future<Consumer> f6 = topic2.subscribe(serverCnx, cmd6.getSubscription(), cmd6.getConsumerId(), cmd6.getSubType(), 0, cmd6.getConsumerName());
    f6.get();
    // 7. unsubscribe exclusive sub
    Future<Void> f7 = topic2.unsubscribe(successSubName2);
    f7.get();
    assertNull(topic2.getPersistentSubscription(successSubName2));
    // 8. unsubscribe active consumer from shared sub.
    PersistentSubscription sub = topic2.getPersistentSubscription(successSubName);
    Consumer cons = sub.getDispatcher().getConsumers().get(0);
    sub.removeConsumer(cons);
    // Verify second consumer become active
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 3);
    assertEquals(topic2.getPersistentSubscription(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.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerId(), 2);
    assertEquals(topic2.getPersistentSubscription(successSubName).getDispatcher().getConsumers().get(0).consumerName(), "C2");
    // 10. unsubscribe shared sub
    Future<Void> f8 = topic2.unsubscribe(successSubName);
    f8.get();
    assertNull(topic2.getPersistentSubscription(successSubName));
}
Also used : CommandSubscribe(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe) PersistentDispatcherSingleActiveConsumer(com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ExecutionException(java.util.concurrent.ExecutionException) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 7 with CommandSubscribe

use of com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe in project pulsar by yahoo.

the class PersistentTopicTest method testConcurrentTopicAndSubscriptionDelete.

// @Test
public void testConcurrentTopicAndSubscriptionDelete() throws Exception {
    // create topic
    final PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
    CommandSubscribe cmd = CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(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() {

        @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();
    assertEquals(gotException.get(), false);
}
Also used : CommandSubscribe(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PersistentDispatcherSingleActiveConsumer(com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) TimeoutException(java.util.concurrent.TimeoutException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 8 with CommandSubscribe

use of com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe in project pulsar by yahoo.

the class PersistentTopicTest method testSubscribeUnsubscribe.

@Test
public void testSubscribeUnsubscribe() throws Exception {
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    CommandSubscribe cmd = CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Exclusive).build();
    // 1. simple subscribe
    Future<Consumer> f1 = topic.subscribe(serverCnx, cmd.getSubscription(), cmd.getConsumerId(), cmd.getSubType(), 0, cmd.getConsumerName());
    f1.get();
    // 2. duplicate subscribe
    Future<Consumer> f2 = topic.subscribe(serverCnx, cmd.getSubscription(), cmd.getConsumerId(), cmd.getSubType(), 0, cmd.getConsumerName());
    try {
        f2.get();
        fail("should fail with exception");
    } catch (ExecutionException ee) {
        // Expected
        assertTrue(ee.getCause() instanceof BrokerServiceException.ConsumerBusyException);
    }
    // 3. simple unsubscribe
    Future<Void> f3 = topic.unsubscribe(successSubName);
    f3.get();
    assertNull(topic.getPersistentSubscription(successSubName));
}
Also used : CommandSubscribe(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe) PersistentDispatcherSingleActiveConsumer(com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

CommandSubscribe (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe)8 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)7 PersistentDispatcherSingleActiveConsumer (com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)6 ExecutionException (java.util.concurrent.ExecutionException)5 Test (org.testng.annotations.Test)5 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)3 Matchers.anyString (org.mockito.Matchers.anyString)3 ByteBuf (io.netty.buffer.ByteBuf)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 PersistentTopics.getPartitionedTopicMetadata (com.yahoo.pulsar.broker.admin.PersistentTopics.getPartitionedTopicMetadata)1 AuthenticationDataCommand (com.yahoo.pulsar.broker.authentication.AuthenticationDataCommand)1 DestinationLookup.lookupDestinationAsync (com.yahoo.pulsar.broker.lookup.DestinationLookup.lookupDestinationAsync)1 ServiceUnitNotReadyException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)1 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)1 Commands (com.yahoo.pulsar.common.api.Commands)1