Search in sources :

Example 21 with TransactionIsolation

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

the class IgniteCacheContainsKeyAbstractSelfTest method testContainsInTx.

/**
     * @throws Exception If failed.
     */
public void testContainsInTx() throws Exception {
    if (atomicityMode() == TRANSACTIONAL) {
        String key = "1";
        for (int i = 0; i < gridCount(); i++) assertFalse("Invalid result on grid: " + i, jcache(i).containsKey(key));
        IgniteCache<String, Integer> cache = jcache(0);
        for (TransactionConcurrency conc : TransactionConcurrency.values()) {
            for (TransactionIsolation iso : TransactionIsolation.values()) {
                try (Transaction tx = grid(0).transactions().txStart(conc, iso)) {
                    assertFalse("Invalid result on grid inside tx", cache.containsKey(key));
                    assertFalse("Key was enlisted to transaction: " + tx, txContainsKey(tx, key));
                    cache.put(key, 1);
                    assertTrue("Invalid result on grid inside tx", cache.containsKey(key));
                // Do not commit.
                }
                for (int i = 0; i < gridCount(); i++) assertFalse("Invalid result on grid: " + i, jcache(i).containsKey(key));
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Example 22 with TransactionIsolation

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

the class IgniteCacheClientNodeChangingTopologyTest method multinode.

/**
     * @param atomicityMode Atomicity mode cache.
     * @param testType Test type.
     * @throws Exception If failed.
     */
private void multinode(CacheAtomicityMode atomicityMode, final TestType testType) throws Exception {
    ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setRebalanceMode(SYNC);
    final int SRV_CNT = 4;
    for (int i = 0; i < SRV_CNT; i++) startGrid(i);
    final int CLIENT_CNT = 4;
    final List<Ignite> clients = new ArrayList<>();
    client = true;
    for (int i = 0; i < CLIENT_CNT; i++) {
        Ignite ignite = startGrid(SRV_CNT + i);
        assertTrue(ignite.configuration().isClientMode());
        clients.add(ignite);
    }
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicInteger threadIdx = new AtomicInteger(0);
    final int THREADS = CLIENT_CNT * 3;
    final ConcurrentHashSet<Integer> putKeys = new ConcurrentHashSet<>();
    IgniteInternalFuture<?> fut;
    try {
        fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int clientIdx = threadIdx.getAndIncrement() % CLIENT_CNT;
                Ignite ignite = clients.get(clientIdx);
                assertTrue(ignite.configuration().isClientMode());
                Thread.currentThread().setName("update-thread-" + ignite.name());
                IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
                boolean useTx = testType == TestType.OPTIMISTIC_TX || testType == TestType.OPTIMISTIC_SERIALIZABLE_TX || testType == TestType.PESSIMISTIC_TX;
                if (useTx || testType == TestType.LOCK) {
                    assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
                }
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                int cntr = 0;
                while (!stop.get()) {
                    TreeMap<Integer, Integer> map = new TreeMap<>();
                    for (int i = 0; i < 100; i++) {
                        Integer key = rnd.nextInt(0, 1000);
                        map.put(key, rnd.nextInt());
                    }
                    try {
                        if (testType == TestType.LOCK) {
                            Lock lock = cache.lockAll(map.keySet());
                            lock.lock();
                            lock.unlock();
                        } else {
                            if (useTx) {
                                IgniteTransactions txs = ignite.transactions();
                                TransactionConcurrency concurrency = testType == TestType.PESSIMISTIC_TX ? PESSIMISTIC : OPTIMISTIC;
                                TransactionIsolation isolation = testType == TestType.OPTIMISTIC_SERIALIZABLE_TX ? SERIALIZABLE : REPEATABLE_READ;
                                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                                    cache.putAll(map);
                                    tx.commit();
                                }
                            } else
                                cache.putAll(map);
                            putKeys.addAll(map.keySet());
                        }
                    } catch (CacheException | IgniteException e) {
                        log.info("Operation failed, ignore: " + e);
                    }
                    if (++cntr % 100 == 0)
                        log.info("Iteration: " + cntr);
                    if (updateBarrier != null)
                        updateBarrier.await();
                }
                return null;
            }
        }, THREADS, "update-thread");
        long stopTime = System.currentTimeMillis() + 60_000;
        while (System.currentTimeMillis() < stopTime) {
            boolean restartClient = ThreadLocalRandom.current().nextBoolean();
            Integer idx = null;
            if (restartClient) {
                log.info("Start client node.");
                client = true;
                IgniteEx ignite = startGrid(SRV_CNT + CLIENT_CNT);
                IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
                assertNotNull(cache);
            } else {
                idx = ThreadLocalRandom.current().nextInt(0, SRV_CNT);
                log.info("Stop server node: " + idx);
                stopGrid(idx);
            }
            updateBarrier = new CyclicBarrier(THREADS + 1, new Runnable() {

                @Override
                public void run() {
                    updateBarrier = null;
                }
            });
            try {
                updateBarrier.await(30_000, TimeUnit.MILLISECONDS);
            } catch (TimeoutException ignored) {
                log.error("Failed to wait for update.");
                for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).dumpDebugInfo();
                U.dumpThreads(log);
                CyclicBarrier barrier0 = updateBarrier;
                if (barrier0 != null)
                    barrier0.reset();
                fail("Failed to wait for update.");
            }
            U.sleep(500);
            if (restartClient) {
                log.info("Stop client node.");
                stopGrid(SRV_CNT + CLIENT_CNT);
            } else {
                log.info("Start server node: " + idx);
                client = false;
                startGrid(idx);
            }
            updateBarrier = new CyclicBarrier(THREADS + 1, new Runnable() {

                @Override
                public void run() {
                    updateBarrier = null;
                }
            });
            try {
                updateBarrier.await(30_000, TimeUnit.MILLISECONDS);
            } catch (TimeoutException ignored) {
                log.error("Failed to wait for update.");
                for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).dumpDebugInfo();
                U.dumpThreads(log);
                CyclicBarrier barrier0 = updateBarrier;
                if (barrier0 != null)
                    barrier0.reset();
                fail("Failed to wait for update.");
            }
            U.sleep(500);
        }
    } finally {
        stop.set(true);
    }
    fut.get(30_000);
    if (testType != TestType.LOCK)
        checkData(null, putKeys, grid(SRV_CNT).cache(DEFAULT_CACHE_NAME), SRV_CNT + CLIENT_CNT);
}
Also used : CacheException(javax.cache.CacheException) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) IgniteKernal(org.apache.ignite.internal.IgniteKernal) TreeMap(java.util.TreeMap) Lock(java.util.concurrent.locks.Lock) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 23 with TransactionIsolation

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

the class CachePutIfAbsentTest method testTxConflictGetAndPutIfAbsent.

/**
     * @throws Exception If failed.
     */
public void testTxConflictGetAndPutIfAbsent() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 10; i++) {
                Integer key = rnd.nextInt(10_000);
                cache.put(key, 2);
                for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                    for (TransactionIsolation isolation : TransactionIsolation.values()) {
                        try (Transaction tx = txs.txStart(concurrency, isolation)) {
                            Object old = cache.getAndPutIfAbsent(key, 3);
                            assertEquals(2, old);
                            Object val = cache.get(key);
                            assertEquals(2, val);
                            tx.commit();
                        }
                        assertEquals((Integer) 2, cache.get(key));
                    }
                }
            }
        } finally {
            ignite0.destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 24 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation 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 25 with TransactionIsolation

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

the class IgniteOnePhaseCommitNearSelfTest method checkKey.

/**
     * @param transactions Transactions instance.
     * @param cache Cache instance.
     * @param key Key.
     */
private void checkKey(IgniteTransactions transactions, Cache<Object, Object> cache, int key) throws Exception {
    cache.put(key, key);
    finalCheck(key, true);
    TransactionIsolation[] isolations = { READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE };
    TransactionConcurrency[] concurrencies = { OPTIMISTIC, PESSIMISTIC };
    for (TransactionIsolation isolation : isolations) {
        for (TransactionConcurrency concurrency : concurrencies) {
            info("Checking transaction [isolation=" + isolation + ", concurrency=" + concurrency + ']');
            try (Transaction tx = transactions.txStart(concurrency, isolation)) {
                cache.put(key, isolation + "-" + concurrency);
                tx.commit();
            }
            finalCheck(key, true);
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Aggregations

TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)42 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)42 Transaction (org.apache.ignite.transactions.Transaction)24 Ignite (org.apache.ignite.Ignite)15 IgniteTransactions (org.apache.ignite.IgniteTransactions)9 IgniteCache (org.apache.ignite.IgniteCache)6 HashMap (java.util.HashMap)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 HashSet (java.util.HashSet)2 IgniteException (org.apache.ignite.IgniteException)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 IgniteTxHeuristicCheckedException (org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException)2 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Callable (java.util.concurrent.Callable)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1