Search in sources :

Example 6 with TransactionDeadlockException

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

the class PerformingTransactions method deadlockDetectionExample.

public static void deadlockDetectionExample() {
    try (Ignite ignite = Ignition.start()) {
        // tag::deadlock[]
        CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();
        cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cfg.setName("myCache");
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cfg);
        try (Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.READ_COMMITTED, 300, 0)) {
            cache.put(1, "1");
            cache.put(2, "1");
            tx.commit();
        } catch (CacheException e) {
            if (e.getCause() instanceof TransactionTimeoutException && e.getCause().getCause() instanceof TransactionDeadlockException)
                System.out.println(e.getCause().getCause().getMessage());
        }
    // end::deadlock[]
    }
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with TransactionDeadlockException

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

the class IgniteUtils method exceptionConverters.

/**
 * Gets map with converters to convert internal checked exceptions to public API unchecked exceptions.
 *
 * @return Exception converters.
 */
private static Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> exceptionConverters() {
    Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> m = new HashMap<>();
    m.put(IgniteInterruptedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteInterruptedException(e.getMessage(), (InterruptedException) e.getCause());
        }
    });
    m.put(IgniteFutureCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteFutureCancelledException(e.getMessage(), e);
        }
    });
    m.put(IgniteFutureTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteFutureTimeoutException(e.getMessage(), e);
        }
    });
    m.put(ClusterGroupEmptyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ClusterGroupEmptyException(e.getMessage(), e);
        }
    });
    m.put(ClusterTopologyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            ClusterTopologyException topEx = new ClusterTopologyException(e.getMessage(), e);
            ClusterTopologyCheckedException checked = (ClusterTopologyCheckedException) e;
            if (checked.retryReadyFuture() != null)
                topEx.retryReadyFuture(new IgniteFutureImpl<>(checked.retryReadyFuture()));
            return topEx;
        }
    });
    m.put(IgniteDeploymentCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteDeploymentException(e.getMessage(), e);
        }
    });
    m.put(ComputeTaskTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ComputeTaskTimeoutException(e.getMessage(), e);
        }
    });
    m.put(ComputeTaskCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ComputeTaskCancelledException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxRollbackCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionRollbackException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxHeuristicCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionHeuristicException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            if (e.getCause() instanceof TransactionDeadlockException)
                return new TransactionTimeoutException(e.getMessage(), e.getCause());
            return new TransactionTimeoutException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxOptimisticCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionOptimisticException(e.getMessage(), e);
        }
    });
    m.put(IgniteClientDisconnectedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteClientDisconnectedException(((IgniteClientDisconnectedCheckedException) e).reconnectFuture(), e.getMessage(), e);
        }
    });
    m.put(IgniteTxSerializationCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionSerializationException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxDuplicateKeyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionDuplicateKeyException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxAlreadyCompletedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionAlreadyCompletedException(e.getMessage(), e);
        }
    });
    return m;
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IgniteDeploymentException(org.apache.ignite.IgniteDeploymentException) ClusterGroupEmptyException(org.apache.ignite.cluster.ClusterGroupEmptyException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) TransactionHeuristicException(org.apache.ignite.transactions.TransactionHeuristicException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) C1(org.apache.ignite.internal.util.typedef.C1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) ComputeTaskCancelledException(org.apache.ignite.compute.ComputeTaskCancelledException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 8 with TransactionDeadlockException

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

the class TxPessimisticDeadlockDetectionTest method doTestDeadlock.

/**
 * @throws Exception If failed.
 */
private void doTestDeadlock(final int txCnt, final boolean loc, boolean lockPrimaryFirst, final boolean clientTx, final Object startKey) throws Exception {
    log.info(">>> Test deadlock [txCnt=" + txCnt + ", loc=" + loc + ", lockPrimaryFirst=" + lockPrimaryFirst + ", clientTx=" + clientTx + ", startKey=" + startKey.getClass().getName() + ']');
    final AtomicInteger threadCnt = new AtomicInteger();
    final CyclicBarrier barrier = new CyclicBarrier(txCnt);
    final AtomicReference<TransactionDeadlockException> deadlockErr = new AtomicReference<>();
    final List<List<Object>> keySets = generateKeys(txCnt, startKey, loc, !lockPrimaryFirst);
    final Set<Object> involvedKeys = new GridConcurrentHashSet<>();
    final Set<Object> involvedLockedKeys = new GridConcurrentHashSet<>();
    final Set<IgniteInternalTx> involvedTxs = new GridConcurrentHashSet<>();
    IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

        @Override
        public void run() {
            int threadNum = threadCnt.incrementAndGet();
            Ignite ignite = loc ? ignite(0) : ignite(clientTx ? threadNum - 1 + txCnt : threadNum - 1);
            IgniteCache<Object, Integer> cache = ignite.cache(CACHE_NAME).withAllowAtomicOpsInTx();
            List<Object> keys = keySets.get(threadNum - 1);
            int txTimeout = 500 + txCnt * 100;
            try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, txTimeout, 0)) {
                involvedTxs.add(((TransactionProxyImpl) tx).tx());
                Object key = keys.get(0);
                involvedKeys.add(key);
                Object k;
                log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key + ']');
                cache.put(key, 0);
                involvedLockedKeys.add(key);
                barrier.await();
                key = keys.get(1);
                ClusterNode primaryNode = ((IgniteCacheProxy) cache).context().affinity().primaryByKey(key, NONE);
                List<Object> primaryKeys = primaryKeys(grid(primaryNode).cache(CACHE_NAME), 5, incrementKey(key, 100 * threadNum));
                Map<Object, Integer> entries = new HashMap<>();
                involvedKeys.add(key);
                entries.put(key, 0);
                for (Object o : primaryKeys) {
                    involvedKeys.add(o);
                    entries.put(o, 1);
                    k = incrementKey(o, +13);
                    involvedKeys.add(k);
                    entries.put(k, 2);
                }
                log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", entries=" + entries + ']');
                cache.putAll(entries);
                tx.commit();
            } catch (Throwable e) {
                // At least one stack trace should contain TransactionDeadlockException.
                if (hasCause(e, TransactionTimeoutException.class) && hasCause(e, TransactionDeadlockException.class)) {
                    if (deadlockErr.compareAndSet(null, cause(e, TransactionDeadlockException.class)))
                        U.error(log, "At least one stack trace should contain " + TransactionDeadlockException.class.getSimpleName(), e);
                }
            }
        }
    }, loc ? 2 : txCnt, "tx-thread");
    try {
        fut.get();
    } catch (IgniteCheckedException e) {
        U.error(null, "Unexpected exception", e);
        fail();
    }
    U.sleep(1000);
    TransactionDeadlockException deadlockE = deadlockErr.get();
    assertNotNull(deadlockE);
    checkAllTransactionsCompleted(involvedKeys, NODES_CNT * 2, CACHE_NAME);
    // Check deadlock report
    String msg = deadlockE.getMessage();
    for (IgniteInternalTx tx : involvedTxs) assertTrue(msg.contains("[txId=" + tx.xidVersion() + ", nodeId=" + tx.nodeId() + ", threadId=" + tx.threadId() + ']'));
    for (Object key : involvedKeys) {
        if (involvedLockedKeys.contains(key))
            assertTrue(msg.contains("[key=" + key + ", cache=" + CACHE_NAME + ']'));
        else
            assertFalse(msg.contains("[key=" + key));
    }
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) CyclicBarrier(java.util.concurrent.CyclicBarrier) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with TransactionDeadlockException

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

the class TxOptimisticDeadlockDetectionCrossCacheTest method doTestDeadlock.

/**
 * @throws Exception If failed.
 */
private boolean doTestDeadlock() throws Exception {
    final AtomicInteger threadCnt = new AtomicInteger();
    final AtomicBoolean deadlock = new AtomicBoolean();
    final AtomicInteger commitCnt = new AtomicInteger();
    grid(0).events().localListen(new CacheLocksListener(), EventType.EVT_CACHE_OBJECT_LOCKED);
    AffinityTopologyVersion waitTopVer = new AffinityTopologyVersion(2, 1);
    IgniteInternalFuture<?> exchFut = grid(0).context().cache().context().exchange().affinityReadyFuture(waitTopVer);
    if (exchFut != null && !exchFut.isDone()) {
        log.info("Waiting for topology exchange future [waitTopVer=" + waitTopVer + ", curTopVer=" + grid(0).context().cache().context().exchange().readyAffinityVersion() + ']');
        exchFut.get();
    }
    log.info("Finished topology exchange future [curTopVer=" + grid(0).context().cache().context().exchange().readyAffinityVersion() + ']');
    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(OPTIMISTIC, 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);
                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();
                commitCnt.incrementAndGet();
            } 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))
                        log.info("Successfully set deadlock flag");
                    else
                        log.info("Deadlock flag was already set");
                } else
                    log.warning("Got not deadlock exception", e);
            }
        }
    }, 2, "tx-thread");
    fut.get();
    assertFalse("Commits must fail", commitCnt.get() == 2);
    assertTrue(deadlock.get());
    for (Ignite ignite : G.allGrids()) {
        IgniteTxManager txMgr = ((IgniteKernal) ignite).context().cache().context().tm();
        Collection<IgniteInternalFuture<?>> futs = txMgr.deadlockDetectionFutures();
        assertTrue(futs.isEmpty());
    }
    return true;
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) 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)

Example 10 with TransactionDeadlockException

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

the class TxDeadlockDetectionUnmasrhalErrorsTest method testDeadlockCacheObjectContext.

/**
 * @throws Exception If failed.
 */
@Test
public void testDeadlockCacheObjectContext() throws Exception {
    IgniteCache<Integer, Integer> cache0 = null;
    IgniteCache<Integer, Integer> cache1 = null;
    try {
        cache0 = getCache(ignite(0), "cache0");
        cache1 = getCache(ignite(0), "cache1");
        IgniteCache<Integer, Integer> clientCache0 = grid(1).cache("cache0");
        awaitPartitionMapExchange();
        final CyclicBarrier barrier = new CyclicBarrier(2);
        final CountDownLatch latch = new CountDownLatch(1);
        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, 1000, 0)) {
                    int key1 = threadNum == 0 ? 0 : 1;
                    log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key1 + ", cache=" + cache1.getName() + ']');
                    cache1.put(key1, 0);
                    barrier.await();
                    int key2 = threadNum == 0 ? 1 : 0;
                    log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key2 + ", cache=" + cache2.getName() + ']');
                    latch.countDown();
                    cache2.put(key2, 1);
                    tx.commit();
                    log.info(">>> Commit done");
                } 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");
        latch.await();
        Ignite client = grid(1);
        try (Transaction tx = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED, 500, 0)) {
            clientCache0.put(0, 3);
            clientCache0.put(1, 3);
            tx.commit();
            log.info(">>> Commit done");
        } catch (CacheException e) {
            assertTrue(X.hasCause(e, TransactionTimeoutException.class));
        } catch (Throwable e) {
            log.error("Unexpected exception occurred", e);
            fail();
        }
        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());
        }
    // assertNotNull(grid(1).context().cache().context().cacheContext(cacheId));
    } 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) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) 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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

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