Search in sources :

Example 11 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class RequestResponseTest method createLeaderAndIsrResponse.

private LeaderAndIsrResponse createLeaderAndIsrResponse() {
    Map<TopicPartition, Errors> responses = new HashMap<>();
    responses.put(new TopicPartition("test", 0), Errors.NONE);
    return new LeaderAndIsrResponse(Errors.NONE, responses);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TopicPartition(org.apache.kafka.common.TopicPartition)

Example 12 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class Fetcher method getTopicMetadata.

/**
     * Get metadata for all topics present in Kafka cluster
     *
     * @param request The MetadataRequest to send
     * @param timeout time for which getting topic metadata is attempted
     * @return The map of topics with their partition information
     */
public Map<String, List<PartitionInfo>> getTopicMetadata(MetadataRequest.Builder request, long timeout) {
    // Save the round trip if no topics are requested.
    if (!request.isAllTopics() && request.topics().isEmpty())
        return Collections.emptyMap();
    long start = time.milliseconds();
    long remaining = timeout;
    do {
        RequestFuture<ClientResponse> future = sendMetadataRequest(request);
        client.poll(future, remaining);
        if (future.failed() && !future.isRetriable())
            throw future.exception();
        if (future.succeeded()) {
            MetadataResponse response = (MetadataResponse) future.value().responseBody();
            Cluster cluster = response.cluster();
            Set<String> unauthorizedTopics = cluster.unauthorizedTopics();
            if (!unauthorizedTopics.isEmpty())
                throw new TopicAuthorizationException(unauthorizedTopics);
            boolean shouldRetry = false;
            Map<String, Errors> errors = response.errors();
            if (!errors.isEmpty()) {
                // if there were errors, we need to check whether they were fatal or whether
                // we should just retry
                log.debug("Topic metadata fetch included errors: {}", errors);
                for (Map.Entry<String, Errors> errorEntry : errors.entrySet()) {
                    String topic = errorEntry.getKey();
                    Errors error = errorEntry.getValue();
                    if (error == Errors.INVALID_TOPIC_EXCEPTION)
                        throw new InvalidTopicException("Topic '" + topic + "' is invalid");
                    else if (error == Errors.UNKNOWN_TOPIC_OR_PARTITION)
                        // in the returned map
                        continue;
                    else if (error.exception() instanceof RetriableException)
                        shouldRetry = true;
                    else
                        throw new KafkaException("Unexpected error fetching metadata for topic " + topic, error.exception());
                }
            }
            if (!shouldRetry) {
                HashMap<String, List<PartitionInfo>> topicsPartitionInfos = new HashMap<>();
                for (String topic : cluster.topics()) topicsPartitionInfos.put(topic, cluster.availablePartitionsForTopic(topic));
                return topicsPartitionInfos;
            }
        }
        long elapsed = time.milliseconds() - start;
        remaining = timeout - elapsed;
        if (remaining > 0) {
            long backoff = Math.min(remaining, retryBackoffMs);
            time.sleep(backoff);
            remaining -= backoff;
        }
    } while (remaining > 0);
    throw new TimeoutException("Timeout expired while fetching topic metadata");
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Cluster(org.apache.kafka.common.Cluster) Errors(org.apache.kafka.common.protocol.Errors) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) KafkaException(org.apache.kafka.common.KafkaException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) RetriableException(org.apache.kafka.common.errors.RetriableException) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 13 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class KafkaConsumerTest method testGracefulClose.

@Test
public void testGracefulClose() throws Exception {
    Map<TopicPartition, Errors> response = new HashMap<>();
    response.put(tp0, Errors.NONE);
    OffsetCommitResponse commitResponse = offsetCommitResponse(response);
    LeaveGroupResponse leaveGroupResponse = new LeaveGroupResponse(Errors.NONE);
    consumerCloseTest(5000, Arrays.asList(commitResponse, leaveGroupResponse), 0, false);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) Test(org.junit.Test)

Example 14 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class CreateTopicsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(Throwable e) {
    Map<String, CreateTopicsResponse.Error> topicErrors = new HashMap<>();
    for (String topic : topics.keySet()) {
        Errors error = Errors.forException(e);
        // Avoid populating the error message if it's a generic one
        String message = error.message().equals(e.getMessage()) ? null : e.getMessage();
        topicErrors.put(topic, new CreateTopicsResponse.Error(error, message));
    }
    short versionId = version();
    switch(versionId) {
        case 0:
        case 1:
            return new CreateTopicsResponse(topicErrors);
        default:
            throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d", versionId, this.getClass().getSimpleName(), ApiKeys.CREATE_TOPICS.latestVersion()));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap)

Example 15 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class MetadataRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(Throwable e) {
    List<MetadataResponse.TopicMetadata> topicMetadatas = new ArrayList<>();
    Errors error = Errors.forException(e);
    List<MetadataResponse.PartitionMetadata> partitions = Collections.emptyList();
    if (topics != null) {
        for (String topic : topics) topicMetadatas.add(new MetadataResponse.TopicMetadata(error, topic, false, partitions));
    }
    short versionId = version();
    switch(versionId) {
        case 0:
        case 1:
        case 2:
            return new MetadataResponse(Collections.<Node>emptyList(), null, MetadataResponse.NO_CONTROLLER_ID, topicMetadatas);
        default:
            throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d", versionId, this.getClass().getSimpleName(), ApiKeys.METADATA.latestVersion()));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) ArrayList(java.util.ArrayList)

Aggregations

Errors (org.apache.kafka.common.protocol.Errors)18 HashMap (java.util.HashMap)16 TopicPartition (org.apache.kafka.common.TopicPartition)12 LinkedHashMap (java.util.LinkedHashMap)9 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Struct (org.apache.kafka.common.protocol.types.Struct)4 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)3 List (java.util.List)2 KafkaException (org.apache.kafka.common.KafkaException)2 OffsetCommitResponse (org.apache.kafka.common.requests.OffsetCommitResponse)2 Test (org.junit.Test)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClientResponse (org.apache.kafka.clients.ClientResponse)1 MockClient (org.apache.kafka.clients.MockClient)1 OffsetOutOfRangeException (org.apache.kafka.clients.consumer.OffsetOutOfRangeException)1 Cluster (org.apache.kafka.common.Cluster)1 InvalidMetadataException (org.apache.kafka.common.errors.InvalidMetadataException)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 RecordTooLargeException (org.apache.kafka.common.errors.RecordTooLargeException)1