Search in sources :

Example 1 with CommandSubscribe

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

the class Commands method newSubscribe.

public static ByteBuf newSubscribe(String topic, String subscription, long consumerId, long requestId, SubType subType, int priorityLevel, String consumerName, boolean isDurable, MessageIdData startMessageId, Map<String, String> metadata, boolean readCompacted, InitialPosition subscriptionInitialPosition) {
    CommandSubscribe.Builder subscribeBuilder = CommandSubscribe.newBuilder();
    subscribeBuilder.setTopic(topic);
    subscribeBuilder.setSubscription(subscription);
    subscribeBuilder.setSubType(subType);
    subscribeBuilder.setConsumerId(consumerId);
    subscribeBuilder.setConsumerName(consumerName);
    subscribeBuilder.setRequestId(requestId);
    subscribeBuilder.setPriorityLevel(priorityLevel);
    subscribeBuilder.setDurable(isDurable);
    subscribeBuilder.setReadCompacted(readCompacted);
    subscribeBuilder.setInitialPosition(subscriptionInitialPosition);
    if (startMessageId != null) {
        subscribeBuilder.setStartMessageId(startMessageId);
    }
    subscribeBuilder.addAllMetadata(CommandUtils.toKeyValueList(metadata));
    CommandSubscribe subscribe = subscribeBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.SUBSCRIBE).setSubscribe(subscribe));
    subscribeBuilder.recycle();
    subscribe.recycle();
    return res;
}
Also used : CommandSubscribe(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with CommandSubscribe

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

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(), cmd.getDurable(), null, Collections.emptyMap(), cmd.getReadCompacted(), InitialPosition.Latest);
    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(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(org.apache.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 3 with CommandSubscribe

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

the class PersistentTopicTest method testDeleteTopicRaceConditions.

@Test
public void testDeleteTopicRaceConditions() throws Exception {
    PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
    // override ledger deletion callback to slow down deletion
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(1000);
            ((DeleteLedgerCallback) invocationOnMock.getArguments()[0]).deleteLedgerComplete(null);
            return null;
        }
    }).when(ledgerMock).asyncDelete(any(DeleteLedgerCallback.class), anyObject());
    ExecutorService executor = Executors.newCachedThreadPool();
    executor.submit(() -> {
        topic.delete();
        return null;
    }).get();
    try {
        String role = "appid1";
        Thread.sleep(10);
        /* delay to ensure that the delete gets executed first */
        Producer producer = new Producer(topic, serverCnx, 1, /* producer id */
        "prod-name", role, false, null, SchemaVersion.Latest);
        topic.addProducer(producer);
        fail("Should have failed");
    } catch (BrokerServiceException e) {
        assertTrue(e instanceof BrokerServiceException.TopicFencedException);
    }
    CommandSubscribe cmd = CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(SubType.Exclusive).build();
    Future<Consumer> f = topic.subscribe(serverCnx, cmd.getSubscription(), cmd.getConsumerId(), cmd.getSubType(), 0, cmd.getConsumerName(), cmd.getDurable(), null, Collections.emptyMap(), cmd.getReadCompacted(), InitialPosition.Latest);
    try {
        f.get();
        fail("should have failed");
    } catch (ExecutionException ee) {
        assertTrue(ee.getCause() instanceof BrokerServiceException.TopicFencedException);
    // Expected
    }
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) CommandSubscribe(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe) DeleteLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteLedgerCallback) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ExecutorService(java.util.concurrent.ExecutorService) Matchers.anyObject(org.mockito.Matchers.anyObject) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 4 with CommandSubscribe

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

the class PersistentTopicTest method testDeleteAndUnsubscribeTopic.

@Test
public void testDeleteAndUnsubscribeTopic() 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(), cmd.getDurable(), null, Collections.emptyMap(), cmd.getReadCompacted(), InitialPosition.Latest);
    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();
                assertFalse(topic.delete().isCompletedExceptionally());
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread unsubscriber = new Thread() {

        @Override
        public void run() {
            try {
                barrier.await();
                // do topic unsubscribe
                topic.unsubscribe(successSubName);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    unsubscriber.start();
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : CommandSubscribe(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 5 with CommandSubscribe

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

the class PersistentTopicTest method testDeleteTopic.

@Test
public void testDeleteTopic() throws Exception {
    // create topic
    PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
    String role = "appid1";
    // 1. delete inactive topic
    topic.delete().get();
    assertNull(brokerService.getTopicReference(successTopicName));
    // 2. delete topic with producer
    topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
    Producer producer = new Producer(topic, serverCnx, 1, /* producer id */
    "prod-name", role, false, null, SchemaVersion.Latest);
    topic.addProducer(producer);
    assertTrue(topic.delete().isCompletedExceptionally());
    topic.removeProducer(producer);
    // 3. delete topic with subscriber
    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(), cmd.getDurable(), null, Collections.emptyMap(), false, /* read compacted */
    InitialPosition.Latest);
    f1.get();
    assertTrue(topic.delete().isCompletedExceptionally());
    topic.unsubscribe(successSubName);
}
Also used : CommandSubscribe(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

CommandSubscribe (org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe)9 PersistentDispatcherSingleActiveConsumer (org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)7 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)7 ExecutionException (java.util.concurrent.ExecutionException)6 Test (org.testng.annotations.Test)6 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 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 GeneratedMessageLite (com.google.protobuf.GeneratedMessageLite)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelOption (io.netty.channel.ChannelOption)1 SslHandler (io.netty.handler.ssl.SslHandler)1 SocketAddress (java.net.SocketAddress)1