Search in sources :

Example 71 with TransactionIsolation

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

the class IgniteTxCachePrimarySyncTest method checkTxSyncMode.

/**
 * @param ignite Node.
 * @param commit If {@code true} commits transaction.
 */
private void checkTxSyncMode(Ignite ignite, boolean commit) {
    IgniteTransactions txs = ignite.transactions();
    IgniteCache<Object, Object> fullSync1 = ignite.cache("fullSync1");
    IgniteCache<Object, Object> fullSync2 = ignite.cache("fullSync2");
    IgniteCache<Object, Object> fullAsync1 = ignite.cache("fullAsync1");
    IgniteCache<Object, Object> fullAsync2 = ignite.cache("fullAsync2");
    IgniteCache<Object, Object> primarySync1 = ignite.cache("primarySync1");
    IgniteCache<Object, Object> primarySync2 = ignite.cache("primarySync2");
    for (int i = 0; i < 3; i++) {
        int key = 0;
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                if (MvccFeatureChecker.forcedMvcc() && !MvccFeatureChecker.isSupported(concurrency, isolation))
                    continue;
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullSync2.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    fullAsync2.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    primarySync2.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    fullAsync2.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 72 with TransactionIsolation

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

the class IgniteTxCachePrimarySyncTest method singleKeyCommit.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void singleKeyCommit(CacheConfiguration<Object, Object> ccfg) throws Exception {
    if (MvccFeatureChecker.forcedMvcc()) {
        if (ccfg.getCacheStoreFactory() != null && !MvccFeatureChecker.isSupported(Feature.CACHE_STORE))
            return;
        if (ccfg.getNearConfiguration() != null && !MvccFeatureChecker.isSupported(Feature.NEAR_CACHE))
            return;
    }
    Ignite ignite = ignite(0);
    IgniteCache<Object, Object> cache = ignite.createCache(ccfg);
    try {
        if (!MvccFeatureChecker.forcedMvcc() || MvccFeatureChecker.isSupported(Feature.NEAR_CACHE))
            ignite(NODES - 1).createNearCache(ccfg.getName(), new NearCacheConfiguration<>());
        for (int i = 1; i < NODES; i++) {
            Ignite node = ignite(i);
            log.info("Test node: " + node.name());
            singleKeyCommit(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {

                @Override
                public void apply(Integer key, IgniteCache<Object, Object> cache) {
                    cache.put(key, key);
                }
            });
            for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                for (final TransactionIsolation isolation : TransactionIsolation.values()) {
                    if (MvccFeatureChecker.forcedMvcc() && !MvccFeatureChecker.isSupported(concurrency, isolation))
                        continue;
                    singleKeyCommit(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {

                        @Override
                        public void apply(Integer key, IgniteCache<Object, Object> cache) {
                            Ignite ignite = cache.unwrap(Ignite.class);
                            try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
                                cache.put(key, key);
                                tx.commit();
                            }
                        }
                    });
                }
            }
        }
    } finally {
        ignite.destroyCache(cache.getName());
    }
}
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) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration)

Example 73 with TransactionIsolation

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

the class OnePhaseCommitAndNodeLeftTest method testTxOneBackups.

/**
 * Tests an explicit transaction on a cache with one backup.
 *
 * @throws Exception If failed.
 */
@Test
public void testTxOneBackups() throws Exception {
    backups = 1;
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            startTransactionAndFailPrimary((ignite, key, val) -> {
                info("Tx pu: [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
                    ignite.cache(DEFAULT_CACHE_NAME).put(key, val);
                    tx.commit();
                }
            });
            stopAllGrids();
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 74 with TransactionIsolation

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

the class GridCacheEmptyEntriesAbstractSelfTest method checkPolicy0.

/**
 * Tests preset eviction policy.
 *
 * @throws Exception If failed.
 */
private void checkPolicy0() throws Exception {
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        txConcurrency = concurrency;
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            txIsolation = isolation;
            if (MvccFeatureChecker.forcedMvcc() && !MvccFeatureChecker.isSupported(concurrency, isolation))
                continue;
            Ignite g = startGrids();
            IgniteCache<String, String> cache = g.cache(DEFAULT_CACHE_NAME);
            try {
                info(">>> Checking policy [txConcurrency=" + txConcurrency + ", txIsolation=" + txIsolation + ", plc=" + plc + ", nearPlc=" + nearPlc + ']');
                checkExplicitTx(g, cache);
                checkImplicitTx(cache);
            } finally {
                stopAllGrids();
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite)

Example 75 with TransactionIsolation

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

the class IgniteCacheNoReadThroughAbstractTest method testNoReadThrough.

/**
 * @throws Exception If failed.
 */
@Test
public void testNoReadThrough() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    for (Integer key : keys()) {
        log.info("Test [key=" + key + ']');
        storeMap.put(key, key);
        assertNull(cache.get(key));
        assertEquals(key, storeMap.get(key));
        assertNull(cache.getAndPut(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertTrue(cache.putIfAbsent(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertNull(cache.getAndRemove(key));
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertNull(cache.getAndPutIfAbsent(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

            @Override
            public Object process(MutableEntry<Integer, Integer> e, Object... args) {
                Integer val = e.getValue();
                assertFalse(e.exists());
                assertNull(val);
                e.setValue(-1);
                return String.valueOf(val);
            }
        });
        assertEquals("null", ret);
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertFalse(cache.replace(key, -1));
        assertEquals(key, storeMap.get(key));
        assertNull(cache.getAndReplace(key, -1));
        assertEquals(key, storeMap.get(key));
        assertFalse(cache.replace(key, key, -1));
        assertEquals(key, storeMap.get(key));
    }
    Set<Integer> keys = new HashSet<>();
    for (int i = 1000_0000; i < 1000_0000 + 1000; i++) {
        keys.add(i);
        storeMap.put(i, i);
    }
    assertTrue(cache.getAll(keys).isEmpty());
    if (atomicityMode() == TRANSACTIONAL) {
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                for (Integer key : keys()) {
                    log.info("Test tx [key=" + key + ", concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertNull(cache.get(key));
                        tx.commit();
                    }
                    assertEquals(key, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertNull(cache.getAndPut(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    cache.remove(key);
                    assertNull(storeMap.get(key));
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.putIfAbsent(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    cache.remove(key);
                    assertNull(storeMap.get(key));
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

                            @Override
                            public Object process(MutableEntry<Integer, Integer> e, Object... args) {
                                Integer val = e.getValue();
                                assertFalse(e.exists());
                                assertNull(val);
                                e.setValue(-1);
                                return String.valueOf(val);
                            }
                        });
                        assertEquals("null", ret);
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.getAll(keys).isEmpty());
                        tx.commit();
                    }
                }
            }
        }
    }
    // Check can load cache when read-through is disabled.
    allowLoad = true;
    Integer key = 1;
    cache.remove(key);
    storeMap.clear();
    storeMap.put(key, 10);
    cache.loadCache(null);
    assertEquals(10, (int) cache.get(key));
    cache.remove(key);
    storeMap.put(key, 11);
    CompletionListenerFuture fut = new CompletionListenerFuture();
    cache.loadAll(F.asSet(key), true, fut);
    fut.get();
    assertEquals(11, (int) cache.get(key));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Aggregations

TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)88 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)71 Test (org.junit.Test)53 Transaction (org.apache.ignite.transactions.Transaction)52 Ignite (org.apache.ignite.Ignite)33 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)27 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)12 ArrayList (java.util.ArrayList)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgniteEx (org.apache.ignite.internal.IgniteEx)9 IgniteException (org.apache.ignite.IgniteException)8 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)7 HashMap (java.util.HashMap)6 Map (java.util.Map)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 TransactionTimeoutException (org.apache.ignite.transactions.TransactionTimeoutException)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4