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