Search in sources :

Example 1 with DescribeTransactionsResult

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

the class TransactionsCommandTest method expectDescribeTransactions.

private void expectDescribeTransactions(Map<String, TransactionDescription> descriptions) {
    DescribeTransactionsResult result = Mockito.mock(DescribeTransactionsResult.class);
    descriptions.forEach((transactionalId, description) -> {
        Mockito.when(result.description(transactionalId)).thenReturn(completedFuture(description));
    });
    Mockito.when(result.all()).thenReturn(completedFuture(descriptions));
    Mockito.when(admin.describeTransactions(descriptions.keySet())).thenReturn(result);
}
Also used : DescribeTransactionsResult(org.apache.kafka.clients.admin.DescribeTransactionsResult)

Example 2 with DescribeTransactionsResult

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

Example 3 with DescribeTransactionsResult

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

DescribeTransactionsResult (org.apache.kafka.clients.admin.DescribeTransactionsResult)3 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 ListTransactionsOptions (org.apache.kafka.clients.admin.ListTransactionsOptions)1 TransactionDescription (org.apache.kafka.clients.admin.TransactionDescription)1 TransactionListing (org.apache.kafka.clients.admin.TransactionListing)1 TransactionalIdNotFoundException (org.apache.kafka.common.errors.TransactionalIdNotFoundException)1