Search in sources :

Example 16 with TransactionOptimisticException

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

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

the class IgniteTxAbstractTest method checkCommit.

/**
     * @param concurrency Concurrency.
     * @param isolation Isolation.
     * @throws Exception If check failed.
     */
protected void checkCommit(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    int gridIdx = RAND.nextInt(gridCount());
    Ignite ignite = grid(gridIdx);
    if (isTestDebug())
        debug("Checking commit on grid: " + ignite.cluster().localNode().id());
    for (int i = 0; i < iterations(); i++) {
        IgniteCache<Integer, String> cache = jcache(gridIdx);
        try (Transaction tx = ignite(gridIdx).transactions().txStart(concurrency, isolation, 0, 0)) {
            int prevKey = -1;
            for (Integer key : getKeys()) {
                // Make sure we have the same locking order for all concurrent transactions.
                assert key >= prevKey : "key: " + key + ", prevKey: " + prevKey;
                if (isTestDebug()) {
                    AffinityFunction aff = cache.getConfiguration(CacheConfiguration.class).getAffinity();
                    int part = aff.partition(key);
                    debug("Key affinity [key=" + key + ", partition=" + part + ", affinity=" + U.toShortString(ignite(gridIdx).affinity(DEFAULT_CACHE_NAME).mapPartitionToPrimaryAndBackups(part)) + ']');
                }
                String val = Integer.toString(key);
                switch(getOp()) {
                    case READ:
                        {
                            if (isTestDebug())
                                debug("Reading key [key=" + key + ", i=" + i + ']');
                            val = cache.get(key);
                            if (isTestDebug())
                                debug("Read value for key [key=" + key + ", val=" + val + ']');
                            break;
                        }
                    case WRITE:
                        {
                            if (isTestDebug())
                                debug("Writing key and value [key=" + key + ", val=" + val + ", i=" + i + ']');
                            cache.put(key, val);
                            break;
                        }
                    case REMOVE:
                        {
                            if (isTestDebug())
                                debug("Removing key [key=" + key + ", i=" + i + ']');
                            cache.remove(key);
                            break;
                        }
                    default:
                        {
                            assert false;
                        }
                }
            }
            tx.commit();
            if (isTestDebug())
                debug("Committed transaction [i=" + i + ", tx=" + tx + ']');
        } catch (TransactionOptimisticException e) {
            if (!(concurrency == OPTIMISTIC && isolation == SERIALIZABLE)) {
                log.error("Unexpected error: " + e, e);
                throw e;
            }
        } catch (Throwable e) {
            log.error("Unexpected error: " + e, e);
            throw e;
        }
    }
    Transaction tx = ignite(gridIdx).transactions().tx();
    assertNull("Thread should not have transaction upon completion", tx);
    if (printMemoryStats()) {
        if (cntr.getAndIncrement() % 100 == 0)
            // Print transaction memory stats.
            ((IgniteKernal) grid(gridIdx)).internalCache(DEFAULT_CACHE_NAME).context().tm().printMemoryStats();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 18 with TransactionOptimisticException

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

the class CacheSerializableTransactionsTest method testTxConflictGetAndPutIfAbsent.

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

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

the class IgniteAccountSerializableTxBenchmark method test.

/** {@inheritDoc} */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    Set<Integer> accountIds = new HashSet<>();
    int accNum = args.batch();
    while (accountIds.size() < accNum) accountIds.add(nextRandom(args.range()));
    while (true) {
        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
            Map<Integer, Account> accounts = (Map) cache.getAll(accountIds);
            if (accounts.size() != accNum)
                throw new Exception("Failed to find accounts: " + accountIds);
            Integer fromId = accountIds.iterator().next();
            int fromBalance = accounts.get(fromId).balance();
            for (Integer id : accountIds) {
                if (id.equals(fromId))
                    continue;
                Account account = accounts.get(id);
                if (fromBalance > 0) {
                    fromBalance--;
                    cache.put(id, new Account(account.balance() + 1));
                }
            }
            cache.put(fromId, new Account(fromBalance));
            tx.commit();
        } catch (TransactionOptimisticException e) {
            continue;
        }
        break;
    }
    return true;
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Account(org.apache.ignite.yardstick.cache.model.Account) Transaction(org.apache.ignite.transactions.Transaction) Map(java.util.Map) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) HashSet(java.util.HashSet)

Example 20 with TransactionOptimisticException

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

the class IgnitePutAllSerializableTxBenchmark method test.

/** {@inheritDoc} */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    ThreadRange r = threadRange();
    Map<Integer, Integer> vals = new HashMap<>();
    for (int i = 0; i < args.batch(); i++) {
        int key = r.nextRandom();
        vals.put(key, key);
    }
    while (true) {
        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
            cache.putAll(vals);
            tx.commit();
        } catch (TransactionOptimisticException e) {
            continue;
        }
        break;
    }
    return true;
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap)

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