Search in sources :

Example 51 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testTxCommitReadOnly1.

/**
 * @throws Exception If failed.
 */
public void testTxCommitReadOnly1() 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);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    tx.rollback();
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 1);
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    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 52 with IgniteTransactions

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

the class CacheTxFastFinishTest method fastFinishTx.

/**
 * @param ignite Node.
 */
private void fastFinishTx(Ignite ignite) {
    IgniteTransactions txs = ignite.transactions();
    IgniteCache cache = ignite.cache(DEFAULT_CACHE_NAME);
    for (boolean commit : new boolean[] { true, false }) {
        for (TransactionConcurrency c : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                try (Transaction tx = txs.txStart(c, isolation)) {
                    checkFastTxFinish(tx, commit);
                }
            }
        }
        for (int i = 0; i < 100; i++) {
            try (Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
                cache.get(i);
                checkFastTxFinish(tx, commit);
            }
            try (Transaction tx = txs.txStart(OPTIMISTIC, READ_COMMITTED)) {
                cache.get(i);
                checkFastTxFinish(tx, commit);
            }
        }
        for (int i = 0; i < 100; i++) {
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.get(i);
                checkNormalTxFinish(tx, commit);
            }
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.get(i);
                checkNormalTxFinish(tx, commit);
            }
        }
        for (int i = 0; i < 100; i++) {
            for (TransactionConcurrency c : TransactionConcurrency.values()) {
                for (TransactionIsolation isolation : TransactionIsolation.values()) {
                    try (Transaction tx = txs.txStart(c, isolation)) {
                        cache.put(i, i);
                        checkNormalTxFinish(tx, commit);
                    }
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 53 with IgniteTransactions

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

the class IgniteCacheConfigVariationsFullApiTest method checkSkipStoreWithTransaction.

/**
 * @param cache Cache instance.
 * @param cacheSkipStore Cache skip store projection.
 * @param data Data set.
 * @param keys Keys list.
 * @param txConcurrency Concurrency mode.
 * @param txIsolation Isolation mode.
 * @throws Exception If failed.
 */
private void checkSkipStoreWithTransaction(IgniteCache<String, Integer> cache, IgniteCache<String, Integer> cacheSkipStore, Map<String, Integer> data, List<String> keys, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation) throws Exception {
    info("Test tx skip store [concurrency=" + txConcurrency + ", isolation=" + txIsolation + ']');
    cache.removeAll(data.keySet());
    checkEmpty(cache, cacheSkipStore);
    IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
    Integer val = -1;
    // Several put check.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : keys) cacheSkipStore.put(key, val);
        for (String key : keys) {
            assertEquals(val, cacheSkipStore.get(key));
            assertEquals(val, cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        tx.commit();
    }
    for (String key : keys) {
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    assertEquals(0, storeStgy.getStoreSize());
    // cacheSkipStore putAll(..)/removeAll(..) check.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        cacheSkipStore.putAll(data);
        tx.commit();
    }
    for (String key : keys) {
        val = data.get(key);
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    storeStgy.putAllToStore(data);
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        cacheSkipStore.removeAll(data.keySet());
        tx.commit();
    }
    for (String key : keys) {
        assertNull(cacheSkipStore.get(key));
        assertNotNull(cache.get(key));
        assertTrue(storeStgy.isInStore(key));
        cache.remove(key);
    }
    assertTrue(storeStgy.getStoreSize() == 0);
    // cache putAll(..)/removeAll(..) check.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        cache.putAll(data);
        for (String key : keys) {
            assertNotNull(cacheSkipStore.get(key));
            assertNotNull(cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        cache.removeAll(data.keySet());
        for (String key : keys) {
            assertNull(cacheSkipStore.get(key));
            assertNull(cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        tx.commit();
    }
    assertTrue(storeStgy.getStoreSize() == 0);
    // putAll(..) from both cacheSkipStore and cache.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        Map<String, Integer> subMap = new HashMap<>();
        for (int i = 0; i < keys.size() / 2; i++) subMap.put(keys.get(i), i);
        cacheSkipStore.putAll(subMap);
        subMap.clear();
        for (int i = keys.size() / 2; i < keys.size(); i++) subMap.put(keys.get(i), i);
        cache.putAll(subMap);
        for (String key : keys) {
            assertNotNull(cacheSkipStore.get(key));
            assertNotNull(cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        tx.commit();
    }
    for (int i = 0; i < keys.size() / 2; i++) {
        String key = keys.get(i);
        assertNotNull(cacheSkipStore.get(key));
        assertNotNull(cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    for (int i = keys.size() / 2; i < keys.size(); i++) {
        String key = keys.get(i);
        assertNotNull(cacheSkipStore.get(key));
        assertNotNull(cache.get(key));
        assertTrue(storeStgy.isInStore(key));
    }
    cache.removeAll(data.keySet());
    for (String key : keys) {
        assertNull(cacheSkipStore.get(key));
        assertNull(cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    // Check that read-through is disabled when cacheSkipStore is used.
    for (int i = 0; i < keys.size(); i++) putToStore(keys.get(i), i);
    assertTrue(cacheSkipStore.size(ALL) == 0);
    assertTrue(cache.size(ALL) == 0);
    assertTrue(storeStgy.getStoreSize() != 0);
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        assertTrue(cacheSkipStore.getAll(data.keySet()).size() == 0);
        for (String key : keys) {
            assertNull(cacheSkipStore.get(key));
            if (txIsolation == READ_COMMITTED) {
                assertNotNull(cache.get(key));
                assertNotNull(cacheSkipStore.get(key));
            }
        }
        tx.commit();
    }
    cache.removeAll(data.keySet());
    val = -1;
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : data.keySet()) {
            storeStgy.putToStore(key, 0);
            assertNull(cacheSkipStore.invoke(key, new SetValueProcessor(val)));
        }
        tx.commit();
    }
    for (String key : data.keySet()) {
        assertEquals(0, storeStgy.getFromStore(key));
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
    }
    cache.removeAll(data.keySet());
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : data.keySet()) {
            storeStgy.putToStore(key, 0);
            assertTrue(cacheSkipStore.putIfAbsent(key, val));
        }
        tx.commit();
    }
    for (String key : data.keySet()) {
        assertEquals(0, storeStgy.getFromStore(key));
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
    }
    cache.removeAll(data.keySet());
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : data.keySet()) {
            storeStgy.putToStore(key, 0);
            assertNull(cacheSkipStore.getAndPut(key, val));
        }
        tx.commit();
    }
    for (String key : data.keySet()) {
        assertEquals(0, storeStgy.getFromStore(key));
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
    }
    cache.removeAll(data.keySet());
    checkEmpty(cache, cacheSkipStore);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 54 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method txConflictInvoke.

/**
 * @param noVal If {@code true} there is no cache value when read in tx.
 * @param rmv If {@code true} invoke does remove value, otherwise put.
 * @throws Exception If failed.
 */
private void txConflictInvoke(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 = cache.invoke(key, new SetValueProcessor(rmv ? null : 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 = cache.invoke(key, new SetValueProcessor(rmv ? null : 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 55 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testCrossCacheTx.

/**
 * @throws Exception If failed.
 */
public void testCrossCacheTx() throws Exception {
    Ignite ignite0 = ignite(0);
    final String CACHE1 = "cache1";
    final String CACHE2 = "cache2";
    try {
        CacheConfiguration<Integer, Integer> ccfg1 = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
        ccfg1.setName(CACHE1);
        ignite0.createCache(ccfg1);
        CacheConfiguration<Integer, Integer> ccfg2 = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
        ccfg2.setName(CACHE2);
        ignite0.createCache(ccfg2);
        Integer newVal = 0;
        List<Integer> keys = testKeys(ignite0.<Integer, Integer>cache(CACHE1));
        for (Ignite ignite : G.allGrids()) {
            log.info("Test node: " + ignite.name());
            IgniteCache<Integer, Integer> cache1 = ignite.cache(CACHE1);
            IgniteCache<Integer, Integer> cache2 = ignite.cache(CACHE2);
            IgniteTransactions txs = ignite.transactions();
            for (Integer key : keys) {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key, newVal);
                    cache2.put(key, newVal);
                    tx.commit();
                }
                checkValue(key, newVal, CACHE1);
                checkValue(key, newVal, CACHE2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key);
                    Object val2 = cache2.get(key);
                    assertEquals(newVal, val1);
                    assertEquals(newVal, val2);
                    tx.commit();
                }
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key, newVal + 1);
                    cache2.put(key, newVal + 1);
                    tx.rollback();
                }
                checkValue(key, newVal, CACHE1);
                checkValue(key, newVal, CACHE2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key);
                    Object val2 = cache2.get(key);
                    assertEquals(newVal, val1);
                    assertEquals(newVal, val2);
                    cache1.put(key, newVal + 1);
                    cache2.put(key, newVal + 1);
                    tx.commit();
                }
                newVal++;
                checkValue(key, newVal, CACHE1);
                checkValue(key, newVal, CACHE2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key, newVal);
                    cache2.put(-key, newVal);
                    tx.commit();
                }
                checkValue(key, newVal, CACHE1);
                checkValue(-key, null, CACHE1);
                checkValue(key, newVal, CACHE2);
                checkValue(-key, newVal, CACHE2);
            }
            newVal++;
            Integer key1 = primaryKey(ignite(0).cache(CACHE1));
            Integer key2 = primaryKey(ignite(1).cache(CACHE1));
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache1.put(key1, newVal);
                cache1.put(key2, newVal);
                cache2.put(key1, newVal);
                cache2.put(key2, newVal);
                tx.commit();
            }
            checkValue(key1, newVal, CACHE1);
            checkValue(key2, newVal, CACHE1);
            checkValue(key1, newVal, CACHE2);
            checkValue(key2, newVal, CACHE2);
            CountDownLatch latch = new CountDownLatch(1);
            IgniteInternalFuture<?> fut = lockKey(latch, cache1, key1);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key1, newVal + 1);
                    cache2.put(key1, newVal + 1);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            latch.countDown();
            fut.get();
            checkValue(key1, 1, CACHE1);
            checkValue(key1, newVal, CACHE2);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache1.put(key1, newVal + 1);
                cache2.put(key1, newVal + 1);
                tx.commit();
            }
            newVal++;
            cache1.put(key2, newVal);
            cache2.put(key2, newVal);
            checkValue(key1, newVal, CACHE1);
            checkValue(key1, newVal, CACHE2);
            latch = new CountDownLatch(1);
            fut = lockKey(latch, cache1, key1);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key1, newVal + 1);
                    cache2.put(key2, newVal + 1);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            latch.countDown();
            fut.get();
            checkValue(key1, 1, CACHE1);
            checkValue(key2, newVal, CACHE2);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key1);
                    Object val2 = cache2.get(key2);
                    assertEquals(1, val1);
                    assertEquals(newVal, val2);
                    updateKey(cache2, key2, 1);
                    cache1.put(key1, newVal + 1);
                    cache2.put(key2, newVal + 1);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            checkValue(key1, 1, CACHE1);
            checkValue(key2, 1, CACHE2);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                Object val1 = cache1.get(key1);
                Object val2 = cache2.get(key2);
                assertEquals(1, val1);
                assertEquals(1, val2);
                cache1.put(key1, newVal + 1);
                cache2.put(key2, newVal + 1);
                tx.commit();
            }
            newVal++;
            checkValue(key1, newVal, CACHE1);
            checkValue(key2, newVal, CACHE2);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key1);
                    Object val2 = cache2.get(key2);
                    assertEquals(newVal, val1);
                    assertEquals(newVal, val2);
                    updateKey(cache2, key2, newVal);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                Object val1 = cache1.get(key1);
                Object val2 = cache2.get(key2);
                assertEquals(newVal, val1);
                assertEquals(newVal, val2);
                tx.commit();
            }
        }
    } finally {
        destroyCache(CACHE1);
        destroyCache(CACHE2);
    }
}
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) CountDownLatch(java.util.concurrent.CountDownLatch)

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