Search in sources :

Example 1 with TransactionInBufferStats

use of org.apache.pulsar.common.policies.data.TransactionInBufferStats in project pulsar by apache.

the class AdminApiTransactionTest method testGetTransactionInBufferStats.

@Test(timeOut = 20000)
public void testGetTransactionInBufferStats() throws Exception {
    initTransaction(2);
    TransactionImpl transaction = (TransactionImpl) getTransaction();
    final String topic = "persistent://public/default/testGetTransactionInBufferStats";
    admin.topics().createNonPartitionedTopic(topic);
    Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES).topic(topic).sendTimeout(0, TimeUnit.SECONDS).create();
    MessageId messageId = producer.newMessage(transaction).value("Hello pulsar!".getBytes()).send();
    TransactionInBufferStats transactionInBufferStats = admin.transactions().getTransactionInBufferStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic).get();
    PositionImpl position = PositionImpl.get(((MessageIdImpl) messageId).getLedgerId(), ((MessageIdImpl) messageId).getEntryId());
    assertEquals(transactionInBufferStats.startPosition, position.toString());
    assertFalse(transactionInBufferStats.aborted);
    transaction.abort().get();
    transactionInBufferStats = admin.transactions().getTransactionInBufferStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic).get();
    assertNull(transactionInBufferStats.startPosition);
    assertTrue(transactionInBufferStats.aborted);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) TransactionInBufferStats(org.apache.pulsar.common.policies.data.TransactionInBufferStats) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with TransactionInBufferStats

use of org.apache.pulsar.common.policies.data.TransactionInBufferStats 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 3 with TransactionInBufferStats

use of org.apache.pulsar.common.policies.data.TransactionInBufferStats in project pulsar by yahoo.

the class AdminApiTransactionTest method testGetTransactionInBufferStats.

@Test(timeOut = 20000)
public void testGetTransactionInBufferStats() throws Exception {
    initTransaction(2);
    TransactionImpl transaction = (TransactionImpl) getTransaction();
    final String topic = "persistent://public/default/testGetTransactionInBufferStats";
    try {
        admin.transactions().getTransactionInBufferStatsAsync(new TxnID(1, 1), topic).get();
        fail("Should failed here");
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof PulsarAdminException.NotFoundException);
        PulsarAdminException.NotFoundException cause = (PulsarAdminException.NotFoundException) ex.getCause();
        assertEquals(cause.getMessage(), "Topic not found");
    }
    try {
        pulsar.getBrokerService().getTopic(topic, false);
        admin.transactions().getTransactionInBufferStatsAsync(new TxnID(1, 1), topic).get();
        fail("Should failed here");
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof PulsarAdminException.NotFoundException);
        PulsarAdminException.NotFoundException cause = (PulsarAdminException.NotFoundException) ex.getCause();
        assertEquals(cause.getMessage(), "Topic not found");
    }
    admin.topics().createNonPartitionedTopic(topic);
    Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES).topic(topic).sendTimeout(0, TimeUnit.SECONDS).create();
    MessageId messageId = producer.newMessage(transaction).value("Hello pulsar!".getBytes()).send();
    TransactionInBufferStats transactionInBufferStats = admin.transactions().getTransactionInBufferStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic).get();
    PositionImpl position = PositionImpl.get(((MessageIdImpl) messageId).getLedgerId(), ((MessageIdImpl) messageId).getEntryId());
    assertEquals(transactionInBufferStats.startPosition, position.toString());
    assertFalse(transactionInBufferStats.aborted);
    transaction.abort().get();
    transactionInBufferStats = admin.transactions().getTransactionInBufferStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic).get();
    assertNull(transactionInBufferStats.startPosition);
    assertTrue(transactionInBufferStats.aborted);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) TransactionInBufferStats(org.apache.pulsar.common.policies.data.TransactionInBufferStats) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with TransactionInBufferStats

use of org.apache.pulsar.common.policies.data.TransactionInBufferStats 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);
        });
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestException(org.apache.pulsar.broker.web.RestException) CoordinatorNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException) 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 5 with TransactionInBufferStats

use of org.apache.pulsar.common.policies.data.TransactionInBufferStats in project pulsar by yahoo.

the class TransactionsImpl method getTransactionInBufferStatsAsync.

@Override
public CompletableFuture<TransactionInBufferStats> getTransactionInBufferStatsAsync(TxnID txnID, String topic) {
    TopicName topicName = TopicName.get(topic);
    WebTarget path = adminV3Transactions.path("transactionInBufferStats");
    path = path.path(topicName.getRestPath(false));
    path = path.path(txnID.getMostSigBits() + "");
    path = path.path(txnID.getLeastSigBits() + "");
    final CompletableFuture<TransactionInBufferStats> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<TransactionInBufferStats>() {

        @Override
        public void completed(TransactionInBufferStats 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) TransactionInBufferStats(org.apache.pulsar.common.policies.data.TransactionInBufferStats) TopicName(org.apache.pulsar.common.naming.TopicName)

Aggregations

TransactionInBufferStats (org.apache.pulsar.common.policies.data.TransactionInBufferStats)16 TxnID (org.apache.pulsar.client.api.transaction.TxnID)10 Map (java.util.Map)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)6 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)6 MessageId (org.apache.pulsar.client.api.MessageId)6 TransactionImpl (org.apache.pulsar.client.impl.transaction.TransactionImpl)6 TransactionMetadata (org.apache.pulsar.common.policies.data.TransactionMetadata)6 Test (org.testng.annotations.Test)6 ExecutionException (java.util.concurrent.ExecutionException)5 RestException (org.apache.pulsar.broker.web.RestException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 WebTarget (javax.ws.rs.client.WebTarget)3 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 Transactions (org.apache.pulsar.client.admin.Transactions)3 TopicName (org.apache.pulsar.common.naming.TopicName)3 TransactionInPendingAckStats (org.apache.pulsar.common.policies.data.TransactionInPendingAckStats)3 CoordinatorNotFoundException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException)3