Search in sources :

Example 21 with TransactionConcurrency

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

the class GridCacheTxLoadFromStoreOnLockSelfTest method checkLoadedValue.

/**
     * @throws Exception If failed.
     */
private void checkLoadedValue(int backups) throws Exception {
    CacheConfiguration<Integer, Integer> cacheCfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    cacheCfg.setCacheMode(CacheMode.PARTITIONED);
    cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    cacheCfg.setRebalanceMode(CacheRebalanceMode.SYNC);
    cacheCfg.setCacheStoreFactory(new StoreFactory());
    cacheCfg.setReadThrough(true);
    cacheCfg.setBackups(backups);
    cacheCfg.setLoadPreviousValue(true);
    IgniteCache<Integer, Integer> cache = ignite(0).createCache(cacheCfg);
    for (int i = 0; i < 10; i++) assertEquals((Integer) i, cache.get(i));
    cache.removeAll();
    assertEquals(0, cache.size());
    for (TransactionConcurrency conc : TransactionConcurrency.values()) {
        for (TransactionIsolation iso : TransactionIsolation.values()) {
            info("Checking transaction [conc=" + conc + ", iso=" + iso + ']');
            try (Transaction tx = ignite(0).transactions().txStart(conc, iso)) {
                for (int i = 0; i < 10; i++) assertEquals("Invalid value for transaction [conc=" + conc + ", iso=" + iso + ']', (Integer) i, cache.get(i));
                tx.commit();
            }
            cache.removeAll();
            assertEquals(0, cache.size());
        }
    }
    cache.destroy();
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 22 with TransactionConcurrency

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

the class GridCacheAbstractFullApiSelfTest method doTransformResourceInjectionInTx.

/**
     * @param ignite Node.
     * @param cache Cache.
     * @param async Use async API.
     * @param oldAsync Use old async API.
     * @throws Exception If failed.
     */
private void doTransformResourceInjectionInTx(Ignite ignite, IgniteCache<String, Integer> cache, boolean async, boolean oldAsync) throws Exception {
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            IgniteTransactions txs = ignite.transactions();
            try (Transaction tx = txs.txStart(concurrency, isolation)) {
                doTransformResourceInjection(ignite, cache, async, oldAsync);
                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 23 with TransactionConcurrency

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

the class IgniteTxCachePrimarySyncTest method singleKeyPrimaryNodeLeft.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void singleKeyPrimaryNodeLeft(CacheConfiguration<Object, Object> ccfg) throws Exception {
    Ignite ignite = ignite(0);
    IgniteCache<Object, Object> cache = ignite.createCache(ccfg);
    try {
        ignite(NODES - 1).createNearCache(ccfg.getName(), new NearCacheConfiguration<>());
        for (int i = 0; i < NODES; i++) {
            Ignite node = ignite(i);
            singleKeyPrimaryNodeLeft(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()) {
                    singleKeyPrimaryNodeLeft(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 24 with TransactionConcurrency

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

use of org.apache.ignite.transactions.TransactionConcurrency 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)

Aggregations

TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)48 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)42 Transaction (org.apache.ignite.transactions.Transaction)27 Ignite (org.apache.ignite.Ignite)18 IgniteTransactions (org.apache.ignite.IgniteTransactions)11 IgniteCache (org.apache.ignite.IgniteCache)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IgniteException (org.apache.ignite.IgniteException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CacheException (javax.cache.CacheException)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Callable (java.util.concurrent.Callable)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 HashSet (java.util.HashSet)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FactoryBuilder (javax.cache.configuration.FactoryBuilder)2 MutableEntry (javax.cache.processor.MutableEntry)2