Search in sources :

Example 11 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testTxCommit.

/**
     * @throws Exception If failed.
     */
public void testTxCommit() throws Exception {
    Ignite ignite0 = ignite(0);
    Ignite ignite1 = ignite(1);
    final IgniteTransactions txs0 = ignite0.transactions();
    final IgniteTransactions txs1 = ignite1.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
            IgniteCache<Integer, Integer> cache1 = ignite1.cache(ccfg.getName());
            List<Integer> keys = testKeys(cache0);
            for (Integer key : keys) {
                log.info("Test key: " + key);
                Integer expVal = null;
                for (int i = 0; i < 100; i++) {
                    try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache0.get(key);
                        assertEquals(expVal, val);
                        cache0.put(key, i);
                        tx.commit();
                        expVal = i;
                    }
                    try (Transaction tx = txs1.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache1.get(key);
                        assertEquals(expVal, val);
                        cache1.put(key, val);
                        tx.commit();
                    }
                    try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache0.get(key);
                        assertEquals(expVal, val);
                        cache0.put(key, val);
                        tx.commit();
                    }
                }
                checkValue(key, expVal, cache0.getName());
                cache0.remove(key);
                try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache0.get(key);
                    assertNull(val);
                    cache0.put(key, expVal + 1);
                    tx.commit();
                }
                checkValue(key, expVal + 1, cache0.getName());
            }
        } 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 12 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method lockKey.

/**
     * @param releaseLatch Release lock latch.
     * @param cache Cache.
     * @param key Key.
     * @return Future.
     * @throws Exception If failed.
     */
private IgniteInternalFuture<?> lockKey(final CountDownLatch releaseLatch, final IgniteCache<Integer, Integer> cache, final Integer key) throws Exception {
    final CountDownLatch lockLatch = new CountDownLatch(1);
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.put(key, 1);
                log.info("Locked key: " + key);
                lockLatch.countDown();
                assertTrue(releaseLatch.await(100000, SECONDS));
                log.info("Commit tx: " + key);
                tx.commit();
            }
            return null;
        }
    }, "lock-thread");
    assertTrue(lockLatch.await(10, SECONDS));
    return fut;
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 13 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method txStreamerLoad.

/**
     * @param ignite Node.
     * @param key Key.
     * @param cacheName Cache name.
     * @param allowOverwrite Streamer flag.
     * @throws Exception If failed.
     */
private void txStreamerLoad(Ignite ignite, Integer key, String cacheName, boolean allowOverwrite) throws Exception {
    IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
    log.info("Test key: " + key);
    Integer loadVal = -1;
    IgniteTransactions txs = ignite.transactions();
    try (IgniteDataStreamer<Integer, Integer> streamer = ignite.dataStreamer(cache.getName())) {
        streamer.allowOverwrite(allowOverwrite);
        streamer.addData(key, loadVal);
    }
    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
        Integer val = cache.get(key);
        assertEquals(loadVal, val);
        tx.commit();
    }
    checkValue(key, loadVal, cache.getName());
    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
        Integer val = cache.get(key);
        assertEquals(loadVal, val);
        cache.put(key, 0);
        tx.commit();
    }
    checkValue(key, 0, cache.getName());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 14 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testTxConflictInvokeAll.

/**
     * @throws Exception If failed
     */
public void testTxConflictInvokeAll() throws Exception {
    Ignite ignite0 = ignite(0);
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
            final Integer key1 = primaryKey(ignite(0).cache(cache0.getName()));
            final Integer key2 = primaryKey(ignite(1).cache(cache0.getName()));
            Map<Integer, Integer> vals = new HashMap<>();
            int newVal = 0;
            for (Ignite ignite : G.allGrids()) {
                log.info("Test node: " + ignite.name());
                IgniteTransactions txs = ignite.transactions();
                IgniteCache<Integer, Integer> cache = ignite.cache(cache0.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Map<Integer, EntryProcessorResult<Integer>> res = cache.invokeAll(F.asSet(key1, key2), new SetValueProcessor(newVal));
                    if (!vals.isEmpty()) {
                        EntryProcessorResult<Integer> res1 = res.get(key1);
                        assertNotNull(res1);
                        assertEquals(vals.get(key1), res1.get());
                        EntryProcessorResult<Integer> res2 = res.get(key2);
                        assertNotNull(res2);
                        assertEquals(vals.get(key2), res2.get());
                    } else
                        assertTrue(res.isEmpty());
                    tx.commit();
                }
                checkValue(key1, newVal, cache.getName());
                checkValue(key2, newVal, cache.getName());
                vals.put(key1, newVal);
                vals.put(key2, newVal);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Map<Integer, EntryProcessorResult<Integer>> res = cache.invokeAll(F.asSet(key1, key2), new SetValueProcessor(newVal + 1));
                        EntryProcessorResult<Integer> res1 = res.get(key1);
                        assertNotNull(res1);
                        assertEquals(vals.get(key1), res1.get());
                        EntryProcessorResult<Integer> res2 = res.get(key2);
                        assertNotNull(res2);
                        assertEquals(vals.get(key2), res2.get());
                        updateKey(cache0, key1, -1);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key1, -1, cache.getName());
                checkValue(key2, newVal, cache.getName());
                vals.put(key1, -1);
                vals.put(key2, newVal);
                newVal++;
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 15 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method incrementTx.

/**
     * @param nearCache If {@code true} near cache is enabled.
     * @param store If {@code true} cache store is enabled.
     * @param restart If {@code true} restarts one node.
     * @throws Exception If failed.
     */
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
    final Ignite srv = ignite(1);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
    final List<Ignite> clients = clients();
    final String cacheName = srv.createCache(ccfg).getName();
    final AtomicBoolean stop = new AtomicBoolean();
    try {
        final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
        for (Ignite client : clients) {
            if (nearCache)
                caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
            else
                caches.add(client.<Integer, Integer>cache(cacheName));
        }
        IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
        final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
        for (int i = 0; i < 30; i++) {
            final AtomicInteger cntr = new AtomicInteger();
            final Integer key = i;
            final AtomicInteger threadIdx = new AtomicInteger();
            final int THREADS = 10;
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = threadIdx.getAndIncrement() % caches.size();
                    IgniteCache<Integer, Integer> cache = caches.get(idx);
                    Ignite ignite = cache.unwrap(Ignite.class);
                    IgniteTransactions txs = ignite.transactions();
                    log.info("Started update thread: " + ignite.name());
                    barrier.await();
                    for (int i = 0; i < 1000; i++) {
                        if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
                            break;
                        try {
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Integer val = cache.get(key);
                                cache.put(key, val == null ? 1 : val + 1);
                                tx.commit();
                            }
                            cntr.incrementAndGet();
                        } catch (TransactionOptimisticException ignore) {
                        // Retry.
                        } catch (IgniteException | CacheException e) {
                            assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
                        }
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
            assertTrue(cntr.get() > 0);
            checkValue(key, cntr.get(), cacheName, restart);
            if (U.currentTimeMillis() > stopTime)
                break;
        }
        stop.set(true);
        if (restartFut != null)
            restartFut.get();
    } finally {
        stop.set(true);
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)77 Transaction (org.apache.ignite.transactions.Transaction)75 Ignite (org.apache.ignite.Ignite)55 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 Callable (java.util.concurrent.Callable)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)9 ArrayList (java.util.ArrayList)8 LinkedHashMap (java.util.LinkedHashMap)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)8