use of org.apache.kafka.common.message.FetchResponseData in project kafka by apache.
the class FetchRequest method getErrorResponse.
@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
// For versions 13+ the error is indicated by setting the top-level error code, and no partitions will be returned.
// For earlier versions, the error is indicated in two ways: by setting the same error code in all partitions,
// and by setting the top-level error code. The form where we set the same error code in all partitions
// is needed in order to maintain backwards compatibility with older versions of the protocol
// in which there was no top-level error code. Note that for incremental fetch responses, there
// may not be any partitions at all in the response. For this reason, the top-level error code
// is essential for them.
Errors error = Errors.forException(e);
List<FetchResponseData.FetchableTopicResponse> topicResponseList = new ArrayList<>();
// For version 13+, we know the client can handle a top level error code, so we don't need to send back partitions too.
if (version() < 13) {
data.topics().forEach(topic -> {
List<FetchResponseData.PartitionData> partitionResponses = topic.partitions().stream().map(partition -> FetchResponse.partitionResponse(partition.partition(), error)).collect(Collectors.toList());
topicResponseList.add(new FetchResponseData.FetchableTopicResponse().setTopic(topic.topic()).setTopicId(topic.topicId()).setPartitions(partitionResponses));
});
}
return new FetchResponse(new FetchResponseData().setThrottleTimeMs(throttleTimeMs).setErrorCode(error.code()).setSessionId(data.sessionId()).setResponses(topicResponseList));
}
use of org.apache.kafka.common.message.FetchResponseData in project kafka by apache.
the class FetchResponse method sizeOf.
/**
* Convenience method to find the size of a response.
*
* @param version The version of the response to use.
* @param partIterator The partition iterator.
* @return The response size in bytes.
*/
public static int sizeOf(short version, Iterator<Map.Entry<TopicIdPartition, FetchResponseData.PartitionData>> partIterator) {
// Since the throttleTimeMs and metadata field sizes are constant and fixed, we can
// use arbitrary values here without affecting the result.
FetchResponseData data = toMessage(Errors.NONE, 0, INVALID_SESSION_ID, partIterator);
ObjectSerializationCache cache = new ObjectSerializationCache();
return 4 + data.size(cache, version);
}
use of org.apache.kafka.common.message.FetchResponseData in project kafka by apache.
the class RequestResponseTest method verifyFetchResponseFullWrite.
private void verifyFetchResponseFullWrite(short version, FetchResponse fetchResponse) throws Exception {
int correlationId = 15;
short responseHeaderVersion = FETCH.responseHeaderVersion(version);
Send send = fetchResponse.toSend(new ResponseHeader(correlationId, responseHeaderVersion), version);
ByteBufferChannel channel = new ByteBufferChannel(send.size());
send.writeTo(channel);
channel.close();
ByteBuffer buf = channel.buffer();
// read the size
int size = buf.getInt();
assertTrue(size > 0);
// read the header
ResponseHeader responseHeader = ResponseHeader.parse(channel.buffer(), responseHeaderVersion);
assertEquals(correlationId, responseHeader.correlationId());
assertEquals(fetchResponse.serialize(version), buf);
FetchResponseData deserialized = new FetchResponseData(new ByteBufferAccessor(buf), version);
ObjectSerializationCache serializationCache = new ObjectSerializationCache();
assertEquals(size, responseHeader.size(serializationCache) + deserialized.size(serializationCache, version));
}
use of org.apache.kafka.common.message.FetchResponseData in project kafka by apache.
the class RequestResponseTest method createFetchResponse.
private FetchResponse createFetchResponse(short version) {
FetchResponseData data = new FetchResponseData();
if (version > 0) {
data.setThrottleTimeMs(345);
}
if (version > 6) {
data.setErrorCode(Errors.NONE.code()).setSessionId(123);
}
MemoryRecords records = MemoryRecords.withRecords(CompressionType.NONE, new SimpleRecord("blah".getBytes()));
FetchResponseData.PartitionData partition = new FetchResponseData.PartitionData().setPartitionIndex(0).setErrorCode(Errors.NONE.code()).setHighWatermark(123L).setRecords(records);
if (version > 3) {
partition.setLastStableOffset(234L);
}
if (version > 4) {
partition.setLogStartOffset(456L);
}
if (version > 10) {
partition.setPreferredReadReplica(1);
}
if (version > 11) {
partition.setDivergingEpoch(new FetchResponseData.EpochEndOffset().setEndOffset(1L).setEpoch(2)).setSnapshotId(new FetchResponseData.SnapshotId().setEndOffset(1L).setEndOffset(2)).setCurrentLeader(new FetchResponseData.LeaderIdAndEpoch().setLeaderEpoch(1).setLeaderId(2));
}
FetchResponseData.FetchableTopicResponse response = new FetchResponseData.FetchableTopicResponse().setTopic("topic").setPartitions(singletonList(partition));
if (version > 12) {
response.setTopicId(Uuid.randomUuid());
}
data.setResponses(singletonList(response));
return new FetchResponse(data);
}
use of org.apache.kafka.common.message.FetchResponseData in project kafka by apache.
the class RaftClientTestContext method assertSentFetchPartitionResponse.
void assertSentFetchPartitionResponse(Errors topLevelError) {
List<RaftResponse.Outbound> sentMessages = drainSentResponses(ApiKeys.FETCH);
assertEquals(1, sentMessages.size(), "Found unexpected sent messages " + sentMessages);
RaftResponse.Outbound raftMessage = sentMessages.get(0);
assertEquals(ApiKeys.FETCH.id, raftMessage.data.apiKey());
FetchResponseData response = (FetchResponseData) raftMessage.data();
assertEquals(topLevelError, Errors.forCode(response.errorCode()));
}
Aggregations