Search in sources :

Example 1 with FindCoordinatorResponseData

use of org.apache.kafka.common.message.FindCoordinatorResponseData 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 2 with FindCoordinatorResponseData

use of org.apache.kafka.common.message.FindCoordinatorResponseData in project kafka by apache.

the class CoordinatorStrategyTest method testSuccessfulOldCoordinatorLookup.

@Test
public void testSuccessfulOldCoordinatorLookup() {
    CoordinatorKey group = CoordinatorKey.byGroupId("foo");
    FindCoordinatorResponseData responseData = new FindCoordinatorResponseData().setErrorCode(Errors.NONE.code()).setHost("localhost").setPort(9092).setNodeId(1);
    AdminApiLookupStrategy.LookupResult<CoordinatorKey> result = runOldLookup(group, responseData);
    assertEquals(singletonMap(group, 1), result.mappedKeys);
    assertEquals(emptyMap(), result.failedKeys);
}
Also used : FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData) Test(org.junit.jupiter.api.Test)

Example 3 with FindCoordinatorResponseData

use of org.apache.kafka.common.message.FindCoordinatorResponseData in project kafka by apache.

the class CoordinatorStrategyTest method testHandleOldResponseRequiresOneKey.

@Test
public void testHandleOldResponseRequiresOneKey() {
    FindCoordinatorResponseData responseData = new FindCoordinatorResponseData().setErrorCode(Errors.NONE.code());
    FindCoordinatorResponse response = new FindCoordinatorResponse(responseData);
    CoordinatorStrategy strategy = new CoordinatorStrategy(CoordinatorType.GROUP, new LogContext());
    strategy.disableBatch();
    assertThrows(IllegalArgumentException.class, () -> strategy.handleResponse(Collections.emptySet(), response));
    CoordinatorKey group1 = CoordinatorKey.byGroupId("foo");
    CoordinatorKey group2 = CoordinatorKey.byGroupId("bar");
    assertThrows(IllegalArgumentException.class, () -> strategy.handleResponse(mkSet(group1, group2), response));
}
Also used : FindCoordinatorResponse(org.apache.kafka.common.requests.FindCoordinatorResponse) LogContext(org.apache.kafka.common.utils.LogContext) FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData) Test(org.junit.jupiter.api.Test)

Example 4 with FindCoordinatorResponseData

use of org.apache.kafka.common.message.FindCoordinatorResponseData in project kafka by apache.

the class CoordinatorStrategyTest method testRetriableCoordinatorLookup.

private void testRetriableCoordinatorLookup(Errors error) {
    CoordinatorKey group1 = CoordinatorKey.byGroupId("foo");
    CoordinatorKey group2 = CoordinatorKey.byGroupId("bar");
    FindCoordinatorResponseData responseData = new FindCoordinatorResponseData().setCoordinators(Arrays.asList(new FindCoordinatorResponseData.Coordinator().setKey("foo").setErrorCode(error.code()), new FindCoordinatorResponseData.Coordinator().setKey("bar").setErrorCode(Errors.NONE.code()).setHost("localhost").setPort(9092).setNodeId(2)));
    AdminApiLookupStrategy.LookupResult<CoordinatorKey> result = runLookup(new HashSet<>(Arrays.asList(group1, group2)), responseData);
    assertEquals(emptyMap(), result.failedKeys);
    assertEquals(singletonMap(group2, 2), result.mappedKeys);
}
Also used : FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData)

Example 5 with FindCoordinatorResponseData

use of org.apache.kafka.common.message.FindCoordinatorResponseData in project kafka by apache.

the class FindCoordinatorRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    FindCoordinatorResponseData response = new FindCoordinatorResponseData();
    if (version() >= 2) {
        response.setThrottleTimeMs(throttleTimeMs);
    }
    Errors error = Errors.forException(e);
    if (version() < MIN_BATCHED_VERSION) {
        return FindCoordinatorResponse.prepareOldResponse(error, Node.noNode());
    } else {
        return FindCoordinatorResponse.prepareErrorResponse(error, data.coordinatorKeys());
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData)

Aggregations

FindCoordinatorResponseData (org.apache.kafka.common.message.FindCoordinatorResponseData)12 Test (org.junit.jupiter.api.Test)4 Coordinator (org.apache.kafka.common.message.FindCoordinatorResponseData.Coordinator)2 FindCoordinatorResponse (org.apache.kafka.common.requests.FindCoordinatorResponse)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Cluster (org.apache.kafka.common.Cluster)1 Node (org.apache.kafka.common.Node)1 DescribeTransactionsResponseData (org.apache.kafka.common.message.DescribeTransactionsResponseData)1 Errors (org.apache.kafka.common.protocol.Errors)1 DescribeTransactionsRequest (org.apache.kafka.common.requests.DescribeTransactionsRequest)1 DescribeTransactionsResponse (org.apache.kafka.common.requests.DescribeTransactionsResponse)1 FindCoordinatorRequest (org.apache.kafka.common.requests.FindCoordinatorRequest)1 LogContext (org.apache.kafka.common.utils.LogContext)1 MockTime (org.apache.kafka.common.utils.MockTime)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1