use of org.apache.pulsar.client.admin.Transactions in project pulsar by yahoo.
the class TransactionsBase method internalGetSlowTransactions.
protected void internalGetSlowTransactions(AsyncResponse asyncResponse, boolean authoritative, long timeout, Integer coordinatorId) {
try {
if (coordinatorId != null) {
validateTopicOwnership(SystemTopicNames.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId), authoritative);
TransactionMetadataStore transactionMetadataStore = pulsar().getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(coordinatorId));
if (transactionMetadataStore == null) {
asyncResponse.resume(new RestException(NOT_FOUND, "Transaction coordinator not found! coordinator id : " + coordinatorId));
return;
}
List<TxnMeta> transactions = transactionMetadataStore.getSlowTransactions(timeout);
List<CompletableFuture<TransactionMetadata>> completableFutures = new ArrayList<>();
for (TxnMeta txnMeta : transactions) {
CompletableFuture<TransactionMetadata> completableFuture = new CompletableFuture<>();
getTransactionMetadata(txnMeta, completableFuture);
completableFutures.add(completableFuture);
}
FutureUtil.waitForAll(completableFutures).whenComplete((v, e) -> {
if (e != null) {
asyncResponse.resume(new RestException(e.getCause()));
return;
}
Map<String, TransactionMetadata> transactionMetadata = new HashMap<>();
for (CompletableFuture<TransactionMetadata> future : completableFutures) {
try {
transactionMetadata.put(future.get().txnId, future.get());
} catch (Exception exception) {
asyncResponse.resume(new RestException(exception.getCause()));
return;
}
}
asyncResponse.resume(transactionMetadata);
});
} else {
getPartitionedTopicMetadataAsync(SystemTopicNames.TRANSACTION_COORDINATOR_ASSIGN, false, false).thenAccept(partitionMetadata -> {
if (partitionMetadata.partitions == 0) {
asyncResponse.resume(new RestException(Response.Status.NOT_FOUND, "Transaction coordinator not found"));
return;
}
List<CompletableFuture<Map<String, TransactionMetadata>>> completableFutures = Lists.newArrayList();
for (int i = 0; i < partitionMetadata.partitions; i++) {
try {
completableFutures.add(pulsar().getAdminClient().transactions().getSlowTransactionsByCoordinatorIdAsync(i, timeout, TimeUnit.MILLISECONDS));
} catch (PulsarServerException e) {
asyncResponse.resume(new RestException(e));
return;
}
}
Map<String, TransactionMetadata> transactionMetadataMaps = new HashMap<>();
FutureUtil.waitForAll(completableFutures).whenComplete((result, e) -> {
if (e != null) {
asyncResponse.resume(new RestException(e));
return;
}
for (CompletableFuture<Map<String, TransactionMetadata>> transactionMetadataMap : completableFutures) {
try {
transactionMetadataMaps.putAll(transactionMetadataMap.get());
} catch (Exception exception) {
asyncResponse.resume(new RestException(exception.getCause()));
return;
}
}
asyncResponse.resume(transactionMetadataMaps);
});
}).exceptionally(ex -> {
log.error("[{}] Failed to get transaction coordinator state.", clientAppId(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
} catch (Exception e) {
asyncResponse.resume(new RestException(e));
}
}
use of org.apache.pulsar.client.admin.Transactions in project pulsar by apache.
the class PulsarAdminToolTest method transactions.
@Test
void transactions() throws Exception {
PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
Transactions transactions = Mockito.mock(Transactions.class);
doReturn(transactions).when(admin).transactions();
CmdTransactions cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("coordinator-stats -c 1"));
verify(transactions).getCoordinatorStatsById(1);
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("coordinator-stats"));
verify(transactions).getCoordinatorStats();
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("coordinator-internal-stats -c 1 -m"));
verify(transactions).getCoordinatorInternalStats(1, true);
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("transaction-in-buffer-stats -m 1 -t test -l 2"));
verify(transactions).getTransactionInBufferStats(new TxnID(1, 2), "test");
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("transaction-in-pending-ack-stats -m 1 -l 2 -t test -s test"));
verify(transactions).getTransactionInPendingAckStats(new TxnID(1, 2), "test", "test");
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("transaction-metadata -m 1 -l 2"));
verify(transactions).getTransactionMetadata(new TxnID(1, 2));
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("slow-transactions -c 1 -t 1h"));
verify(transactions).getSlowTransactionsByCoordinatorId(1, 3600000, TimeUnit.MILLISECONDS);
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("slow-transactions -t 1h"));
verify(transactions).getSlowTransactions(3600000, TimeUnit.MILLISECONDS);
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("transaction-buffer-stats -t test"));
verify(transactions).getTransactionBufferStats("test");
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("pending-ack-stats -t test -s test"));
verify(transactions).getPendingAckStats("test", "test");
cmdTransactions = new CmdTransactions(() -> admin);
cmdTransactions.run(split("pending-ack-internal-stats -t test -s test"));
verify(transactions).getPendingAckInternalStats("test", "test", false);
}
Aggregations