Search in sources :

Example 1 with BrokerServiceException

use of com.yahoo.pulsar.broker.service.BrokerServiceException in project pulsar by yahoo.

the class PersistentTopic method clearBacklog.

/**
     * Clears backlog for a given cursor in the topic.
     * <p>
     * Note: For a replication cursor, just provide the remote cluster name
     * </p>
     *
     * @param cursorName
     * @return
     */
public CompletableFuture<Void> clearBacklog(String cursorName) {
    log.info("[{}] Clearing backlog for cursor {} in the topic.", topic, cursorName);
    PersistentSubscription sub = getPersistentSubscription(cursorName);
    if (sub != null) {
        return sub.clearBacklog();
    }
    PersistentReplicator repl = getPersistentReplicator(cursorName);
    if (repl != null) {
        return repl.clearBacklog();
    }
    return FutureUtil.failedFuture(new BrokerServiceException("Cursor not found"));
}
Also used : BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException)

Example 2 with BrokerServiceException

use of com.yahoo.pulsar.broker.service.BrokerServiceException in project pulsar by yahoo.

the class PersistentTopic method subscribe.

@Override
public CompletableFuture<Consumer> subscribe(final ServerCnx cnx, String subscriptionName, long consumerId, SubType subType, int priorityLevel, String consumerName) {
    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;
    }
    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();
    }
    ledger.asyncOpenCursor(Codec.encode(subscriptionName), new OpenCursorCallback() {

        @Override
        public void openCursorComplete(ManagedCursor cursor, Object ctx) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Opened cursor for {} {}", topic, subscriptionName, consumerId, consumerName);
            }
            try {
                PersistentSubscription subscription = subscriptions.computeIfAbsent(subscriptionName, name -> new PersistentSubscription(PersistentTopic.this, cursor));
                Consumer consumer = new Consumer(subscription, subType, consumerId, priorityLevel, consumerName, brokerService.pulsar().getConfiguration().getMaxUnackedMessagesPerConsumer(), cnx, cnx.getRole());
                subscription.addConsumer(consumer);
                if (!cnx.isActive()) {
                    consumer.close();
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] [{}] [{}] Subscribe failed -- count: {}", topic, subscriptionName, consumer.consumerName(), USAGE_COUNT_UPDATER.get(PersistentTopic.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(PersistentTopic.this);
                future.completeExceptionally(e);
            }
        }

        @Override
        public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
            log.warn("[{}] Failed to create subscription for {}", topic, subscriptionName);
            USAGE_COUNT_UPDATER.decrementAndGet(PersistentTopic.this);
            future.completeExceptionally(new PersistenceException(exception));
        }
    }, null);
    return future;
}
Also used : ReplicationMetrics(com.yahoo.pulsar.broker.stats.ReplicationMetrics) SubType(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe.SubType) PersistentSubscriptionStats(com.yahoo.pulsar.common.policies.data.PersistentSubscriptionStats) LedgerInfo(com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats.LedgerInfo) NamingException(com.yahoo.pulsar.broker.service.BrokerServiceException.NamingException) ConsumerStats(com.yahoo.pulsar.common.policies.data.ConsumerStats) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) PersistentTopicInternalStats(com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats) LoggerFactory(org.slf4j.LoggerFactory) ObjectObjectHashMap(com.carrotsearch.hppc.ObjectObjectHashMap) NamespaceBundleStats(com.yahoo.pulsar.common.policies.data.loadbalancer.NamespaceBundleStats) Policies(com.yahoo.pulsar.common.policies.data.Policies) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) TopicBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ReplicatorStats(com.yahoo.pulsar.common.policies.data.ReplicatorStats) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) FutureUtil(com.yahoo.pulsar.client.util.FutureUtil) Objects(com.google.common.base.Objects) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) CursorStats(com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) Instant(java.time.Instant) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) TopicFencedException(com.yahoo.pulsar.broker.service.BrokerServiceException.TopicFencedException) Lists(com.beust.jcommander.internal.Lists) ZoneId(java.time.ZoneId) Sets(com.google.common.collect.Sets) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) BrokerService(com.yahoo.pulsar.broker.service.BrokerService) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Topic(com.yahoo.pulsar.broker.service.Topic) ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) Consumer(com.yahoo.pulsar.broker.service.Consumer) List(java.util.List) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) AdminResource(com.yahoo.pulsar.broker.admin.AdminResource) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) StatsOutputStream(com.yahoo.pulsar.utils.StatsOutputStream) Entry(org.apache.bookkeeper.mledger.Entry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) UnsupportedVersionException(com.yahoo.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) MessageImpl(com.yahoo.pulsar.client.impl.MessageImpl) PersistentTopicStats(com.yahoo.pulsar.common.policies.data.PersistentTopicStats) ServerCnx(com.yahoo.pulsar.broker.service.ServerCnx) ByteBuf(io.netty.buffer.ByteBuf) FastThreadLocal(io.netty.util.concurrent.FastThreadLocal) ConcurrentOpenHashSet(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashSet) ConsumerBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) ConcurrentOpenHashMap(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap) Codec(com.yahoo.pulsar.common.util.Codec) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) ClusterReplicationMetrics(com.yahoo.pulsar.broker.stats.ClusterReplicationMetrics) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException) PublisherStats(com.yahoo.pulsar.common.policies.data.PublisherStats) Producer(com.yahoo.pulsar.broker.service.Producer) ServerMetadataException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) NamespaceStats(com.yahoo.pulsar.broker.stats.NamespaceStats) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) DateTimeFormatter(java.time.format.DateTimeFormatter) Collections(java.util.Collections) TopicFencedException(com.yahoo.pulsar.broker.service.BrokerServiceException.TopicFencedException) BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CompletableFuture(java.util.concurrent.CompletableFuture) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) ConsumerBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Consumer(com.yahoo.pulsar.broker.service.Consumer) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) NamingException(com.yahoo.pulsar.broker.service.BrokerServiceException.NamingException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) UnsupportedVersionException(com.yahoo.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException)

Example 3 with BrokerServiceException

use of com.yahoo.pulsar.broker.service.BrokerServiceException in project pulsar by yahoo.

the class PersistentSubscription method resetCursor.

@Override
public CompletableFuture<Void> resetCursor(long timestamp) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    PersistentMessageFinder persistentMessageFinder = new PersistentMessageFinder(topicName, cursor);
    if (log.isDebugEnabled()) {
        log.debug("[{}][{}] Resetting subscription to timestamp {}", topicName, subName, timestamp);
    }
    persistentMessageFinder.findMessages(timestamp, new AsyncCallbacks.FindEntryCallback() {

        @Override
        public void findEntryComplete(Position position, Object ctx) {
            final Position finalPosition;
            if (position == null) {
                // this should not happen ideally unless a reset is requested for a time
                // that spans beyond the retention limits (time/size)
                finalPosition = cursor.getFirstPosition();
                if (finalPosition == null) {
                    log.warn("[{}][{}] Unable to find position for timestamp {}. Unable to reset cursor to first position", topicName, subName, timestamp);
                    future.completeExceptionally(new SubscriptionInvalidCursorPosition("Unable to find position for specified timestamp"));
                    return;
                }
                log.info("[{}][{}] Unable to find position for timestamp {}. Resetting cursor to first position {} in ledger", topicName, subName, timestamp, finalPosition);
            } else {
                finalPosition = position;
            }
            if (!IS_FENCED_UPDATER.compareAndSet(PersistentSubscription.this, FALSE, TRUE)) {
                future.completeExceptionally(new SubscriptionBusyException("Failed to fence subscription"));
                return;
            }
            final CompletableFuture<Void> disconnectFuture;
            if (dispatcher != null && dispatcher.isConsumerConnected()) {
                disconnectFuture = dispatcher.disconnectAllConsumers();
            } else {
                disconnectFuture = CompletableFuture.completedFuture(null);
            }
            disconnectFuture.whenComplete((aVoid, throwable) -> {
                if (throwable != null) {
                    log.error("[{}][{}] Failed to disconnect consumer from subscription", topicName, subName, throwable);
                    IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
                    future.completeExceptionally(new SubscriptionBusyException("Failed to disconnect consumers from subscription"));
                    return;
                }
                log.info("[{}][{}] Successfully disconnected consumers from subscription, proceeding with cursor reset", topicName, subName);
                try {
                    cursor.asyncResetCursor(finalPosition, new AsyncCallbacks.ResetCursorCallback() {

                        @Override
                        public void resetComplete(Object ctx) {
                            if (log.isDebugEnabled()) {
                                log.debug("[{}][{}] Successfully reset subscription to timestamp {}", topicName, subName, timestamp);
                            }
                            IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
                            future.complete(null);
                        }

                        @Override
                        public void resetFailed(ManagedLedgerException exception, Object ctx) {
                            log.error("[{}][{}] Failed to reset subscription to timestamp {}", topicName, subName, timestamp, exception);
                            IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
                            // or should we just ask user to retry one more time?
                            if (exception instanceof InvalidCursorPositionException) {
                                future.completeExceptionally(new SubscriptionInvalidCursorPosition(exception.getMessage()));
                            } else if (exception instanceof ConcurrentFindCursorPositionException) {
                                future.completeExceptionally(new SubscriptionBusyException(exception.getMessage()));
                            } else {
                                future.completeExceptionally(new BrokerServiceException(exception));
                            }
                        }
                    });
                } catch (Exception e) {
                    log.error("[{}][{}] Error while resetting cursor", topicName, subName, e);
                    IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
                    future.completeExceptionally(new BrokerServiceException(e));
                }
            });
        }

        @Override
        public void findEntryFailed(ManagedLedgerException exception, Object ctx) {
            // todo - what can go wrong here that needs to be retried?
            if (exception instanceof ConcurrentFindCursorPositionException) {
                future.completeExceptionally(new SubscriptionBusyException(exception.getMessage()));
            } else {
                future.completeExceptionally(new BrokerServiceException(exception));
            }
        }
    });
    return future;
}
Also used : SubType(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe.SubType) PersistentSubscriptionStats(com.yahoo.pulsar.common.policies.data.PersistentSubscriptionStats) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) ConsumerStats(com.yahoo.pulsar.common.policies.data.ConsumerStats) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) SubscriptionFencedException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionFencedException) Dispatcher(com.yahoo.pulsar.broker.service.Dispatcher) ClearBacklogCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ClearBacklogCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Codec(com.yahoo.pulsar.common.util.Codec) Objects(com.google.common.base.Objects) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) Logger(org.slf4j.Logger) BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) ServerMetadataException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException) CopyOnWriteArrayList(com.yahoo.pulsar.utils.CopyOnWriteArrayList) ConcurrentFindCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.ConcurrentFindCursorPositionException) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionInvalidCursorPosition(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) Consumer(com.yahoo.pulsar.broker.service.Consumer) List(java.util.List) AckType(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandAck.AckType) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) InvalidCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.InvalidCursorPositionException) Subscription(com.yahoo.pulsar.broker.service.Subscription) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Position(org.apache.bookkeeper.mledger.Position) SubscriptionInvalidCursorPosition(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException) InvalidCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.InvalidCursorPositionException) ConcurrentFindCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.ConcurrentFindCursorPositionException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) SubscriptionFencedException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionFencedException) BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) ServerMetadataException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException) ConcurrentFindCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.ConcurrentFindCursorPositionException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) InvalidCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.InvalidCursorPositionException) SubscriptionInvalidCursorPosition(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)

Aggregations

BrokerServiceException (com.yahoo.pulsar.broker.service.BrokerServiceException)3 Objects (com.google.common.base.Objects)2 PersistenceException (com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException)2 ServerMetadataException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException)2 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)2 Consumer (com.yahoo.pulsar.broker.service.Consumer)2 SubType (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandSubscribe.SubType)2 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)2 ConsumerStats (com.yahoo.pulsar.common.policies.data.ConsumerStats)2 PersistentSubscriptionStats (com.yahoo.pulsar.common.policies.data.PersistentSubscriptionStats)2 Codec (com.yahoo.pulsar.common.util.Codec)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)2 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)2 Entry (org.apache.bookkeeper.mledger.Entry)2 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)2 IndividualDeletedEntries (org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 Position (org.apache.bookkeeper.mledger.Position)2