Search in sources :

Example 6 with SubscriptionBusyException

use of org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException 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)

Aggregations

SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)6 CompletableFuture (java.util.concurrent.CompletableFuture)4 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)4 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)3 Subscription (org.apache.pulsar.broker.service.Subscription)3 Topic (org.apache.pulsar.broker.service.Topic)3 KeeperException (org.apache.zookeeper.KeeperException)3 ExecutionException (java.util.concurrent.ExecutionException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)2 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)2 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)2 BrokerServiceException (org.apache.pulsar.broker.service.BrokerServiceException)2 PersistenceException (org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException)2 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)2 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)2 RestException (org.apache.pulsar.broker.web.RestException)2 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)2 ObjectObjectHashMap (com.carrotsearch.hppc.ObjectObjectHashMap)1 MoreObjects (com.google.common.base.MoreObjects)1