Search in sources :

Example 1 with TxnAction

use of org.apache.pulsar.common.api.proto.TxnAction in project pulsar by apache.

the class TransactionMetadataStoreService method endTransaction.

public CompletableFuture<Void> endTransaction(TxnID txnID, int txnAction, boolean isTimeout, CompletableFuture<Void> completableFuture) {
    TxnStatus newStatus;
    switch(txnAction) {
        case TxnAction.COMMIT_VALUE:
            newStatus = COMMITTING;
            break;
        case TxnAction.ABORT_VALUE:
            newStatus = ABORTING;
            break;
        default:
            TransactionCoordinatorException.UnsupportedTxnActionException exception = new TransactionCoordinatorException.UnsupportedTxnActionException(txnID, txnAction);
            LOG.error(exception.getMessage());
            completableFuture.completeExceptionally(exception);
            return completableFuture;
    }
    getTxnMeta(txnID).thenAccept(txnMeta -> {
        TxnStatus txnStatus = txnMeta.status();
        if (txnStatus == TxnStatus.OPEN) {
            updateTxnStatus(txnID, newStatus, TxnStatus.OPEN, isTimeout).thenAccept(v -> endTxnInTransactionBuffer(txnID, txnAction).thenAccept(a -> completableFuture.complete(null)).exceptionally(e -> {
                if (!isRetryableException(e.getCause())) {
                    LOG.error("EndTxnInTransactionBuffer fail! TxnId : {}, " + "TxnAction : {}", txnID, txnAction, e);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("EndTxnInTransactionBuffer retry! TxnId : {}, " + "TxnAction : {}", txnID, txnAction, e);
                    }
                    transactionOpRetryTimer.newTimeout(timeout -> endTransaction(txnID, txnAction, isTimeout, completableFuture), endTransactionRetryIntervalTime, TimeUnit.MILLISECONDS);
                    return null;
                }
                completableFuture.completeExceptionally(e.getCause());
                return null;
            })).exceptionally(e -> {
                if (!isRetryableException(e.getCause())) {
                    LOG.error("EndTransaction UpdateTxnStatus fail! TxnId : {}, " + "TxnAction : {}", txnID, txnAction, e);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("EndTransaction UpdateTxnStatus op retry! TxnId : {}, " + "TxnAction : {}", txnID, txnAction, e);
                    }
                    transactionOpRetryTimer.newTimeout(timeout -> endTransaction(txnID, txnAction, isTimeout, completableFuture), endTransactionRetryIntervalTime, TimeUnit.MILLISECONDS);
                    return null;
                }
                completableFuture.completeExceptionally(e.getCause());
                return null;
            });
        } else {
            if ((txnStatus == COMMITTING && txnAction == TxnAction.COMMIT.getValue()) || (txnStatus == ABORTING && txnAction == TxnAction.ABORT.getValue())) {
                endTxnInTransactionBuffer(txnID, txnAction).thenAccept(k -> completableFuture.complete(null)).exceptionally(e -> {
                    if (isRetryableException(e.getCause())) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("EndTxnInTransactionBuffer retry! TxnId : {}, " + "TxnAction : {}", txnID, txnAction, e);
                        }
                        transactionOpRetryTimer.newTimeout(timeout -> endTransaction(txnID, txnAction, isTimeout, completableFuture), endTransactionRetryIntervalTime, TimeUnit.MILLISECONDS);
                        return null;
                    } else {
                        LOG.error("EndTxnInTransactionBuffer fail! TxnId : {}, " + "TxnAction : {}", txnID, txnAction, e);
                    }
                    completableFuture.completeExceptionally(e.getCause());
                    return null;
                });
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("EndTxnInTransactionBuffer op retry! TxnId : {}, TxnAction : {}", txnID, txnAction);
                }
                completableFuture.completeExceptionally(new InvalidTxnStatusException(txnID, newStatus, txnStatus));
            }
        }
    }).exceptionally(e -> {
        if (isRetryableException(e.getCause())) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("End transaction op retry! TxnId : {}, TxnAction : {}", txnID, txnAction, e);
            }
            transactionOpRetryTimer.newTimeout(timeout -> endTransaction(txnID, txnAction, isTimeout, completableFuture), endTransactionRetryIntervalTime, TimeUnit.MILLISECONDS);
            return null;
        }
        completableFuture.completeExceptionally(e.getCause());
        return null;
    });
    return completableFuture;
}
Also used : ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) TransactionMetadataStoreStateException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionMetadataStoreStateException) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) BrokerPersistenceException(org.apache.pulsar.client.api.PulsarClientException.BrokerPersistenceException) LoggerFactory(org.slf4j.LoggerFactory) TransactionMetadataStoreProvider(org.apache.pulsar.transaction.coordinator.TransactionMetadataStoreProvider) MLTransactionLogImpl.getMLTransactionLogName(org.apache.pulsar.transaction.coordinator.impl.MLTransactionLogImpl.getMLTransactionLogName) TransactionRecoverTrackerImpl(org.apache.pulsar.broker.transaction.recover.TransactionRecoverTrackerImpl) TxnID(org.apache.pulsar.client.api.transaction.TxnID) Map(java.util.Map) TxnAction(org.apache.pulsar.common.api.proto.TxnAction) RequestTimeoutException(org.apache.pulsar.client.api.transaction.TransactionBufferClientException.RequestTimeoutException) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ReachMaxPendingOpsException(org.apache.pulsar.client.api.transaction.TransactionBufferClientException.ReachMaxPendingOpsException) ThreadFactory(java.util.concurrent.ThreadFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Executors(java.util.concurrent.Executors) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) TransactionTimeoutTrackerFactoryImpl(org.apache.pulsar.broker.transaction.timeout.TransactionTimeoutTrackerFactoryImpl) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) NamespaceBundleOwnershipListener(org.apache.pulsar.broker.namespace.NamespaceBundleOwnershipListener) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) TopicName(org.apache.pulsar.common.naming.TopicName) TransactionBufferClient(org.apache.pulsar.client.api.transaction.TransactionBufferClient) CompletableFuture(java.util.concurrent.CompletableFuture) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) Deque(java.util.Deque) ArrayList(java.util.ArrayList) TransactionTimeoutTracker(org.apache.pulsar.transaction.coordinator.TransactionTimeoutTracker) ConnectException(org.apache.pulsar.client.api.PulsarClientException.ConnectException) TransactionTimeoutTrackerFactory(org.apache.pulsar.transaction.coordinator.TransactionTimeoutTrackerFactory) TransactionRecoverTracker(org.apache.pulsar.transaction.coordinator.TransactionRecoverTracker) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) CoordinatorNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException) ExecutorService(java.util.concurrent.ExecutorService) ABORTING(org.apache.pulsar.transaction.coordinator.proto.TxnStatus.ABORTING) Logger(org.slf4j.Logger) COMMITTING(org.apache.pulsar.transaction.coordinator.proto.TxnStatus.COMMITTING) Semaphore(java.util.concurrent.Semaphore) TransactionCoordinatorException(org.apache.pulsar.broker.transaction.exception.coordinator.TransactionCoordinatorException) LookupException(org.apache.pulsar.client.api.PulsarClientException.LookupException) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) ConcurrentLongHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongHashMap) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) TransactionCoordinatorException(org.apache.pulsar.broker.transaction.exception.coordinator.TransactionCoordinatorException) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus)

Example 2 with TxnAction

use of org.apache.pulsar.common.api.proto.TxnAction in project pulsar by apache.

the class ServerCnx method handleEndTxnOnPartition.

@Override
protected void handleEndTxnOnPartition(CommandEndTxnOnPartition command) {
    final long requestId = command.getRequestId();
    final String topic = command.getTopic();
    final int txnAction = command.getTxnAction().getValue();
    TxnID txnID = new TxnID(command.getTxnidMostBits(), command.getTxnidLeastBits());
    final long lowWaterMark = command.getTxnidLeastBitsOfLowWatermark();
    if (log.isDebugEnabled()) {
        log.debug("[{}] handleEndTxnOnPartition txnId: [{}], txnAction: [{}]", topic, txnID, txnAction);
    }
    CompletableFuture<Optional<Topic>> topicFuture = service.getTopicIfExists(TopicName.get(topic).toString());
    topicFuture.thenAccept(optionalTopic -> {
        if (optionalTopic.isPresent()) {
            optionalTopic.get().endTxn(txnID, txnAction, lowWaterMark).whenComplete((ignored, throwable) -> {
                if (throwable != null) {
                    log.error("handleEndTxnOnPartition fail!, topic {}, txnId: [{}], " + "txnAction: [{}]", topic, txnID, TxnAction.valueOf(txnAction), throwable);
                    ctx.writeAndFlush(Commands.newEndTxnOnPartitionResponse(requestId, BrokerServiceException.getClientErrorCode(throwable), throwable.getMessage(), txnID.getLeastSigBits(), txnID.getMostSigBits()));
                    return;
                }
                ctx.writeAndFlush(Commands.newEndTxnOnPartitionResponse(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits()));
            });
        } else {
            getBrokerService().getManagedLedgerFactory().asyncExists(TopicName.get(topic).getPersistenceNamingEncoding()).thenAccept((b) -> {
                if (b) {
                    log.error("handleEndTxnOnPartition fail ! The topic {} does not exist in broker, " + "txnId: [{}], txnAction: [{}]", topic, txnID, TxnAction.valueOf(txnAction));
                    ctx.writeAndFlush(Commands.newEndTxnOnPartitionResponse(requestId, ServerError.ServiceNotReady, "The topic " + topic + " does not exist in broker.", txnID.getMostSigBits(), txnID.getLeastSigBits()));
                } else {
                    log.warn("handleEndTxnOnPartition fail ! The topic {} has not been created, " + "txnId: [{}], txnAction: [{}]", topic, txnID, TxnAction.valueOf(txnAction));
                    ctx.writeAndFlush(Commands.newEndTxnOnPartitionResponse(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits()));
                }
            }).exceptionally(e -> {
                log.error("handleEndTxnOnPartition fail ! topic {}, " + "txnId: [{}], txnAction: [{}]", topic, txnID, TxnAction.valueOf(txnAction), e.getCause());
                ctx.writeAndFlush(Commands.newEndTxnOnPartitionResponse(requestId, ServerError.ServiceNotReady, e.getMessage(), txnID.getLeastSigBits(), txnID.getMostSigBits()));
                return null;
            });
        }
    }).exceptionally(e -> {
        log.error("handleEndTxnOnPartition fail ! topic {}, " + "txnId: [{}], txnAction: [{}]", topic, txnID, TxnAction.valueOf(txnAction), e.getCause());
        ctx.writeAndFlush(Commands.newEndTxnOnPartitionResponse(requestId, ServerError.ServiceNotReady, e.getMessage(), txnID.getLeastSigBits(), txnID.getMostSigBits()));
        return null;
    });
}
Also used : CommandAuthResponse(org.apache.pulsar.common.api.proto.CommandAuthResponse) CommandUnsubscribe(org.apache.pulsar.common.api.proto.CommandUnsubscribe) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) CommandProducer(org.apache.pulsar.common.api.proto.CommandProducer) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair) StringUtils(org.apache.commons.lang3.StringUtils) ProtocolVersion(org.apache.pulsar.common.api.proto.ProtocolVersion) TxnID(org.apache.pulsar.client.api.transaction.TxnID) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) TopicOperation(org.apache.pulsar.common.policies.data.TopicOperation) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Map(java.util.Map) BrokerInterceptor(org.apache.pulsar.broker.intercept.BrokerInterceptor) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand) CommandAck(org.apache.pulsar.common.api.proto.CommandAck) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) InterceptException(org.apache.pulsar.common.intercept.InterceptException) CommandFlow(org.apache.pulsar.common.api.proto.CommandFlow) CommandConsumerStats(org.apache.pulsar.common.api.proto.CommandConsumerStats) ServerError(org.apache.pulsar.common.api.proto.ServerError) Set(java.util.Set) CommandNewTxn(org.apache.pulsar.common.api.proto.CommandNewTxn) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SafeRun(org.apache.bookkeeper.mledger.util.SafeRun) SslHandler(io.netty.handler.ssl.SslHandler) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) ChannelOption(io.netty.channel.ChannelOption) SchemaRegistryService(org.apache.pulsar.broker.service.schema.SchemaRegistryService) AuthenticationState(org.apache.pulsar.broker.authentication.AuthenticationState) CommandGetTopicsOfNamespace(org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace) TopicNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.TopicNotFoundException) SchemaType(org.apache.pulsar.common.schema.SchemaType) Commands(org.apache.pulsar.common.protocol.Commands) CommandCloseProducer(org.apache.pulsar.common.api.proto.CommandCloseProducer) Strings(com.google.common.base.Strings) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) SSLSession(javax.net.ssl.SSLSession) CommandGetOrCreateSchema(org.apache.pulsar.common.api.proto.CommandGetOrCreateSchema) ProtocolVersion.v5(org.apache.pulsar.common.api.proto.ProtocolVersion.v5) CommandGetSchema(org.apache.pulsar.common.api.proto.CommandGetSchema) SchemaInfoUtil(org.apache.pulsar.client.impl.schema.SchemaInfoUtil) CommandRedeliverUnacknowledgedMessages(org.apache.pulsar.common.api.proto.CommandRedeliverUnacknowledgedMessages) Metadata(org.apache.pulsar.common.naming.Metadata) Promise(io.netty.util.concurrent.Promise) AuthenticationProvider(org.apache.pulsar.broker.authentication.AuthenticationProvider) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) lombok.val(lombok.val) PulsarService(org.apache.pulsar.broker.PulsarService) KeySharedMode(org.apache.pulsar.common.api.proto.KeySharedMode) CommandEndTxnOnSubscription(org.apache.pulsar.common.api.proto.CommandEndTxnOnSubscription) CommandConnect(org.apache.pulsar.common.api.proto.CommandConnect) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) ProducerAccessMode(org.apache.pulsar.common.api.proto.ProducerAccessMode) CommandLookupTopic(org.apache.pulsar.common.api.proto.CommandLookupTopic) MutableInt(org.apache.commons.lang3.mutable.MutableInt) SocketAddress(java.net.SocketAddress) CommandAddPartitionToTxn(org.apache.pulsar.common.api.proto.CommandAddPartitionToTxn) LoggerFactory(org.slf4j.LoggerFactory) AuthData(org.apache.pulsar.common.api.AuthData) Exceptions(org.apache.pulsar.functions.utils.Exceptions) AuthenticationException(javax.naming.AuthenticationException) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) PersistentTopicsBase.unsafeGetPartitionedTopicMetadataAsync(org.apache.pulsar.broker.admin.impl.PersistentTopicsBase.unsafeGetPartitionedTopicMetadataAsync) CommandEndTxnOnPartition(org.apache.pulsar.common.api.proto.CommandEndTxnOnPartition) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BacklogQuotaType(org.apache.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType) Gauge(io.prometheus.client.Gauge) TxnAction(org.apache.pulsar.common.api.proto.TxnAction) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Schema(org.apache.pulsar.common.api.proto.Schema) TransactionMetadataStoreService(org.apache.pulsar.broker.TransactionMetadataStoreService) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IdentityHashMap(java.util.IdentityHashMap) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) Position(org.apache.bookkeeper.mledger.Position) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) CommandEndTxn(org.apache.pulsar.common.api.proto.CommandEndTxn) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) Objects(java.util.Objects) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Optional(java.util.Optional) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) PulsarHandler(org.apache.pulsar.common.protocol.PulsarHandler) CommandTcClientConnectRequest(org.apache.pulsar.common.api.proto.CommandTcClientConnectRequest) TopicName(org.apache.pulsar.common.naming.TopicName) CommandAddSubscriptionToTxn(org.apache.pulsar.common.api.proto.CommandAddSubscriptionToTxn) Entry(org.apache.bookkeeper.mledger.Entry) CommandSend(org.apache.pulsar.common.api.proto.CommandSend) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) CompletableFuture(java.util.concurrent.CompletableFuture) TopicLookupBase.lookupTopicAsync(org.apache.pulsar.broker.lookup.TopicLookupBase.lookupTopicAsync) KeySharedMeta(org.apache.pulsar.common.api.proto.KeySharedMeta) CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) FastThreadLocal(io.netty.util.concurrent.FastThreadLocal) CommandPartitionedTopicMetadata(org.apache.pulsar.common.api.proto.CommandPartitionedTopicMetadata) CommandSeek(org.apache.pulsar.common.api.proto.CommandSeek) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) NoSuchElementException(java.util.NoSuchElementException) CommandGetLastMessageId(org.apache.pulsar.common.api.proto.CommandGetLastMessageId) CommandUtils(org.apache.pulsar.common.protocol.CommandUtils) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) AuthenticationDataCommand(org.apache.pulsar.broker.authentication.AuthenticationDataCommand) DEFAULT_CONSUMER_EPOCH(org.apache.pulsar.common.protocol.Commands.DEFAULT_CONSUMER_EPOCH) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) FeatureFlags(org.apache.pulsar.common.api.proto.FeatureFlags) CommandCloseConsumer(org.apache.pulsar.common.api.proto.CommandCloseConsumer) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Commands.newLookupErrorResponse(org.apache.pulsar.common.protocol.Commands.newLookupErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) ConcurrentLongHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongHashMap) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) ChannelHandler(io.netty.channel.ChannelHandler) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) TxnID(org.apache.pulsar.client.api.transaction.TxnID) Optional(java.util.Optional)

Example 3 with TxnAction

use of org.apache.pulsar.common.api.proto.TxnAction in project pulsar by apache.

the class ServerCnx method handleEndTxnOnSubscription.

@Override
protected void handleEndTxnOnSubscription(CommandEndTxnOnSubscription command) {
    final long requestId = command.getRequestId();
    final long txnidMostBits = command.getTxnidMostBits();
    final long txnidLeastBits = command.getTxnidLeastBits();
    final String topic = command.getSubscription().getTopic();
    final String subName = command.getSubscription().getSubscription();
    final int txnAction = command.getTxnAction().getValue();
    final TxnID txnID = new TxnID(txnidMostBits, txnidLeastBits);
    final long lowWaterMark = command.getTxnidLeastBitsOfLowWatermark();
    if (log.isDebugEnabled()) {
        log.debug("[{}] [{}] handleEndTxnOnSubscription txnId: [{}], txnAction: [{}]", topic, subName, new TxnID(txnidMostBits, txnidLeastBits), txnAction);
    }
    CompletableFuture<Optional<Topic>> topicFuture = service.getTopicIfExists(TopicName.get(topic).toString());
    topicFuture.thenAccept(optionalTopic -> {
        if (optionalTopic.isPresent()) {
            Subscription subscription = optionalTopic.get().getSubscription(subName);
            if (subscription == null) {
                log.warn("handleEndTxnOnSubscription fail! " + "topic {} subscription {} does not exist. txnId: [{}], txnAction: [{}]", optionalTopic.get().getName(), subName, txnID, TxnAction.valueOf(txnAction));
                ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnidLeastBits, txnidMostBits));
                return;
            }
            CompletableFuture<Void> completableFuture = subscription.endTxn(txnidMostBits, txnidLeastBits, txnAction, lowWaterMark);
            completableFuture.whenComplete((ignored, e) -> {
                if (e != null) {
                    log.error("handleEndTxnOnSubscription fail ! topic: {}, subscription: {}" + "txnId: [{}], txnAction: [{}]", topic, subName, txnID, TxnAction.valueOf(txnAction), e.getCause());
                    ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnidLeastBits, txnidMostBits, BrokerServiceException.getClientErrorCode(e), "Handle end txn on subscription failed."));
                    return;
                }
                ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnidLeastBits, txnidMostBits));
            });
        } else {
            getBrokerService().getManagedLedgerFactory().asyncExists(TopicName.get(topic).getPersistenceNamingEncoding()).thenAccept((b) -> {
                if (b) {
                    log.error("handleEndTxnOnSubscription fail! The topic {} does not exist in broker, " + "subscription: {}, txnId: [{}], txnAction: [{}]", topic, subName, new TxnID(txnidMostBits, txnidLeastBits), TxnAction.valueOf(txnAction));
                    ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits(), ServerError.ServiceNotReady, "The topic " + topic + " does not exist in broker."));
                } else {
                    log.warn("handleEndTxnOnSubscription fail ! The topic {} has not been created, " + "subscription: {} txnId: [{}], txnAction: [{}]", topic, subName, txnID, TxnAction.valueOf(txnAction));
                    ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits()));
                }
            }).exceptionally(e -> {
                log.error("handleEndTxnOnSubscription fail ! topic {}, subscription: {}" + "txnId: [{}], txnAction: [{}]", topic, subName, txnID, TxnAction.valueOf(txnAction), e.getCause());
                ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits(), ServerError.ServiceNotReady, e.getMessage()));
                return null;
            });
        }
    }).exceptionally(e -> {
        log.error("handleEndTxnOnSubscription fail ! topic: {}, subscription: {}" + "txnId: [{}], txnAction: [{}]", topic, subName, txnID, TxnAction.valueOf(txnAction), e.getCause());
        ctx.writeAndFlush(Commands.newEndTxnOnSubscriptionResponse(requestId, txnidLeastBits, txnidMostBits, ServerError.ServiceNotReady, "Handle end txn on subscription failed."));
        return null;
    });
}
Also used : CommandAuthResponse(org.apache.pulsar.common.api.proto.CommandAuthResponse) CommandUnsubscribe(org.apache.pulsar.common.api.proto.CommandUnsubscribe) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) CommandProducer(org.apache.pulsar.common.api.proto.CommandProducer) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) ByteBufPair(org.apache.pulsar.common.protocol.ByteBufPair) StringUtils(org.apache.commons.lang3.StringUtils) ProtocolVersion(org.apache.pulsar.common.api.proto.ProtocolVersion) TxnID(org.apache.pulsar.client.api.transaction.TxnID) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) TopicOperation(org.apache.pulsar.common.policies.data.TopicOperation) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Map(java.util.Map) BrokerInterceptor(org.apache.pulsar.broker.intercept.BrokerInterceptor) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand) CommandAck(org.apache.pulsar.common.api.proto.CommandAck) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) InterceptException(org.apache.pulsar.common.intercept.InterceptException) CommandFlow(org.apache.pulsar.common.api.proto.CommandFlow) CommandConsumerStats(org.apache.pulsar.common.api.proto.CommandConsumerStats) ServerError(org.apache.pulsar.common.api.proto.ServerError) Set(java.util.Set) CommandNewTxn(org.apache.pulsar.common.api.proto.CommandNewTxn) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) SafeRun(org.apache.bookkeeper.mledger.util.SafeRun) SslHandler(io.netty.handler.ssl.SslHandler) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) ChannelOption(io.netty.channel.ChannelOption) SchemaRegistryService(org.apache.pulsar.broker.service.schema.SchemaRegistryService) AuthenticationState(org.apache.pulsar.broker.authentication.AuthenticationState) CommandGetTopicsOfNamespace(org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace) TopicNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.TopicNotFoundException) SchemaType(org.apache.pulsar.common.schema.SchemaType) Commands(org.apache.pulsar.common.protocol.Commands) CommandCloseProducer(org.apache.pulsar.common.api.proto.CommandCloseProducer) Strings(com.google.common.base.Strings) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) SSLSession(javax.net.ssl.SSLSession) CommandGetOrCreateSchema(org.apache.pulsar.common.api.proto.CommandGetOrCreateSchema) ProtocolVersion.v5(org.apache.pulsar.common.api.proto.ProtocolVersion.v5) CommandGetSchema(org.apache.pulsar.common.api.proto.CommandGetSchema) SchemaInfoUtil(org.apache.pulsar.client.impl.schema.SchemaInfoUtil) CommandRedeliverUnacknowledgedMessages(org.apache.pulsar.common.api.proto.CommandRedeliverUnacknowledgedMessages) Metadata(org.apache.pulsar.common.naming.Metadata) Promise(io.netty.util.concurrent.Promise) AuthenticationProvider(org.apache.pulsar.broker.authentication.AuthenticationProvider) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) lombok.val(lombok.val) PulsarService(org.apache.pulsar.broker.PulsarService) KeySharedMode(org.apache.pulsar.common.api.proto.KeySharedMode) CommandEndTxnOnSubscription(org.apache.pulsar.common.api.proto.CommandEndTxnOnSubscription) CommandConnect(org.apache.pulsar.common.api.proto.CommandConnect) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) ProducerAccessMode(org.apache.pulsar.common.api.proto.ProducerAccessMode) CommandLookupTopic(org.apache.pulsar.common.api.proto.CommandLookupTopic) MutableInt(org.apache.commons.lang3.mutable.MutableInt) SocketAddress(java.net.SocketAddress) CommandAddPartitionToTxn(org.apache.pulsar.common.api.proto.CommandAddPartitionToTxn) LoggerFactory(org.slf4j.LoggerFactory) AuthData(org.apache.pulsar.common.api.AuthData) Exceptions(org.apache.pulsar.functions.utils.Exceptions) AuthenticationException(javax.naming.AuthenticationException) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) PersistentTopicsBase.unsafeGetPartitionedTopicMetadataAsync(org.apache.pulsar.broker.admin.impl.PersistentTopicsBase.unsafeGetPartitionedTopicMetadataAsync) CommandEndTxnOnPartition(org.apache.pulsar.common.api.proto.CommandEndTxnOnPartition) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BacklogQuotaType(org.apache.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType) Gauge(io.prometheus.client.Gauge) TxnAction(org.apache.pulsar.common.api.proto.TxnAction) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Schema(org.apache.pulsar.common.api.proto.Schema) TransactionMetadataStoreService(org.apache.pulsar.broker.TransactionMetadataStoreService) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IdentityHashMap(java.util.IdentityHashMap) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) Position(org.apache.bookkeeper.mledger.Position) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) CommandEndTxn(org.apache.pulsar.common.api.proto.CommandEndTxn) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) Objects(java.util.Objects) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Optional(java.util.Optional) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) PulsarHandler(org.apache.pulsar.common.protocol.PulsarHandler) CommandTcClientConnectRequest(org.apache.pulsar.common.api.proto.CommandTcClientConnectRequest) TopicName(org.apache.pulsar.common.naming.TopicName) CommandAddSubscriptionToTxn(org.apache.pulsar.common.api.proto.CommandAddSubscriptionToTxn) Entry(org.apache.bookkeeper.mledger.Entry) CommandSend(org.apache.pulsar.common.api.proto.CommandSend) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) CompletableFuture(java.util.concurrent.CompletableFuture) TopicLookupBase.lookupTopicAsync(org.apache.pulsar.broker.lookup.TopicLookupBase.lookupTopicAsync) KeySharedMeta(org.apache.pulsar.common.api.proto.KeySharedMeta) CommandSubscribe(org.apache.pulsar.common.api.proto.CommandSubscribe) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) FastThreadLocal(io.netty.util.concurrent.FastThreadLocal) CommandPartitionedTopicMetadata(org.apache.pulsar.common.api.proto.CommandPartitionedTopicMetadata) CommandSeek(org.apache.pulsar.common.api.proto.CommandSeek) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) NoSuchElementException(java.util.NoSuchElementException) CommandGetLastMessageId(org.apache.pulsar.common.api.proto.CommandGetLastMessageId) CommandUtils(org.apache.pulsar.common.protocol.CommandUtils) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) AuthenticationDataCommand(org.apache.pulsar.broker.authentication.AuthenticationDataCommand) DEFAULT_CONSUMER_EPOCH(org.apache.pulsar.common.protocol.Commands.DEFAULT_CONSUMER_EPOCH) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) FeatureFlags(org.apache.pulsar.common.api.proto.FeatureFlags) CommandCloseConsumer(org.apache.pulsar.common.api.proto.CommandCloseConsumer) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Commands.newLookupErrorResponse(org.apache.pulsar.common.protocol.Commands.newLookupErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) ConcurrentLongHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongHashMap) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) ChannelHandler(io.netty.channel.ChannelHandler) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) TxnID(org.apache.pulsar.client.api.transaction.TxnID) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) CommandEndTxnOnSubscription(org.apache.pulsar.common.api.proto.CommandEndTxnOnSubscription) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Collections (java.util.Collections)3 Map (java.util.Map)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Semaphore (java.util.concurrent.Semaphore)3 TimeUnit (java.util.concurrent.TimeUnit)3 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)3 ServiceUnitNotReadyException (org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)3 TxnID (org.apache.pulsar.client.api.transaction.TxnID)3 TxnAction (org.apache.pulsar.common.api.proto.TxnAction)3 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)3 TopicName (org.apache.pulsar.common.naming.TopicName)3 FutureUtil (org.apache.pulsar.common.util.FutureUtil)3 ConcurrentLongHashMap (org.apache.pulsar.common.util.collections.ConcurrentLongHashMap)3 TransactionCoordinatorID (org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Strings (com.google.common.base.Strings)2 ByteBuf (io.netty.buffer.ByteBuf)2