Search in sources :

Example 1 with ListTransactionsRequest

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

the class ListTransactionsHandlerTest method testBuildRequestWithFilteredState.

@Test
public void testBuildRequestWithFilteredState() {
    int brokerId = 1;
    BrokerKey brokerKey = new BrokerKey(OptionalInt.of(brokerId));
    TransactionState filteredState = TransactionState.ONGOING;
    ListTransactionsOptions options = new ListTransactionsOptions().filterStates(singleton(filteredState));
    ListTransactionsHandler handler = new ListTransactionsHandler(options, logContext);
    ListTransactionsRequest request = handler.buildRequest(brokerId, singleton(brokerKey)).build();
    assertEquals(Collections.singletonList(filteredState.toString()), request.data().stateFilters());
    assertEquals(Collections.emptyList(), request.data().producerIdFilters());
}
Also used : TransactionState(org.apache.kafka.clients.admin.TransactionState) ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) BrokerKey(org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey) Test(org.junit.jupiter.api.Test)

Example 2 with ListTransactionsRequest

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

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

the class ListTransactionsHandlerTest method testBuildRequestWithFilteredProducerId.

@Test
public void testBuildRequestWithFilteredProducerId() {
    int brokerId = 1;
    BrokerKey brokerKey = new BrokerKey(OptionalInt.of(brokerId));
    long filteredProducerId = 23423L;
    ListTransactionsOptions options = new ListTransactionsOptions().filterProducerIds(singleton(filteredProducerId));
    ListTransactionsHandler handler = new ListTransactionsHandler(options, logContext);
    ListTransactionsRequest request = handler.buildRequest(brokerId, singleton(brokerKey)).build();
    assertEquals(Collections.singletonList(filteredProducerId), request.data().producerIdFilters());
    assertEquals(Collections.emptyList(), request.data().stateFilters());
}
Also used : ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) BrokerKey(org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey) Test(org.junit.jupiter.api.Test)

Example 4 with ListTransactionsRequest

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

the class ListTransactionsHandlerTest method testBuildRequestWithoutFilters.

@Test
public void testBuildRequestWithoutFilters() {
    int brokerId = 1;
    BrokerKey brokerKey = new BrokerKey(OptionalInt.of(brokerId));
    ListTransactionsOptions options = new ListTransactionsOptions();
    ListTransactionsHandler handler = new ListTransactionsHandler(options, logContext);
    ListTransactionsRequest request = handler.buildRequest(brokerId, singleton(brokerKey)).build();
    assertEquals(Collections.emptyList(), request.data().producerIdFilters());
    assertEquals(Collections.emptyList(), request.data().stateFilters());
}
Also used : ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) BrokerKey(org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey) Test(org.junit.jupiter.api.Test)

Aggregations

ListTransactionsRequest (org.apache.kafka.common.requests.ListTransactionsRequest)4 Test (org.junit.jupiter.api.Test)4 ListTransactionsOptions (org.apache.kafka.clients.admin.ListTransactionsOptions)3 BrokerKey (org.apache.kafka.clients.admin.internals.AllBrokersStrategy.BrokerKey)3 TransactionState (org.apache.kafka.clients.admin.TransactionState)1 Node (org.apache.kafka.common.Node)1 ListTransactionsResponseData (org.apache.kafka.common.message.ListTransactionsResponseData)1 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)1 ListTransactionsResponse (org.apache.kafka.common.requests.ListTransactionsResponse)1 MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)1 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1