Search in sources :

Example 1 with KafkaConsumerGroupClient

use of io.confluent.ksql.util.KafkaConsumerGroupClient in project ksql by confluentinc.

the class KafkaTopicsList method build.

public static KafkaTopicsList build(String statementText, Collection<KsqlTopic> ksqlTopics, Map<String, TopicDescription> kafkaTopicDescriptions, KsqlConfig ksqlConfig, KafkaConsumerGroupClient consumerGroupClient) {
    Set<String> registeredNames = getRegisteredKafkaTopicNames(ksqlTopics);
    List<KafkaTopicInfo> kafkaTopicInfoList = new ArrayList<>();
    kafkaTopicDescriptions = new TreeMap<>(filterKsqlInternalTopics(kafkaTopicDescriptions, ksqlConfig));
    Map<String, List<Integer>> topicConsumersAndGroupCount = getTopicConsumerAndGroupCounts(consumerGroupClient);
    for (TopicDescription desp : kafkaTopicDescriptions.values()) {
        kafkaTopicInfoList.add(new KafkaTopicInfo(desp.name(), registeredNames.contains(desp.name()), desp.partitions().stream().map(partition -> partition.replicas().size()).collect(Collectors.toList()), topicConsumersAndGroupCount.getOrDefault(desp.name(), Arrays.asList(0, 0)).get(0), topicConsumersAndGroupCount.getOrDefault(desp.name(), Arrays.asList(0, 0)).get(1)));
    }
    return new KafkaTopicsList(statementText, kafkaTopicInfoList);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) TopicPartition(org.apache.kafka.common.TopicPartition) Arrays(java.util.Arrays) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) Collection(java.util.Collection) Set(java.util.Set) KafkaConsumerGroupClient(io.confluent.ksql.util.KafkaConsumerGroupClient) HashMap(java.util.HashMap) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) List(java.util.List) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KafkaConsumerGroupClientImpl(io.confluent.ksql.util.KafkaConsumerGroupClientImpl) Map(java.util.Map) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Preconditions(com.google.common.base.Preconditions) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KsqlConstants(io.confluent.ksql.util.KsqlConstants) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 2 with KafkaConsumerGroupClient

use of io.confluent.ksql.util.KafkaConsumerGroupClient in project ksql by confluentinc.

the class KafkaTopicsListTest method shouldBuildValidTopicList.

@Test
public void shouldBuildValidTopicList() {
    Collection<KsqlTopic> ksqlTopics = Collections.emptyList();
    // represent the full list of topics
    Map<String, TopicDescription> topicDescriptions = new HashMap<>();
    TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(1, new Node(1, "", 8088), Collections.emptyList(), Collections.emptyList());
    topicDescriptions.put("test-topic", new TopicDescription("test-topic", false, Collections.singletonList(topicPartitionInfo)));
    /**
     * Return POJO for consumerGroupClient
     */
    TopicPartition topicPartition = new TopicPartition("test-topic", 1);
    KafkaConsumerGroupClientImpl.ConsumerSummary consumerSummary = new KafkaConsumerGroupClientImpl.ConsumerSummary("consumer-id");
    consumerSummary.addPartition(topicPartition);
    KafkaConsumerGroupClientImpl.ConsumerGroupSummary consumerGroupSummary = new KafkaConsumerGroupClientImpl.ConsumerGroupSummary();
    consumerGroupSummary.addConsumerSummary(consumerSummary);
    KafkaConsumerGroupClient consumerGroupClient = mock(KafkaConsumerGroupClient.class);
    expect(consumerGroupClient.listGroups()).andReturn(Collections.singletonList("test-topic"));
    expect(consumerGroupClient.describeConsumerGroup("test-topic")).andReturn(consumerGroupSummary);
    replay(consumerGroupClient);
    /**
     * Test
     */
    KafkaTopicsList topicsList = KafkaTopicsList.build("statement test", ksqlTopics, topicDescriptions, new KsqlConfig(Collections.EMPTY_MAP), consumerGroupClient);
    assertThat(topicsList.getTopics().size(), equalTo(1));
    KafkaTopicInfo first = topicsList.getTopics().iterator().next();
    assertThat(first.getConsumerGroupCount(), equalTo(1));
    assertThat(first.getConsumerCount(), equalTo(1));
    assertThat(first.getReplicaInfo().size(), equalTo(1));
}
Also used : HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) KsqlConfig(io.confluent.ksql.util.KsqlConfig) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) KafkaConsumerGroupClientImpl(io.confluent.ksql.util.KafkaConsumerGroupClientImpl) TopicPartition(org.apache.kafka.common.TopicPartition) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaConsumerGroupClient(io.confluent.ksql.util.KafkaConsumerGroupClient) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) Test(org.junit.Test)

Example 3 with KafkaConsumerGroupClient

use of io.confluent.ksql.util.KafkaConsumerGroupClient in project ksql by confluentinc.

the class KafkaTopicsList method getTopicConsumerAndGroupCounts.

/**
 * @return all topics with their associated consumerCount and consumerGroupCount
 */
private static Map<String, List<Integer>> getTopicConsumerAndGroupCounts(KafkaConsumerGroupClient consumerGroupClient) {
    List<String> consumerGroups = consumerGroupClient.listGroups();
    Map<String, AtomicInteger> topicConsumerCount = new HashMap<>();
    Map<String, Set<String>> topicConsumerGroupCount = new HashMap<>();
    for (String group : consumerGroups) {
        Collection<KafkaConsumerGroupClientImpl.ConsumerSummary> consumerSummaryList = consumerGroupClient.describeConsumerGroup(group).consumers();
        for (KafkaConsumerGroupClientImpl.ConsumerSummary consumerSummary : consumerSummaryList) {
            for (TopicPartition topicPartition : consumerSummary.partitions()) {
                topicConsumerCount.computeIfAbsent(topicPartition.topic(), k -> new AtomicInteger()).incrementAndGet();
                topicConsumerGroupCount.computeIfAbsent(topicPartition.topic(), k -> new HashSet<>()).add(group);
            }
        }
    }
    HashMap<String, List<Integer>> results = new HashMap<>();
    topicConsumerCount.forEach((k, v) -> {
        results.computeIfAbsent(k, v1 -> new ArrayList<>()).add(v.intValue());
        results.get(k).add(topicConsumerGroupCount.get(k).size());
    });
    return results;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) TopicPartition(org.apache.kafka.common.TopicPartition) Arrays(java.util.Arrays) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) Collection(java.util.Collection) Set(java.util.Set) KafkaConsumerGroupClient(io.confluent.ksql.util.KafkaConsumerGroupClient) HashMap(java.util.HashMap) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) List(java.util.List) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KafkaConsumerGroupClientImpl(io.confluent.ksql.util.KafkaConsumerGroupClientImpl) Map(java.util.Map) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Preconditions(com.google.common.base.Preconditions) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KsqlConstants(io.confluent.ksql.util.KsqlConstants) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KafkaConsumerGroupClientImpl(io.confluent.ksql.util.KafkaConsumerGroupClientImpl) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)3 KafkaConsumerGroupClient (io.confluent.ksql.util.KafkaConsumerGroupClient)3 KafkaConsumerGroupClientImpl (io.confluent.ksql.util.KafkaConsumerGroupClientImpl)3 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 HashMap (java.util.HashMap)3 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)2 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)2 Preconditions (com.google.common.base.Preconditions)2 KsqlConstants (io.confluent.ksql.util.KsqlConstants)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2