Search in sources :

Example 51 with Errors

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

the class OffsetsForLeaderEpochClient method handleResponse.

@Override
protected OffsetForEpochResult handleResponse(Node node, Map<TopicPartition, SubscriptionState.FetchPosition> requestData, OffsetsForLeaderEpochResponse response) {
    Set<TopicPartition> partitionsToRetry = new HashSet<>(requestData.keySet());
    Set<String> unauthorizedTopics = new HashSet<>();
    Map<TopicPartition, EpochEndOffset> endOffsets = new HashMap<>();
    for (OffsetForLeaderTopicResult topic : response.data().topics()) {
        for (EpochEndOffset partition : topic.partitions()) {
            TopicPartition topicPartition = new TopicPartition(topic.topic(), partition.partition());
            if (!requestData.containsKey(topicPartition)) {
                logger().warn("Received unrequested topic or partition {} from response, ignoring.", topicPartition);
                continue;
            }
            Errors error = Errors.forCode(partition.errorCode());
            switch(error) {
                case NONE:
                    logger().debug("Handling OffsetsForLeaderEpoch response for {}. Got offset {} for epoch {}.", topicPartition, partition.endOffset(), partition.leaderEpoch());
                    endOffsets.put(topicPartition, partition);
                    partitionsToRetry.remove(topicPartition);
                    break;
                case NOT_LEADER_OR_FOLLOWER:
                case REPLICA_NOT_AVAILABLE:
                case KAFKA_STORAGE_ERROR:
                case OFFSET_NOT_AVAILABLE:
                case LEADER_NOT_AVAILABLE:
                case FENCED_LEADER_EPOCH:
                case UNKNOWN_LEADER_EPOCH:
                    logger().debug("Attempt to fetch offsets for partition {} failed due to {}, retrying.", topicPartition, error);
                    break;
                case UNKNOWN_TOPIC_OR_PARTITION:
                    logger().warn("Received unknown topic or partition error in OffsetsForLeaderEpoch request for partition {}.", topicPartition);
                    break;
                case TOPIC_AUTHORIZATION_FAILED:
                    unauthorizedTopics.add(topicPartition.topic());
                    partitionsToRetry.remove(topicPartition);
                    break;
                default:
                    logger().warn("Attempt to fetch offsets for partition {} failed due to: {}, retrying.", topicPartition, error.message());
            }
        }
    }
    if (!unauthorizedTopics.isEmpty())
        throw new TopicAuthorizationException(unauthorizedTopics);
    else
        return new OffsetForEpochResult(endOffsets, partitionsToRetry);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetForLeaderTopicResult(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.OffsetForLeaderTopicResult) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) HashSet(java.util.HashSet)

Example 52 with Errors

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

the class Sender method completeBatch.

/**
 * Complete or retry the given batch of records.
 *
 * @param batch The record batch
 * @param response The produce response
 * @param correlationId The correlation id for the request
 * @param now The current POSIX timestamp in milliseconds
 */
private void completeBatch(ProducerBatch batch, ProduceResponse.PartitionResponse response, long correlationId, long now) {
    Errors error = response.error;
    if (error == Errors.MESSAGE_TOO_LARGE && batch.recordCount > 1 && !batch.isDone() && (batch.magic() >= RecordBatch.MAGIC_VALUE_V2 || batch.isCompressed())) {
        // If the batch is too large, we split the batch and send the split batches again. We do not decrement
        // the retry attempts in this case.
        log.warn("Got error produce response in correlation id {} on topic-partition {}, splitting and retrying ({} attempts left). Error: {}", correlationId, batch.topicPartition, this.retries - batch.attempts(), formatErrMsg(response));
        if (transactionManager != null)
            transactionManager.removeInFlightBatch(batch);
        this.accumulator.splitAndReenqueue(batch);
        maybeRemoveAndDeallocateBatch(batch);
        this.sensors.recordBatchSplit();
    } else if (error != Errors.NONE) {
        if (canRetry(batch, response, now)) {
            log.warn("Got error produce response with correlation id {} on topic-partition {}, retrying ({} attempts left). Error: {}", correlationId, batch.topicPartition, this.retries - batch.attempts() - 1, formatErrMsg(response));
            reenqueueBatch(batch, now);
        } else if (error == Errors.DUPLICATE_SEQUENCE_NUMBER) {
            // If we have received a duplicate sequence error, it means that the sequence number has advanced beyond
            // the sequence of the current batch, and we haven't retained batch metadata on the broker to return
            // the correct offset and timestamp.
            // 
            // The only thing we can do is to return success to the user and not return a valid offset and timestamp.
            completeBatch(batch, response);
        } else {
            // tell the user the result of their request. We only adjust sequence numbers if the batch didn't exhaust
            // its retries -- if it did, we don't know whether the sequence number was accepted or not, and
            // thus it is not safe to reassign the sequence.
            failBatch(batch, response, batch.attempts() < this.retries);
        }
        if (error.exception() instanceof InvalidMetadataException) {
            if (error.exception() instanceof UnknownTopicOrPartitionException) {
                log.warn("Received unknown topic or partition error in produce request on partition {}. The " + "topic-partition may not exist or the user may not have Describe access to it", batch.topicPartition);
            } else {
                log.warn("Received invalid metadata error in produce request on partition {} due to {}. Going " + "to request metadata update now", batch.topicPartition, error.exception(response.errorMessage).toString());
            }
            metadata.requestUpdate();
        }
    } else {
        completeBatch(batch, response);
    }
    // Unmute the completed partition.
    if (guaranteeMessageOrder)
        this.accumulator.unmutePartition(batch.topicPartition);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) InvalidMetadataException(org.apache.kafka.common.errors.InvalidMetadataException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException)

Example 53 with Errors

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

the class RequestResponseTest method createWriteTxnMarkersResponse.

private WriteTxnMarkersResponse createWriteTxnMarkersResponse() {
    final Map<TopicPartition, Errors> errorPerPartitions = new HashMap<>();
    errorPerPartitions.put(new TopicPartition("topic", 73), Errors.NONE);
    final Map<Long, Map<TopicPartition, Errors>> response = new HashMap<>();
    response.put(21L, errorPerPartitions);
    return new WriteTxnMarkersResponse(response);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 54 with Errors

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

the class RequestResponseTest method checkErrorResponse.

private void checkErrorResponse(AbstractRequest req, Throwable e) {
    AbstractResponse response = req.getErrorResponse(e);
    checkResponse(response, req.version());
    Errors error = Errors.forException(e);
    Map<Errors, Integer> errorCounts = response.errorCounts();
    assertEquals(Collections.singleton(error), errorCounts.keySet(), "API Key " + req.apiKey().name + " v" + req.version() + " failed errorCounts test");
    assertTrue(errorCounts.get(error) > 0);
    if (e instanceof UnknownServerException) {
        String responseStr = response.toString();
        assertFalse(responseStr.contains(e.getMessage()), String.format("Unknown message included in response for %s: %s ", req.apiKey(), responseStr));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException)

Example 55 with Errors

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

the class LeaderAndIsrResponseTest method createPartitions.

private List<LeaderAndIsrPartitionError> createPartitions(String topicName, List<Errors> errors) {
    List<LeaderAndIsrPartitionError> partitions = new ArrayList<>();
    int partitionIndex = 0;
    for (Errors error : errors) {
        partitions.add(new LeaderAndIsrPartitionError().setTopicName(topicName).setPartitionIndex(partitionIndex++).setErrorCode(error.code()));
    }
    return partitions;
}
Also used : LeaderAndIsrPartitionError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrPartitionError) Errors(org.apache.kafka.common.protocol.Errors) ArrayList(java.util.ArrayList)

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