Search in sources :

Example 21 with Errors

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

the class MetadataRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, 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);
        case 3:
        case 4:
        case 5:
            return new MetadataResponse(throttleTimeMs, 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)

Example 22 with Errors

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

the class OffsetCommitResponse method toStruct.

@Override
public Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.OFFSET_COMMIT.responseSchema(version));
    struct.setIfExists(THROTTLE_TIME_MS, throttleTimeMs);
    Map<String, Map<Integer, Errors>> topicsData = CollectionUtils.groupDataByTopic(responseData);
    List<Struct> topicArray = new ArrayList<>();
    for (Map.Entry<String, Map<Integer, Errors>> entries : topicsData.entrySet()) {
        Struct topicData = struct.instance(RESPONSES_KEY_NAME);
        topicData.set(TOPIC_NAME, entries.getKey());
        List<Struct> partitionArray = new ArrayList<>();
        for (Map.Entry<Integer, Errors> partitionEntry : entries.getValue().entrySet()) {
            Struct partitionData = topicData.instance(PARTITIONS_KEY_NAME);
            partitionData.set(PARTITION_ID, partitionEntry.getKey());
            partitionData.set(ERROR_CODE, partitionEntry.getValue().code());
            partitionArray.add(partitionData);
        }
        topicData.set(PARTITIONS_KEY_NAME, partitionArray.toArray());
        topicArray.add(topicData);
    }
    struct.set(RESPONSES_KEY_NAME, topicArray.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 23 with Errors

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

the class TxnOffsetCommitRequest method getErrorResponse.

@Override
public TxnOffsetCommitResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    Errors error = Errors.forException(e);
    Map<TopicPartition, Errors> errors = new HashMap<>(offsets.size());
    for (TopicPartition partition : offsets.keySet()) errors.put(partition, error);
    return new TxnOffsetCommitResponse(throttleTimeMs, errors);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition)

Example 24 with Errors

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

the class TxnOffsetCommitResponse method toStruct.

@Override
protected Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.TXN_OFFSET_COMMIT.responseSchema(version));
    struct.set(THROTTLE_TIME_MS, throttleTimeMs);
    Map<String, Map<Integer, Errors>> mappedPartitions = CollectionUtils.groupDataByTopic(errors);
    Object[] partitionsArray = new Object[mappedPartitions.size()];
    int i = 0;
    for (Map.Entry<String, Map<Integer, Errors>> topicAndPartitions : mappedPartitions.entrySet()) {
        Struct topicPartitionsStruct = struct.instance(TOPICS_KEY_NAME);
        topicPartitionsStruct.set(TOPIC_NAME, topicAndPartitions.getKey());
        Map<Integer, Errors> partitionAndErrors = topicAndPartitions.getValue();
        Object[] partitionAndErrorsArray = new Object[partitionAndErrors.size()];
        int j = 0;
        for (Map.Entry<Integer, Errors> partitionAndError : partitionAndErrors.entrySet()) {
            Struct partitionAndErrorStruct = topicPartitionsStruct.instance(PARTITIONS_KEY_NAME);
            partitionAndErrorStruct.set(PARTITION_ID, partitionAndError.getKey());
            partitionAndErrorStruct.set(ERROR_CODE, partitionAndError.getValue().code());
            partitionAndErrorsArray[j++] = partitionAndErrorStruct;
        }
        topicPartitionsStruct.set(PARTITIONS_KEY_NAME, partitionAndErrorsArray);
        partitionsArray[i++] = topicPartitionsStruct;
    }
    struct.set(TOPICS_KEY_NAME, partitionsArray);
    return struct;
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) Map(java.util.Map) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 25 with Errors

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

the class WriteTxnMarkersRequest method getErrorResponse.

@Override
public WriteTxnMarkersResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    Errors error = Errors.forException(e);
    Map<Long, Map<TopicPartition, Errors>> errors = new HashMap<>(markers.size());
    for (TxnMarkerEntry entry : markers) {
        Map<TopicPartition, Errors> errorsPerPartition = new HashMap<>(entry.partitions.size());
        for (TopicPartition partition : entry.partitions) errorsPerPartition.put(partition, error);
        errors.put(entry.producerId, errorsPerPartition);
    }
    return new WriteTxnMarkersResponse(errors);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) HashMap(java.util.HashMap) Map(java.util.Map)

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