Search in sources :

Example 1 with ListTransactionsResponse

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

the class ListTransactionsHandler method handleResponse.

@Override
public ApiResult<AllBrokersStrategy.BrokerKey, Collection<TransactionListing>> handleResponse(Node broker, Set<AllBrokersStrategy.BrokerKey> keys, AbstractResponse abstractResponse) {
    int brokerId = broker.id();
    AllBrokersStrategy.BrokerKey key = requireSingleton(keys, brokerId);
    ListTransactionsResponse response = (ListTransactionsResponse) abstractResponse;
    Errors error = Errors.forCode(response.data().errorCode());
    if (error == Errors.COORDINATOR_LOAD_IN_PROGRESS) {
        log.debug("The `ListTransactions` request sent to broker {} failed because the " + "coordinator is still loading state. Will try again after backing off", brokerId);
        return ApiResult.empty();
    } else if (error == Errors.COORDINATOR_NOT_AVAILABLE) {
        log.debug("The `ListTransactions` request sent to broker {} failed because the " + "coordinator is shutting down", brokerId);
        return ApiResult.failed(key, new CoordinatorNotAvailableException("ListTransactions " + "request sent to broker " + brokerId + " failed because the coordinator is shutting down"));
    } else if (error != Errors.NONE) {
        log.error("The `ListTransactions` request sent to broker {} failed because of an " + "unexpected error {}", brokerId, error);
        return ApiResult.failed(key, error.exception("ListTransactions request " + "sent to broker " + brokerId + " failed with an unexpected exception"));
    } else {
        List<TransactionListing> listings = response.data().transactionStates().stream().map(transactionState -> new TransactionListing(transactionState.transactionalId(), transactionState.producerId(), TransactionState.parse(transactionState.transactionState()))).collect(Collectors.toList());
        return ApiResult.completed(key, listings);
    }
}
Also used : CoordinatorNotAvailableException(org.apache.kafka.common.errors.CoordinatorNotAvailableException) Logger(org.slf4j.Logger) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) Collection(java.util.Collection) Set(java.util.Set) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) Collectors(java.util.stream.Collectors) ListTransactionsRequestData(org.apache.kafka.common.message.ListTransactionsRequestData) ArrayList(java.util.ArrayList) TransactionState(org.apache.kafka.clients.admin.TransactionState) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) List(java.util.List) LogContext(org.apache.kafka.common.utils.LogContext) ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) Errors(org.apache.kafka.common.protocol.Errors) TransactionListing(org.apache.kafka.clients.admin.TransactionListing) Node(org.apache.kafka.common.Node) Errors(org.apache.kafka.common.protocol.Errors) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) CoordinatorNotAvailableException(org.apache.kafka.common.errors.CoordinatorNotAvailableException) TransactionListing(org.apache.kafka.clients.admin.TransactionListing)

Example 2 with ListTransactionsResponse

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

the class KafkaAdminClientTest method testListTransactions.

@Test
public void testListTransactions() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        MetadataResponseData.MetadataResponseBrokerCollection brokers = new MetadataResponseData.MetadataResponseBrokerCollection();
        env.cluster().nodes().forEach(node -> {
            brokers.add(new MetadataResponseData.MetadataResponseBroker().setHost(node.host()).setNodeId(node.id()).setPort(node.port()).setRack(node.rack()));
        });
        env.kafkaClient().prepareResponse(request -> request instanceof MetadataRequest, new MetadataResponse(new MetadataResponseData().setBrokers(brokers), MetadataResponseData.HIGHEST_SUPPORTED_VERSION));
        List<TransactionListing> expected = Arrays.asList(new TransactionListing("foo", 12345L, TransactionState.ONGOING), new TransactionListing("bar", 98765L, TransactionState.PREPARE_ABORT), new TransactionListing("baz", 13579L, TransactionState.COMPLETE_COMMIT));
        assertEquals(Utils.mkSet(0, 1, 2), env.cluster().nodes().stream().map(Node::id).collect(Collectors.toSet()));
        env.cluster().nodes().forEach(node -> {
            ListTransactionsResponseData response = new ListTransactionsResponseData().setErrorCode(Errors.NONE.code());
            TransactionListing listing = expected.get(node.id());
            response.transactionStates().add(new ListTransactionsResponseData.TransactionState().setTransactionalId(listing.transactionalId()).setProducerId(listing.producerId()).setTransactionState(listing.state().toString()));
            env.kafkaClient().prepareResponseFrom(request -> request instanceof ListTransactionsRequest, new ListTransactionsResponse(response), node);
        });
        ListTransactionsResult result = env.adminClient().listTransactions();
        assertEquals(new HashSet<>(expected), new HashSet<>(result.all().get()));
    }
}
Also used : Node(org.apache.kafka.common.Node) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ListTransactionsResponseData(org.apache.kafka.common.message.ListTransactionsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with ListTransactionsResponse

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

the class ListTransactionsHandlerTest method testHandleSuccessfulResponse.

@Test
public void testHandleSuccessfulResponse() {
    int brokerId = 1;
    BrokerKey brokerKey = new BrokerKey(OptionalInt.of(brokerId));
    ListTransactionsOptions options = new ListTransactionsOptions();
    ListTransactionsHandler handler = new ListTransactionsHandler(options, logContext);
    ListTransactionsResponse response = sampleListTransactionsResponse1();
    ApiResult<BrokerKey, Collection<TransactionListing>> result = handler.handleResponse(node, singleton(brokerKey), response);
    assertEquals(singleton(brokerKey), result.completedKeys.keySet());
    assertExpectedTransactions(response.data().transactionStates(), result.completedKeys.get(brokerKey));
}
Also used : ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) Collection(java.util.Collection) BrokerKey(org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey) Test(org.junit.jupiter.api.Test)

Example 4 with ListTransactionsResponse

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

the class ListTransactionsHandlerTest method handleResponseWithError.

private ApiResult<BrokerKey, Collection<TransactionListing>> handleResponseWithError(int brokerId, Errors error) {
    BrokerKey brokerKey = new BrokerKey(OptionalInt.of(brokerId));
    ListTransactionsOptions options = new ListTransactionsOptions();
    ListTransactionsHandler handler = new ListTransactionsHandler(options, logContext);
    ListTransactionsResponse response = new ListTransactionsResponse(new ListTransactionsResponseData().setErrorCode(error.code()));
    return handler.handleResponse(node, singleton(brokerKey), response);
}
Also used : ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) BrokerKey(org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey) ListTransactionsResponseData(org.apache.kafka.common.message.ListTransactionsResponseData)

Aggregations

ListTransactionsResponse (org.apache.kafka.common.requests.ListTransactionsResponse)4 ListTransactionsOptions (org.apache.kafka.clients.admin.ListTransactionsOptions)3 Collection (java.util.Collection)2 BrokerKey (org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey)2 Node (org.apache.kafka.common.Node)2 ListTransactionsResponseData (org.apache.kafka.common.message.ListTransactionsResponseData)2 ListTransactionsRequest (org.apache.kafka.common.requests.ListTransactionsRequest)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 TransactionListing (org.apache.kafka.clients.admin.TransactionListing)1 TransactionState (org.apache.kafka.clients.admin.TransactionState)1 CoordinatorNotAvailableException (org.apache.kafka.common.errors.CoordinatorNotAvailableException)1 ListTransactionsRequestData (org.apache.kafka.common.message.ListTransactionsRequestData)1 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)1 Errors (org.apache.kafka.common.protocol.Errors)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)1