Search in sources :

Example 11 with TransactionDeadlockException

use of org.apache.ignite.transactions.TransactionDeadlockException in project ignite by apache.

the class TxPessimisticDeadlockDetectionCrossCacheTest method doTestDeadlock.

/**
 * @param near0 Near flag for cache0.
 * @param near1 Near flag for cache1.
 */
private void doTestDeadlock(boolean near0, boolean near1) throws Exception {
    IgniteCache<Integer, Integer> cache0 = null;
    IgniteCache<Integer, Integer> cache1 = null;
    try {
        cache0 = getCache(ignite(0), "cache0", near0);
        cache1 = getCache(ignite(0), "cache1", near1);
        awaitPartitionMapExchange();
        final CyclicBarrier barrier = new CyclicBarrier(2);
        final AtomicInteger threadCnt = new AtomicInteger();
        final AtomicBoolean deadlock = new AtomicBoolean();
        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                int threadNum = threadCnt.getAndIncrement();
                Ignite ignite = ignite(0);
                IgniteCache<Integer, Integer> cache1 = ignite.cache("cache" + (threadNum == 0 ? 0 : 1));
                IgniteCache<Integer, Integer> cache2 = ignite.cache("cache" + (threadNum == 0 ? 1 : 0));
                try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 500, 0)) {
                    int key1 = primaryKey(cache1);
                    log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key1 + ", cache=" + cache1.getName() + ']');
                    cache1.put(key1, 0);
                    barrier.await();
                    int key2 = primaryKey(cache2);
                    log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key2 + ", cache=" + cache2.getName() + ']');
                    cache2.put(key2, 1);
                    tx.commit();
                } catch (Throwable e) {
                    // At least one stack trace should contain TransactionDeadlockException.
                    if (hasCause(e, TransactionTimeoutException.class) && hasCause(e, TransactionDeadlockException.class)) {
                        if (deadlock.compareAndSet(false, true))
                            U.error(log, "At least one stack trace should contain " + TransactionDeadlockException.class.getSimpleName(), e);
                    }
                }
            }
        }, 2, "tx-thread");
        fut.get();
        assertTrue(deadlock.get());
        for (int i = 0; i < NODES_CNT; i++) {
            Ignite ignite = ignite(i);
            IgniteTxManager txMgr = ((IgniteKernal) ignite).context().cache().context().tm();
            Collection<IgniteInternalFuture<?>> futs = txMgr.deadlockDetectionFutures();
            assertTrue(futs.isEmpty());
        }
    } finally {
        if (cache0 != null)
            cache0.destroy();
        if (cache1 != null)
            cache1.destroy();
    }
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) Ignite(org.apache.ignite.Ignite)

Aggregations

TransactionDeadlockException (org.apache.ignite.transactions.TransactionDeadlockException)11 TransactionTimeoutException (org.apache.ignite.transactions.TransactionTimeoutException)11 Ignite (org.apache.ignite.Ignite)8 Transaction (org.apache.ignite.transactions.Transaction)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteCache (org.apache.ignite.IgniteCache)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 IgniteKernal (org.apache.ignite.internal.IgniteKernal)6 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 LinkedHashMap (java.util.LinkedHashMap)2 CacheException (javax.cache.CacheException)2