Search in sources :

Example 1 with CreateTopicsRequest

use of org.apache.kafka.common.requests.CreateTopicsRequest in project kafka by apache.

the class KafkaAdminClientTest method testConnectionFailureOnMetadataUpdate.

@Test
public void testConnectionFailureOnMetadataUpdate() throws Exception {
    // This tests the scenario in which we successfully connect to the bootstrap server, but
    // the server disconnects before sending the full response
    Cluster cluster = mockBootstrapCluster();
    try (final AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(Time.SYSTEM, cluster)) {
        Cluster discoveredCluster = mockCluster(3, 0);
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(request -> request instanceof MetadataRequest, null, true);
        env.kafkaClient().prepareResponse(request -> request instanceof MetadataRequest, RequestTestUtils.metadataResponse(discoveredCluster.nodes(), discoveredCluster.clusterResource().clusterId(), 1, Collections.emptyList()));
        env.kafkaClient().prepareResponse(body -> body instanceof CreateTopicsRequest, prepareCreateTopicsResponse("myTopic", Errors.NONE));
        KafkaFuture<Void> future = env.adminClient().createTopics(singleton(new NewTopic("myTopic", Collections.singletonMap(0, asList(0, 1, 2)))), new CreateTopicsOptions().timeoutMs(10000)).all();
        future.get();
    }
}
Also used : MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) Cluster(org.apache.kafka.common.Cluster) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with CreateTopicsRequest

use of org.apache.kafka.common.requests.CreateTopicsRequest in project kafka by apache.

the class KafkaAdminClientTest method testUnreachableBootstrapServer.

@Test
public void testUnreachableBootstrapServer() throws Exception {
    // This tests the scenario in which the bootstrap server is unreachable for a short while,
    // which prevents AdminClient from being able to send the initial metadata request
    Cluster cluster = Cluster.bootstrap(singletonList(new InetSocketAddress("localhost", 8121)));
    Map<Node, Long> unreachableNodes = Collections.singletonMap(cluster.nodes().get(0), 200L);
    try (final AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(Time.SYSTEM, cluster, AdminClientUnitTestEnv.clientConfigs(), unreachableNodes)) {
        Cluster discoveredCluster = mockCluster(3, 0);
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(body -> body instanceof MetadataRequest, RequestTestUtils.metadataResponse(discoveredCluster.nodes(), discoveredCluster.clusterResource().clusterId(), 1, Collections.emptyList()));
        env.kafkaClient().prepareResponse(body -> body instanceof CreateTopicsRequest, prepareCreateTopicsResponse("myTopic", Errors.NONE));
        KafkaFuture<Void> future = env.adminClient().createTopics(singleton(new NewTopic("myTopic", Collections.singletonMap(0, asList(0, 1, 2)))), new CreateTopicsOptions().timeoutMs(10000)).all();
        future.get();
    }
}
Also used : MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) InetSocketAddress(java.net.InetSocketAddress) Node(org.apache.kafka.common.Node) OptionalLong(java.util.OptionalLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cluster(org.apache.kafka.common.Cluster) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with CreateTopicsRequest

use of org.apache.kafka.common.requests.CreateTopicsRequest in project kafka by apache.

the class KafkaAdminClientTest method testCreateTopicsRetryBackoff.

@Test
public void testCreateTopicsRetryBackoff() throws Exception {
    MockTime time = new MockTime();
    int retryBackoff = 100;
    try (final AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, mockCluster(3, 0), newStrMap(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "" + retryBackoff))) {
        MockClient mockClient = env.kafkaClient();
        mockClient.setNodeApiVersions(NodeApiVersions.create());
        AtomicLong firstAttemptTime = new AtomicLong(0);
        AtomicLong secondAttemptTime = new AtomicLong(0);
        mockClient.prepareResponse(body -> {
            firstAttemptTime.set(time.milliseconds());
            return body instanceof CreateTopicsRequest;
        }, null, true);
        mockClient.prepareResponse(body -> {
            secondAttemptTime.set(time.milliseconds());
            return body instanceof CreateTopicsRequest;
        }, prepareCreateTopicsResponse("myTopic", Errors.NONE));
        KafkaFuture<Void> future = env.adminClient().createTopics(singleton(new NewTopic("myTopic", Collections.singletonMap(0, asList(0, 1, 2)))), new CreateTopicsOptions().timeoutMs(10000)).all();
        // Wait until the first attempt has failed, then advance the time
        TestUtils.waitForCondition(() -> mockClient.numAwaitingResponses() == 1, "Failed awaiting CreateTopics first request failure");
        // Wait until the retry call added to the queue in AdminClient
        TestUtils.waitForCondition(() -> ((KafkaAdminClient) env.adminClient()).numPendingCalls() == 1, "Failed to add retry CreateTopics call");
        time.sleep(retryBackoff);
        future.get();
        long actualRetryBackoff = secondAttemptTime.get() - firstAttemptTime.get();
        assertEquals(retryBackoff, actualRetryBackoff, "CreateTopics retry did not await expected backoff");
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

CreateTopicsRequest (org.apache.kafka.common.requests.CreateTopicsRequest)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Cluster (org.apache.kafka.common.Cluster)2 MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)2 InetSocketAddress (java.net.InetSocketAddress)1 OptionalLong (java.util.OptionalLong)1 MockClient (org.apache.kafka.clients.MockClient)1 Node (org.apache.kafka.common.Node)1 MockTime (org.apache.kafka.common.utils.MockTime)1