Search in sources :

Example 1 with ListOffsetsPartitionResponse

use of org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse in project kafka by apache.

the class FetcherTest method testGetOffsetsForTimesWithUnknownOffsetV0.

@Test
public void testGetOffsetsForTimesWithUnknownOffsetV0() {
    buildFetcher();
    // Empty map
    assertTrue(fetcher.offsetsForTimes(new HashMap<>(), time.timer(100L)).isEmpty());
    // Unknown Offset
    client.reset();
    // Ensure metadata has both partition.
    MetadataResponse initialMetadataUpdate = RequestTestUtils.metadataUpdateWithIds(1, singletonMap(topicName, 1), topicIds);
    client.updateMetadata(initialMetadataUpdate);
    // Force LIST_OFFSETS version 0
    Node node = metadata.fetch().nodes().get(0);
    apiVersions.update(node.idString(), NodeApiVersions.create(ApiKeys.LIST_OFFSETS.id, (short) 0, (short) 0));
    ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Collections.singletonList(new ListOffsetsTopicResponse().setName(tp0.topic()).setPartitions(Collections.singletonList(new ListOffsetsPartitionResponse().setPartitionIndex(tp0.partition()).setErrorCode(Errors.NONE.code()).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP).setOldStyleOffsets(Collections.emptyList())))));
    client.prepareResponseFrom(new ListOffsetsResponse(data), metadata.fetch().leaderFor(tp0));
    Map<TopicPartition, Long> timestampToSearch = new HashMap<>();
    timestampToSearch.put(tp0, 0L);
    Map<TopicPartition, OffsetAndTimestamp> offsetAndTimestampMap = fetcher.offsetsForTimes(timestampToSearch, time.timer(Long.MAX_VALUE));
    assertTrue(offsetAndTimestampMap.containsKey(tp0));
    assertNull(offsetAndTimestampMap.get(tp0));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) 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) 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)

Example 2 with ListOffsetsPartitionResponse

use of org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse in project kafka by apache.

the class FetcherTest method testBatchedListOffsetsMetadataErrors.

@Test
public void testBatchedListOffsetsMetadataErrors() {
    buildFetcher();
    ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Collections.singletonList(new ListOffsetsTopicResponse().setName(tp0.topic()).setPartitions(Arrays.asList(new ListOffsetsPartitionResponse().setPartitionIndex(tp0.partition()).setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code()).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP).setOffset(ListOffsetsResponse.UNKNOWN_OFFSET), new ListOffsetsPartitionResponse().setPartitionIndex(tp1.partition()).setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code()).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP).setOffset(ListOffsetsResponse.UNKNOWN_OFFSET)))));
    client.prepareResponse(new ListOffsetsResponse(data));
    Map<TopicPartition, Long> offsetsToSearch = new HashMap<>();
    offsetsToSearch.put(tp0, ListOffsetsRequest.EARLIEST_TIMESTAMP);
    offsetsToSearch.put(tp1, ListOffsetsRequest.EARLIEST_TIMESTAMP);
    assertThrows(TimeoutException.class, () -> fetcher.offsetsForTimes(offsetsToSearch, time.timer(1)));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) TopicPartition(org.apache.kafka.common.TopicPartition) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) Test(org.junit.jupiter.api.Test)

Example 3 with ListOffsetsPartitionResponse

use of org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse in project kafka by apache.

the class FetcherTest method listOffsetResponse.

private ListOffsetsResponse listOffsetResponse(Map<TopicPartition, Long> offsets, Errors error, long timestamp, int leaderEpoch) {
    Map<String, List<ListOffsetsPartitionResponse>> responses = new HashMap<>();
    for (Map.Entry<TopicPartition, Long> entry : offsets.entrySet()) {
        TopicPartition tp = entry.getKey();
        responses.putIfAbsent(tp.topic(), new ArrayList<>());
        responses.get(tp.topic()).add(new ListOffsetsPartitionResponse().setPartitionIndex(tp.partition()).setErrorCode(error.code()).setOffset(entry.getValue()).setTimestamp(timestamp).setLeaderEpoch(leaderEpoch));
    }
    List<ListOffsetsTopicResponse> topics = new ArrayList<>();
    for (Map.Entry<String, List<ListOffsetsPartitionResponse>> response : responses.entrySet()) {
        topics.add(new ListOffsetsTopicResponse().setName(response.getKey()).setPartitions(response.getValue()));
    }
    ListOffsetsResponseData data = new ListOffsetsResponseData().setTopics(topics);
    return new ListOffsetsResponse(data);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) ArrayList(java.util.ArrayList) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 4 with ListOffsetsPartitionResponse

use of org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse in project kafka by apache.

the class FetcherTest method testGetOffsetsForTimesWithUnknownOffset.

private void testGetOffsetsForTimesWithUnknownOffset() {
    client.reset();
    // Ensure metadata has both partitions.
    MetadataResponse initialMetadataUpdate = RequestTestUtils.metadataUpdateWithIds(1, singletonMap(topicName, 1), topicIds);
    client.updateMetadata(initialMetadataUpdate);
    ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Collections.singletonList(new ListOffsetsTopicResponse().setName(tp0.topic()).setPartitions(Collections.singletonList(new ListOffsetsPartitionResponse().setPartitionIndex(tp0.partition()).setErrorCode(Errors.NONE.code()).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP).setOffset(ListOffsetsResponse.UNKNOWN_OFFSET)))));
    client.prepareResponseFrom(new ListOffsetsResponse(data), metadata.fetch().leaderFor(tp0));
    Map<TopicPartition, Long> timestampToSearch = new HashMap<>();
    timestampToSearch.put(tp0, 0L);
    Map<TopicPartition, OffsetAndTimestamp> offsetAndTimestampMap = fetcher.offsetsForTimes(timestampToSearch, time.timer(Long.MAX_VALUE));
    assertTrue(offsetAndTimestampMap.containsKey(tp0));
    assertNull(offsetAndTimestampMap.get(tp0));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) TopicPartition(org.apache.kafka.common.TopicPartition) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp)

Example 5 with ListOffsetsPartitionResponse

use of org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse in project kafka by apache.

the class ListOffsetsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    short versionId = version();
    short errorCode = Errors.forException(e).code();
    List<ListOffsetsTopicResponse> responses = new ArrayList<>();
    for (ListOffsetsTopic topic : data.topics()) {
        ListOffsetsTopicResponse topicResponse = new ListOffsetsTopicResponse().setName(topic.name());
        List<ListOffsetsPartitionResponse> partitions = new ArrayList<>();
        for (ListOffsetsPartition partition : topic.partitions()) {
            ListOffsetsPartitionResponse partitionResponse = new ListOffsetsPartitionResponse().setErrorCode(errorCode).setPartitionIndex(partition.partitionIndex());
            if (versionId == 0) {
                partitionResponse.setOldStyleOffsets(Collections.emptyList());
            } else {
                partitionResponse.setOffset(ListOffsetsResponse.UNKNOWN_OFFSET).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP);
            }
            partitions.add(partitionResponse);
        }
        topicResponse.setPartitions(partitions);
        responses.add(topicResponse);
    }
    ListOffsetsResponseData responseData = new ListOffsetsResponseData().setThrottleTimeMs(throttleTimeMs).setTopics(responses);
    return new ListOffsetsResponse(responseData);
}
Also used : ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) ListOffsetsTopic(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) ArrayList(java.util.ArrayList) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData)

Aggregations

ListOffsetsPartitionResponse (org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse)13 ListOffsetsTopicResponse (org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse)13 ListOffsetsResponseData (org.apache.kafka.common.message.ListOffsetsResponseData)10 TopicPartition (org.apache.kafka.common.TopicPartition)8 ListOffsetsResponse (org.apache.kafka.common.requests.ListOffsetsResponse)8 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 LinkedHashMap (java.util.LinkedHashMap)6 ListOffsetsPartition (org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition)5 ListOffsetsTopic (org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic)5 Errors (org.apache.kafka.common.protocol.Errors)4 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 OffsetAndTimestamp (org.apache.kafka.clients.consumer.OffsetAndTimestamp)3 Node (org.apache.kafka.common.Node)3 Collections.singletonMap (java.util.Collections.singletonMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 InetSocketAddress (java.net.InetSocketAddress)1