Search in sources :

Example 1 with DescribeTransactionsResponse

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

the class DescribeTransactionsHandlerTest method handleResponseError.

private ApiResult<CoordinatorKey, TransactionDescription> handleResponseError(DescribeTransactionsHandler handler, String transactionalId, Errors error) {
    CoordinatorKey key = CoordinatorKey.byTransactionalId(transactionalId);
    Set<CoordinatorKey> keys = mkSet(key);
    DescribeTransactionsResponseData.TransactionState transactionState = new DescribeTransactionsResponseData.TransactionState().setErrorCode(error.code()).setTransactionalId(transactionalId);
    DescribeTransactionsResponse response = new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(transactionState)));
    ApiResult<CoordinatorKey, TransactionDescription> result = handler.handleResponse(node, keys, response);
    assertEquals(emptyMap(), result.completedKeys);
    return result;
}
Also used : DescribeTransactionsResponse(org.apache.kafka.common.requests.DescribeTransactionsResponse) TransactionDescription(org.apache.kafka.clients.admin.TransactionDescription) DescribeTransactionsResponseData(org.apache.kafka.common.message.DescribeTransactionsResponseData)

Example 2 with DescribeTransactionsResponse

use of org.apache.kafka.common.requests.DescribeTransactionsResponse 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 3 with DescribeTransactionsResponse

use of org.apache.kafka.common.requests.DescribeTransactionsResponse 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 4 with DescribeTransactionsResponse

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

the class DescribeTransactionsHandlerTest method testHandleSuccessfulResponse.

@Test
public void testHandleSuccessfulResponse() {
    String transactionalId1 = "foo";
    String transactionalId2 = "bar";
    Set<String> transactionalIds = mkSet(transactionalId1, transactionalId2);
    DescribeTransactionsHandler handler = new DescribeTransactionsHandler(logContext);
    DescribeTransactionsResponseData.TransactionState transactionState1 = sampleTransactionState1(transactionalId1);
    DescribeTransactionsResponseData.TransactionState transactionState2 = sampleTransactionState2(transactionalId2);
    Set<CoordinatorKey> keys = coordinatorKeys(transactionalIds);
    DescribeTransactionsResponse response = new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(asList(transactionState1, transactionState2)));
    ApiResult<CoordinatorKey, TransactionDescription> result = handler.handleResponse(node, keys, response);
    assertEquals(keys, result.completedKeys.keySet());
    assertMatchingTransactionState(node.id(), transactionState1, result.completedKeys.get(CoordinatorKey.byTransactionalId(transactionalId1)));
    assertMatchingTransactionState(node.id(), transactionState2, result.completedKeys.get(CoordinatorKey.byTransactionalId(transactionalId2)));
}
Also used : DescribeTransactionsResponse(org.apache.kafka.common.requests.DescribeTransactionsResponse) TransactionDescription(org.apache.kafka.clients.admin.TransactionDescription) DescribeTransactionsResponseData(org.apache.kafka.common.message.DescribeTransactionsResponseData) Test(org.junit.jupiter.api.Test)

Example 5 with DescribeTransactionsResponse

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

the class DescribeTransactionsHandler method handleResponse.

@Override
public ApiResult<CoordinatorKey, TransactionDescription> handleResponse(Node broker, Set<CoordinatorKey> keys, AbstractResponse abstractResponse) {
    DescribeTransactionsResponse response = (DescribeTransactionsResponse) abstractResponse;
    Map<CoordinatorKey, TransactionDescription> completed = new HashMap<>();
    Map<CoordinatorKey, Throwable> failed = new HashMap<>();
    List<CoordinatorKey> unmapped = new ArrayList<>();
    for (DescribeTransactionsResponseData.TransactionState transactionState : response.data().transactionStates()) {
        CoordinatorKey transactionalIdKey = CoordinatorKey.byTransactionalId(transactionState.transactionalId());
        if (!keys.contains(transactionalIdKey)) {
            log.warn("Response included transactionalId `{}`, which was not requested", transactionState.transactionalId());
            continue;
        }
        Errors error = Errors.forCode(transactionState.errorCode());
        if (error != Errors.NONE) {
            handleError(transactionalIdKey, error, failed, unmapped);
            continue;
        }
        OptionalLong transactionStartTimeMs = transactionState.transactionStartTimeMs() < 0 ? OptionalLong.empty() : OptionalLong.of(transactionState.transactionStartTimeMs());
        completed.put(transactionalIdKey, new TransactionDescription(broker.id(), TransactionState.parse(transactionState.transactionState()), transactionState.producerId(), transactionState.producerEpoch(), transactionState.transactionTimeoutMs(), transactionStartTimeMs, collectTopicPartitions(transactionState)));
    }
    return new ApiResult<>(completed, failed, unmapped);
}
Also used : TransactionDescription(org.apache.kafka.clients.admin.TransactionDescription) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DescribeTransactionsResponseData(org.apache.kafka.common.message.DescribeTransactionsResponseData) Errors(org.apache.kafka.common.protocol.Errors) DescribeTransactionsResponse(org.apache.kafka.common.requests.DescribeTransactionsResponse) OptionalLong(java.util.OptionalLong)

Aggregations

DescribeTransactionsResponseData (org.apache.kafka.common.message.DescribeTransactionsResponseData)5 DescribeTransactionsResponse (org.apache.kafka.common.requests.DescribeTransactionsResponse)5 TransactionDescription (org.apache.kafka.clients.admin.TransactionDescription)3 Test (org.junit.jupiter.api.Test)3 Node (org.apache.kafka.common.Node)2 DescribeTransactionsRequest (org.apache.kafka.common.requests.DescribeTransactionsRequest)2 FindCoordinatorRequest (org.apache.kafka.common.requests.FindCoordinatorRequest)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 OptionalLong (java.util.OptionalLong)1 Cluster (org.apache.kafka.common.Cluster)1 FindCoordinatorResponseData (org.apache.kafka.common.message.FindCoordinatorResponseData)1 Errors (org.apache.kafka.common.protocol.Errors)1 FindCoordinatorResponse (org.apache.kafka.common.requests.FindCoordinatorResponse)1 MockTime (org.apache.kafka.common.utils.MockTime)1