Search in sources :

Example 26 with IgniteTransactions

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

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

the class CacheSerializableTransactionsTest method rollbackNearCacheRead.

/**
     * @param near If {@code true} updates entry using the same near cache.
     * @throws Exception If failed.
     */
private void rollbackNearCacheRead(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));
        cache0.put(key1, -1);
        cache0.put(key2, -1);
        cache0.put(key3, -1);
        try {
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.get(key1);
                cache.get(key2);
                cache.get(key3);
                updateKey(near ? cache : cache0, key2, -2);
                tx.commit();
            }
            fail();
        } catch (TransactionOptimisticException e) {
            log.info("Expected exception: " + e);
        }
        checkValue(key1, -1, cacheName);
        checkValue(key2, -2, cacheName);
        checkValue(key3, -1, 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)

Example 28 with IgniteTransactions

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

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

the class IgniteClientReconnectCacheTest method testReconnectTransactions.

/**
     * @throws Exception If failed.
     */
public void testReconnectTransactions() throws Exception {
    clientMode = true;
    IgniteEx client = startGrid(SRV_CNT);
    Ignite srv = clientRouter(client);
    CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    IgniteCache<Object, Object> cache = client.getOrCreateCache(ccfg);
    final IgniteTransactions txs = client.transactions();
    final Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ);
    cache.put(1, 1);
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            try {
                tx.commit();
                fail();
            } catch (IgniteClientDisconnectedException e) {
                log.info("Expected error: " + e);
                assertNotNull(e.reconnectFuture());
            }
            try {
                txs.txStart();
                fail();
            } catch (IgniteClientDisconnectedException e) {
                log.info("Expected error: " + e);
                assertNotNull(e.reconnectFuture());
            }
        }
    });
    assertNull(txs.tx());
    try (Transaction tx0 = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
        cache.put(1, 1);
        assertEquals(1, cache.get(1));
        tx0.commit();
    }
    try (Transaction tx0 = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
        cache.put(2, 2);
        assertEquals(2, cache.get(2));
        tx0.commit();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 30 with IgniteTransactions

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

the class CrossCacheTxRandomOperationsTest method txOperations.

/**
     * @param concurrency Transaction concurrency.
     * @param isolation Transaction isolation.
     * @param crossCacheTx If {@code true} uses cross cache transaction.
     * @param client If {@code true} uses client node.
     * @throws Exception If failed.
     */
private void txOperations(TransactionConcurrency concurrency, TransactionIsolation isolation, boolean crossCacheTx, boolean client) throws Exception {
    final Map<TestKey, TestValue> expData1 = new HashMap<>();
    final Map<TestKey, TestValue> expData2 = new HashMap<>();
    Ignite ignite = client ? ignite(GRID_CNT - 1) : ignite(0);
    assertEquals(client, (boolean) ignite.configuration().isClientMode());
    final List<IgniteCache<TestKey, TestValue>> caches1 = new ArrayList<>();
    final List<IgniteCache<TestKey, TestValue>> caches2 = new ArrayList<>();
    for (int i = 0; i < GRID_CNT; i++) {
        caches1.add(ignite(i).<TestKey, TestValue>cache(CACHE1));
        caches2.add(ignite(i).<TestKey, TestValue>cache(CACHE2));
    }
    IgniteCache<TestKey, TestValue> cache1 = ignite.cache(CACHE1);
    IgniteCache<TestKey, TestValue> cache2 = ignite.cache(CACHE2);
    assertNotNull(cache1);
    assertNotNull(cache2);
    assertNotSame(cache1, cache2);
    try {
        Random rnd = new Random();
        long seed = System.currentTimeMillis();
        rnd.setSeed(seed);
        log.info("Test tx operations [concurrency=" + concurrency + ", isolation=" + isolation + ", client=" + client + ", seed=" + seed + ']');
        IgniteTransactions txs = ignite.transactions();
        final List<TestKey> keys = new ArrayList<>();
        for (int i = 0; i < KEY_RANGE; i++) keys.add(new TestKey(i));
        CacheConfiguration ccfg = cache1.getConfiguration(CacheConfiguration.class);
        boolean fullSync = ccfg.getWriteSynchronizationMode() == FULL_SYNC;
        boolean optimistic = concurrency == OPTIMISTIC;
        boolean checkData = fullSync && !optimistic;
        long stopTime = System.currentTimeMillis() + 10_000;
        for (int i = 0; i < 10_000; i++) {
            if (i % 100 == 0) {
                if (System.currentTimeMillis() > stopTime) {
                    log.info("Stop on timeout, iteration: " + i);
                    break;
                }
                log.info("Iteration: " + i);
            }
            boolean rollback = i % 10 == 0;
            try (Transaction tx = txs.txStart(concurrency, isolation)) {
                cacheOperation(expData1, rnd, cache1, checkData, rollback);
                if (crossCacheTx)
                    cacheOperation(expData2, rnd, cache2, checkData, rollback);
                if (rollback)
                    tx.rollback();
                else
                    tx.commit();
            }
        }
        if (fullSync) {
            checkData(caches1, keys, expData1);
            checkData(caches2, keys, expData2);
            cache1.removeAll();
            cache2.removeAll();
            checkData(caches1, keys, new HashMap<TestKey, TestValue>());
            checkData(caches2, keys, new HashMap<TestKey, TestValue>());
        }
    } finally {
        cache1.removeAll();
        cache2.removeAll();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) Random(java.util.Random) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

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