Search in sources :

Example 26 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method txConflictRemoveReturnBoolean.

/**
     * @param noVal If {@code true} there is no cache value when do update in tx.
     * @throws Exception If failed.
     */
private void txConflictRemoveReturnBoolean(boolean noVal) throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (final Integer key : keys) {
                log.info("Test key: " + key);
                if (!noVal)
                    cache.put(key, -1);
                if (noVal) {
                    try {
                        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                            boolean res = cache.remove(key);
                            assertFalse(res);
                            updateKey(cache, key, -1);
                            tx.commit();
                        }
                        fail();
                    } catch (TransactionOptimisticException e) {
                        log.info("Expected exception: " + e);
                    }
                    checkValue(key, -1, cache.getName());
                } else {
                    try {
                        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                            boolean res = cache.remove(key);
                            assertTrue(res);
                            txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                                @Override
                                public Void apply(IgniteCache<Integer, Integer> cache) {
                                    cache.remove(key);
                                    return null;
                                }
                            });
                            tx.commit();
                        }
                        fail();
                    } catch (TransactionOptimisticException e) {
                        log.info("Expected exception: " + e);
                    }
                    checkValue(key, null, cache.getName());
                    cache.put(key, -1);
                }
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.remove(key);
                    assertTrue(res);
                    updateKey(cache, key, 2);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                // Check no conflict for removeAll with single key.
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache.removeAll(Collections.singleton(key));
                    txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                        @Override
                        public Void apply(IgniteCache<Integer, Integer> cache) {
                            cache.remove(key);
                            return null;
                        }
                    });
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.remove(key);
                    assertTrue(res);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.remove(key);
                    assertFalse(res);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try {
                    cache.put(key, 1);
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object val = cache.get(key);
                        assertEquals(1, val);
                        boolean res = cache.remove(key);
                        assertTrue(res);
                        updateKey(cache, key, 2);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 27 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method txConflictGetAndPut.

/**
     * @param noVal If {@code true} there is no cache value when read in tx.
     * @param rmv If {@code true} tests remove, otherwise put.
     * @throws Exception If failed.
     */
private void txConflictGetAndPut(boolean noVal, boolean rmv) throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (Integer key : keys) {
                log.info("Test key: " + key);
                Integer expVal = null;
                if (!noVal) {
                    expVal = -1;
                    cache.put(key, expVal);
                }
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = rmv ? cache.getAndRemove(key) : cache.getAndPut(key, 2);
                        assertEquals(expVal, val);
                        updateKey(cache, key, 1);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val = rmv ? cache.getAndRemove(key) : cache.getAndPut(key, 2);
                    assertEquals(1, val);
                    tx.commit();
                }
                checkValue(key, rmv ? null : 2, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 28 with TransactionOptimisticException

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

the class CacheNearReaderUpdateTest method getUpdateMultithreaded.

/**
     * @param ccfg Cache configuration.
     * @param putNodes Nodes executing updates.
     * @param getNodes Nodes executing gets.
     * @param concurrency Transaction concurrency.
     * @param isolation Transaction isolation.
     * @throws Exception If failed.
     */
private void getUpdateMultithreaded(CacheConfiguration<Integer, Integer> ccfg, final List<Ignite> putNodes, final List<Ignite> getNodes, final TransactionConcurrency concurrency, final TransactionIsolation isolation) throws Exception {
    fail("https://issues.apache.org/jira/browse/IGNITE-627");
    log.info("Execute updates [concurrency=" + concurrency + ", isolation=" + isolation + ']');
    final Ignite ignite0 = ignite(0);
    final String cacheName = ignite0.createCache(ccfg).getName();
    try {
        for (int i = 0; i < 5; i++) {
            final Integer key = i;
            final AtomicInteger putThreadIdx = new AtomicInteger();
            final AtomicInteger getThreadIdx = new AtomicInteger();
            final int PUT_THREADS = 20;
            final int GET_THREAD = 20;
            final CyclicBarrier barrier = new CyclicBarrier(PUT_THREADS + GET_THREAD);
            final IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = putThreadIdx.getAndIncrement() % putNodes.size();
                    Ignite ignite = putNodes.get(idx);
                    IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
                    IgniteTransactions txs = ignite.transactions();
                    Thread.currentThread().setName("update-thread-" + ignite.name());
                    barrier.await();
                    for (int i = 0; i < 100; i++) {
                        ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        if (concurrency != null) {
                            try (Transaction tx = txs.txStart(concurrency, isolation)) {
                                cache.put(key, rnd.nextInt());
                                tx.commit();
                            } catch (TransactionOptimisticException ignore) {
                                assertEquals(concurrency, OPTIMISTIC);
                                assertEquals(isolation, SERIALIZABLE);
                            }
                        } else
                            cache.put(key, rnd.nextInt());
                    }
                    return null;
                }
            }, PUT_THREADS, "update-thread");
            IgniteInternalFuture<?> getFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = getThreadIdx.getAndIncrement() % getNodes.size();
                    Ignite ignite = getNodes.get(idx);
                    IgniteCache<Integer, Integer> cache;
                    if (ignite.configuration().isClientMode())
                        cache = ignite.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>());
                    else
                        cache = ignite.cache(cacheName);
                    Thread.currentThread().setName("get-thread-" + ignite.name());
                    barrier.await();
                    while (!updateFut.isDone()) cache.get(key);
                    return null;
                }
            }, GET_THREAD, "get-thread");
            updateFut.get();
            getFut.get();
            Integer val = (Integer) ignite0.cache(cacheName).get(key);
            log.info("Iteration [iter=" + i + ", val=" + val + ']');
            for (Ignite getNode : getNodes) {
                IgniteCache<Integer, Integer> cache = getNode.cache(cacheName);
                if (getNode.configuration().isClientMode() || cache.getConfiguration(CacheConfiguration.class).getNearConfiguration() != null)
                    assertNotNull(getNode.cache(cacheName).localPeek(key));
            }
            checkValue(key, val, cacheName);
            for (int n = 0; n < SRVS + CLIENTS; n++) {
                val = n;
                ignite(n).cache(cacheName).put(key, val);
                checkValue(key, val, cacheName);
            }
        }
    } finally {
        destroyCache(ignite0, cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) CacheLoaderException(javax.cache.integration.CacheLoaderException) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) CyclicBarrier(java.util.concurrent.CyclicBarrier) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 29 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method getRemoveTx.

/**
     * @param nearCache If {@code true} near cache is enabled.
     * @param store If {@code true} cache store is enabled.
     * @throws Exception If failed.
     */
private void getRemoveTx(boolean nearCache, boolean store) throws Exception {
    long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
    final Ignite ignite0 = ignite(0);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 0, store, false);
    final List<Ignite> clients = clients();
    final String cacheName = ignite0.createCache(ccfg).getName();
    try {
        final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
        for (Ignite client : clients) {
            if (nearCache)
                caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
            else
                caches.add(client.<Integer, Integer>cache(cacheName));
        }
        for (int i = 0; i < 100; i++) {
            if (U.currentTimeMillis() > stopTime)
                break;
            final AtomicInteger cntr = new AtomicInteger();
            final Integer key = i;
            final AtomicInteger threadIdx = new AtomicInteger();
            final int THREADS = 10;
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            final IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int thread = threadIdx.getAndIncrement();
                    int idx = thread % caches.size();
                    IgniteCache<Integer, Integer> cache = caches.get(idx);
                    Ignite ignite = cache.unwrap(Ignite.class);
                    IgniteTransactions txs = ignite.transactions();
                    log.info("Started update thread: " + ignite.name());
                    Thread.currentThread().setName("update-thread-" + ignite.name() + "-" + thread);
                    barrier.await();
                    for (int i = 0; i < 50; i++) {
                        while (true) {
                            try {
                                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                                boolean rmv = rnd.nextInt(3) == 0;
                                Integer val;
                                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                    val = cache.get(key);
                                    if (rmv)
                                        cache.remove(key);
                                    else
                                        cache.put(key, val == null ? 1 : val + 1);
                                    tx.commit();
                                    if (rmv) {
                                        if (val != null) {
                                            for (int j = 0; j < val; j++) cntr.decrementAndGet();
                                        }
                                    } else
                                        cntr.incrementAndGet();
                                }
                                break;
                            } catch (TransactionOptimisticException ignore) {
                            // Retry.
                            }
                        }
                    }
                    return null;
                }
            }, THREADS, "update-thread");
            updateFut.get();
            Integer val = cntr.get();
            log.info("Iteration [iter=" + i + ", val=" + val + ']');
            checkValue(key, val == 0 ? null : val, cacheName);
        }
    } finally {
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 30 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method concurrentUpdateNoDeadlock.

/**
     * @param updateNodes Nodes executing updates.
     * @param threads Number of threads executing updates.
     * @param get If {@code true} gets value in transaction.
     * @param restart If {@code true} restarts one node.
     * @param nonSer If {@code true} starts threads executing non-serializable transactions.
     * @throws Exception If failed.
     */
private void concurrentUpdateNoDeadlock(final List<Ignite> updateNodes, int threads, final boolean get, final boolean restart, final boolean nonSer) throws Exception {
    if (FAST)
        return;
    assert updateNodes.size() > 0;
    final Ignite srv = ignite(1);
    final String cacheName = srv.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
    try {
        final int KEYS = 100;
        final AtomicBoolean finished = new AtomicBoolean();
        IgniteInternalFuture<?> fut = restart ? restartFuture(finished, null) : null;
        try {
            for (int i = 0; i < 10; i++) {
                log.info("Iteration: " + i);
                final long stopTime = U.currentTimeMillis() + 10_000;
                final AtomicInteger idx = new AtomicInteger();
                IgniteInternalFuture<?> nonSerFut = null;
                if (nonSer) {
                    nonSerFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                        @Override
                        public Void call() throws Exception {
                            int nodeIdx = idx.getAndIncrement() % updateNodes.size();
                            Ignite node = updateNodes.get(nodeIdx);
                            log.info("Non-serializable tx thread: " + node.name());
                            final IgniteCache<Integer, Integer> cache = node.cache(cacheName);
                            assertNotNull(cache);
                            final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                            while (U.currentTimeMillis() < stopTime) {
                                final TreeMap<Integer, Integer> map = new TreeMap<>();
                                for (int i = 0; i < KEYS / 2; i++) map.put(rnd.nextInt(KEYS), rnd.nextInt());
                                TransactionConcurrency concurrency = rnd.nextBoolean() ? PESSIMISTIC : OPTIMISTIC;
                                doInTransaction(node, concurrency, REPEATABLE_READ, new Callable<Void>() {

                                    @Override
                                    public Void call() throws Exception {
                                        cache.putAll(map);
                                        return null;
                                    }
                                });
                            }
                            return null;
                        }
                    }, 5, "non-ser-thread");
                }
                IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        int nodeIdx = idx.getAndIncrement() % updateNodes.size();
                        Ignite node = updateNodes.get(nodeIdx);
                        log.info("Tx thread: " + node.name());
                        final IgniteTransactions txs = node.transactions();
                        final IgniteCache<Integer, Integer> cache = node.cache(cacheName);
                        assertNotNull(cache);
                        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        while (U.currentTimeMillis() < stopTime) {
                            final Map<Integer, Integer> map = new LinkedHashMap<>();
                            for (int i = 0; i < KEYS / 2; i++) map.put(rnd.nextInt(KEYS), rnd.nextInt());
                            try {
                                if (restart) {
                                    doInTransaction(node, OPTIMISTIC, SERIALIZABLE, new Callable<Void>() {

                                        @Override
                                        public Void call() throws Exception {
                                            if (get) {
                                                for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                                                    if (rnd.nextBoolean()) {
                                                        cache.get(e.getKey());
                                                        if (rnd.nextBoolean())
                                                            cache.put(e.getKey(), e.getValue());
                                                    } else
                                                        cache.put(e.getKey(), e.getValue());
                                                }
                                            } else
                                                cache.putAll(map);
                                            return null;
                                        }
                                    });
                                } else {
                                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                        if (get) {
                                            for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                                                if (rnd.nextBoolean()) {
                                                    cache.get(e.getKey());
                                                    if (rnd.nextBoolean())
                                                        cache.put(e.getKey(), e.getValue());
                                                } else
                                                    cache.put(e.getKey(), e.getValue());
                                            }
                                        } else
                                            cache.putAll(map);
                                        tx.commit();
                                    }
                                }
                            } catch (TransactionOptimisticException ignore) {
                            // No-op.
                            } catch (Throwable e) {
                                log.error("Unexpected error: " + e, e);
                                throw e;
                            }
                        }
                        return null;
                    }
                }, threads, "tx-thread");
                updateFut.get(60, SECONDS);
                if (nonSerFut != null)
                    nonSerFut.get(60, SECONDS);
                IgniteCache<Integer, Integer> cache = srv.cache(cacheName);
                for (int key = 0; key < KEYS; key++) {
                    Integer val = cache.get(key);
                    for (int node = 1; node < SRVS + CLIENTS; node++) assertEquals(val, ignite(node).cache(cache.getName()).get(key));
                }
            }
            finished.set(true);
            if (fut != null)
                fut.get();
        } finally {
            finished.set(true);
        }
    } finally {
        destroyCache(cacheName);
    }
}
Also used : IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) MutableEntry(javax.cache.processor.MutableEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) TreeMap(java.util.TreeMap) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

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