Search in sources :

Example 16 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project ksql by confluentinc.

the class SourceDescription method getPartitions.

private static int getPartitions(KafkaTopicClient topicClient, String kafkaTopicName) {
    Map<String, TopicDescription> stringTopicDescriptionMap = topicClient.describeTopics(Arrays.asList(kafkaTopicName));
    TopicDescription topicDescription = stringTopicDescriptionMap.values().iterator().next();
    return topicDescription.partitions().size();
}
Also used : TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 17 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project apache-kafka-on-k8s by banzaicloud.

the class InternalTopicManager method getNumPartitions.

/**
 * Get the number of partitions for the given topics
 */
// visible for testing
protected Map<String, Integer> getNumPartitions(final Set<String> topics) {
    int remainingRetries = retries;
    boolean retry;
    do {
        retry = false;
        final DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(topics);
        final Map<String, KafkaFuture<TopicDescription>> futures = describeTopicsResult.values();
        final Map<String, Integer> existingNumberOfPartitionsPerTopic = new HashMap<>();
        for (final Map.Entry<String, KafkaFuture<TopicDescription>> topicFuture : futures.entrySet()) {
            try {
                final TopicDescription topicDescription = topicFuture.getValue().get();
                existingNumberOfPartitionsPerTopic.put(topicFuture.getKey(), topicDescription.partitions().size());
            } catch (final InterruptedException fatalException) {
                Thread.currentThread().interrupt();
                log.error(INTERRUPTED_ERROR_MESSAGE, fatalException);
                throw new IllegalStateException(INTERRUPTED_ERROR_MESSAGE, fatalException);
            } catch (final ExecutionException couldNotDescribeTopicException) {
                final Throwable cause = couldNotDescribeTopicException.getCause();
                if (cause instanceof TimeoutException) {
                    retry = true;
                    log.debug("Could not get number of partitions for topic {} due to timeout. " + "Will try again (remaining retries {}).", topicFuture.getKey(), remainingRetries - 1);
                } else {
                    final String error = "Could not get number of partitions for topic {}.";
                    log.debug(error, topicFuture.getKey(), cause.getMessage());
                }
            }
        }
        if (retry) {
            topics.removeAll(existingNumberOfPartitionsPerTopic.keySet());
            continue;
        }
        return existingNumberOfPartitionsPerTopic;
    } while (remainingRetries-- > 0);
    return Collections.emptyMap();
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 18 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project apache-kafka-on-k8s by banzaicloud.

the class WorkerUtilsTest method testCreateRetriesOnTimeout.

@Test
public void testCreateRetriesOnTimeout() throws Throwable {
    adminClient.timeoutNextRequest(1);
    WorkerUtils.createTopics(log, adminClient, Collections.singletonMap(TEST_TOPIC, NEW_TEST_TOPIC), true);
    assertEquals(new TopicDescription(TEST_TOPIC, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()))), adminClient.describeTopics(Collections.singleton(TEST_TOPIC)).values().get(TEST_TOPIC).get());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Test(org.junit.Test)

Example 19 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project apache-kafka-on-k8s by banzaicloud.

the class WorkerUtilsTest method testCreatesNotExistingTopics.

@Test
public void testCreatesNotExistingTopics() throws Throwable {
    // should be no topics before the call
    assertEquals(0, adminClient.listTopics().names().get().size());
    WorkerUtils.createTopics(log, adminClient, Collections.singletonMap(TEST_TOPIC, NEW_TEST_TOPIC), false);
    assertEquals(Collections.singleton(TEST_TOPIC), adminClient.listTopics().names().get());
    assertEquals(new TopicDescription(TEST_TOPIC, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()))), adminClient.describeTopics(Collections.singleton(TEST_TOPIC)).values().get(TEST_TOPIC).get());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Test(org.junit.Test)

Example 20 with TopicDescription

use of org.apache.kafka.clients.admin.TopicDescription in project apache-kafka-on-k8s by banzaicloud.

the class WorkerUtilsTest method testCreateOneTopic.

@Test
public void testCreateOneTopic() throws Throwable {
    Map<String, NewTopic> newTopics = Collections.singletonMap(TEST_TOPIC, NEW_TEST_TOPIC);
    WorkerUtils.createTopics(log, adminClient, newTopics, true);
    assertEquals(Collections.singleton(TEST_TOPIC), adminClient.listTopics().names().get());
    assertEquals(new TopicDescription(TEST_TOPIC, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()))), adminClient.describeTopics(Collections.singleton(TEST_TOPIC)).values().get(TEST_TOPIC).get());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node) NewTopic(org.apache.kafka.clients.admin.NewTopic) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Test(org.junit.Test)

Aggregations

TopicDescription (org.apache.kafka.clients.admin.TopicDescription)22 Test (org.junit.Test)10 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)9 Node (org.apache.kafka.common.Node)8 Map (java.util.Map)6 HashMap (java.util.HashMap)4 AdminClient (org.apache.kafka.clients.admin.AdminClient)4 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 ExecutionException (java.util.concurrent.ExecutionException)3 DescribeTopicsResult (org.apache.kafka.clients.admin.DescribeTopicsResult)3 NewTopic (org.apache.kafka.clients.admin.NewTopic)3 KafkaFuture (org.apache.kafka.common.KafkaFuture)3 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)2 KafkaConsumerGroupClient (io.confluent.ksql.util.KafkaConsumerGroupClient)2 KafkaConsumerGroupClientImpl (io.confluent.ksql.util.KafkaConsumerGroupClientImpl)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2