Search in sources :

Example 6 with Subscription

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

the class PersistentTopic method getNonDurableSubscription.

private CompletableFuture<? extends Subscription> getNonDurableSubscription(String subscriptionName, MessageId startMessageId) {
    CompletableFuture<Subscription> subscriptionFuture = new CompletableFuture<>();
    log.info("[{}][{}] Creating non-durable subscription at msg id {}", topic, subscriptionName, startMessageId);
    // Create a new non-durable cursor only for the first consumer that connects
    Subscription subscription = subscriptions.computeIfAbsent(subscriptionName, name -> {
        MessageIdImpl msgId = startMessageId != null ? (MessageIdImpl) startMessageId : (MessageIdImpl) MessageId.latest;
        long ledgerId = msgId.getLedgerId();
        long entryId = msgId.getEntryId();
        if (msgId instanceof BatchMessageIdImpl) {
            // The client will then be able to discard the first messages in the batch.
            if (((BatchMessageIdImpl) msgId).getBatchIndex() >= 0) {
                entryId = msgId.getEntryId() - 1;
            }
        }
        Position startPosition = new PositionImpl(ledgerId, entryId);
        ManagedCursor cursor = null;
        try {
            cursor = ledger.newNonDurableCursor(startPosition);
        } catch (ManagedLedgerException e) {
            subscriptionFuture.completeExceptionally(e);
        }
        return new PersistentSubscription(this, subscriptionName, cursor);
    });
    if (!subscriptionFuture.isDone()) {
        subscriptionFuture.complete(subscription);
    } else {
        // failed to initialize managed-cursor: clean up created subscription
        subscriptions.remove(subscriptionName);
    }
    return subscriptionFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) Subscription(org.apache.pulsar.broker.service.Subscription) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Aggregations

Subscription (org.apache.pulsar.broker.service.Subscription)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)4 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)4 Topic (org.apache.pulsar.broker.service.Topic)4 KeeperException (org.apache.zookeeper.KeeperException)4 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)3 Position (org.apache.bookkeeper.mledger.Position)3 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)3 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)3 ObjectObjectHashMap (com.carrotsearch.hppc.ObjectObjectHashMap)2 MoreObjects (com.google.common.base.MoreObjects)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Sets (com.google.common.collect.Sets)2 ByteBuf (io.netty.buffer.ByteBuf)2 FastThreadLocal (io.netty.util.concurrent.FastThreadLocal)2 Collections (java.util.Collections)2 List (java.util.List)2