Search in sources :

Example 1 with TransactionCoordinator

use of org.infinispan.transaction.impl.TransactionCoordinator in project infinispan by infinispan.

the class StaleLocksWithCommitDuringStateTransferTest method doTestSuspect.

/**
 * Check that the transaction commit/rollback recovers if the remote node dies during the RPC
 */
private void doTestSuspect(boolean commit) throws Exception {
    MagicKey k1 = new MagicKey("k1", c1);
    MagicKey k2 = new MagicKey("k2", c2);
    tm(c1).begin();
    c1.put(k1, "v1");
    c1.put(k2, "v2");
    // We split the transaction commit in two phases by calling the TransactionCoordinator methods directly
    TransactionTable txTable = TestingUtil.extractComponent(c1, TransactionTable.class);
    TransactionCoordinator txCoordinator = TestingUtil.extractComponent(c1, TransactionCoordinator.class);
    // Execute the prepare on both nodes
    LocalTransaction localTx = txTable.getLocalTransaction(tm(c1).getTransaction());
    CompletionStages.join(txCoordinator.prepare(localTx));
    // Delay the commit on the remote node. Can't used blockNewTransactions because we don't want a StateTransferInProgressException
    AsyncInterceptorChain c2ic = c2.getAdvancedCache().getAsyncInterceptorChain();
    c2ic.addInterceptorBefore(new DelayCommandInterceptor(), StateTransferInterceptor.class);
    // Schedule the remote node to stop on another thread since the main thread will be busy with the commit call
    Thread worker = new Thread("RehasherSim,StaleLocksWithCommitDuringStateTransferTest") {

        @Override
        public void run() {
            try {
                // should be much larger than the lock acquisition timeout
                Thread.sleep(1000);
                manager(c2).stop();
            // stLock.unblockNewTransactions(1000);
            } catch (InterruptedException e) {
                log.errorf(e, "Error stopping cache");
            }
        }
    };
    worker.start();
    try {
        // finally commit or rollback the transaction
        if (commit) {
            CompletionStages.join(txCoordinator.commit(localTx, false));
        } else {
            CompletionStages.join(txCoordinator.rollback(localTx));
        }
        // make the transaction manager forget about our tx so that we don't get rollback exceptions in the log
        tm(c1).suspend();
    } finally {
        // don't leak threads
        worker.join();
    }
    // test that we don't leak locks
    assertEventuallyNotLocked(c1, k1);
    assertEventuallyNotLocked(c1, k2);
}
Also used : LocalTransaction(org.infinispan.transaction.impl.LocalTransaction) TransactionTable(org.infinispan.transaction.impl.TransactionTable) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) TransactionCoordinator(org.infinispan.transaction.impl.TransactionCoordinator) MagicKey(org.infinispan.distribution.MagicKey)

Example 2 with TransactionCoordinator

use of org.infinispan.transaction.impl.TransactionCoordinator in project infinispan by infinispan.

the class StaleLocksWithCommitDuringStateTransferTest method doStateTransferInProgressTest.

/**
 * Check that the transaction commit/rollback recovers if we receive a StateTransferInProgressException from the remote node
 */
private void doStateTransferInProgressTest(boolean commit, final boolean failOnOriginator) throws Exception {
    MagicKey k1 = new MagicKey("k1", c1);
    MagicKey k2 = new MagicKey("k2", c2);
    tm(c1).begin();
    c1.put(k1, "v1");
    c1.put(k2, "v2");
    // We split the transaction commit in two phases by calling the TransactionCoordinator methods directly
    TransactionTable txTable = TestingUtil.extractComponent(c1, TransactionTable.class);
    TransactionCoordinator txCoordinator = TestingUtil.extractComponent(c1, TransactionCoordinator.class);
    // Execute the prepare on both nodes
    LocalTransaction localTx = txTable.getLocalTransaction(tm(c1).getTransaction());
    CompletionStages.join(txCoordinator.prepare(localTx));
    final CountDownLatch commitLatch = new CountDownLatch(1);
    Thread worker = new Thread("RehasherSim,StaleLocksWithCommitDuringStateTransferTest") {

        @Override
        public void run() {
            try {
                // Before calling commit we block transactions on one of the nodes to simulate a state transfer
                final StateTransferLock blockFirst = TestingUtil.extractComponent(failOnOriginator ? c1 : c2, StateTransferLock.class);
                final StateTransferLock blockSecond = TestingUtil.extractComponent(failOnOriginator ? c2 : c1, StateTransferLock.class);
                try {
                    blockFirst.acquireExclusiveTopologyLock();
                    blockSecond.acquireExclusiveTopologyLock();
                    commitLatch.countDown();
                    // should be much larger than the lock acquisition timeout
                    Thread.sleep(1000);
                } finally {
                    blockSecond.releaseExclusiveTopologyLock();
                    blockFirst.releaseExclusiveTopologyLock();
                }
            } catch (Throwable t) {
                log.errorf(t, "Error blocking/unblocking transactions");
            }
        }
    };
    worker.start();
    commitLatch.await(10, TimeUnit.SECONDS);
    try {
        // finally commit or rollback the transaction
        if (commit) {
            tm(c1).commit();
        } else {
            tm(c1).rollback();
        }
        // make the transaction manager forget about our tx so that we don't get rollback exceptions in the log
        tm(c1).suspend();
    } finally {
        // don't leak threads
        worker.join();
    }
    // test that we don't leak locks
    assertEventuallyNotLocked(c1, k1);
    assertEventuallyNotLocked(c2, k1);
    assertEventuallyNotLocked(c1, k2);
    assertEventuallyNotLocked(c2, k2);
}
Also used : LocalTransaction(org.infinispan.transaction.impl.LocalTransaction) TransactionTable(org.infinispan.transaction.impl.TransactionTable) TransactionCoordinator(org.infinispan.transaction.impl.TransactionCoordinator) CountDownLatch(java.util.concurrent.CountDownLatch) MagicKey(org.infinispan.distribution.MagicKey)

Example 3 with TransactionCoordinator

use of org.infinispan.transaction.impl.TransactionCoordinator in project infinispan by infinispan.

the class TransactionXaAdapterTmIntegrationTest method setUp.

@BeforeMethod
public void setUp() throws XAException {
    Configuration configuration = new ConfigurationBuilder().build();
    XaTransactionTable txTable = new XaTransactionTable();
    txCoordinator = new TransactionCoordinator();
    TestingUtil.inject(txTable, configuration, txCoordinator, TransactionOriginatorChecker.LOCAL);
    txTable.start();
    txTable.startXidMapping();
    TransactionFactory gtf = new TransactionFactory();
    gtf.init(false, false, true, false);
    GlobalTransaction globalTransaction = gtf.newGlobalTransaction(null, false);
    EmbeddedBaseTransactionManager tm = new EmbeddedBaseTransactionManager();
    localTx = new LocalXaTransaction(new EmbeddedTransaction(tm), globalTransaction, false, 1, 0);
    xid = EmbeddedTransaction.createXid(uuid);
    InvocationContextFactory icf = new TransactionalInvocationContextFactory();
    CommandsFactory commandsFactory = mock(CommandsFactory.class);
    AsyncInterceptorChain invoker = mock(AsyncInterceptorChain.class);
    when(invoker.invokeAsync(any(), any())).thenReturn(CompletableFutures.completedNull());
    TestingUtil.inject(txCoordinator, commandsFactory, icf, invoker, txTable, configuration);
    xaAdapter = new TransactionXaAdapter(localTx, txTable);
    xaAdapter.start(xid, 0);
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) EmbeddedTransaction(org.infinispan.transaction.tm.EmbeddedTransaction) XaTransactionTable(org.infinispan.transaction.xa.XaTransactionTable) CommandsFactory(org.infinispan.commands.CommandsFactory) Configuration(org.infinispan.configuration.cache.Configuration) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) TransactionCoordinator(org.infinispan.transaction.impl.TransactionCoordinator) EmbeddedBaseTransactionManager(org.infinispan.transaction.tm.EmbeddedBaseTransactionManager) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) TransactionalInvocationContextFactory(org.infinispan.context.impl.TransactionalInvocationContextFactory) TransactionXaAdapter(org.infinispan.transaction.xa.TransactionXaAdapter) TransactionFactory(org.infinispan.transaction.xa.TransactionFactory) LocalXaTransaction(org.infinispan.transaction.xa.LocalXaTransaction) InvocationContextFactory(org.infinispan.context.InvocationContextFactory) TransactionalInvocationContextFactory(org.infinispan.context.impl.TransactionalInvocationContextFactory) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

TransactionCoordinator (org.infinispan.transaction.impl.TransactionCoordinator)3 MagicKey (org.infinispan.distribution.MagicKey)2 AsyncInterceptorChain (org.infinispan.interceptors.AsyncInterceptorChain)2 LocalTransaction (org.infinispan.transaction.impl.LocalTransaction)2 TransactionTable (org.infinispan.transaction.impl.TransactionTable)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 CommandsFactory (org.infinispan.commands.CommandsFactory)1 Configuration (org.infinispan.configuration.cache.Configuration)1 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)1 InvocationContextFactory (org.infinispan.context.InvocationContextFactory)1 TransactionalInvocationContextFactory (org.infinispan.context.impl.TransactionalInvocationContextFactory)1 EmbeddedBaseTransactionManager (org.infinispan.transaction.tm.EmbeddedBaseTransactionManager)1 EmbeddedTransaction (org.infinispan.transaction.tm.EmbeddedTransaction)1 GlobalTransaction (org.infinispan.transaction.xa.GlobalTransaction)1 LocalXaTransaction (org.infinispan.transaction.xa.LocalXaTransaction)1 TransactionFactory (org.infinispan.transaction.xa.TransactionFactory)1 TransactionXaAdapter (org.infinispan.transaction.xa.TransactionXaAdapter)1 XaTransactionTable (org.infinispan.transaction.xa.XaTransactionTable)1 BeforeMethod (org.testng.annotations.BeforeMethod)1