Search in sources :

Example 1 with TxnStatus

use of org.apache.pulsar.transaction.coordinator.proto.TxnStatus in project pulsar by apache.

the class TransactionMetadataStoreProviderTest method testGetTxnStatusSuccess.

@Test
public void testGetTxnStatusSuccess() throws Exception {
    TxnID txnID = this.store.newTransaction(0L).get();
    TxnStatus txnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(txnStatus, TxnStatus.OPEN);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) Test(org.testng.annotations.Test)

Example 2 with TxnStatus

use of org.apache.pulsar.transaction.coordinator.proto.TxnStatus in project pulsar by apache.

the class TransactionMetadataStoreProviderTest method testUpdateTxnStatusSuccess.

@Test
public void testUpdateTxnStatusSuccess() throws Exception {
    TxnID txnID = this.store.newTransaction(0L).get();
    TxnStatus txnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(txnStatus, TxnStatus.OPEN);
    // update the status
    this.store.updateTxnStatus(txnID, TxnStatus.COMMITTING, TxnStatus.OPEN, false).get();
    // get the new status
    TxnStatus newTxnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(newTxnStatus, TxnStatus.COMMITTING);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) Test(org.testng.annotations.Test)

Example 3 with TxnStatus

use of org.apache.pulsar.transaction.coordinator.proto.TxnStatus in project pulsar by apache.

the class TransactionMetadataStoreProviderTest method testUpdateTxnStatusNotExpectedStatus.

@Test
public void testUpdateTxnStatusNotExpectedStatus() throws Exception {
    TxnID txnID = this.store.newTransaction(0L).get();
    TxnStatus txnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(txnStatus, TxnStatus.OPEN);
    // update the status
    try {
        this.store.updateTxnStatus(txnID, TxnStatus.COMMITTING, TxnStatus.COMMITTING, false).get();
        fail("Should fail to update txn status if it is not in expected status");
    } catch (ExecutionException ee) {
        assertTrue(ee.getCause() instanceof InvalidTxnStatusException);
    }
    // get the txn status, it should be changed.
    TxnStatus newTxnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(newTxnStatus, TxnStatus.OPEN);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) ExecutionException(java.util.concurrent.ExecutionException) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) Test(org.testng.annotations.Test)

Example 4 with TxnStatus

use of org.apache.pulsar.transaction.coordinator.proto.TxnStatus in project pulsar by apache.

the class TransactionMetadataStoreProviderTest method testUpdateTxnStatusCannotTransition.

@Test
public void testUpdateTxnStatusCannotTransition() throws Exception {
    TxnID txnID = this.store.newTransaction(0L).get();
    TxnStatus txnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(txnStatus, TxnStatus.OPEN);
    // update the status
    try {
        this.store.updateTxnStatus(txnID, TxnStatus.COMMITTED, TxnStatus.OPEN, false).get();
        fail("Should fail to update txn status if it can not transition to the new status");
    } catch (ExecutionException ee) {
        assertTrue(ee.getCause() instanceof InvalidTxnStatusException);
    }
    // get the txn status, it should be changed.
    TxnStatus newTxnStatus = this.store.getTxnStatus(txnID).get();
    assertEquals(newTxnStatus, TxnStatus.OPEN);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) ExecutionException(java.util.concurrent.ExecutionException) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) Test(org.testng.annotations.Test)

Example 5 with TxnStatus

use of org.apache.pulsar.transaction.coordinator.proto.TxnStatus 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)

Aggregations

TxnID (org.apache.pulsar.client.api.transaction.TxnID)25 TxnStatus (org.apache.pulsar.transaction.coordinator.proto.TxnStatus)25 InvalidTxnStatusException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException)19 Test (org.testng.annotations.Test)18 ExecutionException (java.util.concurrent.ExecutionException)12 ArrayList (java.util.ArrayList)11 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)7 Collections (java.util.Collections)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 ExecutorService (java.util.concurrent.ExecutorService)7 Executors (java.util.concurrent.Executors)7 FutureUtil (org.apache.pulsar.common.util.FutureUtil)7 TransactionCoordinatorID (org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID)7 TransactionMetadataStore (org.apache.pulsar.transaction.coordinator.TransactionMetadataStore)7 TransactionRecoverTracker (org.apache.pulsar.transaction.coordinator.TransactionRecoverTracker)7 TransactionSubscription (org.apache.pulsar.transaction.coordinator.TransactionSubscription)7 TransactionTimeoutTracker (org.apache.pulsar.transaction.coordinator.TransactionTimeoutTracker)7 TxnMeta (org.apache.pulsar.transaction.coordinator.TxnMeta)7 Logger (org.slf4j.Logger)7