Search in sources :

Example 1 with ConsumerProtocolSubscription

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

the class ConsumerProtocol method deserializeSubscription.

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

Example 2 with ConsumerProtocolSubscription

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

the class ConsumerProtocol method serializeSubscription.

public static ByteBuffer serializeSubscription(final Subscription subscription, short version) {
    version = checkSubscriptionVersion(version);
    ConsumerProtocolSubscription data = new ConsumerProtocolSubscription();
    List<String> topics = new ArrayList<>(subscription.topics());
    Collections.sort(topics);
    data.setTopics(topics);
    data.setUserData(subscription.userData() != null ? subscription.userData().duplicate() : null);
    List<TopicPartition> ownedPartitions = new ArrayList<>(subscription.ownedPartitions());
    ownedPartitions.sort(Comparator.comparing(TopicPartition::topic).thenComparing(TopicPartition::partition));
    ConsumerProtocolSubscription.TopicPartition partition = null;
    for (TopicPartition tp : ownedPartitions) {
        if (partition == null || !partition.topic().equals(tp.topic())) {
            partition = new ConsumerProtocolSubscription.TopicPartition().setTopic(tp.topic());
            data.ownedPartitions().add(partition);
        }
        partition.partitions().add(tp.partition());
    }
    return MessageUtil.toVersionPrefixedByteBuffer(version, data);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ConsumerProtocolSubscription(org.apache.kafka.common.message.ConsumerProtocolSubscription) ArrayList(java.util.ArrayList)

Aggregations

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