Search in sources :

Example 21 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method testCrossCacheTx.

/**
     * @throws Exception If failed.
     */
public void testCrossCacheTx() throws Exception {
    Ignite ignite0 = ignite(0);
    final String CACHE1 = "cache1";
    final String CACHE2 = "cache2";
    try {
        CacheConfiguration<Integer, Integer> ccfg1 = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
        ccfg1.setName(CACHE1);
        ignite0.createCache(ccfg1);
        CacheConfiguration<Integer, Integer> ccfg2 = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
        ccfg2.setName(CACHE2);
        ignite0.createCache(ccfg2);
        Integer newVal = 0;
        List<Integer> keys = testKeys(ignite0.<Integer, Integer>cache(CACHE1));
        for (Ignite ignite : G.allGrids()) {
            log.info("Test node: " + ignite.name());
            IgniteCache<Integer, Integer> cache1 = ignite.cache(CACHE1);
            IgniteCache<Integer, Integer> cache2 = ignite.cache(CACHE2);
            IgniteTransactions txs = ignite.transactions();
            for (Integer key : keys) {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key, newVal);
                    cache2.put(key, newVal);
                    tx.commit();
                }
                checkValue(key, newVal, CACHE1);
                checkValue(key, newVal, CACHE2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key);
                    Object val2 = cache2.get(key);
                    assertEquals(newVal, val1);
                    assertEquals(newVal, val2);
                    tx.commit();
                }
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key, newVal + 1);
                    cache2.put(key, newVal + 1);
                    tx.rollback();
                }
                checkValue(key, newVal, CACHE1);
                checkValue(key, newVal, CACHE2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key);
                    Object val2 = cache2.get(key);
                    assertEquals(newVal, val1);
                    assertEquals(newVal, val2);
                    cache1.put(key, newVal + 1);
                    cache2.put(key, newVal + 1);
                    tx.commit();
                }
                newVal++;
                checkValue(key, newVal, CACHE1);
                checkValue(key, newVal, CACHE2);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key, newVal);
                    cache2.put(-key, newVal);
                    tx.commit();
                }
                checkValue(key, newVal, CACHE1);
                checkValue(-key, null, CACHE1);
                checkValue(key, newVal, CACHE2);
                checkValue(-key, newVal, CACHE2);
            }
            newVal++;
            Integer key1 = primaryKey(ignite(0).cache(CACHE1));
            Integer key2 = primaryKey(ignite(1).cache(CACHE1));
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache1.put(key1, newVal);
                cache1.put(key2, newVal);
                cache2.put(key1, newVal);
                cache2.put(key2, newVal);
                tx.commit();
            }
            checkValue(key1, newVal, CACHE1);
            checkValue(key2, newVal, CACHE1);
            checkValue(key1, newVal, CACHE2);
            checkValue(key2, newVal, CACHE2);
            CountDownLatch latch = new CountDownLatch(1);
            IgniteInternalFuture<?> fut = lockKey(latch, cache1, key1);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key1, newVal + 1);
                    cache2.put(key1, newVal + 1);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            latch.countDown();
            fut.get();
            checkValue(key1, 1, CACHE1);
            checkValue(key1, newVal, CACHE2);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache1.put(key1, newVal + 1);
                cache2.put(key1, newVal + 1);
                tx.commit();
            }
            newVal++;
            cache1.put(key2, newVal);
            cache2.put(key2, newVal);
            checkValue(key1, newVal, CACHE1);
            checkValue(key1, newVal, CACHE2);
            latch = new CountDownLatch(1);
            fut = lockKey(latch, cache1, key1);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache1.put(key1, newVal + 1);
                    cache2.put(key2, newVal + 1);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            latch.countDown();
            fut.get();
            checkValue(key1, 1, CACHE1);
            checkValue(key2, newVal, CACHE2);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key1);
                    Object val2 = cache2.get(key2);
                    assertEquals(1, val1);
                    assertEquals(newVal, val2);
                    updateKey(cache2, key2, 1);
                    cache1.put(key1, newVal + 1);
                    cache2.put(key2, newVal + 1);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            checkValue(key1, 1, CACHE1);
            checkValue(key2, 1, CACHE2);
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                Object val1 = cache1.get(key1);
                Object val2 = cache2.get(key2);
                assertEquals(1, val1);
                assertEquals(1, val2);
                cache1.put(key1, newVal + 1);
                cache2.put(key2, newVal + 1);
                tx.commit();
            }
            newVal++;
            checkValue(key1, newVal, CACHE1);
            checkValue(key2, newVal, CACHE2);
            try {
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object val1 = cache1.get(key1);
                    Object val2 = cache2.get(key2);
                    assertEquals(newVal, val1);
                    assertEquals(newVal, val2);
                    updateKey(cache2, key2, newVal);
                    tx.commit();
                }
                fail();
            } catch (TransactionOptimisticException e) {
                log.info("Expected exception: " + e);
            }
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                Object val1 = cache1.get(key1);
                Object val2 = cache2.get(key2);
                assertEquals(newVal, val1);
                assertEquals(newVal, val2);
                tx.commit();
            }
        }
    } finally {
        destroyCache(CACHE1);
        destroyCache(CACHE2);
    }
}
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 22 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method testReadWriteAccountTx.

/**
     * @throws Exception If failed.
     */
public void testReadWriteAccountTx() throws Exception {
    final CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
    ignite(0).createCache(ccfg);
    try {
        final int ACCOUNTS = 50;
        final int VAL_PER_ACCOUNT = 1000;
        IgniteCache<Integer, Account> cache0 = ignite(0).cache(ccfg.getName());
        final Set<Integer> keys = new HashSet<>();
        for (int i = 0; i < ACCOUNTS; i++) {
            cache0.put(i, new Account(VAL_PER_ACCOUNT));
            keys.add(i);
        }
        final List<Ignite> clients = clients();
        final AtomicBoolean stop = new AtomicBoolean();
        final AtomicInteger idx = new AtomicInteger();
        IgniteInternalFuture<?> readFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try {
                    int threadIdx = idx.getAndIncrement();
                    int nodeIdx = threadIdx % (SRVS + CLIENTS);
                    Ignite node = ignite(nodeIdx);
                    IgniteCache<Integer, Account> cache = node.cache(ccfg.getName());
                    IgniteTransactions txs = node.transactions();
                    Integer putKey = ACCOUNTS + threadIdx;
                    while (!stop.get()) {
                        int sum;
                        while (true) {
                            sum = 0;
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Map<Integer, Account> data = cache.getAll(keys);
                                for (int i = 0; i < ACCOUNTS; i++) {
                                    Account account = data.get(i);
                                    assertNotNull(account);
                                    sum += account.value();
                                }
                                if (ThreadLocalRandom.current().nextBoolean())
                                    cache.put(putKey, new Account(sum));
                                tx.commit();
                            } catch (TransactionOptimisticException ignored) {
                                continue;
                            }
                            break;
                        }
                        assertEquals(ACCOUNTS * VAL_PER_ACCOUNT, sum);
                    }
                    return null;
                } catch (Throwable e) {
                    stop.set(true);
                    log.error("Unexpected error: " + e);
                    throw e;
                }
            }
        }, (SRVS + CLIENTS) * 2, "update-thread");
        IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try {
                    int nodeIdx = idx.getAndIncrement() % clients.size();
                    Ignite node = clients.get(nodeIdx);
                    IgniteCache<Integer, Account> cache = node.cache(ccfg.getName());
                    IgniteTransactions txs = node.transactions();
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (!stop.get()) {
                        int id1 = rnd.nextInt(ACCOUNTS);
                        int id2 = rnd.nextInt(ACCOUNTS);
                        while (id2 == id1) id2 = rnd.nextInt(ACCOUNTS);
                        while (true) {
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Account a1 = cache.get(id1);
                                Account a2 = cache.get(id2);
                                assertNotNull(a1);
                                assertNotNull(a2);
                                if (a1.value() > 0) {
                                    a1 = new Account(a1.value() - 1);
                                    a2 = new Account(a2.value() + 1);
                                }
                                cache.put(id1, a1);
                                cache.put(id2, a2);
                                tx.commit();
                            } catch (TransactionOptimisticException ignored) {
                                continue;
                            }
                            break;
                        }
                    }
                    return null;
                } catch (Throwable e) {
                    stop.set(true);
                    log.error("Unexpected error: " + e);
                    throw e;
                }
            }
        }, 2, "update-thread");
        try {
            U.sleep(15_000);
        } finally {
            stop.set(true);
        }
        readFut.get();
        updateFut.get();
        int sum = 0;
        for (int i = 0; i < ACCOUNTS; i++) {
            Account a = cache0.get(i);
            assertNotNull(a);
            assertTrue(a.value() >= 0);
            log.info("Account: " + a.value());
            sum += a.value();
        }
        assertEquals(ACCOUNTS * VAL_PER_ACCOUNT, sum);
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 23 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method txConflictInvoke.

/**
     * @param noVal If {@code true} there is no cache value when read in tx.
     * @param rmv If {@code true} invoke does remove value, otherwise put.
     * @throws Exception If failed.
     */
private void txConflictInvoke(boolean noVal, boolean rmv) 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)) {
                        Integer val = cache.invoke(key, new SetValueProcessor(rmv ? null : 2));
                        assertEquals(expVal, val);
                        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)) {
                    Object val = cache.invoke(key, new SetValueProcessor(rmv ? null : 2));
                    assertEquals(1, val);
                    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 24 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method txConflictRead.

/**
     * @param noVal If {@code true} there is no cache value when read in tx.
     * @param needVer If {@code true} then gets entry, otherwise just value.
     * @throws Exception If failed.
     */
private void txConflictRead(boolean noVal, 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 (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);
                    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((Integer) 1, val.getValue());
                    } else {
                        Object val = cache.get(key);
                        assertEquals(1, val);
                    }
                    tx.commit();
                }
                checkValue(key, 1, 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 25 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method testTxConflictGetAndReplace.

/**
     * @throws Exception If failed.
     */
public void testTxConflictGetAndReplace() 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)) {
                        Object old = cache.getAndReplace(key, 2);
                        assertNull(old);
                        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)) {
                    Object old = cache.getAndReplace(key, 2);
                    assertEquals(1, old);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object old = cache.getAndReplace(key, 2);
                    assertNull(old);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object old = cache.getAndReplace(key, 2);
                        assertNull(old);
                        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)) {
                        Object old = cache.getAndReplace(key, 2);
                        assertEquals(3, old);
                        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)) {
                    Object old = cache.getAndReplace(key, 2);
                    assertEquals(1, old);
                    tx.commit();
                }
                checkValue(key, 2, 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)

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