use of org.apache.kafka.clients.admin.DescribeTopicsResult in project ksql by confluentinc.
the class KafkaTopicClientImplTest method describeTopicReturningUnknownPartitionException.
@SuppressWarnings("unchecked")
private static DescribeTopicsResult describeTopicReturningUnknownPartitionException() {
DescribeTopicsResult describeTopicsResult = niceMock(DescribeTopicsResult.class);
expect(describeTopicsResult.all()).andReturn(failedFuture(new UnknownTopicOrPartitionException("Topic doesn't exist")));
replay(describeTopicsResult);
return describeTopicsResult;
}
use of org.apache.kafka.clients.admin.DescribeTopicsResult in project apache-kafka-on-k8s by banzaicloud.
the class WorkerUtils method verifyTopics.
/**
* Verifies that topics in 'topicsToVerify' list have the same number of partitions as
* described in 'topicsInfo'
* @param log The logger to use
* @param adminClient AdminClient
* @param topicsToVerify List of topics to verify
* @param topicsInfo Map of topic name to topic description, which includes topics in
* 'topicsToVerify' list.
* @throws RuntimeException If one or more topics have different number of partitions than
* described in 'topicsInfo'
*/
private static void verifyTopics(Logger log, AdminClient adminClient, Collection<String> topicsToVerify, Map<String, NewTopic> topicsInfo) throws Throwable {
DescribeTopicsResult topicsResult = adminClient.describeTopics(topicsToVerify, new DescribeTopicsOptions().timeoutMs(CREATE_TOPICS_REQUEST_TIMEOUT));
Map<String, TopicDescription> topicDescriptionMap = topicsResult.all().get();
for (TopicDescription desc : topicDescriptionMap.values()) {
// map will always contain the topic since all topics in 'topicsExists' are in given
// 'topics' map
int partitions = topicsInfo.get(desc.name()).numPartitions();
if (desc.partitions().size() != partitions) {
String str = "Topic '" + desc.name() + "' exists, but has " + desc.partitions().size() + " partitions, while requested " + " number of partitions is " + partitions;
log.warn(str);
throw new RuntimeException(str);
}
}
}
use of org.apache.kafka.clients.admin.DescribeTopicsResult in project ksql by confluentinc.
the class KafkaTopicClientImplTest method getDescribeTopicsResult.
private DescribeTopicsResult getDescribeTopicsResult() {
TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, node, Collections.singletonList(node), Collections.singletonList(node));
TopicDescription topicDescription = new TopicDescription(topicName1, false, Collections.singletonList(topicPartitionInfo));
DescribeTopicsResult describeTopicsResult = mock(DescribeTopicsResult.class);
expect(describeTopicsResult.all()).andReturn(KafkaFuture.completedFuture(Collections.singletonMap(topicName1, topicDescription)));
replay(describeTopicsResult);
return describeTopicsResult;
}
use of org.apache.kafka.clients.admin.DescribeTopicsResult 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.DescribeTopicsResult in project apache-kafka-on-k8s by banzaicloud.
the class ClientAuthenticationFailureTest method testAdminClientWithInvalidCredentials.
@Test
public void testAdminClientWithInvalidCredentials() {
Map<String, Object> props = new HashMap<>(saslClientConfigs);
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:" + server.port());
try (AdminClient client = AdminClient.create(props)) {
DescribeTopicsResult result = client.describeTopics(Collections.singleton("test"));
result.all().get();
fail("Expected an authentication error!");
} catch (Exception e) {
assertTrue("Expected SaslAuthenticationException, got " + e.getCause().getClass(), e.getCause() instanceof SaslAuthenticationException);
}
}
Aggregations