Search in sources :

Example 1 with Consumer

use of org.apache.pulsar.broker.service.Consumer in project incubator-pulsar by apache.

the class NonPersistentDispatcherMultipleConsumers method sendMessages.

@Override
public void sendMessages(List<Entry> entries) {
    Consumer consumer = TOTAL_AVAILABLE_PERMITS_UPDATER.get(this) > 0 ? getNextConsumer() : null;
    if (consumer != null) {
        TOTAL_AVAILABLE_PERMITS_UPDATER.addAndGet(this, -consumer.sendMessages(entries).getTotalSentMessages());
    } else {
        entries.forEach(entry -> {
            int totalMsgs = getBatchSizeforEntry(entry.getDataBuffer(), subscription, -1);
            if (totalMsgs > 0) {
                msgDrop.recordEvent();
            }
            entry.release();
        });
    }
}
Also used : Consumer(org.apache.pulsar.broker.service.Consumer)

Example 2 with Consumer

use of org.apache.pulsar.broker.service.Consumer in project incubator-pulsar by apache.

the class NonPersistentDispatcherSingleActiveConsumer method sendMessages.

@Override
public void sendMessages(List<Entry> entries) {
    Consumer currentConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
    if (currentConsumer != null && currentConsumer.getAvailablePermits() > 0 && currentConsumer.isWritable()) {
        currentConsumer.sendMessages(entries);
    } else {
        entries.forEach(entry -> {
            int totalMsgs = getBatchSizeforEntry(entry.getDataBuffer(), subscription, -1);
            if (totalMsgs > 0) {
                msgDrop.recordEvent();
            }
            entry.release();
        });
    }
}
Also used : AbstractDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.AbstractDispatcherSingleActiveConsumer) Consumer(org.apache.pulsar.broker.service.Consumer)

Example 3 with Consumer

use of org.apache.pulsar.broker.service.Consumer in project incubator-pulsar by apache.

the class AbstractDispatcherSingleActiveConsumer method pickAndScheduleActiveConsumer.

/**
 * @return the previous active consumer if the consumer is changed, otherwise null.
 */
protected boolean pickAndScheduleActiveConsumer() {
    checkArgument(!consumers.isEmpty());
    consumers.sort((c1, c2) -> c1.consumerName().compareTo(c2.consumerName()));
    int index = partitionIndex % consumers.size();
    Consumer prevConsumer = ACTIVE_CONSUMER_UPDATER.getAndSet(this, consumers.get(index));
    Consumer activeConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
    if (prevConsumer == activeConsumer) {
        // Active consumer did not change. Do nothing at this point
        return false;
    } else {
        // If the active consumer is changed, send notification.
        scheduleReadOnActiveConsumer();
        return true;
    }
}
Also used : Consumer(org.apache.pulsar.broker.service.Consumer)

Example 4 with Consumer

use of org.apache.pulsar.broker.service.Consumer in project incubator-pulsar by apache.

the class PersistentTopicConcurrentTest method testConcurrentTopicGCAndSubscriptionDelete.

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

        public void run() {
            try {
                barrier.await();
                // assertTrue(topic.unsubscribe(successSubName).isDone());
                // Thread.sleep(5,0);
                log.info("{} forcing topic GC ", Thread.currentThread());
                for (int i = 0; i < 2000; i++) {
                    topic.checkGC(0);
                }
                log.info("GC done..");
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread unsubscriber = new Thread() {

        public void run() {
            try {
                barrier.await();
                // do subscription delete
                ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
                PersistentSubscription ps = subscriptions.get(successSubName);
                // Thread.sleep(2,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 : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(org.apache.pulsar.broker.service.Consumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic)

Example 5 with Consumer

use of org.apache.pulsar.broker.service.Consumer in project incubator-pulsar by apache.

the class PersistentTopicConcurrentTest method testConcurrentTopicDeleteAndUnsubscribe.

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

        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();
    assertEquals(gotException.get(), false);
}
Also used : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) CountDownLatch(java.util.concurrent.CountDownLatch) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(org.apache.pulsar.broker.service.Consumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic)

Aggregations

Consumer (org.apache.pulsar.broker.service.Consumer)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ConsumerBusyException (org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException)6 AbstractDispatcherSingleActiveConsumer (org.apache.pulsar.broker.service.AbstractDispatcherSingleActiveConsumer)5 BrokerServiceException (org.apache.pulsar.broker.service.BrokerServiceException)5 NamingException (org.apache.pulsar.broker.service.BrokerServiceException.NamingException)5 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)5 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)5 TopicFencedException (org.apache.pulsar.broker.service.BrokerServiceException.TopicFencedException)5 UnsupportedVersionException (org.apache.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)4 Entry (org.apache.bookkeeper.mledger.Entry)4 ProducerBusyException (org.apache.pulsar.broker.service.BrokerServiceException.ProducerBusyException)4 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)4 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)4 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)4 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)4 PulsarApi (org.apache.pulsar.common.api.proto.PulsarApi)4