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);
}
}
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);
}
Aggregations