Search in sources :

Example 36 with TransactionConcurrency

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

the class IgniteCacheInvokeReadThroughAbstractTest method invokeReadThrough.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
protected void invokeReadThrough(CacheConfiguration ccfg) throws Exception {
    Ignite ignite0 = ignite(0);
    ignite0.createCache(ccfg);
    int key = 0;
    for (Ignite node : G.allGrids()) {
        if (node.configuration().isClientMode() && ccfg.getNearConfiguration() != null)
            node.createNearCache(ccfg.getName(), ccfg.getNearConfiguration());
    }
    for (Ignite node : G.allGrids()) {
        log.info("Test for node: " + node.name());
        IgniteCache<Object, Object> cache = node.cache(ccfg.getName());
        for (int i = 0; i < 50; i++) checkReadThrough(cache, key++, null, null);
        Set<Object> keys = new HashSet<>();
        for (int i = 0; i < 5; i++) keys.add(key++);
        checkReadThroughInvokeAll(cache, keys, null, null);
        keys = new HashSet<>();
        for (int i = 0; i < 100; i++) keys.add(key++);
        checkReadThroughInvokeAll(cache, keys, null, null);
        if (ccfg.getAtomicityMode() == TRANSACTIONAL) {
            for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                for (TransactionIsolation isolation : TransactionIsolation.values()) {
                    log.info("Test tx [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    for (int i = 0; i < 50; i++) checkReadThrough(cache, key++, concurrency, isolation);
                    keys = new HashSet<>();
                    for (int i = 0; i < 5; i++) keys.add(key++);
                    checkReadThroughInvokeAll(cache, keys, concurrency, isolation);
                    keys = new HashSet<>();
                    for (int i = 0; i < 100; i++) keys.add(key++);
                    checkReadThroughInvokeAll(cache, keys, concurrency, isolation);
                }
            }
            for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                for (TransactionIsolation isolation : TransactionIsolation.values()) {
                    log.info("Test tx2 [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    for (int i = 0; i < 50; i++) checkReadThroughGetAndInvoke(cache, key++, concurrency, isolation);
                }
            }
        }
    }
    ignite0.cache(ccfg.getName()).removeAll();
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet)

Example 37 with TransactionConcurrency

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

the class IgniteOnePhaseCommitNearReadersTest method putReadersUpdate.

/**
 * @param backups Backups number.
 * @throws Exception If failed.
 */
private void putReadersUpdate(int backups) throws Exception {
    final int SRVS = 3;
    startGrids(SRVS);
    awaitPartitionMapExchange();
    client = true;
    Ignite srv = ignite(0);
    srv.createCache(cacheConfiguration(backups));
    Ignite client1 = startGrid(SRVS);
    IgniteCache<Object, Object> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
    Integer key = primaryKey(srv.cache(DEFAULT_CACHE_NAME));
    Ignite client2 = startGrid(SRVS + 1);
    IgniteCache<Object, Object> cache2 = client2.cache(DEFAULT_CACHE_NAME);
    cache1.put(key, 1);
    cache2.put(key, 2);
    checkCacheData(F.asMap(key, 2), DEFAULT_CACHE_NAME);
    int val = 10;
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            try (Transaction tx = client2.transactions().txStart(concurrency, isolation)) {
                cache2.put(key, val);
                tx.commit();
            }
            checkCacheData(F.asMap(key, val), DEFAULT_CACHE_NAME);
            val++;
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite)

Example 38 with TransactionConcurrency

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

the class IgniteOnePhaseCommitNearReadersTest method putReaderUpdatePrimaryFails.

/**
 * @param backups Backups number.
 * @throws Exception If failed.
 */
private void putReaderUpdatePrimaryFails(int backups) throws Exception {
    testSpi = true;
    final int SRVS = 3;
    startGrids(SRVS);
    awaitPartitionMapExchange();
    client = true;
    Ignite srv = ignite(0);
    srv.createCache(cacheConfiguration(backups));
    Ignite client1 = startGrid(SRVS);
    IgniteCache<Object, Object> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
    Ignite client2 = startGrid(SRVS + 1);
    client = false;
    IgniteCache<Object, Object> cache2 = client2.cache(DEFAULT_CACHE_NAME);
    Integer key = primaryKey(srv.cache(DEFAULT_CACHE_NAME));
    cache1.put(key, 1);
    spi(srv).blockMessages(GridNearTxPrepareResponse.class, client2.name());
    IgniteFuture<?> fut = cache2.putAsync(key, 2);
    U.sleep(1000);
    assertFalse(fut.isDone());
    stopGrid(0);
    fut.get();
    checkCacheData(F.asMap(key, backups == 0 ? null : 2), DEFAULT_CACHE_NAME);
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            srv = startGrid(0);
            awaitPartitionMapExchange(true, true, null);
            key = primaryKey(srv.cache(DEFAULT_CACHE_NAME));
            cache1.put(key, 1);
            spi(srv).blockMessages(GridNearTxPrepareResponse.class, client2.name());
            try (Transaction tx = client2.transactions().txStart(concurrency, isolation)) {
                cache2.putAsync(key, 2);
                fut = tx.commitAsync();
                U.sleep(1000);
                assertFalse(fut.isDone());
                stopGrid(0);
                if (backups == 0)
                    fut.get();
                else {
                    try {
                        fut.get();
                        fail();
                    } catch (TransactionRollbackException ignore) {
                    // Expected.
                    }
                }
            }
            checkCacheData(F.asMap(key, backups == 0 ? null : 1), DEFAULT_CACHE_NAME);
            try (Transaction tx = client2.transactions().txStart(concurrency, isolation)) {
                cache2.putAsync(key, 2);
                tx.commit();
            }
            checkCacheData(F.asMap(key, 2), DEFAULT_CACHE_NAME);
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException)

Example 39 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 40 with TransactionConcurrency

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

the class IgniteCacheReadThroughStoreCallTest method checkLoadCount.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void checkLoadCount(CacheConfiguration<Object, Object> ccfg) throws Exception {
    storeMap.clear();
    Ignite ignite0 = ignite(0);
    ignite0.createCache(ccfg);
    try {
        int key = 0;
        for (Ignite node : G.allGrids()) {
            log.info("Test for node: " + node.name());
            final IgniteCache<Object, Object> cache = node.cache(ccfg.getName());
            for (int i = 0; i < 50; i++) {
                final int k = key++;
                checkReadThrough(cache, new IgniteRunnable() {

                    @Override
                    public void run() {
                        cache.invoke(k, new TestEntryProcessor());
                    }
                }, null, null, 1);
            }
            for (int i = 0; i < 50; i++) {
                final int k = key++;
                checkReadThrough(cache, new IgniteRunnable() {

                    @Override
                    public void run() {
                        cache.put(k, k);
                    }
                }, null, null, 0);
            }
            if (ccfg.getAtomicityMode() == TRANSACTIONAL) {
                for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                    for (TransactionIsolation isolation : values()) {
                        log.info("Test tx [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                        for (int i = 0; i < 50; i++) {
                            final int k = key++;
                            checkReadThrough(cache, new IgniteRunnable() {

                                @Override
                                public void run() {
                                    cache.invoke(k, new TestEntryProcessor());
                                }
                            }, concurrency, isolation, 2);
                        }
                    }
                }
            }
        }
        ignite0.cache(ccfg.getName()).removeAll();
    } finally {
        ignite0.destroyCache(ccfg.getName());
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

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