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