Search in sources :

Example 1 with TransactionCoordinatorStats

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

the class TransactionsBase method internalGetCoordinatorStats.

protected void internalGetCoordinatorStats(AsyncResponse asyncResponse, boolean authoritative, Integer coordinatorId) {
    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;
            }
            asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
        } 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<TransactionCoordinatorStats>> transactionMetadataStoreInfoFutures = Lists.newArrayList();
                for (int i = 0; i < partitionMetadata.partitions; i++) {
                    try {
                        transactionMetadataStoreInfoFutures.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
                    } catch (PulsarServerException e) {
                        asyncResponse.resume(new RestException(e));
                        return;
                    }
                }
                Map<Integer, TransactionCoordinatorStats> stats = new HashMap<>();
                FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result, e) -> {
                    if (e != null) {
                        asyncResponse.resume(new RestException(e));
                        return;
                    }
                    for (int i = 0; i < transactionMetadataStoreInfoFutures.size(); i++) {
                        try {
                            stats.put(i, transactionMetadataStoreInfoFutures.get(i).get());
                        } catch (Exception exception) {
                            asyncResponse.resume(new RestException(exception.getCause()));
                            return;
                        }
                    }
                    asyncResponse.resume(stats);
                });
            }).exceptionally(ex -> {
                log.error("[{}] Failed to get transaction coordinator state.", clientAppId(), ex);
                resumeAsyncResponseExceptionally(asyncResponse, ex);
                return null;
            });
        }
    } else {
        asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "This Broker is not configured with transactionCoordinatorEnabled=true."));
    }
}
Also used : ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) 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) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) 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) 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) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) TransactionMetadata(org.apache.pulsar.common.policies.data.TransactionMetadata) TEMPORARY_REDIRECT(javax.ws.rs.core.Response.Status.TEMPORARY_REDIRECT) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) AsyncResponse(javax.ws.rs.container.AsyncResponse) Transactions(org.apache.pulsar.client.admin.Transactions) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) 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) 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) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) RestException(org.apache.pulsar.broker.web.RestException) ArrayList(java.util.ArrayList) List(java.util.List) TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) HashMap(java.util.HashMap) Map(java.util.Map) 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)

Example 2 with TransactionCoordinatorStats

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

the class AdminApiTransactionTest method testGetTransactionCoordinatorStats.

@Test(timeOut = 20000)
public void testGetTransactionCoordinatorStats() throws Exception {
    initTransaction(2);
    getTransaction().commit().get();
    getTransaction().abort().get();
    TransactionCoordinatorStats transactionCoordinatorstats = admin.transactions().getCoordinatorStatsByIdAsync(1).get();
    verifyCoordinatorStats(transactionCoordinatorstats.state, transactionCoordinatorstats.leastSigBits, transactionCoordinatorstats.lowWaterMark);
    transactionCoordinatorstats = admin.transactions().getCoordinatorStatsByIdAsync(0).get();
    verifyCoordinatorStats(transactionCoordinatorstats.state, transactionCoordinatorstats.leastSigBits, transactionCoordinatorstats.lowWaterMark);
    Map<Integer, TransactionCoordinatorStats> stats = admin.transactions().getCoordinatorStatsAsync().get();
    assertEquals(stats.size(), 2);
    transactionCoordinatorstats = stats.get(0);
    verifyCoordinatorStats(transactionCoordinatorstats.state, transactionCoordinatorstats.leastSigBits, transactionCoordinatorstats.lowWaterMark);
    transactionCoordinatorstats = stats.get(1);
    verifyCoordinatorStats(transactionCoordinatorstats.state, transactionCoordinatorstats.leastSigBits, transactionCoordinatorstats.lowWaterMark);
}
Also used : TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with TransactionCoordinatorStats

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

the class TransactionsBase method internalGetCoordinatorStats.

protected void internalGetCoordinatorStats(AsyncResponse asyncResponse, boolean authoritative, Integer coordinatorId) {
    if (coordinatorId != null) {
        validateTopicOwnership(SystemTopicNames.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;
        }
        asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
    } else {
        getPartitionedTopicMetadataAsync(SystemTopicNames.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<TransactionCoordinatorStats>> transactionMetadataStoreInfoFutures = Lists.newArrayList();
            for (int i = 0; i < partitionMetadata.partitions; i++) {
                try {
                    transactionMetadataStoreInfoFutures.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
                } catch (PulsarServerException e) {
                    asyncResponse.resume(new RestException(e));
                    return;
                }
            }
            Map<Integer, TransactionCoordinatorStats> stats = new HashMap<>();
            FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result, e) -> {
                if (e != null) {
                    asyncResponse.resume(new RestException(e));
                    return;
                }
                for (int i = 0; i < transactionMetadataStoreInfoFutures.size(); i++) {
                    try {
                        stats.put(i, transactionMetadataStoreInfoFutures.get(i).get());
                    } catch (Exception exception) {
                        asyncResponse.resume(new RestException(exception.getCause()));
                        return;
                    }
                }
                asyncResponse.resume(stats);
            });
        }).exceptionally(ex -> {
            log.error("[{}] Failed to get transaction coordinator state.", clientAppId(), ex);
            resumeAsyncResponseExceptionally(asyncResponse, ex);
            return null;
        });
    }
}
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) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) RestException(org.apache.pulsar.broker.web.RestException) ArrayList(java.util.ArrayList) List(java.util.List) TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) HashMap(java.util.HashMap) Map(java.util.Map) 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)

Example 4 with TransactionCoordinatorStats

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

the class TransactionsImpl method getCoordinatorStatsByIdAsync.

@Override
public CompletableFuture<TransactionCoordinatorStats> getCoordinatorStatsByIdAsync(int coordinatorId) {
    WebTarget path = adminV3Transactions.path("coordinatorStats");
    path = path.queryParam("coordinatorId", coordinatorId);
    final CompletableFuture<TransactionCoordinatorStats> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<TransactionCoordinatorStats>() {

        @Override
        public void completed(TransactionCoordinatorStats stats) {
            future.complete(stats);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) WebTarget(javax.ws.rs.client.WebTarget)

Example 5 with TransactionCoordinatorStats

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

the class MLTransactionMetadataStore method getCoordinatorStats.

@Override
public TransactionCoordinatorStats getCoordinatorStats() {
    TransactionCoordinatorStats transactionCoordinatorstats = new TransactionCoordinatorStats();
    transactionCoordinatorstats.setLowWaterMark(getLowWaterMark());
    transactionCoordinatorstats.setState(getState().name());
    transactionCoordinatorstats.setLeastSigBits(sequenceIdGenerator.getCurrentSequenceId());
    return transactionCoordinatorstats;
}
Also used : TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats)

Aggregations

TransactionCoordinatorStats (org.apache.pulsar.common.policies.data.TransactionCoordinatorStats)15 CompletableFuture (java.util.concurrent.CompletableFuture)9 Map (java.util.Map)6 WebTarget (javax.ws.rs.client.WebTarget)5 Lists (com.google.common.collect.Lists)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3 AsyncResponse (javax.ws.rs.container.AsyncResponse)3 Response (javax.ws.rs.core.Response)3 METHOD_NOT_ALLOWED (javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED)3 NOT_FOUND (javax.ws.rs.core.Response.Status.NOT_FOUND)3 SERVICE_UNAVAILABLE (javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE)3 Slf4j (lombok.extern.slf4j.Slf4j)3 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)3 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 AdminResource (org.apache.pulsar.broker.admin.AdminResource)3