Search in sources :

Example 96 with Transaction

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

the class TxDeadlockDetectionTest method doTestFailedMessage.

/**
 * @param failCls Failing message class.
 * @throws Exception If failed.
 */
private void doTestFailedMessage(Class failCls) throws Exception {
    try {
        final int txCnt = 2;
        final CyclicBarrier barrier = new CyclicBarrier(txCnt);
        final AtomicInteger threadCnt = new AtomicInteger();
        final AtomicBoolean deadlock = new AtomicBoolean();
        final AtomicBoolean timeout = new AtomicBoolean();
        TestCommunicationSpi.failCls = failCls;
        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                int num = threadCnt.getAndIncrement();
                Ignite ignite = ignite(num);
                IgniteCache<Object, Integer> cache = ignite.cache(CACHE);
                try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, num == 0 ? 500 : 1500, 0)) {
                    int key1 = primaryKey(ignite((num + 1) % txCnt).cache(CACHE));
                    log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key1 + ']');
                    cache.put(new TestKey(key1), 1);
                    barrier.await();
                    int key2 = primaryKey(cache);
                    log.info(">>> Performs put [node=" + ((IgniteKernal) ignite).localNode() + ", tx=" + tx + ", key=" + key2 + ']');
                    cache.put(new TestKey(key2), 2);
                    tx.commit();
                } catch (Exception e) {
                    timeout.compareAndSet(false, hasCause(e, TransactionTimeoutException.class));
                    deadlock.compareAndSet(false, hasCause(e, TransactionDeadlockException.class));
                }
            }
        }, txCnt, "tx-thread");
        fut.get();
        assertFalse(deadlock.get());
        assertTrue(timeout.get());
        checkDetectionFuts();
    } finally {
        TestCommunicationSpi.failCls = null;
        TestKey.failSer = false;
    }
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException) TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) IOException(java.io.IOException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) Ignite(org.apache.ignite.Ignite)

Example 97 with Transaction

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

the class GridCachePartitionedNodeRestartTxSelfTest method checkCustom.

/**
 * Test {@link GridCacheInternalKey}/{@link GridCacheAtomicLongValue}.
 * @param name Name.
 * @throws Exception If failed.
 */
private void checkCustom(String name) throws Exception {
    for (int i = INIT_GRID_NUM; i < 20; i++) {
        startGrid(i);
        assert PARTITIONED == grid(i).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode();
        try (Transaction tx = grid(i).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            GridCacheInternalKey key = new GridCacheInternalKeyImpl(name, "testGroup");
            GridCacheAtomicLongValue atomicVal = ((GridCacheAtomicLongValue) grid(i).cache(DEFAULT_CACHE_NAME).get(key));
            assertNotNull(atomicVal);
            assertEquals("Custom check failed for node: " + i, (long) i, atomicVal.get());
            atomicVal.set(i + 1);
            grid(i).cache(DEFAULT_CACHE_NAME).put(key, atomicVal);
            tx.commit();
        }
        stopGrid(i);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) GridCacheAtomicLongValue(org.apache.ignite.internal.processors.datastructures.GridCacheAtomicLongValue) GridCacheInternalKeyImpl(org.apache.ignite.internal.processors.datastructures.GridCacheInternalKeyImpl) GridCacheInternalKey(org.apache.ignite.internal.processors.datastructures.GridCacheInternalKey)

Example 98 with Transaction

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

the class CacheLockReleaseNodeLeaveTest method testTxLockRelease.

/**
 * @throws Exception If failed.
 */
public void testTxLockRelease() throws Exception {
    startGrids(2);
    final Ignite ignite0 = ignite(0);
    final Ignite ignite1 = ignite(1);
    final Integer key = primaryKey(ignite1.cache(DEFAULT_CACHE_NAME));
    IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
            ignite0.cache(DEFAULT_CACHE_NAME).get(key);
            return null;
        }
    }, "lock-thread1");
    fut1.get();
    IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try (Transaction tx = ignite1.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                log.info("Start tx lock.");
                ignite1.cache(DEFAULT_CACHE_NAME).get(key);
                log.info("Tx locked key.");
                tx.commit();
            }
            return null;
        }
    }, "lock-thread2");
    U.sleep(1000);
    log.info("Stop node.");
    ignite0.close();
    fut2.get(5, SECONDS);
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 99 with Transaction

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

the class GridCacheAbstractPrimarySyncSelfTest method testPrimarySync.

/**
 * @throws Exception If failed.
 */
public void testPrimarySync() throws Exception {
    for (int i = 0; i < GRID_CNT; i++) {
        for (int j = 0; j < GRID_CNT; j++) {
            IgniteCache<Integer, Integer> cache = grid(j).cache(DEFAULT_CACHE_NAME);
            if (grid(j).affinity(DEFAULT_CACHE_NAME).isPrimary(grid(j).localNode(), i)) {
                try (Transaction tx = grid(j).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache.put(i, i);
                    tx.commit();
                }
                assert cache.get(i) == i;
                break;
            }
        }
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction)

Example 100 with Transaction

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

the class GridCacheBasicOpAbstractTest method testOptimisticTransaction.

/**
 * @throws IgniteCheckedException If test fails.
 */
public void testOptimisticTransaction() throws Exception {
    CountDownLatch latch = new CountDownLatch(9);
    IgnitePredicate<Event> lsnr = new CacheEventListener(latch);
    try {
        IgniteCache<String, String> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
        IgniteCache<String, String> cache2 = ignite2.cache(DEFAULT_CACHE_NAME);
        IgniteCache<String, String> cache3 = ignite3.cache(DEFAULT_CACHE_NAME);
        ignite1.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
        ignite2.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
        ignite3.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
        Transaction tx = ignite1.transactions().txStart(OPTIMISTIC, READ_COMMITTED, 0, 0);
        try {
            cache1.put("tx1", "val1");
            cache1.put("tx2", "val2");
            cache1.put("tx3", "val3");
            assert cache2.get("tx1") == null;
            assert cache2.get("tx2") == null;
            assert cache2.get("tx3") == null;
            assert cache3.get("tx1") == null;
            assert cache3.get("tx2") == null;
            assert cache3.get("tx3") == null;
            tx.commit();
        } catch (CacheException e) {
            tx.rollback();
            throw e;
        }
        assert latch.await(5, SECONDS);
        String b1 = cache2.get("tx1");
        String b2 = cache2.get("tx2");
        String b3 = cache2.get("tx3");
        String c1 = cache3.get("tx1");
        String c2 = cache3.get("tx2");
        String c3 = cache3.get("tx3");
        assert b1 != null : "Invalid value: " + b1;
        assert b2 != null : "Invalid value: " + b2;
        assert b3 != null : "Invalid value: " + b3;
        assert c1 != null : "Invalid value: " + c1;
        assert c2 != null : "Invalid value: " + c2;
        assert c3 != null : "Invalid value: " + c3;
        assert "val1".equals(b1);
        assert "val2".equals(b2);
        assert "val3".equals(b3);
        assert "val1".equals(c1);
        assert "val2".equals(c2);
        assert "val3".equals(c3);
    } finally {
        ignite1.events().stopLocalListen(lsnr);
        ignite2.events().stopLocalListen(lsnr);
        ignite3.events().stopLocalListen(lsnr);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch)

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