Search in sources :

Example 1 with EpochEndOffset

use of org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset 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 2 with EpochEndOffset

use of org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset 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 3 with EpochEndOffset

use of org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset in project kafka by apache.

the class RequestResponseTest method createLeaderEpochResponse.

private OffsetsForLeaderEpochResponse createLeaderEpochResponse() {
    OffsetForLeaderEpochResponseData data = new OffsetForLeaderEpochResponseData();
    data.topics().add(new OffsetForLeaderTopicResult().setTopic("topic1").setPartitions(asList(new EpochEndOffset().setPartition(0).setErrorCode(Errors.NONE.code()).setLeaderEpoch(1).setEndOffset(0), new EpochEndOffset().setPartition(1).setErrorCode(Errors.NONE.code()).setLeaderEpoch(1).setEndOffset(1))));
    data.topics().add(new OffsetForLeaderTopicResult().setTopic("topic2").setPartitions(singletonList(new EpochEndOffset().setPartition(2).setErrorCode(Errors.NONE.code()).setLeaderEpoch(1).setEndOffset(1))));
    return new OffsetsForLeaderEpochResponse(data);
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) OffsetForLeaderTopicResult(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.OffsetForLeaderTopicResult) OffsetForLeaderEpochResponseData(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData)

Example 4 with EpochEndOffset

use of org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset in project kafka by apache.

the class SubscriptionStateTest method testTruncationDetectionWithResetPolicy.

@Test
public void testTruncationDetectionWithResetPolicy() {
    Node broker1 = new Node(1, "localhost", 9092);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    long divergentOffset = 5L;
    int divergentOffsetEpoch = 7;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(divergentOffsetEpoch).setEndOffset(divergentOffset));
    assertEquals(Optional.empty(), truncationOpt);
    assertFalse(state.awaitingValidation(tp0));
    SubscriptionState.FetchPosition updatedPosition = new SubscriptionState.FetchPosition(divergentOffset, Optional.of(divergentOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    assertEquals(updatedPosition, state.position(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Example 5 with EpochEndOffset

use of org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset in project kafka by apache.

the class SubscriptionStateTest method testMaybeCompleteValidationAfterOffsetReset.

@Test
public void testMaybeCompleteValidationAfterOffsetReset() {
    Node broker1 = new Node(1, "localhost", 9092);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    state.requestOffsetReset(tp0);
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(initialOffsetEpoch).setEndOffset(initialOffset + 5));
    assertEquals(Optional.empty(), truncationOpt);
    assertFalse(state.awaitingValidation(tp0));
    assertTrue(state.isOffsetResetNeeded(tp0));
    assertNull(state.position(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Aggregations

EpochEndOffset (org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset)13 Metadata (org.apache.kafka.clients.Metadata)8 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)8 Node (org.apache.kafka.common.Node)8 Test (org.junit.jupiter.api.Test)8 LogTruncation (org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation)7 OffsetForLeaderTopicResult (org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.OffsetForLeaderTopicResult)6 OffsetForLeaderEpochResponseData (org.apache.kafka.common.message.OffsetForLeaderEpochResponseData)5 LogContext (org.apache.kafka.common.utils.LogContext)4 OffsetsForLeaderEpochResponse (org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Errors (org.apache.kafka.common.protocol.Errors)2 DataOutputStream (java.io.DataOutputStream)1 Field (java.lang.reflect.Field)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1