Search in sources :

Example 36 with TransactionOptimisticException

use of org.apache.ignite.transactions.TransactionOptimisticException 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);
        }
    });
    return m;
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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) 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 37 with TransactionOptimisticException

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

the class IgniteTxCacheWriteSynchronizationModesMultithreadedTest method multithreaded.

/**
     * @param syncMode Write synchronization mode.
     * @param backups Number of backups.
     * @param store If {@code true} sets store in cache configuration.
     * @param nearCache If {@code true} creates near cache on one of client nodes.
     * @param restart If {@code true} restarts one node during test.
     * @throws Exception If failed.
     */
private void multithreaded(CacheWriteSynchronizationMode syncMode, int backups, boolean store, boolean nearCache, boolean restart) throws Exception {
    final Ignite ignite = ignite(0);
    createCache(ignite, cacheConfiguration(DEFAULT_CACHE_NAME, syncMode, backups, store), nearCache);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> restartFut = null;
    try {
        if (restart) {
            restartFut = GridTestUtils.runAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    while (!stop.get()) {
                        startGrid(NODES);
                        U.sleep(100);
                        stopGrid(NODES);
                    }
                    return null;
                }
            }, "restart-thread");
        }
        commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {

            @Override
            public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
                cache.put(key, rnd.nextInt());
            }
        });
        commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {

            @Override
            public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                Map<Integer, Integer> map = new TreeMap<>();
                for (int i = 0; i < 100; i++) {
                    Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
                    map.put(key, rnd.nextInt());
                }
                cache.putAll(map);
            }
        });
        commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {

            @Override
            public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                Map<Integer, Integer> map = new TreeMap<>();
                for (int i = 0; i < 100; i++) {
                    Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
                    map.put(key, rnd.nextInt());
                }
                try {
                    try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
                        tx.commit();
                    }
                } catch (CacheException | IgniteException ignored) {
                // No-op.
                }
            }
        });
        commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {

            @Override
            public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                Map<Integer, Integer> map = new LinkedHashMap<>();
                for (int i = 0; i < 10; i++) {
                    Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
                    map.put(key, rnd.nextInt());
                }
                while (true) {
                    try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                        for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
                        tx.commit();
                        break;
                    } catch (TransactionOptimisticException ignored) {
                    // Retry.
                    } catch (CacheException | IgniteException ignored) {
                        break;
                    }
                }
            }
        });
    } finally {
        stop.set(true);
        ignite.destroyCache(DEFAULT_CACHE_NAME);
        if (restartFut != null)
            restartFut.get();
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 38 with TransactionOptimisticException

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

the class GridCacheNearMultiGetSelfTest method checkDoubleGet.

/**
     * @param concurrency Concurrency.
     * @param isolation Isolation.
     * @param put If {@code true}, then value will be pre-stored in cache.
     * @throws Exception If failed.
     */
private void checkDoubleGet(TransactionConcurrency concurrency, TransactionIsolation isolation, boolean put) throws Exception {
    IgniteEx ignite = grid(0);
    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
    Integer key = 1;
    String val = null;
    if (put)
        cache.put(key, val = Integer.toString(key));
    Transaction tx = ignite.transactions().txStart(concurrency, isolation, 0, 0);
    try {
        if (isTestDebug()) {
            info("Started transaction.");
            Affinity<Integer> aff = affinity(cache);
            int part = aff.partition(key);
            if (isTestDebug())
                info("Key affinity [key=" + key + ", partition=" + part + ", affinity=" + U.toShortString(aff.mapKeyToPrimaryAndBackups(key)) + ']');
        }
        for (int i = 0; i < GET_CNT; i++) {
            if (isTestDebug())
                info("Reading key [key=" + key + ", i=" + i + ']');
            String v = cache.get(key);
            assertEquals("Value mismatch for put [val=" + val + ", v=" + v + ", put=" + put + ']', val, v);
            if (isTestDebug())
                info("Read value for key (will read again) [key=" + key + ", val=" + v + ", i=" + i + ']');
        }
        if (isTestDebug())
            info("Committing transaction.");
        tx.commit();
        if (isTestDebug())
            info("Committed transaction: " + tx);
    } catch (TransactionOptimisticException e) {
        if (concurrency != OPTIMISTIC || isolation != SERIALIZABLE) {
            error("Received invalid optimistic failure.", e);
            throw e;
        }
        if (isTestDebug())
            info("Optimistic transaction failure (will rollback) [msg=" + e.getMessage() + ", tx=" + tx.xid() + ']');
        try {
            tx.rollback();
        } catch (IgniteException ex) {
            error("Failed to rollback optimistic failure: " + tx, ex);
            throw ex;
        }
    } catch (Exception e) {
        error("Transaction failed (will rollback): " + tx, e);
        tx.rollback();
        throw e;
    } catch (Error e) {
        error("Error when executing transaction (will rollback): " + tx, e);
        tx.rollback();
        throw e;
    } finally {
        Transaction t = ignite.transactions().tx();
        assert t == null : "Thread should not have transaction upon completion ['t==tx'=" + (t == tx) + ", t=" + t + (t != tx ? "tx=" + tx : "tx=''") + ']';
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException)

Example 39 with TransactionOptimisticException

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

the class IgniteBenchmarkUtils method doInTransaction.

/**
     * @param igniteTx Ignite transaction.
     * @param txConcurrency Transaction concurrency.
     * @param clo Closure.
     * @return Result of closure execution.
     * @throws Exception If failed.
     */
public static <T> T doInTransaction(IgniteTransactions igniteTx, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation, Callable<T> clo) throws Exception {
    while (true) {
        try (Transaction tx = igniteTx.txStart(txConcurrency, txIsolation)) {
            T res = clo.call();
            tx.commit();
            return res;
        } catch (CacheException e) {
            if (e.getCause() instanceof ClusterTopologyException) {
                ClusterTopologyException topEx = (ClusterTopologyException) e.getCause();
                topEx.retryReadyFuture().get();
            } else
                throw e;
        } catch (ClusterTopologyException e) {
            e.retryReadyFuture().get();
        } catch (TransactionRollbackException | TransactionOptimisticException ignore) {
        // Safe to retry right away.
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException)

Aggregations

TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)39 Transaction (org.apache.ignite.transactions.Transaction)38 Ignite (org.apache.ignite.Ignite)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 IgniteTransactions (org.apache.ignite.IgniteTransactions)27 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteException (org.apache.ignite.IgniteException)10 CacheException (javax.cache.CacheException)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 Callable (java.util.concurrent.Callable)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)7 CacheLoaderException (javax.cache.integration.CacheLoaderException)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 TreeMap (java.util.TreeMap)4