Search in sources :

Example 1 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache 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 2 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class RequestUtils method serialize.

public static ByteBuffer serialize(Message header, short headerVersion, Message apiMessage, short apiVersion) {
    ObjectSerializationCache cache = new ObjectSerializationCache();
    int headerSize = header.size(cache, headerVersion);
    int messageSize = apiMessage.size(cache, apiVersion);
    ByteBufferAccessor writable = new ByteBufferAccessor(ByteBuffer.allocate(headerSize + messageSize));
    header.write(writable, cache, headerVersion);
    apiMessage.write(writable, cache, apiVersion);
    writable.flip();
    return writable.buffer();
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor)

Example 3 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class ControlRecordUtilsTest method testDeserializeRecord.

private void testDeserializeRecord(ControlRecordType controlRecordType) {
    final int leaderId = 1;
    final int voterId = 2;
    LeaderChangeMessage data = new LeaderChangeMessage().setLeaderId(leaderId).setVoters(Collections.singletonList(new Voter().setVoterId(voterId)));
    ByteBuffer valueBuffer = ByteBuffer.allocate(256);
    data.write(new ByteBufferAccessor(valueBuffer), new ObjectSerializationCache(), data.highestSupportedVersion());
    valueBuffer.flip();
    byte[] keyData = new byte[] { 0, 0, 0, (byte) controlRecordType.type };
    DefaultRecord record = new DefaultRecord(256, (byte) 0, 0, 0L, 0, ByteBuffer.wrap(keyData), valueBuffer, null);
    LeaderChangeMessage deserializedData = ControlRecordUtils.deserializeLeaderChangeMessage(record);
    assertEquals(leaderId, deserializedData.leaderId());
    assertEquals(Collections.singletonList(new Voter().setVoterId(voterId)), deserializedData.voters());
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) Voter(org.apache.kafka.common.message.LeaderChangeMessage.Voter) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache 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 5 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class RequestHeaderTest method parseHeaderFromBufferWithNonZeroPosition.

@Test
public void parseHeaderFromBufferWithNonZeroPosition() {
    ByteBuffer buffer = ByteBuffer.allocate(64);
    buffer.position(10);
    RequestHeader header = new RequestHeader(ApiKeys.FIND_COORDINATOR, (short) 1, "", 10);
    ObjectSerializationCache serializationCache = new ObjectSerializationCache();
    // size must be called before write to avoid an NPE with the current implementation
    header.size(serializationCache);
    header.write(buffer, serializationCache);
    int limit = buffer.position();
    buffer.position(10);
    buffer.limit(limit);
    RequestHeader parsed = RequestHeader.parse(buffer);
    assertEquals(header, parsed);
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)21 ByteBuffer (java.nio.ByteBuffer)13 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)13 Test (org.junit.jupiter.api.Test)7 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)3 FetchResponseData (org.apache.kafka.common.message.FetchResponseData)2 UpdateMetadataEndpoint (org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 OptionalLong (java.util.OptionalLong)1 ExecutionException (java.util.concurrent.ExecutionException)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 FetchRequestData (org.apache.kafka.common.message.FetchRequestData)1 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)1 Voter (org.apache.kafka.common.message.LeaderChangeMessage.Voter)1 RequestHeaderData (org.apache.kafka.common.message.RequestHeaderData)1 RegisterBrokerRecord (org.apache.kafka.common.metadata.RegisterBrokerRecord)1 TopicRecord (org.apache.kafka.common.metadata.TopicRecord)1 Send (org.apache.kafka.common.network.Send)1