use of org.apache.pulsar.common.policies.data.TransactionInPendingAckStats 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.TransactionInPendingAckStats in project pulsar by apache.
the class TransactionsImpl method getTransactionInPendingAckStatsAsync.
@Override
public CompletableFuture<TransactionInPendingAckStats> getTransactionInPendingAckStatsAsync(TxnID txnID, String topic, String subName) {
WebTarget path = adminV3Transactions.path("transactionInPendingAckStats");
path = path.path(TopicName.get(topic).getRestPath(false));
path = path.path(subName);
path = path.path(txnID.getMostSigBits() + "");
path = path.path(txnID.getLeastSigBits() + "");
final CompletableFuture<TransactionInPendingAckStats> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<TransactionInPendingAckStats>() {
@Override
public void completed(TransactionInPendingAckStats stats) {
future.complete(stats);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.TransactionInPendingAckStats in project pulsar by apache.
the class PendingAckHandleImpl method getTransactionInPendingAckStats.
@Override
public TransactionInPendingAckStats getTransactionInPendingAckStats(TxnID txnID) {
TransactionInPendingAckStats transactionInPendingAckStats = new TransactionInPendingAckStats();
if (cumulativeAckOfTransaction != null && cumulativeAckOfTransaction.getLeft().equals(txnID)) {
PositionImpl position = cumulativeAckOfTransaction.getRight();
StringBuilder stringBuilder = new StringBuilder().append(position.getLedgerId()).append(':').append(position.getEntryId());
if (cumulativeAckOfTransaction.getRight().hasAckSet()) {
BitSetRecyclable bitSetRecyclable = BitSetRecyclable.valueOf(cumulativeAckOfTransaction.getRight().getAckSet());
if (!bitSetRecyclable.isEmpty()) {
stringBuilder.append(":").append(bitSetRecyclable.nextSetBit(0) - 1);
}
}
transactionInPendingAckStats.cumulativeAckPosition = stringBuilder.toString();
}
return transactionInPendingAckStats;
}
use of org.apache.pulsar.common.policies.data.TransactionInPendingAckStats in project pulsar by apache.
the class AdminApiTransactionTest method testGetTransactionPendingAckStats.
@Test(timeOut = 20000)
public void testGetTransactionPendingAckStats() throws Exception {
initTransaction(2);
final String topic = "persistent://public/default/testGetTransactionInBufferStats";
final String subName = "test";
admin.topics().createNonPartitionedTopic(topic);
Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES).topic(topic).create();
Consumer<byte[]> consumer = pulsarClient.newConsumer(Schema.BYTES).topic(topic).subscriptionName(subName).subscribe();
producer.sendAsync("Hello pulsar!".getBytes());
producer.sendAsync("Hello pulsar!".getBytes());
producer.sendAsync("Hello pulsar!".getBytes());
producer.sendAsync("Hello pulsar!".getBytes());
TransactionImpl transaction = (TransactionImpl) getTransaction();
TransactionInPendingAckStats transactionInPendingAckStats = admin.transactions().getTransactionInPendingAckStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic, subName).get();
assertNull(transactionInPendingAckStats.cumulativeAckPosition);
consumer.receive();
consumer.receive();
Message<byte[]> message = consumer.receive();
BatchMessageIdImpl batchMessageId = (BatchMessageIdImpl) message.getMessageId();
consumer.acknowledgeCumulativeAsync(batchMessageId, transaction).get();
transactionInPendingAckStats = admin.transactions().getTransactionInPendingAckStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic, subName).get();
assertEquals(transactionInPendingAckStats.cumulativeAckPosition, String.valueOf(batchMessageId.getLedgerId()) + ':' + batchMessageId.getEntryId() + ':' + batchMessageId.getBatchIndex());
}
Aggregations