Search in sources :

Example 41 with PersistentTopic

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

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);
    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());
    f1.get();
    assertTrue(topic.delete().isCompletedExceptionally());
    topic.unsubscribe(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) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 42 with PersistentTopic

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

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());
    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(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) 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 43 with PersistentTopic

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

the class PersistentTopicTest method testAddRemoveProducer.

@Test
public void testAddRemoveProducer() throws Exception {
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    String role = "appid1";
    // 1. simple add producer
    Producer producer = new Producer(topic, serverCnx, 1, /* producer id */
    "prod-name", role);
    topic.addProducer(producer);
    assertEquals(topic.getProducers().size(), 1);
    // 2. duplicate add
    try {
        topic.addProducer(producer);
        fail("Should have failed with naming exception because producer 'null' is already connected to the topic");
    } catch (BrokerServiceException e) {
        assertTrue(e instanceof BrokerServiceException.NamingException);
    }
    assertEquals(topic.getProducers().size(), 1);
    // 3. add producer for a different topic
    PersistentTopic failTopic = new PersistentTopic(failTopicName, ledgerMock, brokerService);
    Producer failProducer = new Producer(failTopic, serverCnx, 2, /* producer id */
    "prod-name", role);
    try {
        topic.addProducer(failProducer);
        fail("should have failed");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // 4. simple remove producer
    topic.removeProducer(producer);
    assertEquals(topic.getProducers().size(), 0);
    // 5. duplicate remove
    topic.removeProducer(producer);
/* noop */
}
Also used : PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 44 with PersistentTopic

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

the class PersistentTopicTest method testDispatcherSingleConsumerReadFailed.

@Test
public void testDispatcherSingleConsumerReadFailed() throws Exception {
    PersistentTopic topic = spy(new PersistentTopic(successTopicName, ledgerMock, brokerService));
    ManagedCursor cursor = mock(ManagedCursor.class);
    when(cursor.getName()).thenReturn("cursor");
    PersistentDispatcherSingleActiveConsumer dispatcher = new PersistentDispatcherSingleActiveConsumer(cursor, SubType.Exclusive, 1, topic);
    Consumer consumer = mock(Consumer.class);
    dispatcher.readEntriesFailed(new ManagedLedgerException.InvalidCursorPositionException("failed"), consumer);
    verify(topic, atLeast(1)).getBrokerService();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PersistentDispatcherSingleActiveConsumer(com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) PersistentDispatcherSingleActiveConsumer(com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 45 with PersistentTopic

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

the class PersistentTopicTest method testPublishMessageMLFailure.

@Test
public void testPublishMessageMLFailure() throws Exception {
    final String successTopicName = "persistent://prop/use/ns-abc/successTopic";
    final ManagedLedger ledgerMock = mock(ManagedLedger.class);
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    MessageMetadata.Builder messageMetadata = MessageMetadata.newBuilder();
    messageMetadata.setPublishTime(System.currentTimeMillis());
    messageMetadata.setProducerName("prod-name");
    messageMetadata.setSequenceId(1);
    ByteBuf payload = Unpooled.wrappedBuffer("content".getBytes());
    final CountDownLatch latch = new CountDownLatch(1);
    // override asyncAddEntry callback to return error
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((AddEntryCallback) invocationOnMock.getArguments()[1]).addFailed(new ManagedLedgerException("Managed ledger failure"), invocationOnMock.getArguments()[2]);
            return null;
        }
    }).when(ledgerMock).asyncAddEntry(any(ByteBuf.class), any(AddEntryCallback.class), anyObject());
    topic.publishMessage(payload, (exception, ledgerId, entryId) -> {
        if (exception == null) {
            fail("publish should have failed");
        } else {
            latch.countDown();
        }
    });
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Matchers.anyString(org.mockito.Matchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) MessageMetadata(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyObject(org.mockito.Matchers.anyObject) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) 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