Search in sources :

Example 1 with FetchResponseData

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));
}
Also used : Uuid(org.apache.kafka.common.Uuid) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) ForgottenTopic(org.apache.kafka.common.message.FetchRequestData.ForgottenTopic) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) TopicIdPartition(org.apache.kafka.common.TopicIdPartition) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) FetchResponseData(org.apache.kafka.common.message.FetchResponseData) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) IsolationLevel(org.apache.kafka.common.IsolationLevel) List(java.util.List) RecordBatch(org.apache.kafka.common.record.RecordBatch) Map(java.util.Map) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) Collections(java.util.Collections) FetchRequestData(org.apache.kafka.common.message.FetchRequestData) Errors(org.apache.kafka.common.protocol.Errors) FetchResponseData(org.apache.kafka.common.message.FetchResponseData) ArrayList(java.util.ArrayList)

Example 2 with FetchResponseData

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);
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) FetchResponseData(org.apache.kafka.common.message.FetchResponseData)

Example 3 with FetchResponseData

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));
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) FetchResponseData(org.apache.kafka.common.message.FetchResponseData) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) UpdateMetadataEndpoint(org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint) Send(org.apache.kafka.common.network.Send)

Example 4 with FetchResponseData

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);
}
Also used : FetchResponseData(org.apache.kafka.common.message.FetchResponseData) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Example 5 with FetchResponseData

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()));
}
Also used : FetchResponseData(org.apache.kafka.common.message.FetchResponseData)

Aggregations

FetchResponseData (org.apache.kafka.common.message.FetchResponseData)14 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)6 Test (org.junit.jupiter.api.Test)6 Records (org.apache.kafka.common.record.Records)5 Errors (org.apache.kafka.common.protocol.Errors)4 ByteBuffer (java.nio.ByteBuffer)3 OptionalLong (java.util.OptionalLong)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 OptionalInt (java.util.OptionalInt)2 FetchRequestData (org.apache.kafka.common.message.FetchRequestData)2 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)2 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)2 RecordBatch (org.apache.kafka.common.record.RecordBatch)2 Utils (org.apache.kafka.common.utils.Utils)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections.singletonList (java.util.Collections.singletonList)1