Search in sources :

Example 36 with IgniteTransactions

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

the class IgniteCacheNearOnlyTxTest method txMultithreaded.

/**
 * @param optimistic If {@code true} uses optimistic transaction.
 * @throws Exception If failed.
 */
private void txMultithreaded(final boolean optimistic) throws Exception {
    final Ignite ignite1 = ignite(1);
    assertTrue(ignite1.configuration().isClientMode());
    ignite1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
    final AtomicInteger idx = new AtomicInteger();
    final Integer key = 1;
    IgniteCache<Integer, Integer> cache0 = ignite(0).cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Integer> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 5; i++) {
        log.info("Iteration: " + i);
        GridTestUtils.runMultiThreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
                IgniteTransactions txs = ignite1.transactions();
                int val = idx.getAndIncrement();
                for (int i = 0; i < 100; i++) {
                    try (Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ)) {
                        cache.get(key);
                        cache.put(key, val);
                        tx.commit();
                    }
                }
                return null;
            }
        }, 5, "put-thread");
        assertEquals(cache0.localPeek(key), cache1.localPeek(key));
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite)

Example 37 with IgniteTransactions

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

the class IgniteCachePutGetRestartAbstractTest method testTxPutGetRestart.

/**
 * @throws Exception If failed.
 */
public void testTxPutGetRestart() throws Exception {
    int clientGrid = gridCount() - 1;
    assertTrue(ignite(clientGrid).configuration().isClientMode());
    final IgniteTransactions txs = ignite(clientGrid).transactions();
    final IgniteCache<Integer, Integer> cache = jcache(clientGrid);
    updateCache(cache, txs);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("update-thread");
            assertTrue(latch.await(30_000, TimeUnit.MILLISECONDS));
            int iter = 0;
            while (!stop.get()) {
                log.info("Start update: " + iter);
                synchronized (mux) {
                    updateCache(cache, txs);
                }
                log.info("End update: " + iter++);
            }
            log.info("Update iterations: " + iter);
            return null;
        }
    });
    IgniteInternalFuture<?> restartFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("restart-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (!stop.get()) {
                assertTrue(latch.await(30_000, TimeUnit.MILLISECONDS));
                int node = rnd.nextInt(0, gridCount() - 1);
                log.info("Stop node: " + node);
                stopGrid(node);
                U.sleep(100);
                log.info("Start node: " + node);
                startGrid(node);
                latch = new CountDownLatch(1);
                U.sleep(100);
            }
            return null;
        }
    });
    long endTime = System.currentTimeMillis() + 2 * 60_000;
    try {
        int iter = 0;
        while (System.currentTimeMillis() < endTime && !updateFut.isDone() && !restartFut.isDone()) {
            try {
                log.info("Start get: " + iter);
                synchronized (mux) {
                    readCache(cache, txs);
                }
                log.info("End get: " + iter++);
            } finally {
                latch.countDown();
            }
        }
        log.info("Get iterations: " + iter);
    } finally {
        latch.countDown();
        stop.set(true);
    }
    updateFut.get();
    restartFut.get();
    readCache(cache, txs);
}
Also used : IgniteTransactions(org.apache.ignite.IgniteTransactions) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 38 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) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) 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)

Example 39 with IgniteTransactions

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

the class IgniteCacheConfigVariationsFullApiTest method testContainsKeysTx.

/**
 * @throws Exception If failed.
 */
public void testContainsKeysTx() throws Exception {
    if (!txEnabled())
        return;
    IgniteCache<String, Integer> cache = jcache();
    IgniteTransactions txs = ignite(0).transactions();
    Set<String> keys = new HashSet<>();
    for (int i = 0; i < 10; i++) {
        String key = String.valueOf(i);
        keys.add(key);
    }
    try (Transaction tx = txs.txStart()) {
        for (String key : keys) assertNull(key, cache.get(key));
        assertFalse(cache.containsKeys(keys));
        tx.commit();
    }
    try (Transaction tx = txs.txStart()) {
        for (String key : keys) assertNull(key, cache.get(key));
        for (String key : keys) cache.put(key, 0);
        assertTrue(cache.containsKeys(keys));
        tx.commit();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteTransactions(org.apache.ignite.IgniteTransactions) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 40 with IgniteTransactions

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

the class IgniteCacheConfigVariationsFullApiTest method testContainsKeyTx.

/**
 * @throws Exception If failed.
 */
public void testContainsKeyTx() throws Exception {
    if (!txEnabled())
        return;
    IgniteCache<String, Integer> cache = jcache();
    IgniteTransactions txs = ignite(0).transactions();
    for (int i = 0; i < 10; i++) {
        String key = String.valueOf(i);
        try (Transaction tx = txs.txStart()) {
            assertNull(key, cache.get(key));
            assertFalse(cache.containsKey(key));
            tx.commit();
        }
        try (Transaction tx = txs.txStart()) {
            assertNull(key, cache.get(key));
            cache.put(key, i);
            assertTrue(cache.containsKey(key));
            tx.commit();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)81 Transaction (org.apache.ignite.transactions.Transaction)78 Ignite (org.apache.ignite.Ignite)56 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 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)10 ArrayList (java.util.ArrayList)9 Callable (java.util.concurrent.Callable)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 LinkedHashMap (java.util.LinkedHashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8