Search in sources :

Example 86 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method incrementTxMultiple.

/**
 * @param nearCache If {@code true} near cache is enabled.
 * @param store If {@code true} cache store is enabled.
 * @param restart If {@code true} restarts one node.
 * @throws Exception If failed.
 */
private void incrementTxMultiple(boolean nearCache, boolean store, final boolean restart) throws Exception {
    final Ignite srv = ignite(1);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
    final List<Ignite> clients = clients();
    final String cacheName = srv.createCache(ccfg).getName();
    final AtomicBoolean stop = new AtomicBoolean();
    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));
        }
        IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
        for (int i = 0; i < 20; i += 2) {
            final AtomicInteger cntr = new AtomicInteger();
            final Integer key1 = i;
            final Integer key2 = i + 1;
            final AtomicInteger threadIdx = new AtomicInteger();
            final int THREADS = 10;
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            final ConcurrentSkipListSet<Integer> vals1 = new ConcurrentSkipListSet<>();
            final ConcurrentSkipListSet<Integer> vals2 = new ConcurrentSkipListSet<>();
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = threadIdx.getAndIncrement() % 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());
                    barrier.await();
                    final int ITERATIONS_COUNT = SF.applyLB(1000, 50);
                    for (int i = 0; i < ITERATIONS_COUNT; i++) {
                        try {
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Integer val1 = cache.get(key1);
                                Integer val2 = cache.get(key2);
                                Integer newVal1 = val1 == null ? 1 : val1 + 1;
                                Integer newVal2 = val2 == null ? 1 : val2 + 1;
                                cache.put(key1, newVal1);
                                cache.put(key2, newVal2);
                                tx.commit();
                                assertTrue(vals1.add(newVal1));
                                assertTrue(vals2.add(newVal2));
                            }
                            cntr.incrementAndGet();
                        } catch (TransactionOptimisticException ignore) {
                        // Retry.
                        } catch (IgniteException | CacheException e) {
                            assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
                        }
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
            assertTrue(cntr.get() > 0);
            checkValue(key1, cntr.get(), cacheName, restart);
            checkValue(key2, cntr.get(), cacheName, restart);
        }
        stop.set(true);
        if (restartFut != null)
            restartFut.get();
    } finally {
        stop.set(true);
        destroyCache(cacheName);
    }
}
Also used : CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) IgniteCache(org.apache.ignite.IgniteCache) 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) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 87 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method testTxConflictGetAndReplace.

/**
 * @throws Exception If failed.
 */
@Test
public void testTxConflictGetAndReplace() 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);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object old = cache.getAndReplace(key, 2);
                        assertNull(old);
                        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 old = cache.getAndReplace(key, 2);
                    assertEquals(1, old);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object old = cache.getAndReplace(key, 2);
                    assertNull(old);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object old = cache.getAndReplace(key, 2);
                        assertNull(old);
                        updateKey(cache, key, 3);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 3, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object old = cache.getAndReplace(key, 2);
                        assertEquals(3, old);
                        txAsync(cache, OPTIMISTIC, SERIALIZABLE, 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)) {
                    Object old = cache.getAndReplace(key, 2);
                    assertEquals(1, old);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
            }
        } 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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 88 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions 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 {
    assert !updateNodes.isEmpty();
    final Ignite srv = ignite(1);
    final String cacheName = srv.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
    try {
        final int KEYS = SF.apply(20);
        final AtomicBoolean finished = new AtomicBoolean();
        IgniteInternalFuture<?> fut = restart ? restartFuture(finished, null) : null;
        try {
            for (int i = 0; i < SF.applyLB(10, 2); i++) {
                log.info("Iteration: " + i);
                final long stopTime = U.currentTimeMillis() + SF.applyLB(10_000, 1_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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 89 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method txNoConflictContainsKey.

/**
 * @param noVal If {@code true} there is no cache value when do update in tx.
 * @throws Exception If failed.
 */
private void txNoConflictContainsKey(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 (Integer key : keys) {
                log.info("Test key: " + key);
                if (!noVal)
                    cache.put(key, -1);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.containsKey(key);
                    assertEquals(!noVal, res);
                    updateKey(cache, key, 1);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.containsKey(key);
                    assertTrue(res);
                    updateKey(cache, key, 2);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.containsKey(key);
                    assertTrue(res);
                    tx.commit();
                }
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean res = cache.containsKey(key);
                    assertFalse(res);
                    updateKey(cache, key, 3);
                    tx.commit();
                }
                checkValue(key, 3, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 90 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method txConflictRead.

/**
 * @param noVal If {@code true} there is no cache value when read in tx.
 * @param needVer If {@code true} then gets entry, otherwise just value.
 * @throws Exception If failed.
 */
private void txConflictRead(boolean noVal, boolean needVer) 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 (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    if (needVer) {
                        CacheEntry<Integer, Integer> val = cache.getEntry(key);
                        assertEquals(expVal, val == null ? null : val.getValue());
                    } else {
                        Integer val = cache.get(key);
                        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)) {
                    if (needVer) {
                        CacheEntry<Integer, Integer> val = cache.getEntry(key);
                        assertEquals((Integer) 1, val.getValue());
                    } else {
                        Object val = cache.get(key);
                        assertEquals(1, val);
                    }
                    tx.commit();
                }
                checkValue(key, 1, 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)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)120 Transaction (org.apache.ignite.transactions.Transaction)116 Ignite (org.apache.ignite.Ignite)87 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 Test (org.junit.Test)49 IgniteCache (org.apache.ignite.IgniteCache)36 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)29 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)28 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)28 HashMap (java.util.HashMap)26 CacheException (javax.cache.CacheException)26 ArrayList (java.util.ArrayList)23 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)23 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)19 Map (java.util.Map)18 IgniteException (org.apache.ignite.IgniteException)18 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 HashSet (java.util.HashSet)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)16