Search in sources :

Example 1 with Transactions

use of org.apache.pulsar.client.admin.Transactions 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 Transactions

use of org.apache.pulsar.client.admin.Transactions 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 Transactions

use of org.apache.pulsar.client.admin.Transactions in project pulsar by apache.

the class TransactionsBase method internalGetSlowTransactions.

protected void internalGetSlowTransactions(AsyncResponse asyncResponse, boolean authoritative, long timeout, Integer coordinatorId) {
    try {
        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;
                }
                List<TxnMeta> transactions = transactionMetadataStore.getSlowTransactions(timeout);
                List<CompletableFuture<TransactionMetadata>> completableFutures = new ArrayList<>();
                for (TxnMeta txnMeta : transactions) {
                    CompletableFuture<TransactionMetadata> completableFuture = new CompletableFuture<>();
                    getTransactionMetadata(txnMeta, completableFuture);
                    completableFutures.add(completableFuture);
                }
                FutureUtil.waitForAll(completableFutures).whenComplete((v, e) -> {
                    if (e != null) {
                        asyncResponse.resume(new RestException(e.getCause()));
                        return;
                    }
                    Map<String, TransactionMetadata> transactionMetadata = new HashMap<>();
                    for (CompletableFuture<TransactionMetadata> future : completableFutures) {
                        try {
                            transactionMetadata.put(future.get().txnId, future.get());
                        } catch (Exception exception) {
                            asyncResponse.resume(new RestException(exception.getCause()));
                            return;
                        }
                    }
                    asyncResponse.resume(transactionMetadata);
                });
            } 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<Map<String, TransactionMetadata>>> completableFutures = Lists.newArrayList();
                    for (int i = 0; i < partitionMetadata.partitions; i++) {
                        try {
                            completableFutures.add(pulsar().getAdminClient().transactions().getSlowTransactionsByCoordinatorIdAsync(i, timeout, TimeUnit.MILLISECONDS));
                        } catch (PulsarServerException e) {
                            asyncResponse.resume(new RestException(e));
                            return;
                        }
                    }
                    Map<String, TransactionMetadata> transactionMetadataMaps = new HashMap<>();
                    FutureUtil.waitForAll(completableFutures).whenComplete((result, e) -> {
                        if (e != null) {
                            asyncResponse.resume(new RestException(e));
                            return;
                        }
                        for (CompletableFuture<Map<String, TransactionMetadata>> transactionMetadataMap : completableFutures) {
                            try {
                                transactionMetadataMaps.putAll(transactionMetadataMap.get());
                            } catch (Exception exception) {
                                asyncResponse.resume(new RestException(exception.getCause()));
                                return;
                            }
                        }
                        asyncResponse.resume(transactionMetadataMaps);
                    });
                }).exceptionally(ex -> {
                    log.error("[{}] Failed to get transaction coordinator state.", clientAppId(), ex);
                    resumeAsyncResponseExceptionally(asyncResponse, ex);
                    return null;
                });
            }
        } else {
            asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "Broker don't support transaction!"));
        }
    } catch (Exception e) {
        asyncResponse.resume(new RestException(e));
    }
}
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) HashMap(java.util.HashMap) RestException(org.apache.pulsar.broker.web.RestException) ArrayList(java.util.ArrayList) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) 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) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Transactions

use of org.apache.pulsar.client.admin.Transactions in project pulsar by yahoo.

the class PulsarAdminToolTest method transactions.

@Test
void transactions() throws Exception {
    PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
    Transactions transactions = Mockito.mock(Transactions.class);
    doReturn(transactions).when(admin).transactions();
    CmdTransactions cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("coordinator-stats -c 1"));
    verify(transactions).getCoordinatorStatsById(1);
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("coordinator-stats"));
    verify(transactions).getCoordinatorStats();
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("coordinator-internal-stats -c 1 -m"));
    verify(transactions).getCoordinatorInternalStats(1, true);
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("transaction-in-buffer-stats -m 1 -t test -l 2"));
    verify(transactions).getTransactionInBufferStats(new TxnID(1, 2), "test");
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("transaction-in-pending-ack-stats -m 1 -l 2 -t test -s test"));
    verify(transactions).getTransactionInPendingAckStats(new TxnID(1, 2), "test", "test");
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("transaction-metadata -m 1 -l 2"));
    verify(transactions).getTransactionMetadata(new TxnID(1, 2));
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("slow-transactions -c 1 -t 1h"));
    verify(transactions).getSlowTransactionsByCoordinatorId(1, 3600000, TimeUnit.MILLISECONDS);
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("slow-transactions -t 1h"));
    verify(transactions).getSlowTransactions(3600000, TimeUnit.MILLISECONDS);
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("transaction-buffer-stats -t test"));
    verify(transactions).getTransactionBufferStats("test");
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("pending-ack-stats -t test -s test"));
    verify(transactions).getPendingAckStats("test", "test");
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("pending-ack-internal-stats -t test -s test"));
    verify(transactions).getPendingAckInternalStats("test", "test", false);
    cmdTransactions = new CmdTransactions(() -> admin);
    cmdTransactions.run(split("scale-transactionCoordinators -r 3"));
    verify(transactions).scaleTransactionCoordinators(3);
}
Also used : Transactions(org.apache.pulsar.client.admin.Transactions) TxnID(org.apache.pulsar.client.api.transaction.TxnID) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Test(org.testng.annotations.Test)

Example 5 with Transactions

use of org.apache.pulsar.client.admin.Transactions 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)

Aggregations

Transactions (org.apache.pulsar.client.admin.Transactions)12 TxnID (org.apache.pulsar.client.api.transaction.TxnID)12 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 ExecutionException (java.util.concurrent.ExecutionException)9 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)9 RestException (org.apache.pulsar.broker.web.RestException)9 TransactionInBufferStats (org.apache.pulsar.common.policies.data.TransactionInBufferStats)9 TransactionInPendingAckStats (org.apache.pulsar.common.policies.data.TransactionInPendingAckStats)9 TransactionMetadata (org.apache.pulsar.common.policies.data.TransactionMetadata)9 Lists (com.google.common.collect.Lists)6 List (java.util.List)6 Optional (java.util.Optional)6 TimeUnit (java.util.concurrent.TimeUnit)6 AsyncResponse (javax.ws.rs.container.AsyncResponse)6 Response (javax.ws.rs.core.Response)6 METHOD_NOT_ALLOWED (javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED)6 NOT_FOUND (javax.ws.rs.core.Response.Status.NOT_FOUND)6