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);
}
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);
}
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);
}
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));
}
}
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;
}
Aggregations