Search in sources :

Example 1 with TransactionInPendingAckStats

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);
        });
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) RestException(org.apache.pulsar.broker.web.RestException) CoordinatorNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) TransactionNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) TransactionMetadata(org.apache.pulsar.common.policies.data.TransactionMetadata) Transactions(org.apache.pulsar.client.admin.Transactions) TxnID(org.apache.pulsar.client.api.transaction.TxnID) CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) Map(java.util.Map) TransactionInPendingAckStats(org.apache.pulsar.common.policies.data.TransactionInPendingAckStats) TransactionInBufferStats(org.apache.pulsar.common.policies.data.TransactionInBufferStats)

Example 2 with TransactionInPendingAckStats

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) WebTarget(javax.ws.rs.client.WebTarget) TransactionInPendingAckStats(org.apache.pulsar.common.policies.data.TransactionInPendingAckStats)

Example 3 with TransactionInPendingAckStats

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;
}
Also used : BitSetRecyclable(org.apache.pulsar.common.util.collections.BitSetRecyclable) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) TransactionInPendingAckStats(org.apache.pulsar.common.policies.data.TransactionInPendingAckStats)

Example 4 with 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());
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) TransactionInPendingAckStats(org.apache.pulsar.common.policies.data.TransactionInPendingAckStats) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

TransactionInPendingAckStats (org.apache.pulsar.common.policies.data.TransactionInPendingAckStats)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 TxnID (org.apache.pulsar.client.api.transaction.TxnID)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 WebTarget (javax.ws.rs.client.WebTarget)1 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)1 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)1 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)1 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)1 ServiceUnitNotReadyException (org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)1 SubscriptionNotFoundException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException)1 RestException (org.apache.pulsar.broker.web.RestException)1 Transactions (org.apache.pulsar.client.admin.Transactions)1 BatchMessageIdImpl (org.apache.pulsar.client.impl.BatchMessageIdImpl)1 TransactionImpl (org.apache.pulsar.client.impl.transaction.TransactionImpl)1 TransactionInBufferStats (org.apache.pulsar.common.policies.data.TransactionInBufferStats)1 TransactionMetadata (org.apache.pulsar.common.policies.data.TransactionMetadata)1