Search in sources :

Example 6 with DeleteTopicsResult

use of org.apache.kafka.clients.admin.DeleteTopicsResult in project strimzi by strimzi.

the class ControllerIT method deleteTopic.

private void deleteTopic(TestContext context, String topicName, String configMapName) throws InterruptedException, ExecutionException {
    LOGGER.info("Deleting topic {} (ConfigMap {})", topicName, configMapName);
    // Now we can delete the topic
    DeleteTopicsResult dlt = adminClient.deleteTopics(singletonList(topicName));
    dlt.all().get();
    LOGGER.info("Deleted topic {}", topicName);
    // Wait for the configmap to be deleted
    waitFor(context, () -> {
        ConfigMap cm = kubeClient.configMaps().inNamespace(NAMESPACE).withName(configMapName).get();
        LOGGER.info("Polled configmap {}, got {}, waiting for deletion", configMapName, cm);
        return cm == null;
    }, timeout, "Expected the configmap to have been deleted by now");
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult)

Example 7 with DeleteTopicsResult

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

the class KafkaTopicClientImpl method deleteTopics.

@Override
public void deleteTopics(final Collection<String> topicsToDelete) {
    if (topicsToDelete.isEmpty()) {
        return;
    }
    final DeleteTopicsResult deleteTopicsResult = adminClient.get().deleteTopics(topicsToDelete);
    final Map<String, KafkaFuture<Void>> results = deleteTopicsResult.topicNameValues();
    final List<String> failList = Lists.newArrayList();
    final List<Pair<String, Throwable>> exceptionList = Lists.newArrayList();
    for (final Map.Entry<String, KafkaFuture<Void>> entry : results.entrySet()) {
        try {
            entry.getValue().get(30, TimeUnit.SECONDS);
        } catch (final Exception e) {
            final Throwable rootCause = ExceptionUtils.getRootCause(e);
            if (rootCause instanceof TopicDeletionDisabledException) {
                throw new TopicDeletionDisabledException("Topic deletion is disabled. " + "To delete the topic, you must set '" + DELETE_TOPIC_ENABLE + "' to true in " + "the Kafka broker configuration.");
            } else if (rootCause instanceof TopicAuthorizationException) {
                throw new KsqlTopicAuthorizationException(AclOperation.DELETE, Collections.singleton(entry.getKey()));
            } else if (!(rootCause instanceof UnknownTopicOrPartitionException)) {
                LOG.error(String.format("Could not delete topic '%s'", entry.getKey()), e);
                failList.add(entry.getKey());
                exceptionList.add(new Pair<>(entry.getKey(), rootCause));
            }
        }
    }
    if (!failList.isEmpty()) {
        throw new KafkaDeleteTopicsException("Failed to clean up topics: " + String.join(",", failList), exceptionList);
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) TopicDeletionDisabledException(org.apache.kafka.common.errors.TopicDeletionDisabledException) KafkaDeleteTopicsException(io.confluent.ksql.exception.KafkaDeleteTopicsException) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) KafkaDeleteTopicsException(io.confluent.ksql.exception.KafkaDeleteTopicsException) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) ExecutionException(java.util.concurrent.ExecutionException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) TopicDeletionDisabledException(org.apache.kafka.common.errors.TopicDeletionDisabledException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) KsqlException(io.confluent.ksql.util.KsqlException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) Map(java.util.Map) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Pair(io.confluent.ksql.util.Pair)

Example 8 with DeleteTopicsResult

use of org.apache.kafka.clients.admin.DeleteTopicsResult in project starlight-for-kafka by datastax.

the class KafkaAuthorizationTestBase method testDeleteTopicFailed.

@Test(timeOut = 20000)
public void testDeleteTopicFailed() throws PulsarAdminException, InterruptedException {
    String newTopic = "testDeleteTopicFailed";
    String fullNewTopicName = "persistent://" + TENANT + "/" + NAMESPACE + "/" + newTopic;
    admin.topics().createPartitionedTopic(fullNewTopicName, 1);
    @Cleanup AdminClient adminClient = createAdminClient(TENANT + "/" + NAMESPACE, userToken);
    DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Collections.singletonList(newTopic));
    try {
        deleteTopicsResult.all().get();
        fail("Should delete failed!");
    } catch (ExecutionException ex) {
        assertTrue(ex.getMessage().contains("TopicAuthorizationException"));
    }
    try {
        admin.topics().createPartitionedTopic(fullNewTopicName, 1);
    } catch (PulsarAdminException exception) {
        assertTrue(exception.getMessage().contains("This topic already exists"));
    }
    admin.topics().deletePartitionedTopic(fullNewTopicName);
    adminClient.close();
}
Also used : DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) Cleanup(lombok.Cleanup) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.testng.annotations.Test)

Example 9 with DeleteTopicsResult

use of org.apache.kafka.clients.admin.DeleteTopicsResult in project starlight-for-kafka by datastax.

the class KafkaAuthorizationTestBase method testDeleteTopicSuccess.

@Test(timeOut = 20000)
public void testDeleteTopicSuccess() throws PulsarAdminException, InterruptedException {
    String newTopic = "testDeleteTopicSuccess";
    String fullNewTopicName = "persistent://" + TENANT + "/" + NAMESPACE + "/" + newTopic;
    admin.topics().createPartitionedTopic(fullNewTopicName, 1);
    @Cleanup AdminClient adminClient = createAdminClient(TENANT + "/" + NAMESPACE, adminToken);
    DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Collections.singletonList(newTopic));
    try {
        deleteTopicsResult.all().get();
    } catch (ExecutionException ex) {
        fail("Should success but have : " + ex.getMessage());
    }
    List<String> topicList = admin.topics().getList(TENANT + "/" + NAMESPACE);
    topicList.forEach(topic -> {
        if (topic.startsWith(fullNewTopicName)) {
            fail("Delete topic failed!");
        }
    });
    adminClient.close();
}
Also used : DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) ExecutionException(java.util.concurrent.ExecutionException) Cleanup(lombok.Cleanup) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.testng.annotations.Test)

Example 10 with DeleteTopicsResult

use of org.apache.kafka.clients.admin.DeleteTopicsResult in project kop by streamnative.

the class KafkaAuthorizationTestBase method testDeleteTopicFailed.

@Test(timeOut = 20000)
public void testDeleteTopicFailed() throws PulsarAdminException, InterruptedException {
    String newTopic = "testDeleteTopicFailed";
    String fullNewTopicName = "persistent://" + TENANT + "/" + NAMESPACE + "/" + newTopic;
    admin.topics().createPartitionedTopic(fullNewTopicName, 1);
    AdminClient adminClient = createAdminClient(TENANT + "/" + NAMESPACE, userToken);
    DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Collections.singletonList(newTopic));
    try {
        deleteTopicsResult.all().get();
        fail("Should delete failed!");
    } catch (ExecutionException ex) {
        log.info("Test delete topic failed", ex);
        assertTrue(ex.getMessage().contains("TopicAuthorizationException"));
    }
    try {
        admin.topics().createPartitionedTopic(fullNewTopicName, 1);
    } catch (PulsarAdminException exception) {
        assertTrue(exception.getMessage().contains("This topic already exists"));
    }
    admin.topics().deletePartitionedTopic(fullNewTopicName);
    adminClient.close();
}
Also used : DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.testng.annotations.Test)

Aggregations

DeleteTopicsResult (org.apache.kafka.clients.admin.DeleteTopicsResult)26 ExecutionException (java.util.concurrent.ExecutionException)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)6 KafkaFuture (org.apache.kafka.common.KafkaFuture)6 Map (java.util.Map)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)3 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)3 KafkaResponseGetFailedException (io.confluent.ksql.exception.KafkaResponseGetFailedException)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 OptionException (joptsimple.OptionException)2 Cleanup (lombok.Cleanup)2 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)2 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)2 KafkaDeleteTopicsException (io.confluent.ksql.exception.KafkaDeleteTopicsException)1 KafkaTopicException (io.confluent.ksql.exception.KafkaTopicException)1 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)1 KsqlException (io.confluent.ksql.util.KsqlException)1