use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MessageTest method testLongTaggedString.
@Test
public void testLongTaggedString() throws Exception {
char[] chars = new char[1024];
Arrays.fill(chars, 'a');
String longString = new String(chars);
SimpleExampleMessageData message = new SimpleExampleMessageData().setMyString(longString);
ObjectSerializationCache cache = new ObjectSerializationCache();
short version = 1;
int size = message.size(cache, version);
ByteBuffer buf = ByteBuffer.allocate(size);
ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
message.write(byteBufferAccessor, cache, version);
assertEquals(size, buf.position());
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class SnapshotFileReader method handleMetadataBatch.
private void handleMetadataBatch(FileChannelRecordBatch batch) {
List<ApiMessageAndVersion> messages = new ArrayList<>();
for (Record record : batch) {
ByteBufferAccessor accessor = new ByteBufferAccessor(record.value());
try {
ApiMessageAndVersion messageAndVersion = serde.read(accessor, record.valueSize());
messages.add(messageAndVersion);
} catch (Throwable e) {
log.error("unable to read metadata record at offset {}", record.offset(), e);
}
}
listener.handleCommit(MemoryBatchReader.of(Collections.singletonList(Batch.data(batch.baseOffset(), batch.partitionLeaderEpoch(), batch.maxTimestamp(), batch.sizeInBytes(), messages)), reader -> {
}));
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testParsingRecordWithGarbageAtEnd.
/**
* Test attempting to parse an event which has a malformed message version varint.
*/
@Test
public void testParsingRecordWithGarbageAtEnd() {
MetadataRecordSerde serde = new MetadataRecordSerde();
RegisterBrokerRecord message = new RegisterBrokerRecord().setBrokerId(1).setBrokerEpoch(2);
ObjectSerializationCache cache = new ObjectSerializationCache();
ApiMessageAndVersion messageAndVersion = new ApiMessageAndVersion(message, (short) 0);
int size = serde.recordSize(messageAndVersion, cache);
ByteBuffer buffer = ByteBuffer.allocate(size + 1);
serde.write(messageAndVersion, cache, new ByteBufferAccessor(buffer));
buffer.clear();
assertStartsWith("Found 1 byte(s) of garbage after", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), size + 1)).getMessage());
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testDeserializeWithUnhandledFrameVersion.
@Test
public void testDeserializeWithUnhandledFrameVersion() {
ByteBuffer buffer = ByteBuffer.allocate(16);
ByteUtils.writeUnsignedVarint(15, buffer);
buffer.flip();
MetadataRecordSerde serde = new MetadataRecordSerde();
assertStartsWith("Could not deserialize metadata record due to unknown frame version", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), 16)).getMessage());
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testParsingUnsupportedApiKey.
/**
* Test attempting to parse an event which has a unsupported version
*/
@Test
public void testParsingUnsupportedApiKey() {
MetadataRecordSerde serde = new MetadataRecordSerde();
ByteBuffer buffer = ByteBuffer.allocate(64);
// frame version
buffer.put((byte) 0x01);
// apiKey
buffer.put((byte) 0xff);
// apiKey
buffer.put((byte) 0x7f);
// api version
buffer.put((byte) 0x00);
buffer.put((byte) 0x80);
buffer.position(0);
buffer.limit(64);
assertStartsWith("Unknown metadata id ", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), buffer.remaining())).getCause().getMessage());
}
Aggregations