Search in sources :

Example 16 with ByteBufferAccessor

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());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with ByteBufferAccessor

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 -> {
    }));
}
Also used : MemoryBatchReader(org.apache.kafka.raft.internals.MemoryBatchReader) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ControlRecordType(org.apache.kafka.common.record.ControlRecordType) OptionalInt(java.util.OptionalInt) Record(org.apache.kafka.common.record.Record) ArrayList(java.util.ArrayList) KafkaEventQueue(org.apache.kafka.queue.KafkaEventQueue) LogContext(org.apache.kafka.common.utils.LogContext) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) FileRecords(org.apache.kafka.common.record.FileRecords) LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) MetadataRecordSerde(org.apache.kafka.metadata.MetadataRecordSerde) Logger(org.slf4j.Logger) Time(org.apache.kafka.common.utils.Time) Iterator(java.util.Iterator) FileChannelRecordBatch(org.apache.kafka.common.record.FileLogInputStream.FileChannelRecordBatch) File(java.io.File) Batch(org.apache.kafka.raft.Batch) List(java.util.List) EventQueue(org.apache.kafka.queue.EventQueue) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) RaftClient(org.apache.kafka.raft.RaftClient) Collections(java.util.Collections) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) Record(org.apache.kafka.common.record.Record) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor)

Example 18 with ByteBufferAccessor

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());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 19 with ByteBufferAccessor

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());
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 20 with ByteBufferAccessor

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());
}
Also used : MetadataParseException(org.apache.kafka.server.common.serialization.MetadataParseException) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)39 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.jupiter.api.Test)23 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)13 TopicPartition (org.apache.kafka.common.TopicPartition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Uuid (org.apache.kafka.common.Uuid)4 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)4 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)3 UpdateMetadataEndpoint (org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint)3 Send (org.apache.kafka.common.network.Send)3 BufferUnderflowException (java.nio.BufferUnderflowException)2 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2