Search in sources :

Example 6 with ListOffsetsTopic

use of org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic in project kafka by apache.

the class ListOffsetsRequestTest method testDuplicatePartitions.

@Test
public void testDuplicatePartitions() {
    List<ListOffsetsTopic> topics = Collections.singletonList(new ListOffsetsTopic().setName("topic").setPartitions(Arrays.asList(new ListOffsetsPartition().setPartitionIndex(0), new ListOffsetsPartition().setPartitionIndex(0))));
    ListOffsetsRequestData data = new ListOffsetsRequestData().setTopics(topics).setReplicaId(-1);
    ListOffsetsRequest request = ListOffsetsRequest.parse(MessageUtil.toByteBuffer(data, (short) 0), (short) 0);
    assertEquals(Collections.singleton(new TopicPartition("topic", 0)), request.duplicatePartitions());
}
Also used : ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) ListOffsetsRequestData(org.apache.kafka.common.message.ListOffsetsRequestData) ListOffsetsTopic(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 7 with ListOffsetsTopic

use of org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic in project kafka by apache.

the class MessageTest method testListOffsetsRequestVersions.

@Test
public void testListOffsetsRequestVersions() throws Exception {
    List<ListOffsetsTopic> v = Collections.singletonList(new ListOffsetsTopic().setName("topic").setPartitions(Collections.singletonList(new ListOffsetsPartition().setPartitionIndex(0).setTimestamp(123L))));
    Supplier<ListOffsetsRequestData> newRequest = () -> new ListOffsetsRequestData().setTopics(v).setReplicaId(0);
    testAllMessageRoundTrips(newRequest.get());
    testAllMessageRoundTripsFromVersion((short) 2, newRequest.get().setIsolationLevel(IsolationLevel.READ_COMMITTED.id()));
}
Also used : ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) ListOffsetsTopic(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic) Test(org.junit.jupiter.api.Test)

Example 8 with ListOffsetsTopic

use of org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic in project kafka by apache.

the class RequestResponseTest method createListOffsetRequest.

private ListOffsetsRequest createListOffsetRequest(short version) {
    if (version == 0) {
        ListOffsetsTopic topic = new ListOffsetsTopic().setName("test").setPartitions(singletonList(new ListOffsetsPartition().setPartitionIndex(0).setTimestamp(1000000L).setMaxNumOffsets(10).setCurrentLeaderEpoch(5)));
        return ListOffsetsRequest.Builder.forConsumer(false, IsolationLevel.READ_UNCOMMITTED, false).setTargetTimes(singletonList(topic)).build(version);
    } else if (version == 1) {
        ListOffsetsTopic topic = new ListOffsetsTopic().setName("test").setPartitions(singletonList(new ListOffsetsPartition().setPartitionIndex(0).setTimestamp(1000000L).setCurrentLeaderEpoch(5)));
        return ListOffsetsRequest.Builder.forConsumer(true, IsolationLevel.READ_UNCOMMITTED, false).setTargetTimes(singletonList(topic)).build(version);
    } else if (version >= 2 && version <= LIST_OFFSETS.latestVersion()) {
        ListOffsetsPartition partition = new ListOffsetsPartition().setPartitionIndex(0).setTimestamp(1000000L).setCurrentLeaderEpoch(5);
        ListOffsetsTopic topic = new ListOffsetsTopic().setName("test").setPartitions(singletonList(partition));
        return ListOffsetsRequest.Builder.forConsumer(true, IsolationLevel.READ_COMMITTED, false).setTargetTimes(singletonList(topic)).build(version);
    } else {
        throw new IllegalArgumentException("Illegal ListOffsetRequest version " + version);
    }
}
Also used : ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) ListOffsetsTopic(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic)

Example 9 with ListOffsetsTopic

use of org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic in project kafka by apache.

the class ListOffsetsRequestTest method testToListOffsetsTopics.

@Test
public void testToListOffsetsTopics() {
    ListOffsetsPartition lop0 = new ListOffsetsPartition().setPartitionIndex(0).setCurrentLeaderEpoch(1).setMaxNumOffsets(2).setTimestamp(123L);
    ListOffsetsPartition lop1 = new ListOffsetsPartition().setPartitionIndex(1).setCurrentLeaderEpoch(3).setMaxNumOffsets(4).setTimestamp(567L);
    Map<TopicPartition, ListOffsetsPartition> timestampsToSearch = new HashMap<>();
    timestampsToSearch.put(new TopicPartition("topic", 0), lop0);
    timestampsToSearch.put(new TopicPartition("topic", 1), lop1);
    List<ListOffsetsTopic> listOffsetTopics = ListOffsetsRequest.toListOffsetsTopics(timestampsToSearch);
    assertEquals(1, listOffsetTopics.size());
    ListOffsetsTopic topic = listOffsetTopics.get(0);
    assertEquals("topic", topic.name());
    assertEquals(2, topic.partitions().size());
    assertTrue(topic.partitions().contains(lop0));
    assertTrue(topic.partitions().contains(lop1));
}
Also used : ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) ListOffsetsTopic(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 10 with ListOffsetsTopic

use of org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic in project kafka by apache.

the class FetcherTest method testGetOffsetByTimeWithPartitionsRetryCouldTriggerMetadataUpdate.

@Test
public void testGetOffsetByTimeWithPartitionsRetryCouldTriggerMetadataUpdate() {
    List<Errors> retriableErrors = Arrays.asList(Errors.NOT_LEADER_OR_FOLLOWER, Errors.REPLICA_NOT_AVAILABLE, Errors.KAFKA_STORAGE_ERROR, Errors.OFFSET_NOT_AVAILABLE, Errors.LEADER_NOT_AVAILABLE, Errors.FENCED_LEADER_EPOCH, Errors.UNKNOWN_LEADER_EPOCH);
    final int newLeaderEpoch = 3;
    MetadataResponse updatedMetadata = RequestTestUtils.metadataUpdateWithIds("dummy", 3, singletonMap(topicName, Errors.NONE), singletonMap(topicName, 4), tp -> newLeaderEpoch, topicIds);
    Node originalLeader = initialUpdateResponse.buildCluster().leaderFor(tp1);
    Node newLeader = updatedMetadata.buildCluster().leaderFor(tp1);
    assertNotEquals(originalLeader, newLeader);
    for (Errors retriableError : retriableErrors) {
        buildFetcher();
        subscriptions.assignFromUser(mkSet(tp0, tp1));
        client.updateMetadata(initialUpdateResponse);
        final long fetchTimestamp = 10L;
        ListOffsetsPartitionResponse tp0NoError = new ListOffsetsPartitionResponse().setPartitionIndex(tp0.partition()).setErrorCode(Errors.NONE.code()).setTimestamp(fetchTimestamp).setOffset(4L);
        List<ListOffsetsTopicResponse> topics = Collections.singletonList(new ListOffsetsTopicResponse().setName(tp0.topic()).setPartitions(Arrays.asList(tp0NoError, new ListOffsetsPartitionResponse().setPartitionIndex(tp1.partition()).setErrorCode(retriableError.code()).setTimestamp(ListOffsetsRequest.LATEST_TIMESTAMP).setOffset(-1L))));
        ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(topics);
        client.prepareResponseFrom(body -> {
            boolean isListOffsetRequest = body instanceof ListOffsetsRequest;
            if (isListOffsetRequest) {
                ListOffsetsRequest request = (ListOffsetsRequest) body;
                List<ListOffsetsTopic> expectedTopics = Collections.singletonList(new ListOffsetsTopic().setName(tp0.topic()).setPartitions(Arrays.asList(new ListOffsetsPartition().setPartitionIndex(tp1.partition()).setTimestamp(fetchTimestamp).setCurrentLeaderEpoch(ListOffsetsResponse.UNKNOWN_EPOCH), new ListOffsetsPartition().setPartitionIndex(tp0.partition()).setTimestamp(fetchTimestamp).setCurrentLeaderEpoch(ListOffsetsResponse.UNKNOWN_EPOCH))));
                return request.topics().equals(expectedTopics);
            } else {
                return false;
            }
        }, new ListOffsetsResponse(data), originalLeader);
        client.prepareMetadataUpdate(updatedMetadata);
        // If the metadata wasn't updated before retrying, the fetcher would consult the original leader and hit a NOT_LEADER exception.
        // We will count the answered future response in the end to verify if this is the case.
        List<ListOffsetsTopicResponse> topicsWithFatalError = Collections.singletonList(new ListOffsetsTopicResponse().setName(tp0.topic()).setPartitions(Arrays.asList(tp0NoError, new ListOffsetsPartitionResponse().setPartitionIndex(tp1.partition()).setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code()).setTimestamp(ListOffsetsRequest.LATEST_TIMESTAMP).setOffset(-1L))));
        ListOffsetsResponseData dataWithFatalError = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(topicsWithFatalError);
        client.prepareResponseFrom(new ListOffsetsResponse(dataWithFatalError), originalLeader);
        // The request to new leader must only contain one partition tp1 with error.
        client.prepareResponseFrom(body -> {
            boolean isListOffsetRequest = body instanceof ListOffsetsRequest;
            if (isListOffsetRequest) {
                ListOffsetsRequest request = (ListOffsetsRequest) body;
                ListOffsetsTopic requestTopic = request.topics().get(0);
                ListOffsetsPartition expectedPartition = new ListOffsetsPartition().setPartitionIndex(tp1.partition()).setTimestamp(fetchTimestamp).setCurrentLeaderEpoch(newLeaderEpoch);
                return expectedPartition.equals(requestTopic.partitions().get(0));
            } else {
                return false;
            }
        }, listOffsetResponse(tp1, Errors.NONE, fetchTimestamp, 5L), newLeader);
        Map<TopicPartition, OffsetAndTimestamp> offsetAndTimestampMap = fetcher.offsetsForTimes(Utils.mkMap(Utils.mkEntry(tp0, fetchTimestamp), Utils.mkEntry(tp1, fetchTimestamp)), time.timer(Integer.MAX_VALUE));
        assertEquals(Utils.mkMap(Utils.mkEntry(tp0, new OffsetAndTimestamp(4L, fetchTimestamp)), Utils.mkEntry(tp1, new OffsetAndTimestamp(5L, fetchTimestamp))), offsetAndTimestampMap);
        // The NOT_LEADER exception future should not be cleared as we already refreshed the metadata before
        // first retry, thus never hitting.
        assertEquals(1, client.numAwaitingResponses());
        fetcher.close();
    }
}
Also used : ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) Node(org.apache.kafka.common.Node) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) ListOffsetsRequest(org.apache.kafka.common.requests.ListOffsetsRequest) Errors(org.apache.kafka.common.protocol.Errors) ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) ListOffsetsTopic(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) Test(org.junit.jupiter.api.Test)

Aggregations

ListOffsetsPartition (org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition)11 ListOffsetsTopic (org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic)11 Test (org.junit.jupiter.api.Test)6 TopicPartition (org.apache.kafka.common.TopicPartition)5 ListOffsetsPartitionResponse (org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse)5 ListOffsetsTopicResponse (org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse)5 ListOffsetsResponseData (org.apache.kafka.common.message.ListOffsetsResponseData)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 ListOffsetsRequest (org.apache.kafka.common.requests.ListOffsetsRequest)2 InetSocketAddress (java.net.InetSocketAddress)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1