Search in sources :

Example 6 with ByteBufferAccessor

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

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

the class SimpleExampleMessageTest method shouldRoundTripFieldThroughBufferWithNullable.

@Test
public void shouldRoundTripFieldThroughBufferWithNullable() {
    final Uuid uuid = Uuid.randomUuid();
    final ByteBuffer buf1 = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
    final ByteBuffer buf2 = ByteBuffer.wrap(new byte[] { 4, 5, 6 });
    final SimpleExampleMessageData out = new SimpleExampleMessageData();
    out.setProcessId(uuid);
    out.setZeroCopyByteBuffer(buf1);
    out.setNullableZeroCopyByteBuffer(buf2);
    final ByteBuffer buffer = MessageUtil.toByteBuffer(out, (short) 1);
    final SimpleExampleMessageData in = new SimpleExampleMessageData();
    in.read(new ByteBufferAccessor(buffer), (short) 1);
    buf1.rewind();
    buf2.rewind();
    assertEquals(uuid, in.processId());
    assertEquals(buf1, in.zeroCopyByteBuffer());
    assertEquals(buf2, in.nullableZeroCopyByteBuffer());
}
Also used : Uuid(org.apache.kafka.common.Uuid) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with ByteBufferAccessor

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

the class SimpleExampleMessageTest method deserialize.

private SimpleExampleMessageData deserialize(ByteBuffer buf, short version) {
    SimpleExampleMessageData message = new SimpleExampleMessageData();
    message.read(new ByteBufferAccessor(buf.duplicate()), version);
    return message;
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor)

Example 9 with ByteBufferAccessor

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

the class SimpleExampleMessageTest method shouldRoundTripFieldThroughBuffer.

@Test
public void shouldRoundTripFieldThroughBuffer() {
    final Uuid uuid = Uuid.randomUuid();
    final ByteBuffer buf = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
    final SimpleExampleMessageData out = new SimpleExampleMessageData();
    out.setProcessId(uuid);
    out.setZeroCopyByteBuffer(buf);
    final ByteBuffer buffer = MessageUtil.toByteBuffer(out, (short) 1);
    final SimpleExampleMessageData in = new SimpleExampleMessageData();
    in.read(new ByteBufferAccessor(buffer), (short) 1);
    buf.rewind();
    assertEquals(uuid, in.processId());
    assertEquals(buf, in.zeroCopyByteBuffer());
    assertEquals(ByteUtils.EMPTY_BUF, in.nullableZeroCopyByteBuffer());
}
Also used : Uuid(org.apache.kafka.common.Uuid) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with ByteBufferAccessor

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

the class ConsumerProtocol method deserializeAssignment.

public static Assignment deserializeAssignment(final ByteBuffer buffer, short version) {
    version = checkAssignmentVersion(version);
    try {
        ConsumerProtocolAssignment data = new ConsumerProtocolAssignment(new ByteBufferAccessor(buffer), version);
        List<TopicPartition> assignedPartitions = new ArrayList<>();
        for (ConsumerProtocolAssignment.TopicPartition tp : data.assignedPartitions()) {
            for (Integer partition : tp.partitions()) {
                assignedPartitions.add(new TopicPartition(tp.topic(), partition));
            }
        }
        return new Assignment(assignedPartitions, data.userData() != null ? data.userData().duplicate() : null);
    } catch (BufferUnderflowException e) {
        throw new SchemaException("Buffer underflow while parsing consumer protocol's assignment", e);
    }
}
Also used : Assignment(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment) ConsumerProtocolAssignment(org.apache.kafka.common.message.ConsumerProtocolAssignment) SchemaException(org.apache.kafka.common.protocol.types.SchemaException) ConsumerProtocolAssignment(org.apache.kafka.common.message.ConsumerProtocolAssignment) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) BufferUnderflowException(java.nio.BufferUnderflowException)

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