Search in sources :

Example 1 with TransactionPendingAckInternalStats

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

the class TransactionsBase method internalGetPendingAckInternalStats.

protected void internalGetPendingAckInternalStats(AsyncResponse asyncResponse, boolean authoritative, TopicName topicName, String subName, boolean metadata) {
    try {
        if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
            validateTopicOwnership(topicName, authoritative);
            CompletableFuture<Optional<Topic>> topicFuture = pulsar().getBrokerService().getTopics().get(topicName.toString());
            if (topicFuture != null) {
                topicFuture.whenComplete((optionalTopic, e) -> {
                    if (e != null) {
                        asyncResponse.resume(new RestException(e));
                        return;
                    }
                    if (!optionalTopic.isPresent()) {
                        asyncResponse.resume(new RestException(TEMPORARY_REDIRECT, "Topic is not owned by this broker!"));
                        return;
                    }
                    Topic topicObject = optionalTopic.get();
                    if (topicObject instanceof PersistentTopic) {
                        try {
                            ManagedLedger managedLedger = ((PersistentTopic) topicObject).getPendingAckManagedLedger(subName).get();
                            TransactionPendingAckInternalStats stats = new TransactionPendingAckInternalStats();
                            TransactionLogStats pendingAckLogStats = new TransactionLogStats();
                            pendingAckLogStats.managedLedgerName = managedLedger.getName();
                            pendingAckLogStats.managedLedgerInternalStats = managedLedger.getManagedLedgerInternalStats(metadata).get();
                            stats.pendingAckLogStats = pendingAckLogStats;
                            asyncResponse.resume(stats);
                        } catch (Exception exception) {
                            if (exception instanceof ExecutionException) {
                                if (exception.getCause() instanceof ServiceUnitNotReadyException) {
                                    asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, exception.getCause()));
                                    return;
                                } else if (exception.getCause() instanceof NotAllowedException) {
                                    asyncResponse.resume(new RestException(METHOD_NOT_ALLOWED, exception.getCause()));
                                    return;
                                } else if (exception.getCause() instanceof SubscriptionNotFoundException) {
                                    asyncResponse.resume(new RestException(NOT_FOUND, exception.getCause()));
                                    return;
                                }
                            }
                            asyncResponse.resume(new RestException(exception));
                        }
                    } else {
                        asyncResponse.resume(new RestException(BAD_REQUEST, "Topic is not a persistent topic!"));
                    }
                });
            } else {
                asyncResponse.resume(new RestException(TEMPORARY_REDIRECT, "Topic is not owned by this broker!"));
            }
        } else {
            asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "This Broker is not configured with transactionCoordinatorEnabled=true."));
        }
    } catch (Exception e) {
        asyncResponse.resume(new RestException(e.getCause()));
    }
}
Also used : Optional(java.util.Optional) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) RestException(org.apache.pulsar.broker.web.RestException) TransactionLogStats(org.apache.pulsar.common.policies.data.TransactionLogStats) TransactionPendingAckInternalStats(org.apache.pulsar.common.policies.data.TransactionPendingAckInternalStats) 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) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with TransactionPendingAckInternalStats

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

the class TransactionsImpl method getPendingAckInternalStatsAsync.

@Override
public CompletableFuture<TransactionPendingAckInternalStats> getPendingAckInternalStatsAsync(String topic, String subName, boolean metadata) {
    TopicName tn = TopicName.get(topic);
    WebTarget path = adminV3Transactions.path("pendingAckInternalStats");
    path = path.path(tn.getRestPath(false));
    path = path.path(subName);
    path = path.queryParam("metadata", metadata);
    final CompletableFuture<TransactionPendingAckInternalStats> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<TransactionPendingAckInternalStats>() {

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

Example 3 with TransactionPendingAckInternalStats

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

the class TransactionsBase method internalGetPendingAckInternalStats.

protected CompletableFuture<TransactionPendingAckInternalStats> internalGetPendingAckInternalStats(boolean authoritative, String subName, boolean metadata) {
    return getExistingPersistentTopicAsync(authoritative).thenCompose(topic -> topic.getPendingAckManagedLedger(subName)).thenCompose(managedLedger -> managedLedger.getManagedLedgerInternalStats(metadata).thenApply(internalStats -> {
        TransactionLogStats pendingAckLogStats = new TransactionLogStats();
        pendingAckLogStats.managedLedgerName = managedLedger.getName();
        pendingAckLogStats.managedLedgerInternalStats = internalStats;
        return pendingAckLogStats;
    }).thenApply(pendingAckLogStats -> {
        TransactionPendingAckInternalStats stats = new TransactionPendingAckInternalStats();
        stats.pendingAckLogStats = pendingAckLogStats;
        return stats;
    }));
}
Also used : TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) TopicName(org.apache.pulsar.common.naming.TopicName) Topic(org.apache.pulsar.broker.service.Topic) TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) AdminResource(org.apache.pulsar.broker.admin.AdminResource) TransactionLogStats(org.apache.pulsar.common.policies.data.TransactionLogStats) METHOD_NOT_ALLOWED(javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) SystemTopicNames(org.apache.pulsar.common.naming.SystemTopicNames) ArrayList(java.util.ArrayList) TxnID(org.apache.pulsar.client.api.transaction.TxnID) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) Lists(com.google.common.collect.Lists) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) TransactionInBufferStats(org.apache.pulsar.common.policies.data.TransactionInBufferStats) CoordinatorNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException) SERVICE_UNAVAILABLE(javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE) TransactionMetadata(org.apache.pulsar.common.policies.data.TransactionMetadata) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) AsyncResponse(javax.ws.rs.container.AsyncResponse) Transactions(org.apache.pulsar.client.admin.Transactions) TransactionBufferStats(org.apache.pulsar.common.policies.data.TransactionBufferStats) TransactionNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) TransactionCoordinatorInternalStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorInternalStats) Response(javax.ws.rs.core.Response) TransactionPendingAckInternalStats(org.apache.pulsar.common.policies.data.TransactionPendingAckInternalStats) TransactionPendingAckStats(org.apache.pulsar.common.policies.data.TransactionPendingAckStats) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Codec(org.apache.pulsar.common.util.Codec) Optional(java.util.Optional) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) TransactionInPendingAckStats(org.apache.pulsar.common.policies.data.TransactionInPendingAckStats) TransactionLogStats(org.apache.pulsar.common.policies.data.TransactionLogStats) TransactionPendingAckInternalStats(org.apache.pulsar.common.policies.data.TransactionPendingAckInternalStats)

Example 4 with TransactionPendingAckInternalStats

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

the class TransactionsImpl method getPendingAckInternalStatsAsync.

@Override
public CompletableFuture<TransactionPendingAckInternalStats> getPendingAckInternalStatsAsync(String topic, String subName, boolean metadata) {
    TopicName tn = TopicName.get(topic);
    WebTarget path = adminV3Transactions.path("pendingAckInternalStats");
    path = path.path(tn.getRestPath(false));
    path = path.path(subName);
    path = path.queryParam("metadata", metadata);
    final CompletableFuture<TransactionPendingAckInternalStats> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<TransactionPendingAckInternalStats>() {

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

Example 5 with TransactionPendingAckInternalStats

use of org.apache.pulsar.common.policies.data.TransactionPendingAckInternalStats in project incubator-pulsar by apache.

the class AdminApiTransactionTest method testGetPendingAckInternalStats.

@Test(timeOut = 20000)
public void testGetPendingAckInternalStats() throws Exception {
    initTransaction(1);
    TransactionImpl transaction = (TransactionImpl) getTransaction();
    final String topic = "persistent://public/default/testGetPendingAckInternalStats";
    final String subName = "test";
    try {
        admin.transactions().getPendingAckInternalStatsAsync(topic, subName, true).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().getPendingAckInternalStatsAsync(topic, subName, true).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).create();
    Consumer<byte[]> consumer = pulsarClient.newConsumer(Schema.BYTES).topic(topic).subscriptionName(subName).subscribe();
    MessageId messageId = producer.send("Hello pulsar!".getBytes());
    consumer.acknowledgeAsync(messageId, transaction).get();
    TransactionPendingAckInternalStats stats = admin.transactions().getPendingAckInternalStatsAsync(topic, subName, true).get();
    ManagedLedgerInternalStats managedLedgerInternalStats = stats.pendingAckLogStats.managedLedgerInternalStats;
    assertEquals(TopicName.get(TopicDomain.persistent.toString(), "public", "default", "testGetPendingAckInternalStats" + "-" + subName + SystemTopicNames.PENDING_ACK_STORE_SUFFIX).getPersistenceNamingEncoding(), stats.pendingAckLogStats.managedLedgerName);
    verifyManagedLegerInternalStats(managedLedgerInternalStats, 16);
    ManagedLedgerInternalStats finalManagedLedgerInternalStats = managedLedgerInternalStats;
    managedLedgerInternalStats.cursors.forEach((s, cursorStats) -> {
        assertEquals(s, SystemTopicNames.PENDING_ACK_STORE_CURSOR_NAME);
        assertEquals(cursorStats.readPosition, finalManagedLedgerInternalStats.lastConfirmedEntry);
    });
    stats = admin.transactions().getPendingAckInternalStatsAsync(topic, subName, false).get();
    managedLedgerInternalStats = stats.pendingAckLogStats.managedLedgerInternalStats;
    assertEquals(TopicName.get(TopicDomain.persistent.toString(), "public", "default", "testGetPendingAckInternalStats" + "-" + subName + SystemTopicNames.PENDING_ACK_STORE_SUFFIX).getPersistenceNamingEncoding(), stats.pendingAckLogStats.managedLedgerName);
    assertNull(managedLedgerInternalStats.ledgers.get(0).metadata);
}
Also used : ManagedLedgerInternalStats(org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) TransactionPendingAckInternalStats(org.apache.pulsar.common.policies.data.TransactionPendingAckInternalStats) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

TransactionPendingAckInternalStats (org.apache.pulsar.common.policies.data.TransactionPendingAckInternalStats)9 CompletableFuture (java.util.concurrent.CompletableFuture)5 ExecutionException (java.util.concurrent.ExecutionException)5 TopicName (org.apache.pulsar.common.naming.TopicName)5 Optional (java.util.Optional)3 WebTarget (javax.ws.rs.client.WebTarget)3 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)3 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)3 Topic (org.apache.pulsar.broker.service.Topic)3 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)3 RestException (org.apache.pulsar.broker.web.RestException)3 MessageId (org.apache.pulsar.client.api.MessageId)3 TransactionImpl (org.apache.pulsar.client.impl.transaction.TransactionImpl)3 ManagedLedgerInternalStats (org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats)3 Test (org.testng.annotations.Test)3 Lists (com.google.common.collect.Lists)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2