Search in sources :

Example 41 with Transaction

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

the class ClusterStateAbstractTest method deactivateWithPendingTransaction.

/**
 * @throws Exception if failed.
 */
private void deactivateWithPendingTransaction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    final Ignite ignite0 = grid(0);
    final IgniteCache<Object, Object> cache0 = ignite0.cache(CACHE_NAME);
    try (Transaction tx = ignite0.transactions().txStart(concurrency, isolation)) {
        cache0.put(1, "1");
        GridTestUtils.assertThrowsAnyCause(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                grid(0).active(false);
                return null;
            }
        }, IgniteException.class, "Failed to deactivate cluster (must invoke the method outside of an active transaction or lock).");
    }
    assertNull(cache0.get(1));
    assertNull(ignite0.transactions().tx());
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException)

Example 42 with Transaction

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

the class GridCacheAbstractRemoveFailureTest method putAndRemove.

/**
 * @param duration Test duration.
 * @param txConcurrency Transaction concurrency if test explicit transaction.
 * @param txIsolation Transaction isolation if test explicit transaction.
 * @throws Exception If failed.
 */
private void putAndRemove(long duration, final TransactionConcurrency txConcurrency, final TransactionIsolation txIsolation) throws Exception {
    assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
    grid(0).destroyCache(DEFAULT_CACHE_NAME);
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setCacheMode(cacheMode());
    if (cacheMode() == PARTITIONED)
        ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode());
    ccfg.setNearConfiguration(nearCache());
    final IgniteCache<Integer, Integer> sndCache0 = grid(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicLong cntr = new AtomicLong();
    final AtomicLong errCntr = new AtomicLong();
    // Expected values in cache.
    final Map<Integer, GridTuple<Integer>> expVals = new ConcurrentHashMap<>();
    final AtomicReference<CyclicBarrier> cmp = new AtomicReference<>();
    IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("update-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteTransactions txs = sndCache0.unwrap(Ignite.class).transactions();
            while (!stop.get()) {
                for (int i = 0; i < 100; i++) {
                    int key = rnd.nextInt(KEYS_CNT);
                    boolean put = rnd.nextInt(0, 100) > 10;
                    while (true) {
                        try {
                            if (put) {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.put(key, i);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.put(key, i);
                                if (!failed)
                                    expVals.put(key, F.t(i));
                            } else {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.remove(key);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.remove(key);
                                if (!failed)
                                    expVals.put(key, F.<Integer>t(null));
                            }
                            break;
                        } catch (CacheException e) {
                            if (put)
                                log.error("Put failed [key=" + key + ", val=" + i + ']', e);
                            else
                                log.error("Remove failed [key=" + key + ']', e);
                            errCntr.incrementAndGet();
                        }
                    }
                }
                cntr.addAndGet(100);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    IgniteInternalFuture killFut = createAndRunConcurrentAction(stop, cmp);
    try {
        long stopTime = duration + U.currentTimeMillis();
        long nextAssert = U.currentTimeMillis() + ASSERT_FREQ;
        while (U.currentTimeMillis() < stopTime) {
            long start = System.nanoTime();
            long ops = cntr.longValue();
            U.sleep(1000);
            long diff = cntr.longValue() - ops;
            double time = (System.nanoTime() - start) / 1_000_000_000d;
            long opsPerSecond = (long) (diff / time);
            log.info("Operations/second: " + opsPerSecond);
            if (U.currentTimeMillis() >= nextAssert) {
                CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            cmp.set(null);
                            log.info("Checking cache content.");
                            assertCacheContent(expVals);
                            log.info("Finished check cache content.");
                        } catch (Throwable e) {
                            log.error("Unexpected error: " + e, e);
                            throw e;
                        }
                    }
                });
                log.info("Start cache content check.");
                cmp.set(barrier);
                try {
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    U.dumpThreads(log);
                    fail("Failed to check cache content: " + e);
                }
                log.info("Cache content check done.");
                nextAssert = System.currentTimeMillis() + ASSERT_FREQ;
            }
        }
    } finally {
        stop.set(true);
    }
    killFut.get();
    updateFut.get();
    log.info("Test finished. Update errors: " + errCntr.get());
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 43 with Transaction

use of org.apache.ignite.transactions.Transaction 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 44 with Transaction

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

the class GridCacheTxLoadFromStoreOnLockSelfTest method checkLoadedValue.

/**
 * @throws Exception If failed.
 */
private void checkLoadedValue(int backups) throws Exception {
    CacheConfiguration<Integer, Integer> cacheCfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    cacheCfg.setCacheMode(CacheMode.PARTITIONED);
    cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    cacheCfg.setRebalanceMode(CacheRebalanceMode.SYNC);
    cacheCfg.setCacheStoreFactory(new StoreFactory());
    cacheCfg.setReadThrough(true);
    cacheCfg.setBackups(backups);
    cacheCfg.setLoadPreviousValue(true);
    IgniteCache<Integer, Integer> cache = ignite(0).createCache(cacheCfg);
    for (int i = 0; i < 10; i++) assertEquals((Integer) i, cache.get(i));
    cache.removeAll();
    assertEquals(0, cache.size());
    for (TransactionConcurrency conc : TransactionConcurrency.values()) {
        for (TransactionIsolation iso : TransactionIsolation.values()) {
            info("Checking transaction [conc=" + conc + ", iso=" + iso + ']');
            try (Transaction tx = ignite(0).transactions().txStart(conc, iso)) {
                for (int i = 0; i < 10; i++) assertEquals("Invalid value for transaction [conc=" + conc + ", iso=" + iso + ']', (Integer) i, cache.get(i));
                tx.commit();
            }
            cache.removeAll();
            assertEquals(0, cache.size());
        }
    }
    cache.destroy();
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 45 with Transaction

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

the class CacheSerializableTransactionsTest method rollbackNearCacheRead.

/**
 * @param near If {@code true} updates entry using the same near cache.
 * @throws Exception If failed.
 */
private void rollbackNearCacheRead(boolean near) throws Exception {
    Ignite ignite0 = ignite(0);
    IgniteCache<Integer, Integer> cache0 = ignite0.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false));
    final String cacheName = cache0.getName();
    try {
        Ignite ignite = ignite(SRVS);
        IgniteCache<Integer, Integer> cache = ignite.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>());
        IgniteTransactions txs = ignite.transactions();
        Integer key1 = primaryKey(ignite(0).cache(cacheName));
        Integer key2 = primaryKey(ignite(1).cache(cacheName));
        Integer key3 = primaryKey(ignite(2).cache(cacheName));
        cache0.put(key1, -1);
        cache0.put(key2, -1);
        cache0.put(key3, -1);
        try {
            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                cache.get(key1);
                cache.get(key2);
                cache.get(key3);
                updateKey(near ? cache : cache0, key2, -2);
                tx.commit();
            }
            fail();
        } catch (TransactionOptimisticException e) {
            log.info("Expected exception: " + e);
        }
        checkValue(key1, -1, cacheName);
        checkValue(key2, -2, cacheName);
        checkValue(key3, -1, cacheName);
        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
            cache.put(key1, key1);
            cache.put(key2, key2);
            cache.put(key3, key3);
            tx.commit();
        }
        checkValue(key1, key1, cacheName);
        checkValue(key2, key2, cacheName);
        checkValue(key3, key3, cacheName);
    } finally {
        destroyCache(cacheName);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

Transaction (org.apache.ignite.transactions.Transaction)493 Ignite (org.apache.ignite.Ignite)204 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)183 IgniteCache (org.apache.ignite.IgniteCache)88 IgniteTransactions (org.apache.ignite.IgniteTransactions)78 IgniteException (org.apache.ignite.IgniteException)74 CacheException (javax.cache.CacheException)60 HashMap (java.util.HashMap)54 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)45 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)44 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)42 ArrayList (java.util.ArrayList)41 Callable (java.util.concurrent.Callable)41 Map (java.util.Map)36 IgniteEx (org.apache.ignite.internal.IgniteEx)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 IgniteKernal (org.apache.ignite.internal.IgniteKernal)30 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)30 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29