Search in sources :

Example 26 with TransactionConcurrency

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

the class IgniteClientReconnectFailoverTest method testReconnectTxCache.

/**
     * @throws Exception If failed.
     */
public void testReconnectTxCache() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Integer> cache = client.cache(TX_CACHE);
    assertNotNull(cache);
    assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
    final IgniteTransactions txs = client.transactions();
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                TreeMap<Integer, Integer> map = new TreeMap<>();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < 5; i++) {
                    Integer key = rnd.nextInt(0, 100_000);
                    cache.put(key, key);
                    assertEquals(key, cache.get(key));
                    map.put(key, key);
                }
                for (TransactionConcurrency txConcurrency : TransactionConcurrency.values()) {
                    try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
                        for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                            cache.put(e.getKey(), e.getValue());
                            assertNotNull(cache.get(e.getKey()));
                        }
                        tx.commit();
                    }
                }
                cache.putAll(map);
                Map<Integer, Integer> res = cache.getAll(map.keySet());
                assertEquals(map, res);
            } catch (IgniteClientDisconnectedException e) {
                throw e;
            } catch (IgniteException e) {
                log.info("Ignore error: " + e);
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            return null;
        }
    });
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteTransactions(org.apache.ignite.IgniteTransactions) TreeMap(java.util.TreeMap) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 27 with TransactionConcurrency

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

the class CachePutIfAbsentTest 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()) {
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 10; i++) {
                Integer key = rnd.nextInt(10_000);
                cache.put(key, 2);
                for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                    for (TransactionIsolation isolation : TransactionIsolation.values()) {
                        try (Transaction tx = txs.txStart(concurrency, isolation)) {
                            Object old = cache.getAndPutIfAbsent(key, 3);
                            assertEquals(2, old);
                            Object val = cache.get(key);
                            assertEquals(2, val);
                            tx.commit();
                        }
                        assertEquals((Integer) 2, cache.get(key));
                    }
                }
            }
        } finally {
            ignite0.destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 28 with TransactionConcurrency

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

the class CacheTxFastFinishTest method fastFinishTx.

/**
     * @param ignite Node.
     */
private void fastFinishTx(Ignite ignite) {
    IgniteTransactions txs = ignite.transactions();
    IgniteCache cache = ignite.cache(DEFAULT_CACHE_NAME);
    for (boolean commit : new boolean[] { true, false }) {
        for (TransactionConcurrency c : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                try (Transaction tx = txs.txStart(c, isolation)) {
                    checkFastTxFinish(tx, commit);
                }
            }
        }
        for (int i = 0; i < 100; i++) {
            try (Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
                cache.get(i);
                checkFastTxFinish(tx, commit);
            }
            try (Transaction tx = txs.txStart(OPTIMISTIC, READ_COMMITTED)) {
                cache.get(i);
                checkFastTxFinish(tx, commit);
            }
        }
        for (int i = 0; i < 100; i++) {
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.get(i);
                checkNormalTxFinish(tx, commit);
            }
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.get(i);
                checkNormalTxFinish(tx, commit);
            }
        }
        for (int i = 0; i < 100; i++) {
            for (TransactionConcurrency c : TransactionConcurrency.values()) {
                for (TransactionIsolation isolation : TransactionIsolation.values()) {
                    try (Transaction tx = txs.txStart(c, isolation)) {
                        cache.put(i, i);
                        checkNormalTxFinish(tx, commit);
                    }
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 29 with TransactionConcurrency

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

the class IgniteOnePhaseCommitNearSelfTest method checkKey.

/**
     * @param transactions Transactions instance.
     * @param cache Cache instance.
     * @param key Key.
     */
private void checkKey(IgniteTransactions transactions, Cache<Object, Object> cache, int key) throws Exception {
    cache.put(key, key);
    finalCheck(key, true);
    TransactionIsolation[] isolations = { READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE };
    TransactionConcurrency[] concurrencies = { OPTIMISTIC, PESSIMISTIC };
    for (TransactionIsolation isolation : isolations) {
        for (TransactionConcurrency concurrency : concurrencies) {
            info("Checking transaction [isolation=" + isolation + ", concurrency=" + concurrency + ']');
            try (Transaction tx = transactions.txStart(concurrency, isolation)) {
                cache.put(key, isolation + "-" + concurrency);
                tx.commit();
            }
            finalCheck(key, true);
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Example 30 with TransactionConcurrency

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

the class IgniteCacheNoReadThroughAbstractTest method testNoReadThrough.

/**
     * @throws Exception If failed.
     */
public void testNoReadThrough() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    for (Integer key : keys()) {
        log.info("Test [key=" + key + ']');
        storeMap.put(key, key);
        assertNull(cache.get(key));
        assertEquals(key, storeMap.get(key));
        assertNull(cache.getAndPut(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertTrue(cache.putIfAbsent(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertNull(cache.getAndRemove(key));
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertNull(cache.getAndPutIfAbsent(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

            @Override
            public Object process(MutableEntry<Integer, Integer> e, Object... args) {
                Integer val = e.getValue();
                assertFalse(e.exists());
                assertNull(val);
                e.setValue(-1);
                return String.valueOf(val);
            }
        });
        assertEquals("null", ret);
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertFalse(cache.replace(key, -1));
        assertEquals(key, storeMap.get(key));
        assertNull(cache.getAndReplace(key, -1));
        assertEquals(key, storeMap.get(key));
        assertFalse(cache.replace(key, key, -1));
        assertEquals(key, storeMap.get(key));
    }
    Set<Integer> keys = new HashSet<>();
    for (int i = 1000_0000; i < 1000_0000 + 1000; i++) {
        keys.add(i);
        storeMap.put(i, i);
    }
    assertTrue(cache.getAll(keys).isEmpty());
    if (atomicityMode() == TRANSACTIONAL) {
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                for (Integer key : keys()) {
                    log.info("Test tx [key=" + key + ", concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertNull(cache.get(key));
                        tx.commit();
                    }
                    assertEquals(key, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertNull(cache.getAndPut(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    cache.remove(key);
                    assertNull(storeMap.get(key));
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.putIfAbsent(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    cache.remove(key);
                    assertNull(storeMap.get(key));
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

                            @Override
                            public Object process(MutableEntry<Integer, Integer> e, Object... args) {
                                Integer val = e.getValue();
                                assertFalse(e.exists());
                                assertNull(val);
                                e.setValue(-1);
                                return String.valueOf(val);
                            }
                        });
                        assertEquals("null", ret);
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.getAll(keys).isEmpty());
                        tx.commit();
                    }
                }
            }
        }
    }
    // Check can load cache when read-through is disabled.
    allowLoad = true;
    Integer key = 1;
    cache.remove(key);
    storeMap.clear();
    storeMap.put(key, 10);
    cache.loadCache(null);
    assertEquals(10, (int) cache.get(key));
    cache.remove(key);
    storeMap.put(key, 11);
    CompletionListenerFuture fut = new CompletionListenerFuture();
    cache.loadAll(F.asSet(key), true, fut);
    fut.get();
    assertEquals(11, (int) cache.get(key));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet)

Aggregations

TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)48 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)42 Transaction (org.apache.ignite.transactions.Transaction)27 Ignite (org.apache.ignite.Ignite)18 IgniteTransactions (org.apache.ignite.IgniteTransactions)11 IgniteCache (org.apache.ignite.IgniteCache)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IgniteException (org.apache.ignite.IgniteException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CacheException (javax.cache.CacheException)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Callable (java.util.concurrent.Callable)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 HashSet (java.util.HashSet)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FactoryBuilder (javax.cache.configuration.FactoryBuilder)2 MutableEntry (javax.cache.processor.MutableEntry)2