Search in sources :

Example 91 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method testConflictResolution.

/**
 * @throws Exception If failed.
 */
@Test
public void testConflictResolution() throws Exception {
    final Ignite ignite = ignite(0);
    final String cacheName = ignite.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
    try {
        final Map<Integer, Integer> keys = new HashMap<>();
        for (int i = 0; i < 500; i++) keys.put(i, i);
        final int THREADS = 5;
        for (int i = 0; i < 10; i++) {
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            final AtomicInteger commitCntr = new AtomicInteger(0);
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
                    IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        cache.putAll(keys);
                        barrier.await();
                        tx.commit();
                        commitCntr.incrementAndGet();
                    } catch (TransactionOptimisticException e) {
                        log.info("Optimistic error: " + e);
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            int commits = commitCntr.get();
            log.info("Iteration [iter=" + i + ", commits=" + commits + ']');
            assertTrue(commits > 0);
        }
    } finally {
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 92 with IgniteTransactions

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

the class CacheSerializableTransactionsTest method accountTx.

/**
 * @param getAll If {@code true} uses getAll/putAll in transaction.
 * @param nearCache If {@code true} near cache is enabled.
 * @param nonSer If {@code true} starts threads executing non-serializable transactions.
 * @param restart If {@code true} restarts one node.
 * @throws Exception If failed.
 */
private void accountTx(final boolean getAll, final boolean nearCache, final boolean nonSer, final boolean restart) throws Exception {
    final Ignite srv = ignite(1);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
    final String cacheName = srv.createCache(ccfg).getName();
    try {
        final List<Ignite> clients = clients();
        final int ACCOUNTS = SF.applyLB(100, 10);
        final int VAL_PER_ACCOUNT = SF.applyLB(10_000, 50);
        IgniteCache<Integer, Account> srvCache = srv.cache(cacheName);
        for (int i = 0; i < ACCOUNTS; i++) srvCache.put(i, new Account(VAL_PER_ACCOUNT));
        final AtomicInteger idx = new AtomicInteger();
        final int THREADS = SF.applyLB(20, 5);
        final long testTime = SF.applyLB(30_000, 5_000);
        final long stopTime = System.currentTimeMillis() + testTime;
        IgniteInternalFuture<?> nonSerFut = null;
        if (nonSer) {
            nonSerFut = runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() {
                    int nodeIdx = idx.getAndIncrement() % clients.size();
                    Ignite node = clients.get(nodeIdx);
                    Thread.currentThread().setName("update-pessimistic-" + node.name());
                    log.info("Pessimistic tx thread: " + node.name());
                    final IgniteTransactions txs = node.transactions();
                    final IgniteCache<Integer, Account> cache = nearCache ? node.createNearCache(cacheName, new NearCacheConfiguration<Integer, Account>()) : node.<Integer, Account>cache(cacheName);
                    assertNotNull(cache);
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (U.currentTimeMillis() < stopTime) {
                        int id1 = rnd.nextInt(ACCOUNTS);
                        int id2 = rnd.nextInt(ACCOUNTS);
                        while (id2 == id1) id2 = rnd.nextInt(ACCOUNTS);
                        if (id1 > id2) {
                            int tmp = id1;
                            id1 = id2;
                            id2 = tmp;
                        }
                        try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            Account a1 = cache.get(id1);
                            Account a2 = cache.get(id2);
                            assertNotNull(a1);
                            assertNotNull(a2);
                            if (a1.value() > 0) {
                                a1 = new Account(a1.value() - 1);
                                a2 = new Account(a2.value() + 1);
                            }
                            cache.put(id1, a1);
                            cache.put(id2, a2);
                            tx.commit();
                        }
                    }
                    return null;
                }
            }, 10, "non-ser-thread");
        }
        final IgniteInternalFuture<?> fut = runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() {
                int nodeIdx = idx.getAndIncrement() % clients.size();
                Ignite node = clients.get(nodeIdx);
                Thread.currentThread().setName("update-" + node.name());
                log.info("Tx thread: " + node.name());
                final IgniteTransactions txs = node.transactions();
                final IgniteCache<Integer, Account> cache = nearCache ? node.createNearCache(cacheName, new NearCacheConfiguration<>()) : node.cache(cacheName);
                assertNotNull(cache);
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (U.currentTimeMillis() < stopTime) {
                    int id1 = rnd.nextInt(ACCOUNTS);
                    int id2 = rnd.nextInt(ACCOUNTS);
                    while (id2 == id1) id2 = rnd.nextInt(ACCOUNTS);
                    try {
                        while (true) {
                            try {
                                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                    if (getAll) {
                                        Map<Integer, Account> map = cache.getAll(F.asSet(id1, id2));
                                        Account a1 = cache.get(id1);
                                        Account a2 = cache.get(id2);
                                        assertNotNull(a1);
                                        assertNotNull(a2);
                                        if (a1.value() > 0) {
                                            a1 = new Account(a1.value() - 1);
                                            a2 = new Account(a2.value() + 1);
                                        }
                                        map.put(id1, a1);
                                        map.put(id2, a2);
                                        cache.putAll(map);
                                    } else {
                                        Account a1 = cache.get(id1);
                                        Account a2 = cache.get(id2);
                                        assertNotNull(a1);
                                        assertNotNull(a2);
                                        if (a1.value() > 0) {
                                            a1 = new Account(a1.value() - 1);
                                            a2 = new Account(a2.value() + 1);
                                        }
                                        cache.put(id1, a1);
                                        cache.put(id2, a2);
                                    }
                                    tx.commit();
                                }
                                break;
                            } catch (TransactionOptimisticException ignore) {
                            // Retry.
                            } catch (IgniteException | CacheException e) {
                                assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
                            }
                        }
                    } catch (Throwable e) {
                        log.error("Unexpected error: " + e, e);
                        throw e;
                    }
                }
                return null;
            }
        }, THREADS, "tx-thread");
        IgniteInternalFuture<?> restartFut = restart ? restartFuture(null, fut) : null;
        fut.get(testTime + 30_000);
        if (nonSerFut != null)
            nonSerFut.get();
        if (restartFut != null)
            restartFut.get();
        int sum = 0;
        for (int i = 0; i < ACCOUNTS; i++) {
            Account a = srvCache.get(i);
            assertNotNull(a);
            assertTrue(a.value() >= 0);
            log.info("Account: " + a.value());
            sum += a.value();
        }
        assertEquals(ACCOUNTS * VAL_PER_ACCOUNT, sum);
        for (int node = 0; node < SRVS + CLIENTS; node++) {
            log.info("Verify node: " + node);
            Ignite ignite = ignite(node);
            IgniteCache<Integer, Account> cache = ignite.cache(cacheName);
            sum = 0;
            try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                Map<Integer, Account> map = new HashMap<>();
                for (int i = 0; i < ACCOUNTS; i++) {
                    Account a = cache.get(i);
                    assertNotNull(a);
                    map.put(i, a);
                    sum += a.value();
                }
                Account a1 = map.get(0);
                Account a2 = map.get(1);
                if (a1.value() > 0) {
                    a1 = new Account(a1.value() - 1);
                    a2 = new Account(a2.value() + 1);
                    map.put(0, a1);
                    map.put(1, a2);
                }
                cache.putAll(map);
                tx.commit();
            }
            assertEquals(ACCOUNTS * VAL_PER_ACCOUNT, sum);
        }
    } finally {
        destroyCache(cacheName);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 93 with IgniteTransactions

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

the class CacheTxFastFinishTest method fastFinishTx.

/**
 * @param ignite Node.
 */
protected 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, true);
            }
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.get(i);
                checkNormalTxFinish(tx, commit, true);
            }
        }
        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, false);
                    }
                }
            }
        }
    }
}
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 94 with IgniteTransactions

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

the class IgniteTxCachePrimarySyncTest method checkTxSyncMode.

/**
 * @param ignite Node.
 * @param commit If {@code true} commits transaction.
 */
private void checkTxSyncMode(Ignite ignite, boolean commit) {
    IgniteTransactions txs = ignite.transactions();
    IgniteCache<Object, Object> fullSync1 = ignite.cache("fullSync1");
    IgniteCache<Object, Object> fullSync2 = ignite.cache("fullSync2");
    IgniteCache<Object, Object> fullAsync1 = ignite.cache("fullAsync1");
    IgniteCache<Object, Object> fullAsync2 = ignite.cache("fullAsync2");
    IgniteCache<Object, Object> primarySync1 = ignite.cache("primarySync1");
    IgniteCache<Object, Object> primarySync2 = ignite.cache("primarySync2");
    for (int i = 0; i < 3; i++) {
        int key = 0;
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                if (MvccFeatureChecker.forcedMvcc() && !MvccFeatureChecker.isSupported(concurrency, isolation))
                    continue;
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullSync2.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    fullAsync2.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    primarySync2.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    fullAsync2.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 95 with IgniteTransactions

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

the class IgniteCacheThreadLocalTxTest method checkTx.

/**
 * @param concurrency Tx concurrency.
 * @param isolation Tx isolation.
 * @param node Node.
 * @param cache Cache.
 * @param read {@code True} if read in tx.
 * @param write {@code True} if write in tx.
 * @param endOp Operation to test.
 */
private void checkTx(TransactionConcurrency concurrency, TransactionIsolation isolation, Ignite node, IgniteCache<Object, Object> cache, boolean read, boolean write, int endOp) {
    IgniteTransactions txs = node.transactions();
    checkNoTx(node);
    Transaction tx = txs.txStart(concurrency, isolation);
    assertEquals(tx, txs.tx());
    try {
        txs.txStart(concurrency, isolation);
        fail();
    } catch (IllegalStateException expected) {
    // No-op.
    }
    if (read)
        cache.get(ThreadLocalRandom.current().nextInt(100_000));
    if (write)
        cache.put(ThreadLocalRandom.current().nextInt(100_000), 1);
    try {
        txs.txStart(concurrency, isolation);
        fail();
    } catch (IllegalStateException expected) {
    // No-op.
    }
    assertEquals(tx, txs.tx());
    IgniteFuture fut = null;
    switch(endOp) {
        case 0:
            tx.commit();
            break;
        case 1:
            fut = tx.commitAsync();
            break;
        case 2:
            tx.rollback();
            break;
        case 3:
            fut = tx.rollbackAsync();
            break;
        case 4:
            tx.close();
            break;
        default:
            fail();
    }
    if (fut != null)
        fut.get();
    checkNoTx(node);
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)120 Transaction (org.apache.ignite.transactions.Transaction)116 Ignite (org.apache.ignite.Ignite)87 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 Test (org.junit.Test)49 IgniteCache (org.apache.ignite.IgniteCache)36 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)29 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)28 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)28 HashMap (java.util.HashMap)26 CacheException (javax.cache.CacheException)26 ArrayList (java.util.ArrayList)23 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)23 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)19 Map (java.util.Map)18 IgniteException (org.apache.ignite.IgniteException)18 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 HashSet (java.util.HashSet)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)16