Search in sources :

Example 81 with TransactionIsolation

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

the class TxWithSmallTimeoutAndContentionOneKeyTest method test.

/**
 * https://issues.apache.org/jira/browse/IGNITE-9042
 *
 * @throws Exception If failed.
 */
@Test
public void test() throws Exception {
    Assume.assumeFalse("https://issues.apache.org/jira/browse/IGNITE-10455", MvccFeatureChecker.forcedMvcc());
    startGrids(4);
    IgniteEx igClient = startClientGrid(getConfiguration("client").setConsistentId("Client"));
    igClient.cluster().active(true);
    AtomicBoolean stop = new AtomicBoolean(false);
    IgniteCache<Integer, Long> cache = igClient.cache(DEFAULT_CACHE_NAME);
    int threads = 1;
    int keyId = 0;
    CountDownLatch finishLatch = new CountDownLatch(threads);
    AtomicLong cnt = new AtomicLong();
    IgniteInternalFuture<Long> f = runMultiThreadedAsync(() -> {
        IgniteTransactions txMgr = igClient.transactions();
        while (!stop.get()) {
            long newVal = cnt.getAndIncrement();
            TransactionConcurrency concurrency = transactionConcurrency();
            TransactionIsolation transactionIsolation = transactionIsolation();
            try (Transaction tx = txMgr.txStart(concurrency, transactionIsolation, randomTimeOut(), 1)) {
                cache.put(keyId, newVal);
                tx.commit();
            } catch (Throwable e) {
            // Ignore.
            }
        }
        finishLatch.countDown();
    }, threads, "tx-runner");
    runAsync(() -> {
        try {
            Thread.sleep(TIME_TO_EXECUTE);
        } catch (InterruptedException ignore) {
        // Ignore.
        }
        stop.set(true);
    });
    finishLatch.await();
    f.get();
    IdleVerifyResultV2 idleVerifyResult = idleVerify(igClient, DEFAULT_CACHE_NAME);
    log.info("Current counter value:" + cnt.get());
    Long val = cache.get(keyId);
    log.info("Last commited value:" + val);
    if (idleVerifyResult.hasConflicts()) {
        SB sb = new SB();
        sb.a("\n");
        buildConflicts("Hash conflicts:\n", sb, idleVerifyResult.hashConflicts());
        buildConflicts("Counters conflicts:\n", sb, idleVerifyResult.counterConflicts());
        System.out.println(sb);
        fail();
    }
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) SB(org.apache.ignite.internal.util.typedef.internal.SB) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) AtomicLong(java.util.concurrent.atomic.AtomicLong) IdleVerifyResultV2(org.apache.ignite.internal.processors.cache.verify.IdleVerifyResultV2) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 82 with TransactionIsolation

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

the class TxPartitionCounterStateWithFilterTest method testAssignCountersInTxWithFilter.

/**
 */
@Test
public void testAssignCountersInTxWithFilter() {
    for (Ignite ig : G.allGrids()) {
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                try {
                    ignite(0).createCache(cacheConfiguration(cacheMode, backups, CacheAtomicityMode.TRANSACTIONAL));
                    IgniteCache<Integer, Integer> cache = ig.cache(DEFAULT_CACHE_NAME);
                    assertNotNull(cache);
                    int partId = 0;
                    List<Integer> keys = partitionKeys(cache, partId, 2, 0);
                    int key = keys.get(0), val = 0;
                    if (!sameTx)
                        cache.put(key, val);
                    try (Transaction tx = ig.transactions().txStart(concurrency, isolation)) {
                        if (sameTx)
                            cache.put(key, val);
                        Object prev = cache.getAndPutIfAbsent(key, val + 1);
                        assertNotNull(prev);
                        cache.put(keys.get(1), val);
                        tx.commit();
                    }
                    assertEquals(Integer.valueOf(val), cache.get(key));
                    assertEquals(Integer.valueOf(val), cache.get(keys.get(1)));
                    for (Ignite ignite : G.allGrids()) {
                        if (ignite.configuration().isClientMode())
                            continue;
                        PartitionUpdateCounter cntr = counter(partId, ignite.name());
                        if (cntr != null)
                            assertEquals("Expecting counter for node=" + ignite.name(), 2, cntr.get());
                    }
                } finally {
                    ignite(0).destroyCache(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) PartitionUpdateCounter(org.apache.ignite.internal.processors.cache.PartitionUpdateCounter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 83 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation 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();
    Ignite srv = ignite(0);
    srv.createCache(cacheConfiguration(backups));
    Ignite client1 = startClientGrid(SRVS);
    IgniteCache<Object, Object> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
    Ignite client2 = startClientGrid(SRVS + 1);
    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 84 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation 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();
    Ignite srv = ignite(0);
    srv.createCache(cacheConfiguration(backups));
    Ignite client1 = startClientGrid(SRVS);
    IgniteCache<Object, Object> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
    Integer key = primaryKey(srv.cache(DEFAULT_CACHE_NAME));
    Ignite client2 = startClientGrid(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 85 with TransactionIsolation

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

the class IgniteTxStoreExceptionAbstractSelfTest method testPutBackupTx.

/**
 * @throws Exception If failed.
 */
@Test
public void testPutBackupTx() throws Exception {
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            checkPutTx(true, concurrency, isolation, keyForNode(grid(0).localNode(), BACKUP));
            checkPutTx(false, concurrency, isolation, keyForNode(grid(0).localNode(), BACKUP));
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Test(org.junit.Test)

Aggregations

TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)88 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)71 Test (org.junit.Test)53 Transaction (org.apache.ignite.transactions.Transaction)52 Ignite (org.apache.ignite.Ignite)33 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)27 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)12 ArrayList (java.util.ArrayList)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgniteEx (org.apache.ignite.internal.IgniteEx)9 IgniteException (org.apache.ignite.IgniteException)8 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)7 HashMap (java.util.HashMap)6 Map (java.util.Map)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 TransactionTimeoutException (org.apache.ignite.transactions.TransactionTimeoutException)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4