Search in sources :

Example 51 with Transaction

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

the class CacheSerializableTransactionsTest method testTxConflictInvokeAll.

/**
 * @throws Exception If failed
 */
public void testTxConflictInvokeAll() throws Exception {
    Ignite ignite0 = ignite(0);
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
            final Integer key1 = primaryKey(ignite(0).cache(cache0.getName()));
            final Integer key2 = primaryKey(ignite(1).cache(cache0.getName()));
            Map<Integer, Integer> vals = new HashMap<>();
            int newVal = 0;
            for (Ignite ignite : G.allGrids()) {
                log.info("Test node: " + ignite.name());
                IgniteTransactions txs = ignite.transactions();
                IgniteCache<Integer, Integer> cache = ignite.cache(cache0.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Map<Integer, EntryProcessorResult<Integer>> res = cache.invokeAll(F.asSet(key1, key2), new SetValueProcessor(newVal));
                    if (!vals.isEmpty()) {
                        EntryProcessorResult<Integer> res1 = res.get(key1);
                        assertNotNull(res1);
                        assertEquals(vals.get(key1), res1.get());
                        EntryProcessorResult<Integer> res2 = res.get(key2);
                        assertNotNull(res2);
                        assertEquals(vals.get(key2), res2.get());
                    } else
                        assertTrue(res.isEmpty());
                    tx.commit();
                }
                checkValue(key1, newVal, cache.getName());
                checkValue(key2, newVal, cache.getName());
                vals.put(key1, newVal);
                vals.put(key2, newVal);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Map<Integer, EntryProcessorResult<Integer>> res = cache.invokeAll(F.asSet(key1, key2), new SetValueProcessor(newVal + 1));
                        EntryProcessorResult<Integer> res1 = res.get(key1);
                        assertNotNull(res1);
                        assertEquals(vals.get(key1), res1.get());
                        EntryProcessorResult<Integer> res2 = res.get(key2);
                        assertNotNull(res2);
                        assertEquals(vals.get(key2), res2.get());
                        updateKey(cache0, key1, -1);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key1, -1, cache.getName());
                checkValue(key2, newVal, cache.getName());
                vals.put(key1, -1);
                vals.put(key2, newVal);
                newVal++;
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 52 with Transaction

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

the class CacheSerializableTransactionsTest method testTxCommitReadOnlyGetAll.

/**
 * @throws Exception If failed.
 */
private void testTxCommitReadOnlyGetAll(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);
            Set<Integer> keys = new HashSet<>();
            for (int i = 0; i < 100; i++) keys.add(i);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                if (needVer) {
                    Collection<CacheEntry<Integer, Integer>> c = cache.getEntries(keys);
                    assertTrue(c.isEmpty());
                } else {
                    Map<Integer, Integer> map = cache.getAll(keys);
                    assertTrue(map.isEmpty());
                }
                tx.commit();
            }
            for (Integer key : keys) checkValue(key, null, cache.getName());
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                if (needVer) {
                    Collection<CacheEntry<Integer, Integer>> c = cache.getEntries(keys);
                    assertTrue(c.isEmpty());
                } else {
                    Map<Integer, Integer> map = cache.getAll(keys);
                    assertTrue(map.isEmpty());
                }
                tx.rollback();
            }
            for (Integer key : keys) checkValue(key, null, 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) CacheEntry(org.apache.ignite.cache.CacheEntry) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 53 with Transaction

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

the class CacheSerializableTransactionsTest method testTxConflictReadWrite3.

/**
 * @throws Exception If failed.
 */
public void testTxConflictReadWrite3() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
        List<Integer> readKeys = new ArrayList<>();
        List<Integer> writeKeys = new ArrayList<>();
        readKeys.add(primaryKey(cache));
        writeKeys.add(primaryKeys(cache, 1, 1000_0000).get(0));
        if (ccfg.getBackups() > 0) {
            readKeys.add(backupKey(cache));
            writeKeys.add(backupKeys(cache, 1, 1000_0000).get(0));
        }
        if (ccfg.getCacheMode() == PARTITIONED) {
            readKeys.add(nearKey(cache));
            writeKeys.add(nearKeys(cache, 1, 1000_0000).get(0));
        }
        try {
            for (Integer readKey : readKeys) {
                for (Integer writeKey : writeKeys) {
                    try {
                        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                            cache.get(readKey);
                            cache.put(writeKey, writeKey);
                            updateKey(cache, readKey, 0);
                            tx.commit();
                        }
                        fail();
                    } catch (TransactionOptimisticException ignored) {
                    // Expected exception.
                    }
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        cache.get(readKey);
                        cache.put(writeKey, writeKey);
                        tx.commit();
                    }
                }
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 54 with Transaction

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

the class CacheSerializableTransactionsTest method testTxConflictReplace.

/**
 * @throws Exception If failed.
 */
public void testTxConflictReplace() 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)) {
                        boolean replace = cache.replace(key, 2);
                        assertFalse(replace);
                        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)) {
                    boolean replace = cache.replace(key, 2);
                    assertTrue(replace);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 2);
                    assertFalse(replace);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean replace = cache.replace(key, 2);
                        assertFalse(replace);
                        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)) {
                        boolean replace = cache.replace(key, 2);
                        assertTrue(replace);
                        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)) {
                    boolean replace = cache.replace(key, 2);
                    assertTrue(replace);
                    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 55 with Transaction

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

the class CacheSerializableTransactionsTest method txNoConflictUpdate.

/**
 * @throws Exception If failed.
 * @param noVal If {@code true} there is no cache value when do update in tx.
 * @param rmv If {@code true} tests remove, otherwise put.
 * @param getAfterUpdate If {@code true} tries to get value in tx after update.
 */
private void txNoConflictUpdate(boolean noVal, boolean rmv, boolean getAfterUpdate) 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)) {
                    if (rmv)
                        cache.remove(key);
                    else
                        cache.put(key, 2);
                    if (getAfterUpdate) {
                        Object val = cache.get(key);
                        if (rmv)
                            assertNull(val);
                        else
                            assertEquals(2, val);
                    }
                    if (!rmv)
                        updateKey(cache, key, 1);
                    tx.commit();
                }
                checkValue(key, rmv ? null : 2, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache.put(key, 3);
                    tx.commit();
                }
                checkValue(key, 3, cache.getName());
            }
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < 100; i++) map.put(i, i);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                if (rmv)
                    cache.removeAll(map.keySet());
                else
                    cache.putAll(map);
                if (getAfterUpdate) {
                    Map<Integer, Integer> res = cache.getAll(map.keySet());
                    if (rmv) {
                        for (Integer key : map.keySet()) assertNull(res.get(key));
                    } else {
                        for (Integer key : map.keySet()) assertEquals(map.get(key), res.get(key));
                    }
                }
                txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                    @Override
                    public Void apply(IgniteCache<Integer, Integer> cache) {
                        Map<Integer, Integer> map = new HashMap<>();
                        for (int i = 0; i < 100; i++) map.put(i, -1);
                        cache.putAll(map);
                        return null;
                    }
                });
                tx.commit();
            }
            for (int i = 0; i < 100; i++) checkValue(i, rmv ? null : i, cache.getName());
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Aggregations

Transaction (org.apache.ignite.transactions.Transaction)493 Ignite (org.apache.ignite.Ignite)204 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)183 IgniteCache (org.apache.ignite.IgniteCache)88 IgniteTransactions (org.apache.ignite.IgniteTransactions)78 IgniteException (org.apache.ignite.IgniteException)74 CacheException (javax.cache.CacheException)60 HashMap (java.util.HashMap)54 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)45 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)44 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)42 ArrayList (java.util.ArrayList)41 Callable (java.util.concurrent.Callable)41 Map (java.util.Map)36 IgniteEx (org.apache.ignite.internal.IgniteEx)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 IgniteKernal (org.apache.ignite.internal.IgniteKernal)30 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)30 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29