Search in sources :

Example 6 with InvalidTopicException

use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.

the class KafkaAdminClient method handleDescribeTopicsByIds.

private Map<Uuid, KafkaFuture<TopicDescription>> handleDescribeTopicsByIds(Collection<Uuid> topicIds, DescribeTopicsOptions options) {
    final Map<Uuid, KafkaFutureImpl<TopicDescription>> topicFutures = new HashMap<>(topicIds.size());
    final List<Uuid> topicIdsList = new ArrayList<>();
    for (Uuid topicId : topicIds) {
        if (topicIdIsUnrepresentable(topicId)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new InvalidTopicException("The given topic id '" + topicId + "' cannot be represented in a request."));
            topicFutures.put(topicId, future);
        } else if (!topicFutures.containsKey(topicId)) {
            topicFutures.put(topicId, new KafkaFutureImpl<>());
            topicIdsList.add(topicId);
        }
    }
    final long now = time.milliseconds();
    Call call = new Call("describeTopicsWithIds", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        MetadataRequest.Builder createRequest(int timeoutMs) {
            return new MetadataRequest.Builder(new MetadataRequestData().setTopics(convertTopicIdsToMetadataRequestTopic(topicIdsList)).setAllowAutoTopicCreation(false).setIncludeTopicAuthorizedOperations(options.includeAuthorizedOperations()));
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            MetadataResponse response = (MetadataResponse) abstractResponse;
            // Handle server responses for particular topics.
            Cluster cluster = response.buildCluster();
            Map<Uuid, Errors> errors = response.errorsByTopicId();
            for (Map.Entry<Uuid, KafkaFutureImpl<TopicDescription>> entry : topicFutures.entrySet()) {
                Uuid topicId = entry.getKey();
                KafkaFutureImpl<TopicDescription> future = entry.getValue();
                String topicName = cluster.topicName(topicId);
                if (topicName == null) {
                    future.completeExceptionally(new InvalidTopicException("TopicId " + topicId + " not found."));
                    continue;
                }
                Errors topicError = errors.get(topicId);
                if (topicError != null) {
                    future.completeExceptionally(topicError.exception());
                    continue;
                }
                Integer authorizedOperations = response.topicAuthorizedOperations(topicName).get();
                TopicDescription topicDescription = getTopicDescriptionFromCluster(cluster, topicName, topicId, authorizedOperations);
                future.complete(topicDescription);
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(topicFutures.values(), throwable);
        }
    };
    if (!topicIdsList.isEmpty()) {
        runnable.call(call, now);
    }
    return new HashMap<>(topicFutures);
}
Also used : HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) Cluster(org.apache.kafka.common.Cluster) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MetadataRequestData(org.apache.kafka.common.message.MetadataRequestData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Errors(org.apache.kafka.common.protocol.Errors) Uuid(org.apache.kafka.common.Uuid) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 7 with InvalidTopicException

use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.

the class KafkaAdminClient method handleDescribeTopicsByNames.

private Map<String, KafkaFuture<TopicDescription>> handleDescribeTopicsByNames(final Collection<String> topicNames, DescribeTopicsOptions options) {
    final Map<String, KafkaFutureImpl<TopicDescription>> topicFutures = new HashMap<>(topicNames.size());
    final ArrayList<String> topicNamesList = new ArrayList<>();
    for (String topicName : topicNames) {
        if (topicNameIsUnrepresentable(topicName)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new InvalidTopicException("The given topic name '" + topicName + "' cannot be represented in a request."));
            topicFutures.put(topicName, future);
        } else if (!topicFutures.containsKey(topicName)) {
            topicFutures.put(topicName, new KafkaFutureImpl<>());
            topicNamesList.add(topicName);
        }
    }
    final long now = time.milliseconds();
    Call call = new Call("describeTopics", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        private boolean supportsDisablingTopicCreation = true;

        @Override
        MetadataRequest.Builder createRequest(int timeoutMs) {
            if (supportsDisablingTopicCreation)
                return new MetadataRequest.Builder(new MetadataRequestData().setTopics(convertToMetadataRequestTopic(topicNamesList)).setAllowAutoTopicCreation(false).setIncludeTopicAuthorizedOperations(options.includeAuthorizedOperations()));
            else
                return MetadataRequest.Builder.allTopics();
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            MetadataResponse response = (MetadataResponse) abstractResponse;
            // Handle server responses for particular topics.
            Cluster cluster = response.buildCluster();
            Map<String, Errors> errors = response.errors();
            for (Map.Entry<String, KafkaFutureImpl<TopicDescription>> entry : topicFutures.entrySet()) {
                String topicName = entry.getKey();
                KafkaFutureImpl<TopicDescription> future = entry.getValue();
                Errors topicError = errors.get(topicName);
                if (topicError != null) {
                    future.completeExceptionally(topicError.exception());
                    continue;
                }
                if (!cluster.topics().contains(topicName)) {
                    future.completeExceptionally(new UnknownTopicOrPartitionException("Topic " + topicName + " not found."));
                    continue;
                }
                Uuid topicId = cluster.topicId(topicName);
                Integer authorizedOperations = response.topicAuthorizedOperations(topicName).get();
                TopicDescription topicDescription = getTopicDescriptionFromCluster(cluster, topicName, topicId, authorizedOperations);
                future.complete(topicDescription);
            }
        }

        @Override
        boolean handleUnsupportedVersionException(UnsupportedVersionException exception) {
            if (supportsDisablingTopicCreation) {
                supportsDisablingTopicCreation = false;
                return true;
            }
            return false;
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(topicFutures.values(), throwable);
        }
    };
    if (!topicNamesList.isEmpty()) {
        runnable.call(call, now);
    }
    return new HashMap<>(topicFutures);
}
Also used : HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) ArrayList(java.util.ArrayList) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) Cluster(org.apache.kafka.common.Cluster) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MetadataRequestData(org.apache.kafka.common.message.MetadataRequestData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Errors(org.apache.kafka.common.protocol.Errors) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) Uuid(org.apache.kafka.common.Uuid) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 8 with InvalidTopicException

use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.

the class KafkaAdminClient method handleDeleteTopicsUsingNames.

private Map<String, KafkaFuture<Void>> handleDeleteTopicsUsingNames(final Collection<String> topicNames, final DeleteTopicsOptions options) {
    final Map<String, KafkaFutureImpl<Void>> topicFutures = new HashMap<>(topicNames.size());
    final List<String> validTopicNames = new ArrayList<>(topicNames.size());
    for (String topicName : topicNames) {
        if (topicNameIsUnrepresentable(topicName)) {
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new InvalidTopicException("The given topic name '" + topicName + "' cannot be represented in a request."));
            topicFutures.put(topicName, future);
        } else if (!topicFutures.containsKey(topicName)) {
            topicFutures.put(topicName, new KafkaFutureImpl<>());
            validTopicNames.add(topicName);
        }
    }
    if (!validTopicNames.isEmpty()) {
        final long now = time.milliseconds();
        final long deadline = calcDeadlineMs(now, options.timeoutMs());
        final Call call = getDeleteTopicsCall(options, topicFutures, validTopicNames, Collections.emptyMap(), now, deadline);
        runnable.call(call, now);
    }
    return new HashMap<>(topicFutures);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl)

Example 9 with InvalidTopicException

use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.

the class KafkaProducer method waitOnMetadata.

/**
 * Wait for cluster metadata including partitions for the given topic to be available.
 * @param topic The topic we want metadata for
 * @param partition A specific partition expected to exist in metadata, or null if there's no preference
 * @param nowMs The current time in ms
 * @param maxWaitMs The maximum time in ms for waiting on the metadata
 * @return The cluster containing topic metadata and the amount of time we waited in ms
 * @throws TimeoutException if metadata could not be refreshed within {@code max.block.ms}
 * @throws KafkaException for all Kafka-related exceptions, including the case where this method is called after producer close
 */
private ClusterAndWaitTime waitOnMetadata(String topic, Integer partition, long nowMs, long maxWaitMs) throws InterruptedException {
    // add topic to metadata topic list if it is not there already and reset expiry
    Cluster cluster = metadata.fetch();
    if (cluster.invalidTopics().contains(topic))
        throw new InvalidTopicException(topic);
    metadata.add(topic, nowMs);
    Integer partitionsCount = cluster.partitionCountForTopic(topic);
    // or within the known partition range
    if (partitionsCount != null && (partition == null || partition < partitionsCount))
        return new ClusterAndWaitTime(cluster, 0);
    long remainingWaitMs = maxWaitMs;
    long elapsed = 0;
    // is stale and the number of partitions for this topic has increased in the meantime.
    do {
        if (partition != null) {
            log.trace("Requesting metadata update for partition {} of topic {}.", partition, topic);
        } else {
            log.trace("Requesting metadata update for topic {}.", topic);
        }
        metadata.add(topic, nowMs + elapsed);
        int version = metadata.requestUpdateForTopic(topic);
        sender.wakeup();
        try {
            metadata.awaitUpdate(version, remainingWaitMs);
        } catch (TimeoutException ex) {
            // Rethrow with original maxWaitMs to prevent logging exception with remainingWaitMs
            throw new TimeoutException(String.format("Topic %s not present in metadata after %d ms.", topic, maxWaitMs));
        }
        cluster = metadata.fetch();
        elapsed = time.milliseconds() - nowMs;
        if (elapsed >= maxWaitMs) {
            throw new TimeoutException(partitionsCount == null ? String.format("Topic %s not present in metadata after %d ms.", topic, maxWaitMs) : String.format("Partition %d of topic %s with partition count %d is not present in metadata after %d ms.", partition, topic, partitionsCount, maxWaitMs));
        }
        metadata.maybeThrowExceptionForTopic(topic);
        remainingWaitMs = maxWaitMs - elapsed;
        partitionsCount = cluster.partitionCountForTopic(topic);
    } while (partitionsCount == null || (partition != null && partition >= partitionsCount));
    return new ClusterAndWaitTime(cluster, elapsed);
}
Also used : InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Cluster(org.apache.kafka.common.Cluster) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 10 with InvalidTopicException

use of org.apache.kafka.common.errors.InvalidTopicException in project kafka by apache.

the class PartitionLeaderStrategy method handleTopicError.

private void handleTopicError(String topic, Errors topicError, Set<TopicPartition> requestPartitions, Map<TopicPartition, Throwable> failed) {
    switch(topicError) {
        case UNKNOWN_TOPIC_OR_PARTITION:
        case LEADER_NOT_AVAILABLE:
        case BROKER_NOT_AVAILABLE:
            log.debug("Metadata request for topic {} returned topic-level error {}. Will retry", topic, topicError);
            break;
        case TOPIC_AUTHORIZATION_FAILED:
            log.error("Received authorization failure for topic {} in `Metadata` response", topic, topicError.exception());
            failAllPartitionsForTopic(topic, requestPartitions, failed, tp -> new TopicAuthorizationException("Failed to fetch metadata for partition " + tp + " due to topic authorization failure", Collections.singleton(topic)));
            break;
        case INVALID_TOPIC_EXCEPTION:
            log.error("Received invalid topic error for topic {} in `Metadata` response", topic, topicError.exception());
            failAllPartitionsForTopic(topic, requestPartitions, failed, tp -> new InvalidTopicException("Failed to fetch metadata for partition " + tp + " due to invalid topic `" + topic + "`", Collections.singleton(topic)));
            break;
        default:
            log.error("Received unexpected error for topic {} in `Metadata` response", topic, topicError.exception());
            failAllPartitionsForTopic(topic, requestPartitions, failed, tp -> topicError.exception("Failed to fetch metadata for partition " + tp + " due to unexpected error for topic `" + topic + "`"));
    }
}
Also used : InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException)

Aggregations

InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)23 HashMap (java.util.HashMap)15 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)10 Errors (org.apache.kafka.common.protocol.Errors)10 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)8 Cluster (org.apache.kafka.common.Cluster)7 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)7 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)6 TopicPartition (org.apache.kafka.common.TopicPartition)5 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)5 List (java.util.List)4 TreeMap (java.util.TreeMap)4 KafkaException (org.apache.kafka.common.KafkaException)4 KafkaFuture (org.apache.kafka.common.KafkaFuture)4 TimeoutException (org.apache.kafka.common.errors.TimeoutException)4 ApiError (org.apache.kafka.common.requests.ApiError)4 Test (org.junit.jupiter.api.Test)4 LinkedHashMap (java.util.LinkedHashMap)3