use of org.apache.pulsar.common.policies.data.TransactionMetadata in project pulsar by apache.
the class AdminApiTransactionTest method testGetSlowTransactions.
@Test(timeOut = 20000)
public void testGetSlowTransactions() throws Exception {
initTransaction(2);
TransactionImpl transaction1 = (TransactionImpl) pulsarClient.newTransaction().withTransactionTimeout(60, TimeUnit.SECONDS).build().get();
TransactionImpl transaction2 = (TransactionImpl) pulsarClient.newTransaction().withTransactionTimeout(60, TimeUnit.SECONDS).build().get();
pulsarClient.newTransaction().withTransactionTimeout(20, TimeUnit.SECONDS).build();
pulsarClient.newTransaction().withTransactionTimeout(20, TimeUnit.SECONDS).build();
Map<String, TransactionMetadata> transactionMetadataMap = admin.transactions().getSlowTransactionsAsync(30, TimeUnit.SECONDS).get();
assertEquals(transactionMetadataMap.size(), 2);
TxnID txnID1 = new TxnID(transaction1.getTxnIdMostBits(), transaction1.getTxnIdLeastBits());
TxnID txnID2 = new TxnID(transaction2.getTxnIdMostBits(), transaction2.getTxnIdLeastBits());
TransactionMetadata transactionMetadata = transactionMetadataMap.get(txnID1.toString());
assertNotNull(transactionMetadata);
assertEquals(transactionMetadata.timeoutAt, 60000);
transactionMetadata = transactionMetadataMap.get(txnID2.toString());
assertNotNull(transactionMetadata);
assertEquals(transactionMetadata.timeoutAt, 60000);
}
use of org.apache.pulsar.common.policies.data.TransactionMetadata in project pulsar by apache.
the class TransactionsBase method getTransactionMetadata.
private void getTransactionMetadata(TxnMeta txnMeta, CompletableFuture<TransactionMetadata> transactionMetadataFuture) throws PulsarServerException {
Transactions transactions = pulsar().getAdminClient().transactions();
TransactionMetadata transactionMetadata = new TransactionMetadata();
TxnID txnID = txnMeta.id();
transactionMetadata.txnId = txnID.toString();
transactionMetadata.status = txnMeta.status().name();
transactionMetadata.openTimestamp = txnMeta.getOpenTimestamp();
transactionMetadata.timeoutAt = txnMeta.getTimeoutAt();
List<CompletableFuture<TransactionInPendingAckStats>> ackedPartitionsFutures = new ArrayList<>();
Map<String, Map<String, CompletableFuture<TransactionInPendingAckStats>>> ackFutures = new HashMap<>();
txnMeta.ackedPartitions().forEach(transactionSubscription -> {
String topic = transactionSubscription.getTopic();
String subName = transactionSubscription.getSubscription();
CompletableFuture<TransactionInPendingAckStats> future = transactions.getTransactionInPendingAckStatsAsync(txnID, topic, subName);
ackedPartitionsFutures.add(future);
if (ackFutures.containsKey(topic)) {
ackFutures.get(topic).put(transactionSubscription.getSubscription(), future);
} else {
Map<String, CompletableFuture<TransactionInPendingAckStats>> pendingAckStatsMap = new HashMap<>();
pendingAckStatsMap.put(transactionSubscription.getSubscription(), future);
ackFutures.put(topic, pendingAckStatsMap);
}
});
List<CompletableFuture<TransactionInBufferStats>> producedPartitionsFutures = new ArrayList<>();
Map<String, CompletableFuture<TransactionInBufferStats>> produceFutures = new HashMap<>();
txnMeta.producedPartitions().forEach(topic -> {
CompletableFuture<TransactionInBufferStats> future = transactions.getTransactionInBufferStatsAsync(txnID, topic);
producedPartitionsFutures.add(future);
produceFutures.put(topic, future);
});
FutureUtil.waitForAll(ackedPartitionsFutures).whenComplete((v, e) -> {
if (e != null) {
transactionMetadataFuture.completeExceptionally(e);
return;
}
FutureUtil.waitForAll(producedPartitionsFutures).whenComplete((x, t) -> {
if (t != null) {
transactionMetadataFuture.completeExceptionally(e);
return;
}
Map<String, Map<String, TransactionInPendingAckStats>> ackedPartitions = new HashMap<>();
Map<String, TransactionInBufferStats> producedPartitions = new HashMap<>();
for (String topic : ackFutures.keySet()) {
Map<String, TransactionInPendingAckStats> subs = new HashMap<>();
for (String sub : ackFutures.get(topic).keySet()) {
try {
subs.put(sub, ackFutures.get(topic).get(sub).get());
} catch (Exception exception) {
transactionMetadataFuture.completeExceptionally(exception);
return;
}
}
ackedPartitions.put(topic, subs);
}
for (String topic : produceFutures.keySet()) {
try {
producedPartitions.put(topic, produceFutures.get(topic).get());
} catch (Exception exception) {
transactionMetadataFuture.completeExceptionally(exception);
return;
}
}
transactionMetadata.ackedPartitions = ackedPartitions;
transactionMetadata.producedPartitions = producedPartitions;
transactionMetadataFuture.complete(transactionMetadata);
});
});
}
use of org.apache.pulsar.common.policies.data.TransactionMetadata in project pulsar by apache.
the class TransactionsBase method internalGetSlowTransactions.
protected void internalGetSlowTransactions(AsyncResponse asyncResponse, boolean authoritative, long timeout, Integer coordinatorId) {
try {
if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
if (coordinatorId != null) {
validateTopicOwnership(TopicName.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(TopicName.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;
});
}
} else {
asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "Broker don't support transaction!"));
}
} catch (Exception e) {
asyncResponse.resume(new RestException(e));
}
}
use of org.apache.pulsar.common.policies.data.TransactionMetadata in project pulsar by yahoo.
the class TransactionsBase method getTransactionMetadata.
private void getTransactionMetadata(TxnMeta txnMeta, CompletableFuture<TransactionMetadata> transactionMetadataFuture) throws PulsarServerException {
Transactions transactions = pulsar().getAdminClient().transactions();
TransactionMetadata transactionMetadata = new TransactionMetadata();
TxnID txnID = txnMeta.id();
transactionMetadata.txnId = txnID.toString();
transactionMetadata.status = txnMeta.status().name();
transactionMetadata.openTimestamp = txnMeta.getOpenTimestamp();
transactionMetadata.timeoutAt = txnMeta.getTimeoutAt();
List<CompletableFuture<TransactionInPendingAckStats>> ackedPartitionsFutures = new ArrayList<>();
Map<String, Map<String, CompletableFuture<TransactionInPendingAckStats>>> ackFutures = new HashMap<>();
txnMeta.ackedPartitions().forEach(transactionSubscription -> {
String topic = transactionSubscription.getTopic();
String subName = transactionSubscription.getSubscription();
CompletableFuture<TransactionInPendingAckStats> future = transactions.getTransactionInPendingAckStatsAsync(txnID, topic, subName);
ackedPartitionsFutures.add(future);
if (ackFutures.containsKey(topic)) {
ackFutures.get(topic).put(transactionSubscription.getSubscription(), future);
} else {
Map<String, CompletableFuture<TransactionInPendingAckStats>> pendingAckStatsMap = new HashMap<>();
pendingAckStatsMap.put(transactionSubscription.getSubscription(), future);
ackFutures.put(topic, pendingAckStatsMap);
}
});
List<CompletableFuture<TransactionInBufferStats>> producedPartitionsFutures = new ArrayList<>();
Map<String, CompletableFuture<TransactionInBufferStats>> produceFutures = new HashMap<>();
txnMeta.producedPartitions().forEach(topic -> {
CompletableFuture<TransactionInBufferStats> future = transactions.getTransactionInBufferStatsAsync(txnID, topic);
producedPartitionsFutures.add(future);
produceFutures.put(topic, future);
});
FutureUtil.waitForAll(ackedPartitionsFutures).whenComplete((v, e) -> {
if (e != null) {
transactionMetadataFuture.completeExceptionally(e);
return;
}
FutureUtil.waitForAll(producedPartitionsFutures).whenComplete((x, t) -> {
if (t != null) {
transactionMetadataFuture.completeExceptionally(e);
return;
}
Map<String, Map<String, TransactionInPendingAckStats>> ackedPartitions = new HashMap<>();
Map<String, TransactionInBufferStats> producedPartitions = new HashMap<>();
for (String topic : ackFutures.keySet()) {
Map<String, TransactionInPendingAckStats> subs = new HashMap<>();
for (String sub : ackFutures.get(topic).keySet()) {
try {
subs.put(sub, ackFutures.get(topic).get(sub).get());
} catch (Exception exception) {
transactionMetadataFuture.completeExceptionally(exception);
return;
}
}
ackedPartitions.put(topic, subs);
}
for (String topic : produceFutures.keySet()) {
try {
producedPartitions.put(topic, produceFutures.get(topic).get());
} catch (Exception exception) {
transactionMetadataFuture.completeExceptionally(exception);
return;
}
}
transactionMetadata.ackedPartitions = ackedPartitions;
transactionMetadata.producedPartitions = producedPartitions;
transactionMetadataFuture.complete(transactionMetadata);
});
});
}
use of org.apache.pulsar.common.policies.data.TransactionMetadata in project pulsar by yahoo.
the class TransactionsImpl method getSlowTransactionsByCoordinatorIdAsync.
@Override
public CompletableFuture<Map<String, TransactionMetadata>> getSlowTransactionsByCoordinatorIdAsync(Integer coordinatorId, long timeout, TimeUnit timeUnit) {
WebTarget path = adminV3Transactions.path("slowTransactions");
path = path.path(timeUnit.toMillis(timeout) + "");
if (coordinatorId != null) {
path = path.queryParam("coordinatorId", coordinatorId);
}
final CompletableFuture<Map<String, TransactionMetadata>> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<Map<String, TransactionMetadata>>() {
@Override
public void completed(Map<String, TransactionMetadata> metadataMap) {
future.complete(metadataMap);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
Aggregations