Search in sources :

Example 16 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalDeletePartitionedTopic.

protected void internalDeletePartitionedTopic(boolean authoritative) {
    validateAdminAccessOnProperty(topicName.getProperty());
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    int numPartitions = partitionMetadata.partitions;
    if (numPartitions > 0) {
        final CompletableFuture<Void> future = new CompletableFuture<>();
        final AtomicInteger count = new AtomicInteger(numPartitions);
        try {
            for (int i = 0; i < numPartitions; i++) {
                TopicName topicNamePartition = topicName.getPartition(i);
                pulsar().getAdminClient().persistentTopics().deleteAsync(topicNamePartition.toString()).whenComplete((r, ex) -> {
                    if (ex != null) {
                        if (ex instanceof NotFoundException) {
                            // partition is failed to be deleted
                            if (log.isDebugEnabled()) {
                                log.debug("[{}] Partition not found: {}", clientAppId(), topicNamePartition);
                            }
                        } else {
                            future.completeExceptionally(ex);
                            log.error("[{}] Failed to delete partition {}", clientAppId(), topicNamePartition, ex);
                            return;
                        }
                    } else {
                        log.info("[{}] Deleted partition {}", clientAppId(), topicNamePartition);
                    }
                    if (count.decrementAndGet() == 0) {
                        future.complete(null);
                    }
                });
            }
            future.get();
        } catch (Exception e) {
            Throwable t = e.getCause();
            if (t instanceof PreconditionFailedException) {
                throw new RestException(Status.PRECONDITION_FAILED, "Topic has active producers/subscriptions");
            } else {
                throw new RestException(t);
            }
        }
    }
    // Only tries to delete the znode for partitioned topic when all its partitions are successfully deleted
    String path = path(PARTITIONED_TOPIC_PATH_ZNODE, namespaceName.toString(), domain(), topicName.getEncodedLocalName());
    try {
        globalZk().delete(path, -1);
        globalZkCache().invalidate(path);
        // we wait for the data to be synced in all quorums and the observers
        Thread.sleep(PARTITIONED_TOPIC_WAIT_SYNC_TIME_MS);
        log.info("[{}] Deleted partitioned topic {}", clientAppId(), topicName);
    } catch (KeeperException.NoNodeException nne) {
        throw new RestException(Status.NOT_FOUND, "Partitioned topic does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to delete partitioned topic {}", clientAppId(), topicName, e);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) 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) TopicName(org.apache.pulsar.common.naming.TopicName) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) KeeperException(org.apache.zookeeper.KeeperException)

Example 17 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata 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 18 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalSkipAllMessages.

protected void internalSkipAllMessages(String subName, boolean authoritative) {
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions > 0) {
        try {
            for (int i = 0; i < partitionMetadata.partitions; i++) {
                pulsar().getAdminClient().persistentTopics().skipAllMessages(topicName.getPartition(i).toString(), subName);
            }
        } catch (Exception e) {
            throw new RestException(e);
        }
    } else {
        validateAdminOperationOnTopic(authoritative);
        PersistentTopic topic = (PersistentTopic) getTopicReference(topicName);
        try {
            if (subName.startsWith(topic.replicatorPrefix)) {
                String remoteCluster = PersistentReplicator.getRemoteCluster(subName);
                PersistentReplicator repl = (PersistentReplicator) topic.getPersistentReplicator(remoteCluster);
                checkNotNull(repl);
                repl.clearBacklog().get();
            } else {
                PersistentSubscription sub = topic.getSubscription(subName);
                checkNotNull(sub);
                sub.clearBacklog().get();
            }
            log.info("[{}] Cleared backlog on {} {}", clientAppId(), topicName, subName);
        } catch (NullPointerException npe) {
            throw new RestException(Status.NOT_FOUND, "Subscription not found");
        } catch (Exception exception) {
            log.error("[{}] Failed to skip all messages {} {}", clientAppId(), topicName, subName, exception);
            throw new RestException(exception);
        }
    }
}
Also used : PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) 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 19 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalGetPartitionedStats.

protected PartitionedTopicStats internalGetPartitionedStats(boolean authoritative) {
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions == 0) {
        throw new RestException(Status.NOT_FOUND, "Partitioned Topic not found");
    }
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    PartitionedTopicStats stats = new PartitionedTopicStats(partitionMetadata);
    try {
        for (int i = 0; i < partitionMetadata.partitions; i++) {
            PersistentTopicStats partitionStats = pulsar().getAdminClient().persistentTopics().getStats(topicName.getPartition(i).toString());
            stats.add(partitionStats);
            stats.partitions.put(topicName.getPartition(i).toString(), partitionStats);
        }
    } catch (Exception e) {
        throw new RestException(e);
    }
    return stats;
}
Also used : PartitionedTopicStats(org.apache.pulsar.common.policies.data.PartitionedTopicStats) RestException(org.apache.pulsar.broker.web.RestException) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) 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 20 with PartitionedTopicMetadata

use of org.apache.pulsar.common.partition.PartitionedTopicMetadata in project incubator-pulsar by apache.

the class AdminResource method getPartitionedTopicMetadata.

protected PartitionedTopicMetadata getPartitionedTopicMetadata(TopicName topicName, boolean authoritative) {
    validateClusterOwnership(topicName.getCluster());
    // validates global-namespace contains local/peer cluster: if peer/local cluster present then lookup can
    // serve/redirect request else fail partitioned-metadata-request so, client fails while creating
    // producer/consumer
    validateGlobalNamespaceOwnership(topicName.getNamespaceObject());
    try {
        checkConnect(topicName);
    } catch (WebApplicationException e) {
        validateAdminAccessOnProperty(topicName.getProperty());
    } catch (Exception e) {
        // unknown error marked as internal server error
        log.warn("Unexpected error while authorizing lookup. topic={}, role={}. Error: {}", topicName, clientAppId(), e.getMessage(), e);
        throw new RestException(e);
    }
    String path = path(PARTITIONED_TOPIC_PATH_ZNODE, namespaceName.toString(), domain(), topicName.getEncodedLocalName());
    PartitionedTopicMetadata partitionMetadata = fetchPartitionedTopicMetadata(pulsar(), path);
    if (log.isDebugEnabled()) {
        log.debug("[{}] Total number of partitions for topic {} is {}", clientAppId(), topicName, partitionMetadata.partitions);
    }
    return partitionMetadata;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RestException(org.apache.pulsar.broker.web.RestException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

PartitionedTopicMetadata (org.apache.pulsar.common.partition.PartitionedTopicMetadata)28 ExecutionException (java.util.concurrent.ExecutionException)18 RestException (org.apache.pulsar.broker.web.RestException)17 KeeperException (org.apache.zookeeper.KeeperException)17 IOException (java.io.IOException)15 WebApplicationException (javax.ws.rs.WebApplicationException)15 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)15 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)15 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)15 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)15 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)14 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)14 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)14 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)14 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)14 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)11 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 TopicName (org.apache.pulsar.common.naming.TopicName)6 WebTarget (javax.ws.rs.client.WebTarget)3