Search in sources :

Example 36 with Transaction

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

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

the class CacheSerializableTransactionsTest method testTxCommitReadOnly2.

/**
 * @throws Exception If failed.
 */
public void testTxCommitReadOnly2() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (final Integer key : keys) {
                log.info("Test key: " + key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                        @Override
                        public Void apply(IgniteCache<Integer, Integer> cache) {
                            cache.get(key);
                            return null;
                        }
                    });
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                        @Override
                        public Void apply(IgniteCache<Integer, Integer> cache) {
                            cache.get(key);
                            return null;
                        }
                    });
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 38 with Transaction

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

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

the class CacheSerializableTransactionsTest method testTxConflictCasReplace.

/**
 * @throws Exception If failed.
 */
public void testTxConflictCasReplace() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (final Integer key : keys) {
                log.info("Test key: " + key);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean replace = cache.replace(key, 1, 2);
                        assertFalse(replace);
                        updateKey(cache, key, 1);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 1, 2);
                    assertTrue(replace);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 1, 2);
                    assertFalse(replace);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 2);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean replace = cache.replace(key, 2, 1);
                        assertTrue(replace);
                        updateKey(cache, key, 3);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 3, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean replace = cache.replace(key, 3, 4);
                        assertTrue(replace);
                        txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                            @Override
                            public Void apply(IgniteCache<Integer, Integer> cache) {
                                cache.remove(key);
                                return null;
                            }
                        });
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 1);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 2, 3);
                    assertFalse(replace);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 1, 3);
                    assertTrue(replace);
                    tx.commit();
                }
                checkValue(key, 3, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 40 with Transaction

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

the class CacheSerializableTransactionsTest method checkReadWriteTransactionsNoDeadlock.

/**
 * @param multiNode Multi-node test flag.
 * @throws Exception If failed.
 */
private void checkReadWriteTransactionsNoDeadlock(final boolean multiNode) throws Exception {
    final Ignite ignite0 = ignite(0);
    for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        ignite0.createCache(ccfg);
        try {
            final long stopTime = U.currentTimeMillis() + 10_000;
            final AtomicInteger idx = new AtomicInteger();
            GridTestUtils.runMultiThreaded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Ignite ignite = multiNode ? ignite(idx.incrementAndGet() % (SRVS + CLIENTS)) : ignite0;
                    IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (U.currentTimeMillis() < stopTime) {
                        try {
                            try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                                for (int i = 0; i < 10; i++) {
                                    Integer key = rnd.nextInt(30);
                                    if (rnd.nextBoolean())
                                        cache.get(key);
                                    else
                                        cache.put(key, key);
                                }
                                tx.commit();
                            }
                        } catch (TransactionOptimisticException ignore) {
                        // No-op.
                        }
                    }
                    return null;
                }
            }, 32, "test-thread");
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Aggregations

Transaction (org.apache.ignite.transactions.Transaction)493 Ignite (org.apache.ignite.Ignite)204 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)183 IgniteCache (org.apache.ignite.IgniteCache)88 IgniteTransactions (org.apache.ignite.IgniteTransactions)78 IgniteException (org.apache.ignite.IgniteException)74 CacheException (javax.cache.CacheException)60 HashMap (java.util.HashMap)54 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)45 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)44 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)42 ArrayList (java.util.ArrayList)41 Callable (java.util.concurrent.Callable)41 Map (java.util.Map)36 IgniteEx (org.apache.ignite.internal.IgniteEx)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 IgniteKernal (org.apache.ignite.internal.IgniteKernal)30 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)30 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29