Search in sources :

Example 16 with KafkaFuture

use of org.apache.kafka.common.KafkaFuture in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method alterConfigs.

@Override
public AlterConfigsResult alterConfigs(Map<ConfigResource, Config> configs, final AlterConfigsOptions options) {
    final Map<ConfigResource, KafkaFutureImpl<Void>> allFutures = new HashMap<>();
    // We must make a separate AlterConfigs request for every BROKER resource we want to alter
    // and send the request to that specific broker. Other resources are grouped together into
    // a single request that may be sent to any broker.
    final Collection<ConfigResource> unifiedRequestResources = new ArrayList<>();
    for (ConfigResource resource : configs.keySet()) {
        if (resource.type() == ConfigResource.Type.BROKER && !resource.isDefault()) {
            NodeProvider nodeProvider = new ConstantNodeIdProvider(Integer.parseInt(resource.name()));
            allFutures.putAll(alterConfigs(configs, options, Collections.singleton(resource), nodeProvider));
        } else
            unifiedRequestResources.add(resource);
    }
    if (!unifiedRequestResources.isEmpty())
        allFutures.putAll(alterConfigs(configs, options, unifiedRequestResources, new LeastLoadedNodeProvider()));
    return new AlterConfigsResult(new HashMap<ConfigResource, KafkaFuture<Void>>(allFutures));
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ConfigResource(org.apache.kafka.common.config.ConfigResource)

Example 17 with KafkaFuture

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

the class KafkaAdminClientTest method testUpdateFeatures.

private void testUpdateFeatures(Map<String, FeatureUpdate> featureUpdates, ApiError topLevelError, Map<String, ApiError> featureUpdateErrors) throws Exception {
    try (final AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().prepareResponse(body -> body instanceof UpdateFeaturesRequest, UpdateFeaturesResponse.createWithErrors(topLevelError, featureUpdateErrors, 0));
        final Map<String, KafkaFuture<Void>> futures = env.adminClient().updateFeatures(featureUpdates, new UpdateFeaturesOptions().timeoutMs(10000)).values();
        for (final Map.Entry<String, KafkaFuture<Void>> entry : futures.entrySet()) {
            final KafkaFuture<Void> future = entry.getValue();
            final ApiError error = featureUpdateErrors.get(entry.getKey());
            if (topLevelError.error() == Errors.NONE) {
                assertNotNull(error);
                if (error.error() == Errors.NONE) {
                    future.get();
                } else {
                    final ExecutionException e = assertThrows(ExecutionException.class, future::get);
                    assertEquals(e.getCause().getClass(), error.exception().getClass());
                }
            } else {
                final ExecutionException e = assertThrows(ExecutionException.class, future::get);
                assertEquals(e.getCause().getClass(), topLevelError.exception().getClass());
            }
        }
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) UpdateFeaturesRequest(org.apache.kafka.common.requests.UpdateFeaturesRequest) ApiError(org.apache.kafka.common.requests.ApiError) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with KafkaFuture

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

the class KafkaAdminClientTest method testDescribeReplicaLogDirsUnexpected.

@Test
public void testDescribeReplicaLogDirsUnexpected() throws ExecutionException, InterruptedException {
    TopicPartitionReplica expected = new TopicPartitionReplica("topic", 12, 1);
    TopicPartitionReplica unexpected = new TopicPartitionReplica("topic", 12, 2);
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        String broker1log0 = "/var/data/kafka0";
        String broker1log1 = "/var/data/kafka1";
        int broker1Log0PartitionSize = 987654321;
        int broker1Log0OffsetLag = 24;
        int broker1Log1PartitionSize = 123456789;
        int broker1Log1OffsetLag = 4321;
        env.kafkaClient().prepareResponseFrom(new DescribeLogDirsResponse(new DescribeLogDirsResponseData().setResults(asList(prepareDescribeLogDirsResult(expected, broker1log0, broker1Log0PartitionSize, broker1Log0OffsetLag, false), prepareDescribeLogDirsResult(unexpected, broker1log1, broker1Log1PartitionSize, broker1Log1OffsetLag, true)))), env.cluster().nodeById(expected.brokerId()));
        DescribeReplicaLogDirsResult result = env.adminClient().describeReplicaLogDirs(asList(expected));
        Map<TopicPartitionReplica, KafkaFuture<DescribeReplicaLogDirsResult.ReplicaLogDirInfo>> values = result.values();
        assertEquals(TestUtils.toSet(asList(expected)), values.keySet());
        assertNotNull(values.get(expected));
        assertEquals(broker1log0, values.get(expected).get().getCurrentReplicaLogDir());
        assertEquals(broker1Log0OffsetLag, values.get(expected).get().getCurrentReplicaOffsetLag());
        assertEquals(broker1log1, values.get(expected).get().getFutureReplicaLogDir());
        assertEquals(broker1Log1OffsetLag, values.get(expected).get().getFutureReplicaOffsetLag());
    }
}
Also used : TopicPartitionReplica(org.apache.kafka.common.TopicPartitionReplica) KafkaFuture(org.apache.kafka.common.KafkaFuture) DescribeLogDirsResponseData(org.apache.kafka.common.message.DescribeLogDirsResponseData) DescribeLogDirsResponse(org.apache.kafka.common.requests.DescribeLogDirsResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 19 with KafkaFuture

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

the class KafkaAdminClientTest method testDescribeLogDirsDeprecated.

@SuppressWarnings("deprecation")
@Test
public void testDescribeLogDirsDeprecated() throws ExecutionException, InterruptedException {
    Set<Integer> brokers = singleton(0);
    TopicPartition tp = new TopicPartition("topic", 12);
    String logDir = "/var/data/kafka";
    Errors error = Errors.NONE;
    int offsetLag = 24;
    long partitionSize = 1234567890;
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponseFrom(prepareDescribeLogDirsResponse(error, logDir, tp, partitionSize, offsetLag), env.cluster().nodeById(0));
        DescribeLogDirsResult result = env.adminClient().describeLogDirs(brokers);
        Map<Integer, KafkaFuture<Map<String, DescribeLogDirsResponse.LogDirInfo>>> deprecatedValues = result.values();
        assertEquals(brokers, deprecatedValues.keySet());
        assertNotNull(deprecatedValues.get(0));
        assertDescriptionContains(deprecatedValues.get(0).get(), logDir, tp, error, offsetLag, partitionSize);
        Map<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>> deprecatedAll = result.all().get();
        assertEquals(brokers, deprecatedAll.keySet());
        assertDescriptionContains(deprecatedAll.get(0), logDir, tp, error, offsetLag, partitionSize);
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) DescribeLogDirsResponse(org.apache.kafka.common.requests.DescribeLogDirsResponse) Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) Map(java.util.Map) HashMap(java.util.HashMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 20 with KafkaFuture

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

the class KafkaAdminClientTest method testDescribeLogDirs.

@Test
public void testDescribeLogDirs() throws ExecutionException, InterruptedException {
    Set<Integer> brokers = singleton(0);
    String logDir = "/var/data/kafka";
    TopicPartition tp = new TopicPartition("topic", 12);
    long partitionSize = 1234567890;
    long offsetLag = 24;
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponseFrom(prepareDescribeLogDirsResponse(Errors.NONE, logDir, tp, partitionSize, offsetLag), 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));
        assertDescriptionContains(descriptions.get(0).get(), logDir, tp, partitionSize, offsetLag);
        Map<Integer, Map<String, LogDirDescription>> allDescriptions = result.allDescriptions().get();
        assertEquals(brokers, allDescriptions.keySet());
        assertDescriptionContains(allDescriptions.get(0), logDir, tp, partitionSize, offsetLag);
        // Empty results when not authorized with version < 3
        env.kafkaClient().prepareResponseFrom(prepareEmptyDescribeLogDirsResponse(Optional.empty()), env.cluster().nodeById(0));
        final DescribeLogDirsResult errorResult = env.adminClient().describeLogDirs(brokers);
        ExecutionException exception = assertThrows(ExecutionException.class, () -> errorResult.allDescriptions().get());
        assertTrue(exception.getCause() instanceof ClusterAuthorizationException);
        // Empty results with an error with version >= 3
        env.kafkaClient().prepareResponseFrom(prepareEmptyDescribeLogDirsResponse(Optional.of(Errors.UNKNOWN_SERVER_ERROR)), env.cluster().nodeById(0));
        final DescribeLogDirsResult errorResult2 = env.adminClient().describeLogDirs(brokers);
        exception = assertThrows(ExecutionException.class, () -> errorResult2.allDescriptions().get());
        assertTrue(exception.getCause() instanceof UnknownServerException);
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) TopicPartition(org.apache.kafka.common.TopicPartition) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

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