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;
}
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();
}
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)));
}
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);
}
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));
}
Aggregations