Search in sources :

Example 1 with DescribeTopicsResult

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;
}
Also used : UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult)

Example 2 with 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);
        }
    }
}
Also used : DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) DescribeTopicsOptions(org.apache.kafka.clients.admin.DescribeTopicsOptions) TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 3 with DescribeTopicsResult

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;
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 4 with 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();
}
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 5 with DescribeTopicsResult

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);
    }
}
Also used : HashMap(java.util.HashMap) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) SaslAuthenticationException(org.apache.kafka.common.errors.SaslAuthenticationException) SaslAuthenticationException(org.apache.kafka.common.errors.SaslAuthenticationException) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Aggregations

DescribeTopicsResult (org.apache.kafka.clients.admin.DescribeTopicsResult)7 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AdminClient (org.apache.kafka.clients.admin.AdminClient)2 KafkaFuture (org.apache.kafka.common.KafkaFuture)2 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)2 ExecutionException (java.util.concurrent.ExecutionException)1 DescribeTopicsOptions (org.apache.kafka.clients.admin.DescribeTopicsOptions)1 TopicListing (org.apache.kafka.clients.admin.TopicListing)1 SaslAuthenticationException (org.apache.kafka.common.errors.SaslAuthenticationException)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)1 Test (org.junit.Test)1