Search in sources :

Example 1 with FindCoordinatorRequest

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

the class KafkaProducerTest method testInitTransactionTimeout.

@Test
public void testInitTransactionTimeout() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
    configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 500);
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime(1);
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
    ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
    metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
        client.prepareResponse(request -> request instanceof FindCoordinatorRequest && ((FindCoordinatorRequest) request).data().keyType() == FindCoordinatorRequest.CoordinatorType.TRANSACTION.id(), FindCoordinatorResponse.prepareResponse(Errors.NONE, "bad-transaction", NODE));
        assertThrows(TimeoutException.class, producer::initTransactions);
        client.prepareResponse(request -> request instanceof FindCoordinatorRequest && ((FindCoordinatorRequest) request).data().keyType() == FindCoordinatorRequest.CoordinatorType.TRANSACTION.id(), FindCoordinatorResponse.prepareResponse(Errors.NONE, "bad-transaction", NODE));
        client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
        // retry initialization should work
        producer.initTransactions();
    }
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) 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)

Example 2 with FindCoordinatorRequest

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

the class TransactionManagerTest method prepareFindCoordinatorResponse.

private void prepareFindCoordinatorResponse(Errors error, boolean shouldDisconnect, final CoordinatorType coordinatorType, final String coordinatorKey) {
    client.prepareResponse(body -> {
        FindCoordinatorRequest findCoordinatorRequest = (FindCoordinatorRequest) body;
        assertEquals(coordinatorType, CoordinatorType.forId(findCoordinatorRequest.data().keyType()));
        String key = findCoordinatorRequest.data().coordinatorKeys().isEmpty() ? findCoordinatorRequest.data().key() : findCoordinatorRequest.data().coordinatorKeys().get(0);
        assertEquals(coordinatorKey, key);
        return true;
    }, FindCoordinatorResponse.prepareResponse(error, coordinatorKey, brokerNode), shouldDisconnect);
}
Also used : FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest)

Example 3 with FindCoordinatorRequest

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

the class KafkaAdminClientTest method testRetryDescribeTransactionsAfterNotCoordinatorError.

@Test
public void testRetryDescribeTransactionsAfterNotCoordinatorError() throws Exception {
    MockTime time = new MockTime();
    int retryBackoffMs = 100;
    Cluster cluster = mockCluster(3, 0);
    Map<String, Object> configOverride = newStrMap(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "" + retryBackoffMs);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, configOverride)) {
        String transactionalId = "foo";
        Iterator<Node> nodeIterator = env.cluster().nodes().iterator();
        Node coordinator1 = nodeIterator.next();
        Node coordinator2 = nodeIterator.next();
        env.kafkaClient().prepareResponse(request -> request instanceof FindCoordinatorRequest, new FindCoordinatorResponse(new FindCoordinatorResponseData().setCoordinators(Arrays.asList(new FindCoordinatorResponseData.Coordinator().setKey(transactionalId).setErrorCode(Errors.NONE.code()).setNodeId(coordinator1.id()).setHost(coordinator1.host()).setPort(coordinator1.port())))));
        env.kafkaClient().prepareResponseFrom(request -> {
            if (!(request instanceof DescribeTransactionsRequest)) {
                return false;
            } else {
                // Backoff needed here for the retry of FindCoordinator
                time.sleep(retryBackoffMs);
                return true;
            }
        }, new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(new DescribeTransactionsResponseData.TransactionState().setErrorCode(Errors.NOT_COORDINATOR.code()).setTransactionalId(transactionalId)))), coordinator1);
        env.kafkaClient().prepareResponse(request -> request instanceof FindCoordinatorRequest, new FindCoordinatorResponse(new FindCoordinatorResponseData().setCoordinators(Arrays.asList(new FindCoordinatorResponseData.Coordinator().setKey(transactionalId).setErrorCode(Errors.NONE.code()).setNodeId(coordinator2.id()).setHost(coordinator2.host()).setPort(coordinator2.port())))));
        TransactionDescription expected = new TransactionDescription(coordinator2.id(), TransactionState.COMPLETE_COMMIT, 12345L, 15, 10000L, OptionalLong.empty(), emptySet());
        env.kafkaClient().prepareResponseFrom(request -> request instanceof DescribeTransactionsRequest, new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(new DescribeTransactionsResponseData.TransactionState().setErrorCode(Errors.NONE.code()).setProducerEpoch((short) expected.producerEpoch()).setProducerId(expected.producerId()).setTransactionalId(transactionalId).setTransactionTimeoutMs(10000).setTransactionStartTimeMs(-1).setTransactionState(expected.state().toString())))), coordinator2);
        DescribeTransactionsResult result = env.adminClient().describeTransactions(singleton(transactionalId));
        KafkaFuture<TransactionDescription> future = result.description(transactionalId);
        assertEquals(expected, future.get());
    }
}
Also used : FindCoordinatorResponse(org.apache.kafka.common.requests.FindCoordinatorResponse) Node(org.apache.kafka.common.Node) DescribeTransactionsResponseData(org.apache.kafka.common.message.DescribeTransactionsResponseData) Cluster(org.apache.kafka.common.Cluster) FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData) DescribeTransactionsResponse(org.apache.kafka.common.requests.DescribeTransactionsResponse) FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) DescribeTransactionsRequest(org.apache.kafka.common.requests.DescribeTransactionsRequest) MockTime(org.apache.kafka.common.utils.MockTime) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with FindCoordinatorRequest

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

the class KafkaAdminClientTest method testDescribeTransactions.

@Test
public void testDescribeTransactions() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        String transactionalId = "foo";
        Node coordinator = env.cluster().nodes().iterator().next();
        TransactionDescription expected = new TransactionDescription(coordinator.id(), TransactionState.COMPLETE_COMMIT, 12345L, 15, 10000L, OptionalLong.empty(), emptySet());
        env.kafkaClient().prepareResponse(request -> request instanceof FindCoordinatorRequest, prepareFindCoordinatorResponse(Errors.NONE, transactionalId, coordinator));
        env.kafkaClient().prepareResponseFrom(request -> request instanceof DescribeTransactionsRequest, new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(new DescribeTransactionsResponseData.TransactionState().setErrorCode(Errors.NONE.code()).setProducerEpoch((short) expected.producerEpoch()).setProducerId(expected.producerId()).setTransactionalId(transactionalId).setTransactionTimeoutMs(10000).setTransactionStartTimeMs(-1).setTransactionState(expected.state().toString())))), coordinator);
        DescribeTransactionsResult result = env.adminClient().describeTransactions(singleton(transactionalId));
        KafkaFuture<TransactionDescription> future = result.description(transactionalId);
        assertEquals(expected, future.get());
    }
}
Also used : DescribeTransactionsResponse(org.apache.kafka.common.requests.DescribeTransactionsResponse) FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) Node(org.apache.kafka.common.Node) DescribeTransactionsResponseData(org.apache.kafka.common.message.DescribeTransactionsResponseData) DescribeTransactionsRequest(org.apache.kafka.common.requests.DescribeTransactionsRequest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 5 with FindCoordinatorRequest

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

the class KafkaProducerTest method testInitTransactionsResponseAfterTimeout.

@Test
public void testInitTransactionsResponseAfterTimeout() throws Exception {
    int maxBlockMs = 500;
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
    configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, maxBlockMs);
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime();
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
    ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
    metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time);
    try {
        client.prepareResponse(request -> request instanceof FindCoordinatorRequest && ((FindCoordinatorRequest) request).data().keyType() == FindCoordinatorRequest.CoordinatorType.TRANSACTION.id(), FindCoordinatorResponse.prepareResponse(Errors.NONE, "bad-transaction", NODE));
        Future<?> future = executor.submit(producer::initTransactions);
        TestUtils.waitForCondition(client::hasInFlightRequests, "Timed out while waiting for expected `InitProducerId` request to be sent");
        time.sleep(maxBlockMs);
        TestUtils.assertFutureThrows(future, TimeoutException.class);
        client.respond(initProducerIdResponse(1L, (short) 5, Errors.NONE));
        Thread.sleep(1000);
        producer.initTransactions();
    } finally {
        producer.close(Duration.ZERO);
    }
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ExecutorService(java.util.concurrent.ExecutorService) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) 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

FindCoordinatorRequest (org.apache.kafka.common.requests.FindCoordinatorRequest)6 Test (org.junit.jupiter.api.Test)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MockTime (org.apache.kafka.common.utils.MockTime)3 HashMap (java.util.HashMap)2 MockClient (org.apache.kafka.clients.MockClient)2 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)2 Node (org.apache.kafka.common.Node)2 DescribeTransactionsResponseData (org.apache.kafka.common.message.DescribeTransactionsResponseData)2 DescribeTransactionsRequest (org.apache.kafka.common.requests.DescribeTransactionsRequest)2 DescribeTransactionsResponse (org.apache.kafka.common.requests.DescribeTransactionsResponse)2 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)2 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 Time (org.apache.kafka.common.utils.Time)2 ExecutorService (java.util.concurrent.ExecutorService)1 Cluster (org.apache.kafka.common.Cluster)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 FindCoordinatorResponseData (org.apache.kafka.common.message.FindCoordinatorResponseData)1 FindCoordinatorResponse (org.apache.kafka.common.requests.FindCoordinatorResponse)1