Search in sources :

Example 1 with TransactionSubscription

use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by apache.

the class TransactionMetadataStoreServiceTest method testAddAckedPartitionToTxn.

@Test
public void testAddAckedPartitionToTxn() throws Exception {
    TransactionMetadataStoreService transactionMetadataStoreService = pulsar.getTransactionMetadataStoreService();
    transactionMetadataStoreService.handleTcClientConnect(TransactionCoordinatorID.get(0)).get();
    Awaitility.await().until(() -> transactionMetadataStoreService.getStores().size() == 1);
    MLTransactionMetadataStore transactionMetadataStore = (MLTransactionMetadataStore) pulsar.getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(0));
    checkTransactionMetadataStoreReady(transactionMetadataStore);
    TxnID txnID = transactionMetadataStoreService.newTransaction(TransactionCoordinatorID.get(0), 5000).get();
    List<TransactionSubscription> partitions = new ArrayList<>();
    partitions.add(TransactionSubscription.builder().topic("ptn-1").subscription("sub-1").build());
    partitions.add(TransactionSubscription.builder().topic("ptn-2").subscription("sub-1").build());
    partitions.add(TransactionSubscription.builder().topic("ptn-3").subscription("sub-1").build());
    transactionMetadataStoreService.addAckedPartitionToTxn(txnID, partitions);
    TxnMeta txn = transactionMetadataStoreService.getTxnMeta(txnID).get();
    assertEquals(txn.status(), TxnStatus.OPEN);
    transactionMetadataStoreService.removeTransactionMetadataStore(TransactionCoordinatorID.get(0));
    Assert.assertEquals(transactionMetadataStoreService.getStores().size(), 0);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) ArrayList(java.util.ArrayList) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) TransactionMetadataStoreService(org.apache.pulsar.broker.TransactionMetadataStoreService) Test(org.testng.annotations.Test)

Example 2 with TransactionSubscription

use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by apache.

the class MLTransactionMetadataStore method subscriptionToTxnSubscription.

public static List<TransactionSubscription> subscriptionToTxnSubscription(List<Subscription> subscriptions) {
    List<TransactionSubscription> transactionSubscriptions = new ArrayList<>(subscriptions.size());
    for (Subscription subscription : subscriptions) {
        TransactionSubscription.TransactionSubscriptionBuilder transactionSubscriptionBuilder = TransactionSubscription.builder();
        transactionSubscriptionBuilder.subscription(subscription.getSubscription());
        transactionSubscriptionBuilder.topic(subscription.getTopic());
        transactionSubscriptions.add(transactionSubscriptionBuilder.build());
    }
    return transactionSubscriptions;
}
Also used : TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) ArrayList(java.util.ArrayList) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) Subscription(org.apache.pulsar.common.api.proto.Subscription)

Example 3 with TransactionSubscription

use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by yahoo.

the class TransactionEndToEndTest method oneTransactionOneTopicWithMultiSubTest.

@Test
public void oneTransactionOneTopicWithMultiSubTest() throws Exception {
    String topic = NAMESPACE1 + "/oneTransactionOneTopicWithMultiSubTest";
    final String subName1 = "test1";
    final String subName2 = "test2";
    @Cleanup Consumer<byte[]> consumer1 = pulsarClient.newConsumer().topic(topic).subscriptionName(subName1).acknowledgmentGroupTime(0, TimeUnit.MILLISECONDS).subscribe();
    Awaitility.await().until(consumer1::isConnected);
    @Cleanup Consumer<byte[]> consumer2 = pulsarClient.newConsumer().topic(topic).subscriptionName(subName2).acknowledgmentGroupTime(0, TimeUnit.MILLISECONDS).subscribe();
    Awaitility.await().until(consumer2::isConnected);
    @Cleanup Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).sendTimeout(0, TimeUnit.SECONDS).enableBatching(false).create();
    MessageId messageId = producer.send(("Hello Pulsar").getBytes(UTF_8));
    TransactionImpl txn = (TransactionImpl) getTxn();
    consumer1.acknowledgeAsync(messageId, txn).get();
    consumer2.acknowledgeAsync(messageId, txn).get();
    boolean flag = false;
    for (int i = 0; i < getPulsarServiceList().size(); i++) {
        TransactionMetadataStoreService transactionMetadataStoreService = getPulsarServiceList().get(i).getTransactionMetadataStoreService();
        if (transactionMetadataStoreService.getStores().containsKey(TransactionCoordinatorID.get(txn.getTxnIdMostBits()))) {
            List<TransactionSubscription> list = transactionMetadataStoreService.getTxnMeta(new TxnID(txn.getTxnIdMostBits(), txn.getTxnIdLeastBits())).get().ackedPartitions();
            flag = true;
            assertEquals(list.size(), 2);
            if (list.get(0).getSubscription().equals(subName1)) {
                assertEquals(list.get(1).getSubscription(), subName2);
            } else {
                assertEquals(list.get(0).getSubscription(), subName2);
                assertEquals(list.get(1).getSubscription(), subName1);
            }
        }
    }
    assertTrue(flag);
}
Also used : TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) Cleanup(lombok.Cleanup) TransactionMetadataStoreService(org.apache.pulsar.broker.TransactionMetadataStoreService) TxnID(org.apache.pulsar.client.api.transaction.TxnID) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 4 with TransactionSubscription

use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by yahoo.

the class MLTransactionMetadataStore method addAckedPartitionToTxn.

@Override
public CompletableFuture<Void> addAckedPartitionToTxn(TxnID txnID, List<TransactionSubscription> txnSubscriptions) {
    CompletableFuture<Void> promise = new CompletableFuture<>();
    internalPinnedExecutor.execute(() -> {
        if (!checkIfReady()) {
            promise.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "add acked partition"));
            return;
        }
        getTxnPositionPair(txnID).thenCompose(txnMetaListPair -> {
            TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(txnID.getMostSigBits()).setTxnidLeastBits(txnID.getLeastSigBits()).setMetadataOp(TransactionMetadataOp.ADD_SUBSCRIPTION).addAllSubscriptions(txnSubscriptionToSubscription(txnSubscriptions)).setLastModificationTime(System.currentTimeMillis()).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
            return transactionLog.append(transactionMetadataEntry).thenAccept(position -> {
                appendLogCount.increment();
                try {
                    synchronized (txnMetaListPair.getLeft()) {
                        txnMetaListPair.getLeft().addAckedPartitions(txnSubscriptions);
                        txnMetaMap.get(txnID.getLeastSigBits()).getRight().add(position);
                    }
                    promise.complete(null);
                } catch (InvalidTxnStatusException e) {
                    transactionLog.deletePosition(Collections.singletonList(position));
                    log.error("TxnID : " + txnMetaListPair.getLeft().id().toString() + " add acked subscription error with TxnStatus : " + txnMetaListPair.getLeft().status().name(), e);
                    promise.completeExceptionally(e);
                }
            });
        }).exceptionally(ex -> {
            promise.completeExceptionally(ex);
            return null;
        });
    });
    return promise;
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) TransactionLogReplayCallback(org.apache.pulsar.transaction.coordinator.TransactionLogReplayCallback) TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) ArrayList(java.util.ArrayList) TransactionTimeoutTracker(org.apache.pulsar.transaction.coordinator.TransactionTimeoutTracker) TxnID(org.apache.pulsar.client.api.transaction.TxnID) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry) Duration(java.time.Duration) TransactionRecoverTracker(org.apache.pulsar.transaction.coordinator.TransactionRecoverTracker) NoSuchElementException(java.util.NoSuchElementException) Subscription(org.apache.pulsar.common.api.proto.Subscription) ExecutorService(java.util.concurrent.ExecutorService) TransactionMetadataOp(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry.TransactionMetadataOp) Logger(org.slf4j.Logger) Position(org.apache.bookkeeper.mledger.Position) TransactionMetadataStoreState(org.apache.pulsar.transaction.coordinator.TransactionMetadataStoreState) Executors(java.util.concurrent.Executors) TransactionNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionNotFoundException) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) FutureUtil(org.apache.pulsar.common.util.FutureUtil) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry)

Example 5 with TransactionSubscription

use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by yahoo.

the class MLTransactionMetadataStore method txnSubscriptionToSubscription.

public static List<Subscription> txnSubscriptionToSubscription(List<TransactionSubscription> tnxSubscriptions) {
    List<Subscription> subscriptions = new ArrayList<>(tnxSubscriptions.size());
    for (TransactionSubscription transactionSubscription : tnxSubscriptions) {
        Subscription subscription = new Subscription().setSubscription(transactionSubscription.getSubscription()).setTopic(transactionSubscription.getTopic());
        subscriptions.add(subscription);
    }
    return subscriptions;
}
Also used : ArrayList(java.util.ArrayList) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) Subscription(org.apache.pulsar.common.api.proto.Subscription)

Aggregations

TransactionSubscription (org.apache.pulsar.transaction.coordinator.TransactionSubscription)14 ArrayList (java.util.ArrayList)11 TxnID (org.apache.pulsar.client.api.transaction.TxnID)8 Subscription (org.apache.pulsar.common.api.proto.Subscription)8 TransactionMetadataStoreService (org.apache.pulsar.broker.TransactionMetadataStoreService)6 Test (org.testng.annotations.Test)6 TxnMeta (org.apache.pulsar.transaction.coordinator.TxnMeta)5 Cleanup (lombok.Cleanup)3 MessageId (org.apache.pulsar.client.api.MessageId)3 TransactionImpl (org.apache.pulsar.client.impl.transaction.TransactionImpl)3 MLTransactionMetadataStore (org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore)3 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)2 Duration (java.time.Duration)2 Collections (java.util.Collections)2 List (java.util.List)2 NoSuchElementException (java.util.NoSuchElementException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 ExecutorService (java.util.concurrent.ExecutorService)2