Search in sources :

Example 1 with TransactionListing

use of org.apache.kafka.clients.admin.TransactionListing 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 TransactionListing

use of org.apache.kafka.clients.admin.TransactionListing in project kafka by apache.

the class TransactionsCommandTest method expectListTransactions.

private void expectListTransactions(ListTransactionsOptions options, Map<Integer, Collection<TransactionListing>> listingsByBroker) {
    ListTransactionsResult listResult = Mockito.mock(ListTransactionsResult.class);
    Mockito.when(admin.listTransactions(options)).thenReturn(listResult);
    List<TransactionListing> allListings = new ArrayList<>();
    listingsByBroker.values().forEach(allListings::addAll);
    Mockito.when(listResult.all()).thenReturn(completedFuture(allListings));
    Mockito.when(listResult.allByBrokerId()).thenReturn(completedFuture(listingsByBroker));
}
Also used : ArrayList(java.util.ArrayList) ListTransactionsResult(org.apache.kafka.clients.admin.ListTransactionsResult) TransactionListing(org.apache.kafka.clients.admin.TransactionListing)

Example 3 with TransactionListing

use of org.apache.kafka.clients.admin.TransactionListing in project kafka by apache.

the class TransactionsCommandTest method testFindHangingFilterByTransactionInProgressWithSamePartition.

@Test
public void testFindHangingFilterByTransactionInProgressWithSamePartition() throws Exception {
    TopicPartition topicPartition = new TopicPartition("foo", 5);
    String[] args = new String[] { "--bootstrap-server", "localhost:9092", "find-hanging", "--topic", topicPartition.topic(), "--partition", String.valueOf(topicPartition.partition()) };
    long producerId = 132L;
    short producerEpoch = 5;
    long lastTimestamp = time.milliseconds() - TimeUnit.MINUTES.toMillis(60);
    int coordinatorEpoch = 19;
    long txnStartOffset = 29384L;
    expectDescribeProducers(topicPartition, producerId, producerEpoch, lastTimestamp, OptionalInt.of(coordinatorEpoch), OptionalLong.of(txnStartOffset));
    String transactionalId = "bar";
    TransactionListing listing = new TransactionListing(transactionalId, producerId, TransactionState.ONGOING);
    expectListTransactions(new ListTransactionsOptions().filterProducerIds(singleton(producerId)), singletonMap(1, Collections.singletonList(listing)));
    // The coordinator shows an active transaction with the same epoch
    // which includes the partition, so no hanging transaction should
    // be detected.
    TransactionDescription description = new TransactionDescription(1, TransactionState.ONGOING, producerId, producerEpoch, 60000, OptionalLong.of(lastTimestamp), singleton(topicPartition));
    expectDescribeTransactions(singletonMap(transactionalId, description));
    execute(args);
    assertNormalExit();
    assertNoHangingTransactions();
}
Also used : TransactionDescription(org.apache.kafka.clients.admin.TransactionDescription) ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) TopicPartition(org.apache.kafka.common.TopicPartition) TransactionListing(org.apache.kafka.clients.admin.TransactionListing) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with TransactionListing

use of org.apache.kafka.clients.admin.TransactionListing in project kafka by apache.

the class ListTransactionsHandlerTest method assertExpectedTransactions.

private void assertExpectedTransactions(List<ListTransactionsResponseData.TransactionState> expected, Collection<TransactionListing> actual) {
    assertEquals(expected.size(), actual.size());
    Map<String, ListTransactionsResponseData.TransactionState> expectedMap = expected.stream().collect(Collectors.toMap(ListTransactionsResponseData.TransactionState::transactionalId, Function.identity()));
    for (TransactionListing actualListing : actual) {
        ListTransactionsResponseData.TransactionState expectedState = expectedMap.get(actualListing.transactionalId());
        assertNotNull(expectedState);
        assertExpectedTransactionState(expectedState, actualListing);
    }
}
Also used : TransactionState(org.apache.kafka.clients.admin.TransactionState) TransactionListing(org.apache.kafka.clients.admin.TransactionListing) ListTransactionsResponseData(org.apache.kafka.common.message.ListTransactionsResponseData)

Example 5 with TransactionListing

use of org.apache.kafka.clients.admin.TransactionListing in project kafka by apache.

the class TransactionsCommandTest method testFindHangingWithNoTransactionDescription.

@Test
public void testFindHangingWithNoTransactionDescription() throws Exception {
    TopicPartition topicPartition = new TopicPartition("foo", 5);
    String[] args = new String[] { "--bootstrap-server", "localhost:9092", "find-hanging", "--topic", topicPartition.topic(), "--partition", String.valueOf(topicPartition.partition()) };
    long producerId = 132L;
    short producerEpoch = 5;
    long lastTimestamp = time.milliseconds() - TimeUnit.MINUTES.toMillis(60);
    int coordinatorEpoch = 19;
    long txnStartOffset = 29384L;
    expectDescribeProducers(topicPartition, producerId, producerEpoch, lastTimestamp, OptionalInt.of(coordinatorEpoch), OptionalLong.of(txnStartOffset));
    String transactionalId = "bar";
    TransactionListing listing = new TransactionListing(transactionalId, producerId, TransactionState.ONGOING);
    expectListTransactions(new ListTransactionsOptions().filterProducerIds(singleton(producerId)), singletonMap(1, Collections.singletonList(listing)));
    DescribeTransactionsResult result = Mockito.mock(DescribeTransactionsResult.class);
    Mockito.when(result.description(transactionalId)).thenReturn(failedFuture(new TransactionalIdNotFoundException(transactionalId + " not found")));
    Mockito.when(admin.describeTransactions(singleton(transactionalId))).thenReturn(result);
    execute(args);
    assertNormalExit();
    assertHangingTransaction(topicPartition, producerId, producerEpoch, coordinatorEpoch, txnStartOffset, lastTimestamp);
}
Also used : TransactionalIdNotFoundException(org.apache.kafka.common.errors.TransactionalIdNotFoundException) ListTransactionsOptions(org.apache.kafka.clients.admin.ListTransactionsOptions) TopicPartition(org.apache.kafka.common.TopicPartition) DescribeTransactionsResult(org.apache.kafka.clients.admin.DescribeTransactionsResult) TransactionListing(org.apache.kafka.clients.admin.TransactionListing) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TransactionListing (org.apache.kafka.clients.admin.TransactionListing)6 ListTransactionsOptions (org.apache.kafka.clients.admin.ListTransactionsOptions)4 TopicPartition (org.apache.kafka.common.TopicPartition)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArrayList (java.util.ArrayList)2 TransactionDescription (org.apache.kafka.clients.admin.TransactionDescription)2 TransactionState (org.apache.kafka.clients.admin.TransactionState)2 Collection (java.util.Collection)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 DescribeTransactionsResult (org.apache.kafka.clients.admin.DescribeTransactionsResult)1 ListTransactionsResult (org.apache.kafka.clients.admin.ListTransactionsResult)1 Node (org.apache.kafka.common.Node)1 CoordinatorNotAvailableException (org.apache.kafka.common.errors.CoordinatorNotAvailableException)1 TransactionalIdNotFoundException (org.apache.kafka.common.errors.TransactionalIdNotFoundException)1 ListTransactionsRequestData (org.apache.kafka.common.message.ListTransactionsRequestData)1 ListTransactionsResponseData (org.apache.kafka.common.message.ListTransactionsResponseData)1 Errors (org.apache.kafka.common.protocol.Errors)1