Search in sources :

Example 6 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IndexingSpiQuerySelfTest method testIndexingSpiFailure.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testIndexingSpiFailure() throws Exception {
    IgniteConfiguration cfg = configuration();
    cfg.setIndexingSpi(new MyBrokenIndexingSpi());
    Ignite ignite = Ignition.start(cfg);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(CACHE_NAME);
    ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    final IgniteCache<Integer, Integer> cache = ignite.createCache(ccfg);
    final IgniteTransactions txs = ignite.transactions();
    for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (final TransactionIsolation isolation : TransactionIsolation.values()) {
            System.out.println("Run in transaction: " + concurrency + " " + isolation);
            GridTestUtils.assertThrowsWithCause(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Transaction tx;
                    try (Transaction tx0 = tx = txs.txStart(concurrency, isolation)) {
                        cache.put(1, 1);
                        tx0.commit();
                    }
                    assertEquals(TransactionState.ROLLED_BACK, tx.state());
                    return null;
                }
            }, IgniteTxHeuristicCheckedException.class);
        }
    }
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 7 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IndexingSpiQueryTxSelfTest method testIndexingSpiWithTx.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testIndexingSpiWithTx() throws Exception {
    IgniteEx ignite = grid(0);
    final IgniteCache<Integer, Integer> cache = ignite.cache("test-cache");
    final IgniteTransactions txs = ignite.transactions();
    for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (final TransactionIsolation isolation : TransactionIsolation.values()) {
            System.out.println("Run in transaction: " + concurrency + " " + isolation);
            GridTestUtils.assertThrowsWithCause(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Transaction tx;
                    try (Transaction tx0 = tx = txs.txStart(concurrency, isolation)) {
                        cache.put(1, 1);
                        tx0.commit();
                    }
                    assertEquals(TransactionState.ROLLED_BACK, tx.state());
                    return null;
                }
            }, IgniteTxHeuristicCheckedException.class);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 8 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions 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 9 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method testTxLoadFromStore.

/**
     * @throws Exception If failed.
     */
public void testTxLoadFromStore() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        if (ccfg.getCacheStoreFactory() == null)
            continue;
        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 storeVal = -1;
                storeMap.put(key, storeVal);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertEquals(storeVal, val);
                    tx.commit();
                }
                checkValue(key, storeVal, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    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) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 10 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method testRandomOperations.

/**
     * @throws Exception If failed.
     */
public void testRandomOperations() throws Exception {
    Ignite ignite0 = ignite(0);
    long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (Ignite ignite : G.allGrids()) {
                log.info("Test node: " + ignite.name());
                IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
                IgniteTransactions txs = ignite.transactions();
                final int KEYS = 100;
                for (int i = 0; i < 1000; i++) {
                    Integer key1 = rnd.nextInt(KEYS);
                    Integer key2;
                    if (rnd.nextBoolean()) {
                        key2 = rnd.nextInt(KEYS);
                        while (key2.equals(key1)) key2 = rnd.nextInt(KEYS);
                    } else
                        key2 = key1 + 1;
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        randomOperation(rnd, cache, key1);
                        randomOperation(rnd, cache, key2);
                        tx.commit();
                    }
                    if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
                        break;
                }
                for (int key = 0; key < KEYS; key++) {
                    Integer val = cache0.get(key);
                    for (int node = 1; node < SRVS + CLIENTS; node++) assertEquals(val, ignite(node).cache(cache.getName()).get(key));
                }
                if (U.currentTimeMillis() > stopTime)
                    break;
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)77 Transaction (org.apache.ignite.transactions.Transaction)75 Ignite (org.apache.ignite.Ignite)55 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)54 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)28 IgniteCache (org.apache.ignite.IgniteCache)22 IgniteException (org.apache.ignite.IgniteException)18 CacheException (javax.cache.CacheException)17 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)13 HashMap (java.util.HashMap)12 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Callable (java.util.concurrent.Callable)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)9 ArrayList (java.util.ArrayList)8 LinkedHashMap (java.util.LinkedHashMap)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)8