Search in sources :

Example 1 with ConsumerProtocolAssignment

use of org.apache.kafka.common.message.ConsumerProtocolAssignment 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)

Example 2 with ConsumerProtocolAssignment

use of org.apache.kafka.common.message.ConsumerProtocolAssignment in project kafka by apache.

the class ConsumerProtocol method serializeAssignment.

public static ByteBuffer serializeAssignment(final Assignment assignment, short version) {
    version = checkAssignmentVersion(version);
    ConsumerProtocolAssignment data = new ConsumerProtocolAssignment();
    data.setUserData(assignment.userData() != null ? assignment.userData().duplicate() : null);
    assignment.partitions().forEach(tp -> {
        ConsumerProtocolAssignment.TopicPartition partition = data.assignedPartitions().find(tp.topic());
        if (partition == null) {
            partition = new ConsumerProtocolAssignment.TopicPartition().setTopic(tp.topic());
            data.assignedPartitions().add(partition);
        }
        partition.partitions().add(tp.partition());
    });
    return MessageUtil.toVersionPrefixedByteBuffer(version, data);
}
Also used : ConsumerProtocolAssignment(org.apache.kafka.common.message.ConsumerProtocolAssignment) TopicPartition(org.apache.kafka.common.TopicPartition)

Aggregations

TopicPartition (org.apache.kafka.common.TopicPartition)2 ConsumerProtocolAssignment (org.apache.kafka.common.message.ConsumerProtocolAssignment)2 BufferUnderflowException (java.nio.BufferUnderflowException)1 ArrayList (java.util.ArrayList)1 Assignment (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1 SchemaException (org.apache.kafka.common.protocol.types.SchemaException)1