Search in sources :

Example 31 with Errors

use of org.apache.kafka.common.protocol.Errors in project apache-kafka-on-k8s by banzaicloud.

the class AlterReplicaLogDirsResponse method toStruct.

@Override
protected Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.ALTER_REPLICA_LOG_DIRS.responseSchema(version));
    struct.set(THROTTLE_TIME_MS, throttleTimeMs);
    Map<String, Map<Integer, Errors>> responsesByTopic = CollectionUtils.groupDataByTopic(responses);
    List<Struct> topicStructArray = new ArrayList<>();
    for (Map.Entry<String, Map<Integer, Errors>> responsesByTopicEntry : responsesByTopic.entrySet()) {
        Struct topicStruct = struct.instance(TOPICS_KEY_NAME);
        topicStruct.set(TOPIC_NAME, responsesByTopicEntry.getKey());
        List<Struct> partitionStructArray = new ArrayList<>();
        for (Map.Entry<Integer, Errors> responsesByPartitionEntry : responsesByTopicEntry.getValue().entrySet()) {
            Struct partitionStruct = topicStruct.instance(PARTITIONS_KEY_NAME);
            Errors response = responsesByPartitionEntry.getValue();
            partitionStruct.set(PARTITION_ID, responsesByPartitionEntry.getKey());
            partitionStruct.set(ERROR_CODE, response.code());
            partitionStructArray.add(partitionStruct);
        }
        topicStruct.set(PARTITIONS_KEY_NAME, partitionStructArray.toArray());
        topicStructArray.add(topicStruct);
    }
    struct.set(TOPICS_KEY_NAME, topicStructArray.toArray());
    return struct;
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 32 with Errors

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

the class OffsetFetchRequest method getErrorResponse.

public OffsetFetchResponse getErrorResponse(int throttleTimeMs, Errors error) {
    Map<TopicPartition, OffsetFetchResponse.PartitionData> responsePartitions = new HashMap<>();
    if (version() < 2) {
        OffsetFetchResponse.PartitionData partitionError = new OffsetFetchResponse.PartitionData(OffsetFetchResponse.INVALID_OFFSET, Optional.empty(), OffsetFetchResponse.NO_METADATA, error);
        for (OffsetFetchRequestTopic topic : this.data.topics()) {
            for (int partitionIndex : topic.partitionIndexes()) {
                responsePartitions.put(new TopicPartition(topic.name(), partitionIndex), partitionError);
            }
        }
        return new OffsetFetchResponse(error, responsePartitions);
    }
    if (version() == 2) {
        return new OffsetFetchResponse(error, responsePartitions);
    }
    if (version() >= 3 && version() < 8) {
        return new OffsetFetchResponse(throttleTimeMs, error, responsePartitions);
    }
    List<String> groupIds = groupIds();
    Map<String, Errors> errorsMap = new HashMap<>(groupIds.size());
    Map<String, Map<TopicPartition, OffsetFetchResponse.PartitionData>> partitionMap = new HashMap<>(groupIds.size());
    for (String g : groupIds) {
        errorsMap.put(g, error);
        partitionMap.put(g, responsePartitions);
    }
    return new OffsetFetchResponse(throttleTimeMs, errorsMap, partitionMap);
}
Also used : HashMap(java.util.HashMap) OffsetFetchRequestTopic(org.apache.kafka.common.message.OffsetFetchRequestData.OffsetFetchRequestTopic) Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with Errors

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

the class OffsetsForLeaderEpochRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    Errors error = Errors.forException(e);
    OffsetForLeaderEpochResponseData responseData = new OffsetForLeaderEpochResponseData();
    data.topics().forEach(topic -> {
        OffsetForLeaderTopicResult topicData = new OffsetForLeaderTopicResult().setTopic(topic.topic());
        topic.partitions().forEach(partition -> topicData.partitions().add(new EpochEndOffset().setPartition(partition.partition()).setErrorCode(error.code()).setLeaderEpoch(UNDEFINED_EPOCH).setEndOffset(UNDEFINED_EPOCH_OFFSET)));
        responseData.topics().add(topicData);
    });
    return new OffsetsForLeaderEpochResponse(responseData);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) OffsetForLeaderTopicResult(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.OffsetForLeaderTopicResult) OffsetForLeaderEpochResponseData(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData)

Example 34 with Errors

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

the class FetchRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    // For versions 13+ the error is indicated by setting the top-level error code, and no partitions will be returned.
    // For earlier versions, the error is indicated in two ways: by setting the same error code in all partitions,
    // and by setting the top-level error code.  The form where we set the same error code in all partitions
    // is needed in order to maintain backwards compatibility with older versions of the protocol
    // in which there was no top-level error code. Note that for incremental fetch responses, there
    // may not be any partitions at all in the response.  For this reason, the top-level error code
    // is essential for them.
    Errors error = Errors.forException(e);
    List<FetchResponseData.FetchableTopicResponse> topicResponseList = new ArrayList<>();
    // For version 13+, we know the client can handle a top level error code, so we don't need to send back partitions too.
    if (version() < 13) {
        data.topics().forEach(topic -> {
            List<FetchResponseData.PartitionData> partitionResponses = topic.partitions().stream().map(partition -> FetchResponse.partitionResponse(partition.partition(), error)).collect(Collectors.toList());
            topicResponseList.add(new FetchResponseData.FetchableTopicResponse().setTopic(topic.topic()).setTopicId(topic.topicId()).setPartitions(partitionResponses));
        });
    }
    return new FetchResponse(new FetchResponseData().setThrottleTimeMs(throttleTimeMs).setErrorCode(error.code()).setSessionId(data.sessionId()).setResponses(topicResponseList));
}
Also used : Uuid(org.apache.kafka.common.Uuid) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) ForgottenTopic(org.apache.kafka.common.message.FetchRequestData.ForgottenTopic) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) TopicIdPartition(org.apache.kafka.common.TopicIdPartition) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) FetchResponseData(org.apache.kafka.common.message.FetchResponseData) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) IsolationLevel(org.apache.kafka.common.IsolationLevel) List(java.util.List) RecordBatch(org.apache.kafka.common.record.RecordBatch) Map(java.util.Map) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) Collections(java.util.Collections) FetchRequestData(org.apache.kafka.common.message.FetchRequestData) Errors(org.apache.kafka.common.protocol.Errors) FetchResponseData(org.apache.kafka.common.message.FetchResponseData) ArrayList(java.util.ArrayList)

Example 35 with Errors

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

the class FindCoordinatorRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    FindCoordinatorResponseData response = new FindCoordinatorResponseData();
    if (version() >= 2) {
        response.setThrottleTimeMs(throttleTimeMs);
    }
    Errors error = Errors.forException(e);
    if (version() < MIN_BATCHED_VERSION) {
        return FindCoordinatorResponse.prepareOldResponse(error, Node.noNode());
    } else {
        return FindCoordinatorResponse.prepareErrorResponse(error, data.coordinatorKeys());
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData)

Aggregations

Errors (org.apache.kafka.common.protocol.Errors)167 HashMap (java.util.HashMap)115 TopicPartition (org.apache.kafka.common.TopicPartition)87 Map (java.util.Map)61 ArrayList (java.util.ArrayList)46 LinkedHashMap (java.util.LinkedHashMap)31 Test (org.junit.jupiter.api.Test)31 List (java.util.List)19 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)19 HashSet (java.util.HashSet)18 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)18 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)17 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)17 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)17 KafkaException (org.apache.kafka.common.KafkaException)16 Node (org.apache.kafka.common.Node)16 Cluster (org.apache.kafka.common.Cluster)15 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)14 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)14 Collections (java.util.Collections)13