Search in sources :

Example 1 with ListOffsetsResponseData

use of org.apache.kafka.common.message.ListOffsetsResponseData 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)

Example 2 with ListOffsetsResponseData

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

the class ListOffsetsRequestTest method testGetErrorResponseV0.

@Test
public void testGetErrorResponseV0() {
    List<ListOffsetsTopic> topics = Arrays.asList(new ListOffsetsTopic().setName("topic").setPartitions(Collections.singletonList(new ListOffsetsPartition().setPartitionIndex(0))));
    ListOffsetsRequest request = ListOffsetsRequest.Builder.forConsumer(true, IsolationLevel.READ_UNCOMMITTED, false).setTargetTimes(topics).build((short) 0);
    ListOffsetsResponse response = (ListOffsetsResponse) request.getErrorResponse(0, Errors.NOT_LEADER_OR_FOLLOWER.exception());
    List<ListOffsetsTopicResponse> v = Collections.singletonList(new ListOffsetsTopicResponse().setName("topic").setPartitions(Collections.singletonList(new ListOffsetsPartitionResponse().setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code()).setOldStyleOffsets(Collections.emptyList()).setPartitionIndex(0))));
    ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(v);
    ListOffsetsResponse expectedResponse = new ListOffsetsResponse(data);
    assertEquals(expectedResponse.data().topics(), response.data().topics());
    assertEquals(expectedResponse.throttleTimeMs(), response.throttleTimeMs());
}
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) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) Test(org.junit.jupiter.api.Test)

Example 3 with ListOffsetsResponseData

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

the class ListOffsetsRequestTest method testGetErrorResponse.

@Test
public void testGetErrorResponse() {
    for (short version = 1; version <= ApiKeys.LIST_OFFSETS.latestVersion(); version++) {
        List<ListOffsetsTopic> topics = Arrays.asList(new ListOffsetsTopic().setName("topic").setPartitions(Collections.singletonList(new ListOffsetsPartition().setPartitionIndex(0))));
        ListOffsetsRequest request = ListOffsetsRequest.Builder.forConsumer(true, IsolationLevel.READ_COMMITTED, false).setTargetTimes(topics).build(version);
        ListOffsetsResponse response = (ListOffsetsResponse) request.getErrorResponse(0, Errors.NOT_LEADER_OR_FOLLOWER.exception());
        List<ListOffsetsTopicResponse> v = Collections.singletonList(new ListOffsetsTopicResponse().setName("topic").setPartitions(Collections.singletonList(new ListOffsetsPartitionResponse().setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code()).setLeaderEpoch(ListOffsetsResponse.UNKNOWN_EPOCH).setOffset(ListOffsetsResponse.UNKNOWN_OFFSET).setPartitionIndex(0).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP))));
        ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(v);
        ListOffsetsResponse expectedResponse = new ListOffsetsResponse(data);
        assertEquals(expectedResponse.data().topics(), response.data().topics());
        assertEquals(expectedResponse.throttleTimeMs(), response.throttleTimeMs());
    }
}
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) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) Test(org.junit.jupiter.api.Test)

Example 4 with ListOffsetsResponseData

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

the class KafkaConsumerTest method listOffsetsResponse.

private ListOffsetsResponse listOffsetsResponse(Map<TopicPartition, Long> partitionOffsets, Map<TopicPartition, Errors> partitionErrors) {
    Map<String, ListOffsetsTopicResponse> responses = new HashMap<>();
    for (Map.Entry<TopicPartition, Long> partitionOffset : partitionOffsets.entrySet()) {
        TopicPartition tp = partitionOffset.getKey();
        ListOffsetsTopicResponse topic = responses.computeIfAbsent(tp.topic(), k -> new ListOffsetsTopicResponse().setName(tp.topic()));
        topic.partitions().add(new ListOffsetsPartitionResponse().setPartitionIndex(tp.partition()).setErrorCode(Errors.NONE.code()).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP).setOffset(partitionOffset.getValue()));
    }
    for (Map.Entry<TopicPartition, Errors> partitionError : partitionErrors.entrySet()) {
        TopicPartition tp = partitionError.getKey();
        ListOffsetsTopicResponse topic = responses.computeIfAbsent(tp.topic(), k -> new ListOffsetsTopicResponse().setName(tp.topic()));
        topic.partitions().add(new ListOffsetsPartitionResponse().setPartitionIndex(tp.partition()).setErrorCode(partitionError.getValue().code()).setTimestamp(ListOffsetsResponse.UNKNOWN_TIMESTAMP).setOffset(ListOffsetsResponse.UNKNOWN_OFFSET));
    }
    ListOffsetsResponseData data = new ListOffsetsResponseData().setTopics(new ArrayList<>(responses.values()));
    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) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) OptionalLong(java.util.OptionalLong) 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) AbstractMap(java.util.AbstractMap)

Example 5 with ListOffsetsResponseData

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

the class SaslAuthenticatorTest method testConvertListOffsetResponseToSaslHandshakeResponse.

@Test
public void testConvertListOffsetResponseToSaslHandshakeResponse() {
    ListOffsetsResponseData data = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Collections.singletonList(new ListOffsetsTopicResponse().setName("topic").setPartitions(Collections.singletonList(new ListOffsetsPartitionResponse().setErrorCode(Errors.NONE.code()).setLeaderEpoch(ListOffsetsResponse.UNKNOWN_EPOCH).setPartitionIndex(0).setOffset(0).setTimestamp(0)))));
    ListOffsetsResponse response = new ListOffsetsResponse(data);
    ByteBuffer buffer = RequestTestUtils.serializeResponseWithHeader(response, LIST_OFFSETS.latestVersion(), 0);
    final RequestHeader header0 = new RequestHeader(LIST_OFFSETS, LIST_OFFSETS.latestVersion(), "id", SaslClientAuthenticator.MIN_RESERVED_CORRELATION_ID);
    assertThrows(SchemaException.class, () -> NetworkClient.parseResponse(buffer.duplicate(), header0));
    final RequestHeader header1 = new RequestHeader(LIST_OFFSETS, LIST_OFFSETS.latestVersion(), "id", 1);
    assertThrows(IllegalStateException.class, () -> NetworkClient.parseResponse(buffer.duplicate(), header1));
}
Also used : ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) RequestHeader(org.apache.kafka.common.requests.RequestHeader) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ListOffsetsResponseData (org.apache.kafka.common.message.ListOffsetsResponseData)21 ListOffsetsTopicResponse (org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse)20 ListOffsetsResponse (org.apache.kafka.common.requests.ListOffsetsResponse)18 TopicPartition (org.apache.kafka.common.TopicPartition)17 Test (org.junit.jupiter.api.Test)16 HashMap (java.util.HashMap)13 Node (org.apache.kafka.common.Node)12 ArrayList (java.util.ArrayList)10 Cluster (org.apache.kafka.common.Cluster)10 PartitionInfo (org.apache.kafka.common.PartitionInfo)10 ListOffsetsPartitionResponse (org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 ListOffsetsResultInfo (org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo)8 LinkedHashMap (java.util.LinkedHashMap)5 ListOffsetsPartition (org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition)4 ListOffsetsTopic (org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic)4 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)4 Map (java.util.Map)3 OffsetAndTimestamp (org.apache.kafka.clients.consumer.OffsetAndTimestamp)3 Errors (org.apache.kafka.common.protocol.Errors)3