Search in sources :

Example 46 with Transaction

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

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

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

the class CacheSerializableTransactionsTest method testTxRollbackIfLocked1.

/**
 * @throws Exception If failed.
 */
public void testTxRollbackIfLocked1() throws Exception {
    Ignite ignite0 = ignite(0);
    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);
                CountDownLatch latch = new CountDownLatch(1);
                IgniteInternalFuture<?> fut = lockKey(latch, cache, key);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        cache.put(key, 2);
                        log.info("Commit");
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                latch.countDown();
                fut.get();
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    cache.put(key, 2);
                    tx.commit();
                }
                checkValue(key, 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 49 with Transaction

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

Example 50 with Transaction

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

the class CacheSerializableTransactionsTest method testTxCommit.

/**
 * @throws Exception If failed.
 */
public void testTxCommit() throws Exception {
    Ignite ignite0 = ignite(0);
    Ignite ignite1 = ignite(1);
    final IgniteTransactions txs0 = ignite0.transactions();
    final IgniteTransactions txs1 = ignite1.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
            IgniteCache<Integer, Integer> cache1 = ignite1.cache(ccfg.getName());
            List<Integer> keys = testKeys(cache0);
            for (Integer key : keys) {
                log.info("Test key: " + key);
                Integer expVal = null;
                for (int i = 0; i < 100; i++) {
                    try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache0.get(key);
                        assertEquals(expVal, val);
                        cache0.put(key, i);
                        tx.commit();
                        expVal = i;
                    }
                    try (Transaction tx = txs1.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache1.get(key);
                        assertEquals(expVal, val);
                        cache1.put(key, val);
                        tx.commit();
                    }
                    try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache0.get(key);
                        assertEquals(expVal, val);
                        cache0.put(key, val);
                        tx.commit();
                    }
                }
                checkValue(key, expVal, cache0.getName());
                cache0.remove(key);
                try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache0.get(key);
                    assertNull(val);
                    cache0.put(key, expVal + 1);
                    tx.commit();
                }
                checkValue(key, expVal + 1, cache0.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)

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