Search in sources :

Example 61 with Transaction

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

the class CacheSerializableTransactionsTest method testTxConflictRemoveWithOldValue.

/**
 * @throws Exception If failed.
 */
public void testTxConflictRemoveWithOldValue() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (final Integer key : keys) {
                log.info("Test key: " + key);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean rmv = cache.remove(key, 2);
                        assertFalse(rmv);
                        updateKey(cache, key, 1);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 1);
                    assertTrue(rmv);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 2);
                    assertFalse(rmv);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 2);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean rmv = cache.remove(key, 2);
                        assertTrue(rmv);
                        updateKey(cache, key, 3);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 3, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean rmv = cache.remove(key, 3);
                        assertTrue(rmv);
                        txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                            @Override
                            public Void apply(IgniteCache<Integer, Integer> cache) {
                                cache.remove(key);
                                return null;
                            }
                        });
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 1);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 2);
                    assertFalse(rmv);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 1);
                    assertTrue(rmv);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 62 with Transaction

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

the class CacheSerializableTransactionsTest method testReadLockPessimisticTxConflict.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("UnnecessaryLocalVariable")
public void testReadLockPessimisticTxConflict() throws Exception {
    Ignite ignite0 = ignite(0);
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        ignite0.createCache(ccfg);
        try {
            Ignite ignite = ignite0;
            IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
            Integer writeKey = Integer.MAX_VALUE;
            List<Integer> readKeys = testKeys(cache);
            for (Integer readKey : readKeys) {
                CountDownLatch latch = new CountDownLatch(1);
                IgniteInternalFuture<?> fut = lockKey(latch, cache, readKey);
                try {
                    // No conflict for write, conflict with pessimistic tx for read.
                    try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                        cache.put(writeKey, writeKey);
                        cache.get(readKey);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                } finally {
                    latch.countDown();
                }
                fut.get();
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
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) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 63 with Transaction

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

the class GridCacheAtomicStampedApiSelfAbstractTest method testIsolation.

/**
 * @throws Exception If failed.
 */
public void testIsolation() throws Exception {
    Ignite ignite = grid(0);
    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cfg.setName("MyCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);
    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);
    try {
        String atomicName = UUID.randomUUID().toString();
        String initVal = "qwerty";
        String initStamp = "asdf";
        IgniteAtomicStamped<String, String> atomicStamped = ignite.atomicStamped(atomicName, initVal, initStamp, true);
        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1, 1);
            assertEquals(initVal, atomicStamped.value());
            assertEquals(initStamp, atomicStamped.stamp());
            assertEquals(initVal, atomicStamped.get().get1());
            assertEquals(initStamp, atomicStamped.get().get2());
            assertTrue(atomicStamped.compareAndSet(initVal, "b", initStamp, "d"));
            tx.rollback();
        }
        assertEquals(0, cache.size());
        assertEquals("b", atomicStamped.value());
        assertEquals("d", atomicStamped.stamp());
        atomicStamped.close();
        assertTrue(atomicStamped.removed());
    } finally {
        ignite.destroyCache(cfg.getName());
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 64 with Transaction

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

the class IgniteWalFlushFailoverTest method flushingErrorTest.

/**
 * @throws Exception if failed.
 */
private void flushingErrorTest() throws Exception {
    final IgniteEx grid = startGrid(0);
    FileWriteAheadLogManager wal = (FileWriteAheadLogManager) grid.context().cache().context().wal();
    boolean mmap = GridTestUtils.getFieldValue(wal, "mmap");
    if (mmap)
        return;
    wal.setFileIOFactory(new FailingFileIOFactory(canFail));
    try {
        grid.active(true);
        IgniteCache<Object, Object> cache = grid.cache(TEST_CACHE);
        final int iterations = 100;
        canFail.set(true);
        for (int i = 0; i < iterations; i++) {
            Transaction tx = grid.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.READ_COMMITTED);
            cache.put(i, "testValue" + i);
            Thread.sleep(100L);
            tx.commitAsync().get();
        }
    } catch (Exception expected) {
    // There can be any exception. Do nothing.
    }
    // We should await successful stop of node.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return grid.context().gateway().getState() == GridKernalState.STOPPED;
        }
    }, getTestTimeout());
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteEx(org.apache.ignite.internal.IgniteEx) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException)

Example 65 with Transaction

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

the class IgniteWalFlushMultiNodeFailoverAbstractSelfTest method failWhilePut.

/**
 * @throws Exception if failed.
 */
public void failWhilePut(boolean failWhileStart) throws Exception {
    final Ignite grid = startGridsMultiThreaded(gridCount());
    IgniteWriteAheadLogManager wal = ((IgniteKernal) grid).context().cache().context().wal();
    boolean mmap = GridTestUtils.getFieldValue(wal, "mmap");
    if (mmap)
        return;
    grid.active(true);
    IgniteCache<Object, Object> cache = grid.cache(TEST_CACHE);
    for (int i = 0; i < ITRS; i++) {
        while (true) {
            try (Transaction tx = grid.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.READ_COMMITTED)) {
                cache.put(i, "testValue" + i);
                tx.commit();
                break;
            } catch (Exception expected) {
            // Expected exception.
            }
        }
        if (i == ITRS / 4) {
            try {
                if (failWhileStart)
                    canFail.set(true);
                startGrid(gridCount());
                FileWriteAheadLogManager wal0 = (FileWriteAheadLogManager) grid(gridCount()).context().cache().context().wal();
                wal0.setFileIOFactory(new FailingFileIOFactory(canFail));
                grid.cluster().setBaselineTopology(grid.cluster().topologyVersion());
                waitForRebalancing();
            } catch (Exception expected) {
            // There can be any exception. Do nothing.
            }
        }
        if (i == ITRS / 2)
            canFail.set(true);
    }
    // We should await successful stop of node.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return grid.cluster().nodes().size() == gridCount();
        }
    }, getTestTimeout());
    stopAllGrids();
    canFail.set(false);
    Ignite grid0 = startGrids(gridCount() + 1);
    FileWriteAheadLogManager wal0 = (FileWriteAheadLogManager) grid(gridCount()).context().cache().context().wal();
    wal0.setFileIOFactory(new FailingFileIOFactory(canFail));
    grid0.active(true);
    cache = grid0.cache(TEST_CACHE);
    for (int i = 0; i < ITRS; i++) assertEquals(cache.get(i), "testValue" + i);
}
Also used : IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) Transaction(org.apache.ignite.transactions.Transaction) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) Ignite(org.apache.ignite.Ignite) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException)

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