Search in sources :

Example 6 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method testTxReadInParallerTxWrite.

/**
     * Transactional read in parallel with changing the same data.
     *
     * @throws Exception If failed.
     */
public void testTxReadInParallerTxWrite() throws Exception {
    final Ignite ignite = ignite(0);
    final Integer key = 1;
    final Integer val = 1;
    final CountDownLatch readLatch = new CountDownLatch(1);
    final CountDownLatch writeLatch = new CountDownLatch(1);
    final Exception[] err = { null };
    final String cacheName = ignite.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
    final IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
    try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
        cache.put(key, val);
        tx.commit();
    }
    try {
        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(OPTIMISTIC, SERIALIZABLE)) {
                    assertTrue(cache.get(key).equals(val));
                    readLatch.countDown();
                    writeLatch.await(10, TimeUnit.SECONDS);
                    try {
                        tx.commit();
                    } catch (TransactionOptimisticException e) {
                        log.info("Expected exception: " + e);
                        err[0] = e;
                    }
                }
                return null;
            }
        }, "read-thread");
        GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
                readLatch.await(10, TimeUnit.SECONDS);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache.put(key, val);
                    tx.commit();
                }
                writeLatch.countDown();
                return null;
            }
        }, "write-thread").get();
        fut.get();
        assertNotNull("Expected exception was not thrown", err[0]);
    } finally {
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) 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) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 7 with TransactionOptimisticException

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

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

Example 9 with TransactionOptimisticException

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

Example 10 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method testTxConflictReadWrite3.

/**
     * @throws Exception If failed.
     */
public void testTxConflictReadWrite3() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
        List<Integer> readKeys = new ArrayList<>();
        List<Integer> writeKeys = new ArrayList<>();
        readKeys.add(primaryKey(cache));
        writeKeys.add(primaryKeys(cache, 1, 1000_0000).get(0));
        if (ccfg.getBackups() > 0) {
            readKeys.add(backupKey(cache));
            writeKeys.add(backupKeys(cache, 1, 1000_0000).get(0));
        }
        if (ccfg.getCacheMode() == PARTITIONED) {
            readKeys.add(nearKey(cache));
            writeKeys.add(nearKeys(cache, 1, 1000_0000).get(0));
        }
        try {
            for (Integer readKey : readKeys) {
                for (Integer writeKey : writeKeys) {
                    try {
                        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                            cache.get(readKey);
                            cache.put(writeKey, writeKey);
                            updateKey(cache, readKey, 0);
                            tx.commit();
                        }
                        fail();
                    } catch (TransactionOptimisticException ignored) {
                    // Expected exception.
                    }
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        cache.get(readKey);
                        cache.put(writeKey, writeKey);
                        tx.commit();
                    }
                }
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)39 Transaction (org.apache.ignite.transactions.Transaction)38 Ignite (org.apache.ignite.Ignite)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 IgniteTransactions (org.apache.ignite.IgniteTransactions)27 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteException (org.apache.ignite.IgniteException)10 CacheException (javax.cache.CacheException)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 Callable (java.util.concurrent.Callable)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)7 CacheLoaderException (javax.cache.integration.CacheLoaderException)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 TreeMap (java.util.TreeMap)4