Search in sources :

Example 16 with Consumer

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

the class PersistentTopicConcurrentTest method testConcurrentTopicAndSubscriptionDelete.

// @Test
public void testConcurrentTopicAndSubscriptionDelete() 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("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();
                // 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 17 with Consumer

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

the class AbstractDispatcherSingleActiveConsumer method addConsumer.

public synchronized void addConsumer(Consumer consumer) throws BrokerServiceException {
    if (IS_CLOSED_UPDATER.get(this) == TRUE) {
        log.warn("[{}] Dispatcher is already closed. Closing consumer ", this.topicName, consumer);
        consumer.disconnect();
    }
    if (subscriptionType == SubType.Exclusive && !consumers.isEmpty()) {
        throw new ConsumerBusyException("Exclusive consumer is already connected");
    }
    if (isConsumersExceededOnTopic()) {
        log.warn("[{}] Attempting to add consumer to topic which reached max consumers limit", this.topicName);
        throw new ConsumerBusyException("Topic reached max consumers limit");
    }
    if (subscriptionType == SubType.Failover && isConsumersExceededOnSubscription()) {
        log.warn("[{}] Attempting to add consumer to subscription which reached max consumers limit", this.topicName);
        throw new ConsumerBusyException("Subscription reached max consumers limit");
    }
    consumers.add(consumer);
    if (!pickAndScheduleActiveConsumer()) {
        // the active consumer is not changed
        Consumer currentActiveConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
        if (null == currentActiveConsumer) {
            if (log.isDebugEnabled()) {
                log.debug("Current active consumer disappears while adding consumer {}", consumer);
            }
        } else {
            consumer.notifyActiveConsumerChange(currentActiveConsumer);
        }
    }
}
Also used : ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) Consumer(org.apache.pulsar.broker.service.Consumer)

Example 18 with Consumer

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

the class NonPersistentTopic method subscribe.

@Override
public CompletableFuture<Consumer> subscribe(final ServerCnx cnx, String subscriptionName, long consumerId, SubType subType, int priorityLevel, String consumerName, boolean isDurable, MessageId startMessageId, Map<String, String> metadata, boolean readCompacted, InitialPosition initialPosition) {
    final CompletableFuture<Consumer> future = new CompletableFuture<>();
    if (hasBatchMessagePublished && !cnx.isBatchMessageCompatibleVersion()) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Consumer doesn't support batch-message {}", topic, subscriptionName);
        }
        future.completeExceptionally(new UnsupportedVersionException("Consumer doesn't support batch-message"));
        return future;
    }
    if (subscriptionName.startsWith(replicatorPrefix)) {
        log.warn("[{}] Failed to create subscription for {}", topic, subscriptionName);
        future.completeExceptionally(new NamingException("Subscription with reserved subscription name attempted"));
        return future;
    }
    if (readCompacted) {
        future.completeExceptionally(new NotAllowedException("readCompacted only valid on persistent topics"));
        return future;
    }
    lock.readLock().lock();
    try {
        if (isFenced) {
            log.warn("[{}] Attempting to subscribe to a fenced topic", topic);
            future.completeExceptionally(new TopicFencedException("Topic is temporarily unavailable"));
            return future;
        }
        USAGE_COUNT_UPDATER.incrementAndGet(this);
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] [{}] Added consumer -- count: {}", topic, subscriptionName, consumerName, USAGE_COUNT_UPDATER.get(this));
        }
    } finally {
        lock.readLock().unlock();
    }
    NonPersistentSubscription subscription = subscriptions.computeIfAbsent(subscriptionName, name -> new NonPersistentSubscription(this, subscriptionName));
    try {
        Consumer consumer = new Consumer(subscription, subType, topic, consumerId, priorityLevel, consumerName, 0, cnx, cnx.getRole(), metadata, readCompacted, initialPosition);
        subscription.addConsumer(consumer);
        if (!cnx.isActive()) {
            consumer.close();
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] [{}] Subscribe failed -- count: {}", topic, subscriptionName, consumer.consumerName(), USAGE_COUNT_UPDATER.get(NonPersistentTopic.this));
            }
            future.completeExceptionally(new BrokerServiceException("Connection was closed while the opening the cursor "));
        } else {
            log.info("[{}][{}] Created new subscription for {}", topic, subscriptionName, consumerId);
            future.complete(consumer);
        }
    } catch (BrokerServiceException e) {
        if (e instanceof ConsumerBusyException) {
            log.warn("[{}][{}] Consumer {} {} already connected", topic, subscriptionName, consumerId, consumerName);
        } else if (e instanceof SubscriptionBusyException) {
            log.warn("[{}][{}] {}", topic, subscriptionName, e.getMessage());
        }
        USAGE_COUNT_UPDATER.decrementAndGet(NonPersistentTopic.this);
        future.completeExceptionally(e);
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) TopicFencedException(org.apache.pulsar.broker.service.BrokerServiceException.TopicFencedException) Consumer(org.apache.pulsar.broker.service.Consumer) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) UnsupportedVersionException(org.apache.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException)

Example 19 with Consumer

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

the class PersistentDispatcherMultipleConsumers method readEntriesFailed.

@Override
public synchronized void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
    ReadType readType = (ReadType) ctx;
    long waitTimeMillis = readFailureBackoff.next();
    if (exception instanceof NoMoreEntriesToReadException) {
        if (cursor.getNumberOfEntriesInBacklog() == 0) {
            // Topic has been terminated and there are no more entries to read
            // Notify the consumer only if all the messages were already acknowledged
            consumerList.forEach(Consumer::reachedEndOfTopic);
        }
    } else if (!(exception instanceof TooManyRequestsException)) {
        log.error("[{}] Error reading entries at {} : {}, Read Type {} - Retrying to read in {} seconds", name, cursor.getReadPosition(), exception.getMessage(), readType, waitTimeMillis / 1000.0);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Error reading entries at {} : {}, Read Type {} - Retrying to read in {} seconds", name, cursor.getReadPosition(), exception.getMessage(), readType, waitTimeMillis / 1000.0);
        }
    }
    if (shouldRewindBeforeReadingOrReplaying) {
        shouldRewindBeforeReadingOrReplaying = false;
        cursor.rewind();
    }
    if (readType == ReadType.Normal) {
        havePendingRead = false;
    } else {
        havePendingReplayRead = false;
        if (exception instanceof ManagedLedgerException.InvalidReplayPositionException) {
            PositionImpl markDeletePosition = (PositionImpl) cursor.getMarkDeletedPosition();
            messagesToReplay.removeIf((ledgerId, entryId) -> {
                return ComparisonChain.start().compare(ledgerId, markDeletePosition.getLedgerId()).compare(entryId, markDeletePosition.getEntryId()).result() <= 0;
            });
        }
    }
    readBatchSize = 1;
    topic.getBrokerService().executor().schedule(() -> {
        synchronized (PersistentDispatcherMultipleConsumers.this) {
            if (!havePendingRead) {
                log.info("[{}] Retrying read operation", name);
                readMoreEntries();
            } else {
                log.info("[{}] Skipping read retry: havePendingRead {}", name, havePendingRead, exception);
            }
        }
    }, waitTimeMillis, TimeUnit.MILLISECONDS);
}
Also used : NoMoreEntriesToReadException(org.apache.bookkeeper.mledger.ManagedLedgerException.NoMoreEntriesToReadException) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) Consumer(org.apache.pulsar.broker.service.Consumer) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl)

Example 20 with Consumer

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

the class PersistentDispatcherSingleActiveConsumer method scheduleReadOnActiveConsumer.

protected void scheduleReadOnActiveConsumer() {
    if (havePendingRead && cursor.cancelPendingReadRequest()) {
        havePendingRead = false;
    }
    if (havePendingRead) {
        return;
    }
    if (subscriptionType != SubType.Failover || serviceConfig.getActiveConsumerFailoverDelayTimeMillis() <= 0) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Rewind cursor and read more entries without delay", name);
        }
        cursor.rewind();
        Consumer activeConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
        notifyActiveConsumerChanged(activeConsumer);
        readMoreEntries(activeConsumer);
        return;
    }
    if (readOnActiveConsumerTask != null) {
        return;
    }
    readOnActiveConsumerTask = topic.getBrokerService().executor().schedule(() -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Rewind cursor and read more entries after {} ms delay", name, serviceConfig.getActiveConsumerFailoverDelayTimeMillis());
        }
        cursor.rewind();
        Consumer activeConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
        notifyActiveConsumerChanged(activeConsumer);
        readMoreEntries(activeConsumer);
        readOnActiveConsumerTask = null;
    }, serviceConfig.getActiveConsumerFailoverDelayTimeMillis(), TimeUnit.MILLISECONDS);
}
Also used : Consumer(org.apache.pulsar.broker.service.Consumer) AbstractDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.AbstractDispatcherSingleActiveConsumer)

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