Search in sources :

Example 66 with TransactionIsolation

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

the class CacheTxFastFinishTest method fastFinishTx.

/**
 * @param ignite Node.
 */
protected 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, true);
            }
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.get(i);
                checkNormalTxFinish(tx, commit, true);
            }
        }
        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, false);
                    }
                }
            }
        }
    }
}
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 67 with TransactionIsolation

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

the class GridCacheBinaryObjectsAbstractSelfTest method testKeepBinaryTxOverwrite.

/**
 * @throws Exception if failed.
 */
@Test
public void testKeepBinaryTxOverwrite() throws Exception {
    if (atomicityMode() != TRANSACTIONAL)
        return;
    IgniteCache<Integer, TestObject> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
    cache.put(0, new TestObject(1));
    for (TransactionConcurrency conc : TransactionConcurrency.values()) {
        for (TransactionIsolation iso : TransactionIsolation.values()) {
            try (Transaction tx = ignite(0).transactions().txStart(conc, iso)) {
                cache.withKeepBinary().get(0);
                cache.invoke(0, new ObjectEntryProcessor());
                tx.commit();
            }
        }
    }
}
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 68 with TransactionIsolation

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

the class IgnitePessimisticTxSuspendResumeTest method testExplicitLockAndSuspendResume.

/**
 * Test explicit locks, implicit transactions and suspend/resume of pessimistic transactions.
 *
 * @throws Exception If failed.
 */
@Test
public void testExplicitLockAndSuspendResume() throws Exception {
    // TODO: IGNITE-9324 Lock operations are not supported when MVCC is enabled.
    if (FORCE_MVCC)
        return;
    executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>() {

        @Override
        public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                List<Lock> locks = new ArrayList<>(10);
                for (int j = 0; j < 10; j++) {
                    cache.put(j, j);
                    Lock lock = cache.lock(j);
                    locks.add(lock);
                    lock.lock();
                    // Re-enter.
                    if (j >= 5) {
                        lock = cache.lock(j);
                        locks.add(lock);
                        lock.lock();
                    }
                    cache.put(j, j);
                }
                final Transaction tx = ignite.transactions().txStart(transactionConcurrency(), isolation);
                for (int j = 10; j < 20; j++) cache.put(j, j);
                tx.suspend();
                assertNull(cache.get(10));
                for (int j = 10; j < 20; j++) assertFalse("Locked key " + j, cache.lock(j).tryLock());
                for (int i = 0; i < 10; i++) {
                    final int key = i;
                    GridTestUtils.runAsync(() -> {
                        tx.resume();
                        cache.put(key + 10, key + 10);
                        cache.put(key + 20, key + 20);
                        tx.suspend();
                        assertFalse("Locked key " + key, cache.lock(key).tryLock());
                        assertFalse("Locked key " + (key + 10), cache.lock(key + 10).tryLock());
                        assertFalse("Locked key " + (key + 20), cache.lock(key + 20).tryLock());
                        cache.put(key + 30, key + 30);
                        Lock lock = cache.lock(key + 30);
                        assertTrue("Can't lock key " + (key + 30), lock.tryLock());
                        cache.put(key + 30, key + 30);
                        lock.unlock();
                        cache.put(key + 30, key + 30);
                    }).get(FUT_TIMEOUT);
                }
                for (int j = 10; j < 30; j++) assertFalse("Locked key " + j, cache.lock(j).tryLock());
                tx.resume();
                tx.commit();
                for (Lock lock : locks) lock.unlock();
                for (int i = 0; i < 30; i++) assertEquals(i, (int) cache.get(i));
                GridTestUtils.runAsync(() -> {
                    for (int j = 0; j < 40; j++) {
                        Lock lock = cache.lock(j);
                        assertTrue("Can't lock key " + j, lock.tryLock());
                        cache.put(j, j);
                        lock.unlock();
                        cache.put(j, j);
                    }
                }).get(FUT_TIMEOUT);
                cache.removeAll();
            }
        }
    });
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) Lock(java.util.concurrent.locks.Lock) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 69 with TransactionIsolation

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

the class IgniteTxCachePrimarySyncTest method singleKeyCommitFromPrimary.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void singleKeyCommitFromPrimary(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 {
        for (int i = 0; i < SRVS; i++) {
            Ignite node = ignite(i);
            singleKeyCommitFromPrimary(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;
                    singleKeyCommitFromPrimary(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)

Example 70 with TransactionIsolation

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

the class IgniteTxCachePrimarySyncTest method checkWaitPrimaryResponse.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void checkWaitPrimaryResponse(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());
            checkWaitPrimaryResponse(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {

                @Override
                public void apply(Integer key, IgniteCache<Object, Object> cache) {
                    cache.put(key, key);
                }
            });
            checkWaitPrimaryResponse(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {

                @Override
                public void apply(Integer key, IgniteCache<Object, Object> cache) {
                    Map<Integer, Integer> map = new HashMap<>();
                    for (int i = 0; i < 50; i++) map.put(i, i);
                    map.put(key, key);
                    cache.putAll(map);
                }
            });
            for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                for (final TransactionIsolation isolation : TransactionIsolation.values()) {
                    if (MvccFeatureChecker.forcedMvcc() && !MvccFeatureChecker.isSupported(concurrency, isolation))
                        continue;
                    checkWaitPrimaryResponse(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();
                            }
                        }
                    });
                    checkWaitPrimaryResponse(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {

                        @Override
                        public void apply(Integer key, IgniteCache<Object, Object> cache) {
                            Map<Integer, Integer> map = new HashMap<>();
                            for (int i = 0; i < 50; i++) map.put(i, i);
                            map.put(key, key);
                            Ignite ignite = cache.unwrap(Ignite.class);
                            try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
                                cache.putAll(map);
                                tx.commit();
                            }
                        }
                    });
                }
            }
        }
    } finally {
        ignite.destroyCache(cache.getName());
    }
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

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