Search in sources :

Example 1 with SubscriptionInvalidCursorPosition

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

the class PersistentTopicsBase method internalCreateSubscription.

protected void internalCreateSubscription(String subscriptionName, MessageIdImpl messageId, boolean authoritative) {
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    log.info("[{}][{}] Creating subscription {} at message id {}", clientAppId(), topicName, subscriptionName, messageId);
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    try {
        if (partitionMetadata.partitions > 0) {
            // Create the subscription on each partition
            List<CompletableFuture<Void>> futures = Lists.newArrayList();
            PulsarAdmin admin = pulsar().getAdminClient();
            for (int i = 0; i < partitionMetadata.partitions; i++) {
                futures.add(admin.persistentTopics().createSubscriptionAsync(topicName.getPartition(i).toString(), subscriptionName, messageId));
            }
            FutureUtil.waitForAll(futures).join();
        } else {
            validateAdminOperationOnTopic(authoritative);
            PersistentTopic topic = (PersistentTopic) getOrCreateTopic(topicName);
            if (topic.getSubscriptions().containsKey(subscriptionName)) {
                throw new RestException(Status.CONFLICT, "Subscription already exists for topic");
            }
            PersistentSubscription subscription = (PersistentSubscription) topic.createSubscription(subscriptionName, InitialPosition.Latest).get();
            subscription.resetCursor(PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId())).get();
            log.info("[{}][{}] Successfully created subscription {} at message id {}", clientAppId(), topicName, subscriptionName, messageId);
        }
    } catch (Exception e) {
        Throwable t = e.getCause();
        log.warn("[{}] [{}] Failed to create subscription {} at message id {}", clientAppId(), topicName, subscriptionName, messageId, e);
        if (t instanceof SubscriptionInvalidCursorPosition) {
            throw new RestException(Status.PRECONDITION_FAILED, "Unable to find position for position specified: " + t.getMessage());
        } else {
            throw new RestException(e);
        }
    }
}
Also used : SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RestException(org.apache.pulsar.broker.web.RestException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 2 with SubscriptionInvalidCursorPosition

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

the class PersistentTopicsBase method internalResetCursorOnPosition.

protected void internalResetCursorOnPosition(String subName, boolean authoritative, MessageIdImpl messageId) {
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    log.info("[{}][{}] received reset cursor on subscription {} to position {}", clientAppId(), topicName, subName, messageId);
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions > 0) {
        log.warn("[{}] Not supported operation on partitioned-topic {} {}", clientAppId(), topicName, subName);
        throw new RestException(Status.METHOD_NOT_ALLOWED, "Reset-cursor at position is not allowed for partitioned-topic");
    } else {
        validateAdminOperationOnTopic(authoritative);
        PersistentTopic topic = (PersistentTopic) getTopicReference(topicName);
        if (topic == null) {
            throw new RestException(Status.NOT_FOUND, "Topic not found");
        }
        try {
            PersistentSubscription sub = topic.getSubscription(subName);
            checkNotNull(sub);
            sub.resetCursor(PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId())).get();
            log.info("[{}][{}] successfully reset cursor on subscription {} to position {}", clientAppId(), topicName, subName, messageId);
        } catch (Exception e) {
            Throwable t = e.getCause();
            log.warn("[{}] [{}] Failed to reset cursor on subscription {} to position {}", clientAppId(), topicName, subName, messageId, e);
            if (e instanceof NullPointerException) {
                throw new RestException(Status.NOT_FOUND, "Subscription not found");
            } else if (t instanceof SubscriptionInvalidCursorPosition) {
                throw new RestException(Status.PRECONDITION_FAILED, "Unable to find position for position specified: " + t.getMessage());
            } else {
                throw new RestException(e);
            }
        }
    }
}
Also used : SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RestException(org.apache.pulsar.broker.web.RestException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 3 with SubscriptionInvalidCursorPosition

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

the class PersistentTopicsBase method internalResetCursor.

protected void internalResetCursor(String subName, long timestamp, boolean authoritative) {
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions > 0) {
        int numParts = partitionMetadata.partitions;
        int numPartException = 0;
        Exception partitionException = null;
        try {
            for (int i = 0; i < numParts; i++) {
                pulsar().getAdminClient().persistentTopics().resetCursor(topicName.getPartition(i).toString(), subName, timestamp);
            }
        } catch (PreconditionFailedException pfe) {
            // throw the last exception if all partitions get this error
            // any other exception on partition is reported back to user
            ++numPartException;
            partitionException = pfe;
        } catch (Exception e) {
            log.warn("[{}] [{}] Failed to reset cursor on subscription {} to time {}", clientAppId(), topicName, subName, timestamp, e);
            throw new RestException(e);
        }
        // report an error to user if unable to reset for all partitions
        if (numPartException == numParts) {
            log.warn("[{}] [{}] Failed to reset cursor on subscription {} to time {}", clientAppId(), topicName, subName, timestamp, partitionException);
            throw new RestException(Status.PRECONDITION_FAILED, partitionException.getMessage());
        } else if (numPartException > 0) {
            log.warn("[{}][{}] partial errors for reset cursor on subscription {} to time {} - ", clientAppId(), topicName, subName, timestamp, partitionException);
        }
    } else {
        validateAdminOperationOnTopic(authoritative);
        log.info("[{}][{}] received reset cursor on subscription {} to time {}", clientAppId(), topicName, subName, timestamp);
        PersistentTopic topic = (PersistentTopic) getTopicReference(topicName);
        if (topic == null) {
            throw new RestException(Status.NOT_FOUND, "Topic not found");
        }
        try {
            PersistentSubscription sub = topic.getSubscription(subName);
            checkNotNull(sub);
            sub.resetCursor(timestamp).get();
            log.info("[{}][{}] reset cursor on subscription {} to time {}", clientAppId(), topicName, subName, timestamp);
        } catch (Exception e) {
            Throwable t = e.getCause();
            log.warn("[{}] [{}] Failed to reset cursor on subscription {} to time {}", clientAppId(), topicName, subName, timestamp, e);
            if (e instanceof NullPointerException) {
                throw new RestException(Status.NOT_FOUND, "Subscription not found");
            } else if (e instanceof NotAllowedException) {
                throw new RestException(Status.METHOD_NOT_ALLOWED, e.getMessage());
            } else if (t instanceof SubscriptionInvalidCursorPosition) {
                throw new RestException(Status.PRECONDITION_FAILED, "Unable to find position for timestamp specified -" + t.getMessage());
            } else {
                throw new RestException(e);
            }
        }
    }
}
Also used : SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RestException(org.apache.pulsar.broker.web.RestException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 4 with SubscriptionInvalidCursorPosition

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

the class PersistentSubscription method resetCursor.

private void resetCursor(Position finalPosition, CompletableFuture<Void> future) {
    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 position {}", topicName, subName, finalPosition);
                    }
                    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 position {}", topicName, subName, finalPosition, 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));
        }
    });
}
Also used : BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) InvalidCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.InvalidCursorPositionException) ConcurrentFindCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.ConcurrentFindCursorPositionException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) SubscriptionFencedException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionFencedException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) ConcurrentFindCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.ConcurrentFindCursorPositionException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) InvalidCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.InvalidCursorPositionException) SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)

Aggregations

ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)4 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)4 SubscriptionInvalidCursorPosition (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition)4 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)3 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)3 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)3 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)3 RestException (org.apache.pulsar.broker.web.RestException)3 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)3 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)3 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 PartitionedTopicMetadata (org.apache.pulsar.common.partition.PartitionedTopicMetadata)3 KeeperException (org.apache.zookeeper.KeeperException)3 CompletableFuture (java.util.concurrent.CompletableFuture)1 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)1