Search in sources :

Example 56 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions 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 57 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testTxConflictGetAndReplace.

/**
 * @throws Exception If failed.
 */
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)

Example 58 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testConflictResolution.

/**
 * @throws Exception If failed.
 */
public void testConflictResolution() throws Exception {
    final Ignite ignite = ignite(0);
    final String cacheName = ignite.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
    try {
        final Map<Integer, Integer> keys = new HashMap<>();
        for (int i = 0; i < 500; i++) keys.put(i, i);
        final int THREADS = 5;
        for (int i = 0; i < 10; i++) {
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            final AtomicInteger commitCntr = new AtomicInteger(0);
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
                    IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        cache.putAll(keys);
                        barrier.await();
                        tx.commit();
                        commitCntr.incrementAndGet();
                    } catch (TransactionOptimisticException e) {
                        log.info("Optimistic error: " + e);
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            int commits = commitCntr.get();
            log.info("Iteration [iter=" + i + ", commits=" + commits + ']');
            assertTrue(commits > 0);
        }
    } finally {
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite)

Example 59 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testTxCommitReadWriteTwoNodes.

/**
 * @throws Exception If failed.
 */
public void testTxCommitReadWriteTwoNodes() 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);
            Integer key0 = primaryKey(ignite(0).cache(DEFAULT_CACHE_NAME));
            Integer key1 = primaryKey(ignite(1).cache(DEFAULT_CACHE_NAME));
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.put(key0, key0);
                cache.get(key1);
                tx.commit();
            }
        } 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 60 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions 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)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)81 Transaction (org.apache.ignite.transactions.Transaction)78 Ignite (org.apache.ignite.Ignite)56 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)54 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)28 IgniteCache (org.apache.ignite.IgniteCache)22 IgniteException (org.apache.ignite.IgniteException)18 CacheException (javax.cache.CacheException)17 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)13 HashMap (java.util.HashMap)12 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)10 ArrayList (java.util.ArrayList)9 Callable (java.util.concurrent.Callable)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 LinkedHashMap (java.util.LinkedHashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8