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