Search in sources :

Example 21 with PersistentSubscription

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

the class PersistentTopicE2ETest method testTopicDeleteWithDisconnectedSubscription.

@Test
public void testTopicDeleteWithDisconnectedSubscription() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic8";
    final String subName = "sub1";
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    // 1. client connect
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(topicRef);
    assertNotNull(subRef);
    assertTrue(subRef.getDispatcher().isConsumerConnected());
    // 2. client disconnect
    consumer.close();
    assertFalse(subRef.getDispatcher().isConsumerConnected());
    // 3. delete topic
    admin.persistentTopics().delete(topicName);
    try {
        admin.persistentTopics().getStats(topicName);
    } catch (PulsarAdminException e) {
    // ok
    }
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 22 with PersistentSubscription

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

the class PersistentTopicE2ETest method testConsumerFlowControl.

@Test
public void testConsumerFlowControl() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic2";
    final String subName = "sub2";
    Message msg;
    int recvQueueSize = 4;
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    conf.setReceiverQueueSize(recvQueueSize);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(subRef);
    // 1. initial receive queue size recorded
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(getAvailablePermits(subRef), recvQueueSize);
    for (int i = 0; i < recvQueueSize / 2; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    // 2. queue size re-adjusted after successful receive of half of window size
    Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
    assertEquals(getAvailablePermits(subRef), recvQueueSize);
    consumer.close();
    assertFalse(subRef.getDispatcher().isConsumerConnected());
}
Also used : Message(com.yahoo.pulsar.client.api.Message) 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)

Example 23 with PersistentSubscription

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

the class PersistentTopicE2ETest method testSimpleCloseTopic.

@Test
public void testSimpleCloseTopic() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic5";
    final String subName = "sub5";
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
    Producer producer = pulsarClient.createProducer(topicName);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
    assertNotNull(subRef);
    Message msg;
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
        msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    producer.close();
    consumer.close();
    topicRef.close().get();
    assertNull(pulsar.getBrokerService().getTopicReference(topicName));
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) 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)

Example 24 with PersistentSubscription

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

the class PersistentTopicTest method testAddRemoveConsumer.

@Test
public void testAddRemoveConsumer() throws Exception {
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    PersistentSubscription sub = new PersistentSubscription(topic, cursorMock);
    // 1. simple add consumer
    Consumer consumer = new Consumer(sub, SubType.Exclusive, 1, /* consumer id */
    0, "Cons1", /* consumer name */
    50000, serverCnx, "myrole-1");
    sub.addConsumer(consumer);
    assertTrue(sub.getDispatcher().isConsumerConnected());
    // 2. duplicate add consumer
    try {
        sub.addConsumer(consumer);
        fail("Should fail with ConsumerBusyException");
    } catch (BrokerServiceException e) {
        assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
    }
    // 3. simple remove consumer
    sub.removeConsumer(consumer);
    assertFalse(sub.getDispatcher().isConsumerConnected());
    // 4. duplicate remove consumer
    try {
        sub.removeConsumer(consumer);
        fail("Should fail with ServerMetadataException");
    } catch (BrokerServiceException e) {
        assertTrue(e instanceof BrokerServiceException.ServerMetadataException);
    }
}
Also used : PersistentDispatcherSingleActiveConsumer(com.yahoo.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 25 with PersistentSubscription

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

the class ServerCnx method handleConsumerStats.

@Override
protected void handleConsumerStats(CommandConsumerStats commandConsumerStats) {
    if (log.isDebugEnabled()) {
        log.debug("Received CommandConsumerStats call from {}", remoteAddress);
    }
    final long requestId = commandConsumerStats.getRequestId();
    final String topicName = commandConsumerStats.getTopicName();
    final String subscriptionName = commandConsumerStats.getSubscriptionName();
    final long consumerId = commandConsumerStats.getConsumerId();
    if (log.isDebugEnabled()) {
        log.debug("CommandConsumerStats[requestId = {}, topicName = {}, subscriptionName = {}, consumerId = {}]", requestId, topicName, subscriptionName, consumerId);
    }
    ByteBuf msg = null;
    try {
        PersistentTopic topic = (PersistentTopic) getBrokerService().getTopicReference(topicName);
        if (topic != null) {
            if (topic.getSubscriptions().containsKey(subscriptionName)) {
                PersistentSubscription subscription = topic.getSubscriptions().get(subscriptionName);
                boolean consumerFound = false;
                for (Consumer consumer : subscription.getConsumers()) {
                    if (consumer.consumerId() == consumerId) {
                        consumerFound = true;
                        msg = Commands.newConsumerStatsResponse(createConsumerStatsResponse(consumer, subscription, requestId));
                        break;
                    }
                }
                if (!consumerFound) {
                    log.error("Failed to get consumer-stats response - Consumer not found for CommandConsumerStats[remoteAddress = {}, requestId = {}, topicName = {}, subscriptionName = {}, consumerId = {}]", remoteAddress, requestId, topicName, subscriptionName, consumerId);
                    msg = Commands.newConsumerStatsResponse(ServerError.ConsumerNotFound, "Consumer " + consumerId + " not found", requestId);
                }
            } else {
                log.error("Failed to get consumer-stats response - Subscription  not found for CommandConsumerStats[remoteAddress = {}, requestId = {}, topicName = {}, subscriptionName = {}, consumerId = {}]", remoteAddress, requestId, topicName, subscriptionName, consumerId);
                msg = Commands.newConsumerStatsResponse(ServerError.SubscriptionNotFound, "Subscription " + subscriptionName + " not found", requestId);
            }
        } else {
            log.error("Failed to get consumer-stats response - Topic not found for CommandConsumerStats[remoteAddress = {}, requestId = {}, topicName = {}, subscriptionName = {}, consumerId = {}]", remoteAddress, requestId, topicName, subscriptionName, consumerId);
            msg = Commands.newConsumerStatsResponse(ServerError.TopicNotFound, "Topic " + topicName + " not found", requestId);
        }
    } catch (Exception e) {
        log.error("Failed to get consumer-stats response - Exception: {} for CommandConsumerStats[remoteAddress = {}, requestId = {}, topicName = {}, subscriptionName = {}, consumerId = {}]", e, remoteAddress, requestId, topicName, subscriptionName, consumerId);
        msg = Commands.newConsumerStatsResponse(ServerError.UnknownError, "Exception: " + e, requestId);
    } finally {
        if (msg != null) {
            ctx.writeAndFlush(msg);
        }
    }
}
Also used : CommandCloseConsumer(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ByteBuf(io.netty.buffer.ByteBuf) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) AuthenticationException(javax.naming.AuthenticationException) ServiceUnitNotReadyException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

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