Search in sources :

Example 21 with InvalidTopicException

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

the class KafkaAdminClient method handleDeleteTopicsUsingIds.

private Map<Uuid, KafkaFuture<Void>> handleDeleteTopicsUsingIds(final Collection<Uuid> topicIds, final DeleteTopicsOptions options) {
    final Map<Uuid, KafkaFutureImpl<Void>> topicFutures = new HashMap<>(topicIds.size());
    final List<Uuid> validTopicIds = new ArrayList<>(topicIds.size());
    for (Uuid topicId : topicIds) {
        if (topicId.equals(Uuid.ZERO_UUID)) {
            KafkaFutureImpl<Void> 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<>());
            validTopicIds.add(topicId);
        }
    }
    if (!validTopicIds.isEmpty()) {
        final long now = time.milliseconds();
        final long deadline = calcDeadlineMs(now, options.timeoutMs());
        final Call call = getDeleteTopicsWithIdsCall(options, topicFutures, validTopicIds, Collections.emptyMap(), now, deadline);
        runnable.call(call, now);
    }
    return new HashMap<>(topicFutures);
}
Also used : Uuid(org.apache.kafka.common.Uuid) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl)

Example 22 with InvalidTopicException

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

the class KafkaAdminClient method listPartitionReassignments.

@Override
public ListPartitionReassignmentsResult listPartitionReassignments(Optional<Set<TopicPartition>> partitions, ListPartitionReassignmentsOptions options) {
    final KafkaFutureImpl<Map<TopicPartition, PartitionReassignment>> partitionReassignmentsFuture = new KafkaFutureImpl<>();
    if (partitions.isPresent()) {
        for (TopicPartition tp : partitions.get()) {
            String topic = tp.topic();
            int partition = tp.partition();
            if (topicNameIsUnrepresentable(topic)) {
                partitionReassignmentsFuture.completeExceptionally(new InvalidTopicException("The given topic name '" + topic + "' cannot be represented in a request."));
            } else if (partition < 0) {
                partitionReassignmentsFuture.completeExceptionally(new InvalidTopicException("The given partition index " + partition + " is not valid."));
            }
            if (partitionReassignmentsFuture.isCompletedExceptionally())
                return new ListPartitionReassignmentsResult(partitionReassignmentsFuture);
        }
    }
    final long now = time.milliseconds();
    runnable.call(new Call("listPartitionReassignments", calcDeadlineMs(now, options.timeoutMs()), new ControllerNodeProvider()) {

        @Override
        ListPartitionReassignmentsRequest.Builder createRequest(int timeoutMs) {
            ListPartitionReassignmentsRequestData listData = new ListPartitionReassignmentsRequestData();
            listData.setTimeoutMs(timeoutMs);
            if (partitions.isPresent()) {
                Map<String, ListPartitionReassignmentsTopics> reassignmentTopicByTopicName = new HashMap<>();
                for (TopicPartition tp : partitions.get()) {
                    if (!reassignmentTopicByTopicName.containsKey(tp.topic()))
                        reassignmentTopicByTopicName.put(tp.topic(), new ListPartitionReassignmentsTopics().setName(tp.topic()));
                    reassignmentTopicByTopicName.get(tp.topic()).partitionIndexes().add(tp.partition());
                }
                listData.setTopics(new ArrayList<>(reassignmentTopicByTopicName.values()));
            }
            return new ListPartitionReassignmentsRequest.Builder(listData);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            ListPartitionReassignmentsResponse response = (ListPartitionReassignmentsResponse) abstractResponse;
            Errors error = Errors.forCode(response.data().errorCode());
            switch(error) {
                case NONE:
                    break;
                case NOT_CONTROLLER:
                    handleNotControllerError(error);
                    break;
                default:
                    partitionReassignmentsFuture.completeExceptionally(new ApiError(error, response.data().errorMessage()).exception());
                    break;
            }
            Map<TopicPartition, PartitionReassignment> reassignmentMap = new HashMap<>();
            for (OngoingTopicReassignment topicReassignment : response.data().topics()) {
                String topicName = topicReassignment.name();
                for (OngoingPartitionReassignment partitionReassignment : topicReassignment.partitions()) {
                    reassignmentMap.put(new TopicPartition(topicName, partitionReassignment.partitionIndex()), new PartitionReassignment(partitionReassignment.replicas(), partitionReassignment.addingReplicas(), partitionReassignment.removingReplicas()));
                }
            }
            partitionReassignmentsFuture.complete(reassignmentMap);
        }

        @Override
        void handleFailure(Throwable throwable) {
            partitionReassignmentsFuture.completeExceptionally(throwable);
        }
    }, now);
    return new ListPartitionReassignmentsResult(partitionReassignmentsFuture);
}
Also used : ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) ListPartitionReassignmentsResponse(org.apache.kafka.common.requests.ListPartitionReassignmentsResponse) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) ListPartitionReassignmentsRequest(org.apache.kafka.common.requests.ListPartitionReassignmentsRequest) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) Errors(org.apache.kafka.common.protocol.Errors) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) TopicPartition(org.apache.kafka.common.TopicPartition) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) ApiError(org.apache.kafka.common.requests.ApiError) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 23 with InvalidTopicException

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

the class DescribeProducersHandler method handlePartitionError.

private void handlePartitionError(TopicPartition topicPartition, ApiError apiError, Map<TopicPartition, Throwable> failed, List<TopicPartition> unmapped) {
    switch(apiError.error()) {
        case NOT_LEADER_OR_FOLLOWER:
            if (options.brokerId().isPresent()) {
                // Typically these errors are retriable, but if the user specified the brokerId
                // explicitly, then they are fatal.
                int brokerId = options.brokerId().getAsInt();
                log.error("Not leader error in `DescribeProducers` response for partition {} " + "for brokerId {} set in options", topicPartition, brokerId, apiError.exception());
                failed.put(topicPartition, apiError.error().exception("Failed to describe active producers " + "for partition " + topicPartition + " on brokerId " + brokerId));
            } else {
                // Otherwise, we unmap the partition so that we can find the new leader
                log.debug("Not leader error in `DescribeProducers` response for partition {}. " + "Will retry later.", topicPartition);
                unmapped.add(topicPartition);
            }
            break;
        case UNKNOWN_TOPIC_OR_PARTITION:
            log.debug("Unknown topic/partition error in `DescribeProducers` response for partition {}. " + "Will retry later.", topicPartition);
            break;
        case INVALID_TOPIC_EXCEPTION:
            log.error("Invalid topic in `DescribeProducers` response for partition {}", topicPartition, apiError.exception());
            failed.put(topicPartition, new InvalidTopicException("Failed to fetch metadata for partition " + topicPartition + " due to invalid topic error: " + apiError.messageWithFallback(), Collections.singleton(topicPartition.topic())));
            break;
        case TOPIC_AUTHORIZATION_FAILED:
            log.error("Authorization failed in `DescribeProducers` response for partition {}", topicPartition, apiError.exception());
            failed.put(topicPartition, new TopicAuthorizationException("Failed to describe " + "active producers for partition " + topicPartition + " due to authorization failure on topic" + " `" + topicPartition.topic() + "`", Collections.singleton(topicPartition.topic())));
            break;
        default:
            log.error("Unexpected error in `DescribeProducers` response for partition {}", topicPartition, apiError.exception());
            failed.put(topicPartition, apiError.error().exception("Failed to describe active " + "producers for partition " + topicPartition + " due to unexpected error"));
            break;
    }
}
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