Search in sources :

Example 21 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class KafkaAdminClientTest method testDescribeConfigsPartialResponse.

@Test
public void testDescribeConfigsPartialResponse() throws Exception {
    ConfigResource topic = new ConfigResource(ConfigResource.Type.TOPIC, "topic");
    ConfigResource topic2 = new ConfigResource(ConfigResource.Type.TOPIC, "topic2");
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(new DescribeConfigsResponse(new DescribeConfigsResponseData().setResults(asList(new DescribeConfigsResponseData.DescribeConfigsResult().setResourceName(topic.name()).setResourceType(topic.type().id()).setErrorCode(Errors.NONE.code()).setConfigs(emptyList())))));
        Map<ConfigResource, KafkaFuture<Config>> result = env.adminClient().describeConfigs(asList(topic, topic2)).values();
        assertEquals(new HashSet<>(asList(topic, topic2)), result.keySet());
        result.get(topic);
        TestUtils.assertFutureThrows(result.get(topic2), ApiException.class);
    }
}
Also used : DescribeConfigsResponseData(org.apache.kafka.common.message.DescribeConfigsResponseData) KafkaFuture(org.apache.kafka.common.KafkaFuture) ConfigResource(org.apache.kafka.common.config.ConfigResource) DescribeConfigsResponse(org.apache.kafka.common.requests.DescribeConfigsResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 22 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class KafkaAdminClientTest method testDescribeLogDirsOfflineDir.

@Test
public void testDescribeLogDirsOfflineDir() throws ExecutionException, InterruptedException {
    Set<Integer> brokers = singleton(0);
    String logDir = "/var/data/kafka";
    Errors error = Errors.KAFKA_STORAGE_ERROR;
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponseFrom(prepareDescribeLogDirsResponse(error, logDir, emptyList()), env.cluster().nodeById(0));
        DescribeLogDirsResult result = env.adminClient().describeLogDirs(brokers);
        Map<Integer, KafkaFuture<Map<String, LogDirDescription>>> descriptions = result.descriptions();
        assertEquals(brokers, descriptions.keySet());
        assertNotNull(descriptions.get(0));
        Map<String, LogDirDescription> descriptionsMap = descriptions.get(0).get();
        assertEquals(singleton(logDir), descriptionsMap.keySet());
        assertEquals(error.exception().getClass(), descriptionsMap.get(logDir).error().getClass());
        assertEquals(emptySet(), descriptionsMap.get(logDir).replicaInfos().keySet());
        Map<Integer, Map<String, LogDirDescription>> allDescriptions = result.allDescriptions().get();
        assertEquals(brokers, allDescriptions.keySet());
        Map<String, LogDirDescription> allMap = allDescriptions.get(0);
        assertNotNull(allMap);
        assertEquals(singleton(logDir), allMap.keySet());
        assertEquals(error.exception().getClass(), allMap.get(logDir).error().getClass());
        assertEquals(emptySet(), allMap.get(logDir).replicaInfos().keySet());
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) KafkaFuture(org.apache.kafka.common.KafkaFuture) Map(java.util.Map) HashMap(java.util.HashMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 23 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class ListTransactionsResultTest method testPartialFailure.

@Test
public void testPartialFailure() throws Exception {
    KafkaFutureImpl<Collection<TransactionListing>> future1 = new KafkaFutureImpl<>();
    KafkaFutureImpl<Collection<TransactionListing>> future2 = new KafkaFutureImpl<>();
    Map<Integer, KafkaFutureImpl<Collection<TransactionListing>>> brokerFutures = new HashMap<>();
    brokerFutures.put(1, future1);
    brokerFutures.put(2, future2);
    future.complete(brokerFutures);
    List<TransactionListing> broker1Listings = asList(new TransactionListing("foo", 12345L, TransactionState.ONGOING), new TransactionListing("bar", 98765L, TransactionState.PREPARE_ABORT));
    future1.complete(broker1Listings);
    future2.completeExceptionally(new KafkaException());
    Map<Integer, KafkaFuture<Collection<TransactionListing>>> resultBrokerFutures = result.byBrokerId().get();
    // Ensure that the future for broker 1 completes successfully
    assertEquals(Utils.mkSet(1, 2), resultBrokerFutures.keySet());
    assertEquals(broker1Listings, resultBrokerFutures.get(1).get());
    // Everything else should fail
    assertFutureThrows(result.all(), KafkaException.class);
    assertFutureThrows(result.allByBrokerId(), KafkaException.class);
    assertFutureThrows(resultBrokerFutures.get(2), KafkaException.class);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) Collection(java.util.Collection) KafkaException(org.apache.kafka.common.KafkaException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Test(org.junit.jupiter.api.Test)

Example 24 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class MockAdminClient method handleDescribeTopicsUsingIds.

public synchronized Map<Uuid, KafkaFuture<TopicDescription>> handleDescribeTopicsUsingIds(Collection<Uuid> topicIds, DescribeTopicsOptions options) {
    Map<Uuid, KafkaFuture<TopicDescription>> topicDescriptions = new HashMap<>();
    if (timeoutNextRequests > 0) {
        for (Uuid requestedTopicId : topicIds) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            topicDescriptions.put(requestedTopicId, future);
        }
        --timeoutNextRequests;
        return topicDescriptions;
    }
    for (Uuid requestedTopicId : topicIds) {
        for (Map.Entry<String, TopicMetadata> topicDescription : allTopics.entrySet()) {
            String topicName = topicDescription.getKey();
            Uuid topicId = this.topicIds.get(topicName);
            if (topicId != null && topicId.equals(requestedTopicId) && !topicDescription.getValue().markedForDeletion) {
                if (topicDescription.getValue().fetchesRemainingUntilVisible > 0) {
                    topicDescription.getValue().fetchesRemainingUntilVisible--;
                } else {
                    TopicMetadata topicMetadata = topicDescription.getValue();
                    KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
                    future.complete(new TopicDescription(topicName, topicMetadata.isInternalTopic, topicMetadata.partitions, Collections.emptySet(), topicId));
                    topicDescriptions.put(requestedTopicId, future);
                    break;
                }
            }
        }
        if (!topicDescriptions.containsKey(requestedTopicId)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new UnknownTopicIdException("Topic id" + requestedTopicId + " not found."));
            topicDescriptions.put(requestedTopicId, future);
        }
    }
    return topicDescriptions;
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Uuid(org.apache.kafka.common.Uuid) UnknownTopicIdException(org.apache.kafka.common.errors.UnknownTopicIdException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 25 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project kafka by apache.

the class MockAdminClient method handleDeleteTopicsUsingNames.

private Map<String, KafkaFuture<Void>> handleDeleteTopicsUsingNames(Collection<String> topicNameCollection, DeleteTopicsOptions options) {
    Map<String, KafkaFuture<Void>> deleteTopicsResult = new HashMap<>();
    Collection<String> topicNames = new ArrayList<>(topicNameCollection);
    if (timeoutNextRequests > 0) {
        for (final String topicName : topicNames) {
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            deleteTopicsResult.put(topicName, future);
        }
        --timeoutNextRequests;
        return deleteTopicsResult;
    }
    for (final String topicName : topicNames) {
        KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
        if (allTopics.remove(topicName) == null) {
            future.completeExceptionally(new UnknownTopicOrPartitionException(String.format("Topic %s does not exist.", topicName)));
        } else {
            topicNames.remove(topicIds.remove(topicName));
            future.complete(null);
        }
        deleteTopicsResult.put(topicName, future);
    }
    return deleteTopicsResult;
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) ArrayList(java.util.ArrayList) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Aggregations

KafkaFuture (org.apache.kafka.common.KafkaFuture)70 HashMap (java.util.HashMap)51 Map (java.util.Map)37 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)31 ExecutionException (java.util.concurrent.ExecutionException)22 TimeoutException (org.apache.kafka.common.errors.TimeoutException)21 ArrayList (java.util.ArrayList)15 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)15 Test (org.junit.jupiter.api.Test)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 TopicPartition (org.apache.kafka.common.TopicPartition)13 ConfigResource (org.apache.kafka.common.config.ConfigResource)12 HashSet (java.util.HashSet)11 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)10 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)8 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)7 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)7 DescribeTopicsResult (org.apache.kafka.clients.admin.DescribeTopicsResult)6 Node (org.apache.kafka.common.Node)6 TopicPartitionReplica (org.apache.kafka.common.TopicPartitionReplica)6