Search in sources :

Example 56 with Transaction

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

the class CacheSerializableTransactionsTest method checkNoReadLockConflict.

/**
 * @param ignite Node.
 * @param readCacheName Cache name for get.
 * @param writeCacheName Cache name for put.
 * @param entry If {@code true} then uses 'getEntry' to read value, otherwise uses 'get'.
 * @param putKey Write key counter.
 * @throws Exception If failed.
 */
private void checkNoReadLockConflict(final Ignite ignite, String readCacheName, String writeCacheName, final boolean entry, final AtomicInteger putKey) throws Exception {
    final int THREADS = 64;
    final IgniteCache<Integer, Integer> readCache = ignite.cache(readCacheName);
    final IgniteCache<Integer, Integer> writeCache = ignite.cache(writeCacheName);
    List<Integer> readKeys = testKeys(readCache);
    for (final Integer readKey : readKeys) {
        final CyclicBarrier barrier = new CyclicBarrier(THREADS);
        readCache.put(readKey, Integer.MIN_VALUE);
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                    if (entry)
                        readCache.get(readKey);
                    else
                        readCache.getEntry(readKey);
                    barrier.await();
                    writeCache.put(putKey.incrementAndGet(), 0);
                    tx.commit();
                }
                return null;
            }
        }, THREADS, "test-thread");
        assertEquals((Integer) Integer.MIN_VALUE, readCache.get(readKey));
        readCache.put(readKey, readKey);
        assertEquals(readKey, readCache.get(readKey));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) 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) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 57 with Transaction

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

the class CacheSerializableTransactionsTest method rollbackIfLockedPartialLock.

/**
 * @param locKey If {@code true} gets lock for local key.
 * @throws Exception If failed.
 */
private void rollbackIfLockedPartialLock(boolean locKey) 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);
            final Integer key1 = primaryKey(ignite(1).cache(cache.getName()));
            final Integer key2 = locKey ? primaryKey(cache) : primaryKey(ignite(2).cache(cache.getName()));
            CountDownLatch latch = new CountDownLatch(1);
            IgniteInternalFuture<?> fut = lockKey(latch, cache, key1);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache.put(key1, 2);
                    cache.put(key2, 2);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            latch.countDown();
            fut.get();
            checkValue(key1, 1, cache.getName());
            checkValue(key2, null, cache.getName());
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.put(key1, 2);
                cache.put(key2, 2);
                tx.commit();
            }
            checkValue(key1, 2, cache.getName());
            checkValue(key2, 2, cache.getName());
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 58 with Transaction

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

the class CacheSerializableTransactionsTest method rollbackNearCacheWrite.

/**
 * @param near If {@code true} locks entry using the same near cache.
 * @throws Exception If failed.
 */
private void rollbackNearCacheWrite(boolean near) throws Exception {
    Ignite ignite0 = ignite(0);
    IgniteCache<Integer, Integer> cache0 = ignite0.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false));
    final String cacheName = cache0.getName();
    try {
        Ignite ignite = ignite(SRVS);
        IgniteCache<Integer, Integer> cache = ignite.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>());
        IgniteTransactions txs = ignite.transactions();
        Integer key1 = primaryKey(ignite(0).cache(cacheName));
        Integer key2 = primaryKey(ignite(1).cache(cacheName));
        Integer key3 = primaryKey(ignite(2).cache(cacheName));
        CountDownLatch latch = new CountDownLatch(1);
        IgniteInternalFuture<?> fut = null;
        try {
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.put(key1, key1);
                cache.put(key2, key2);
                cache.put(key3, key3);
                fut = lockKey(latch, near ? cache : cache0, key2);
                tx.commit();
            }
            fail();
        } catch (TransactionOptimisticException e) {
            log.info("Expected exception: " + e);
        }
        latch.countDown();
        assert fut != null;
        fut.get();
        checkValue(key1, null, cacheName);
        checkValue(key2, 1, cacheName);
        checkValue(key3, null, cacheName);
        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
            cache.put(key1, key1);
            cache.put(key2, key2);
            cache.put(key3, key3);
            tx.commit();
        }
        checkValue(key1, key1, cacheName);
        checkValue(key2, key2, cacheName);
        checkValue(key3, key3, cacheName);
    } finally {
        destroyCache(cacheName);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 59 with Transaction

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

the class CacheSerializableTransactionsTest method txConflictReadWrite.

/**
 * @param noVal If {@code true} there is no cache value when read in tx.
 * @param rmv If {@code true} tests remove, otherwise put.
 * @throws Exception If failed.
 */
private void txConflictReadWrite(boolean noVal, boolean rmv, boolean needVer) 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 (Integer key : keys) {
                log.info("Test key: " + key);
                Integer expVal = null;
                if (!noVal) {
                    expVal = -1;
                    cache.put(key, expVal);
                }
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        if (needVer) {
                            CacheEntry<Integer, Integer> val = cache.getEntry(key);
                            assertEquals(expVal, val == null ? null : val.getValue());
                        } else {
                            Integer val = cache.get(key);
                            assertEquals(expVal, val);
                        }
                        updateKey(cache, key, 1);
                        if (rmv)
                            cache.remove(key);
                        else
                            cache.put(key, 2);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    if (needVer) {
                        CacheEntry<Integer, Integer> val = cache.getEntry(key);
                        assertEquals(1, (Object) val.getValue());
                    } else {
                        Integer val = cache.get(key);
                        assertEquals(1, (Object) val);
                    }
                    if (rmv)
                        cache.remove(key);
                    else
                        cache.put(key, 2);
                    tx.commit();
                }
                checkValue(key, rmv ? null : 2, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 60 with Transaction

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

the class CacheSerializableTransactionsTest method testTxConflictPutIfAbsent.

/**
 * @throws Exception If failed.
 */
public void testTxConflictPutIfAbsent() 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 (Integer key : keys) {
                log.info("Test key: " + key);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean put = cache.putIfAbsent(key, 2);
                        assertTrue(put);
                        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 put = cache.putIfAbsent(key, 2);
                    assertFalse(put);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean put = cache.putIfAbsent(key, 2);
                    assertTrue(put);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean put = cache.putIfAbsent(key, 2);
                        assertFalse(put);
                        updateKey(cache, key, 3);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 3, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

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