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