Search in sources :

Example 1 with TransactionDescription

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

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

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

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

Example 5 with TransactionDescription

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

the class TransactionsCommandTest method testDescribeTransaction.

@Test
public void testDescribeTransaction() throws Exception {
    String transactionalId = "foo";
    String[] args = new String[] { "--bootstrap-server", "localhost:9092", "describe", "--transactional-id", transactionalId };
    DescribeTransactionsResult describeResult = Mockito.mock(DescribeTransactionsResult.class);
    int coordinatorId = 5;
    long transactionStartTime = time.milliseconds();
    KafkaFuture<TransactionDescription> describeFuture = completedFuture(new TransactionDescription(coordinatorId, TransactionState.ONGOING, 12345L, 15, 10000, OptionalLong.of(transactionStartTime), singleton(new TopicPartition("bar", 0))));
    Mockito.when(describeResult.description(transactionalId)).thenReturn(describeFuture);
    Mockito.when(admin.describeTransactions(singleton(transactionalId))).thenReturn(describeResult);
    // Add a little time so that we can see a positive transaction duration in the output
    time.sleep(5000);
    execute(args);
    assertNormalExit();
    List<List<String>> table = readOutputAsTable();
    assertEquals(2, table.size());
    List<String> expectedHeaders = asList(TransactionsCommand.DescribeTransactionsCommand.HEADERS);
    assertEquals(expectedHeaders, table.get(0));
    List<String> expectedRow = asList(String.valueOf(coordinatorId), transactionalId, "12345", "15", "Ongoing", "10000", String.valueOf(transactionStartTime), "5000", "bar-0");
    assertEquals(expectedRow, table.get(1));
}
Also used : TransactionDescription(org.apache.kafka.clients.admin.TransactionDescription) TopicPartition(org.apache.kafka.common.TopicPartition) DescribeTransactionsResult(org.apache.kafka.clients.admin.DescribeTransactionsResult) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TransactionDescription (org.apache.kafka.clients.admin.TransactionDescription)6 Test (org.junit.jupiter.api.Test)4 TopicPartition (org.apache.kafka.common.TopicPartition)3 DescribeTransactionsResponseData (org.apache.kafka.common.message.DescribeTransactionsResponseData)3 DescribeTransactionsResponse (org.apache.kafka.common.requests.DescribeTransactionsResponse)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArrayList (java.util.ArrayList)2 ListTransactionsOptions (org.apache.kafka.clients.admin.ListTransactionsOptions)2 TransactionListing (org.apache.kafka.clients.admin.TransactionListing)2 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 OptionalLong (java.util.OptionalLong)1 DescribeTransactionsResult (org.apache.kafka.clients.admin.DescribeTransactionsResult)1 Errors (org.apache.kafka.common.protocol.Errors)1