Search in sources :

Example 31 with Transaction

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

the class CacheKeepBinaryTransactionTest method testBinaryContains.

/**
 * @throws Exception If failed.
 */
public void testBinaryContains() throws Exception {
    IgniteEx ignite = grid(0);
    IgniteCache<Object, Object> cache = ignite.cache("tx-cache").withKeepBinary();
    try (Transaction tx = ignite.transactions().txStart()) {
        BinaryObject key = ignite.binary().builder("test2").setField("id", 1).build();
        assertFalse(cache.containsKey(key));
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteEx(org.apache.ignite.internal.IgniteEx) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 32 with Transaction

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

the class CacheSerializableTransactionsTest 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()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (Integer key : keys) {
                log.info("Test key: " + key);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object old = cache.getAndPutIfAbsent(key, 2);
                        assertNull(old);
                        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)) {
                    Object old = cache.getAndPutIfAbsent(key, 2);
                    assertEquals(1, old);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Object old = cache.getAndPutIfAbsent(key, 2);
                    assertNull(old);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Object old = cache.getAndPutIfAbsent(key, 4);
                        assertEquals(2, old);
                        updateKey(cache, key, 3);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 3, cache.getName());
            }
        } 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) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 33 with Transaction

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

the class CacheSerializableTransactionsTest method testNoReadLockConflictMultiNode.

/**
 * @throws Exception If failed.
 */
public void testNoReadLockConflictMultiNode() throws Exception {
    Ignite ignite0 = ignite(0);
    for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        final AtomicInteger putKey = new AtomicInteger(1_000_000);
        ignite0.createCache(ccfg);
        try {
            final int THREADS = 64;
            IgniteCache<Integer, Integer> cache0 = ignite0.cache(ccfg.getName());
            List<Integer> readKeys = testKeys(cache0);
            for (final Integer readKey : readKeys) {
                final CyclicBarrier barrier = new CyclicBarrier(THREADS);
                cache0.put(readKey, Integer.MIN_VALUE);
                final AtomicInteger idx = new AtomicInteger();
                GridTestUtils.runMultiThreaded(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        Ignite ignite = ignite(idx.incrementAndGet() % (CLIENTS + SRVS));
                        IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
                        try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                            cache.get(readKey);
                            barrier.await();
                            cache.put(putKey.incrementAndGet(), 0);
                            tx.commit();
                        }
                        return null;
                    }
                }, THREADS, "test-thread");
                assertEquals((Integer) Integer.MIN_VALUE, cache0.get(readKey));
                cache0.put(readKey, readKey);
                assertEquals(readKey, cache0.get(readKey));
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite)

Example 34 with Transaction

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

the class CacheSerializableTransactionsTest method incrementTx.

/**
 * @param nearCache If {@code true} near cache is enabled.
 * @param store If {@code true} cache store is enabled.
 * @param restart If {@code true} restarts one node.
 * @throws Exception If failed.
 */
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
    final Ignite srv = ignite(1);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
    final List<Ignite> clients = clients();
    final String cacheName = srv.createCache(ccfg).getName();
    final AtomicBoolean stop = new AtomicBoolean();
    try {
        final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
        for (Ignite client : clients) {
            if (nearCache)
                caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
            else
                caches.add(client.<Integer, Integer>cache(cacheName));
        }
        IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
        final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
        for (int i = 0; i < 30; i++) {
            final AtomicInteger cntr = new AtomicInteger();
            final Integer key = i;
            final AtomicInteger threadIdx = new AtomicInteger();
            final int THREADS = 10;
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = threadIdx.getAndIncrement() % caches.size();
                    IgniteCache<Integer, Integer> cache = caches.get(idx);
                    Ignite ignite = cache.unwrap(Ignite.class);
                    IgniteTransactions txs = ignite.transactions();
                    log.info("Started update thread: " + ignite.name());
                    barrier.await();
                    for (int i = 0; i < 1000; i++) {
                        if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
                            break;
                        try {
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Integer val = cache.get(key);
                                cache.put(key, val == null ? 1 : val + 1);
                                tx.commit();
                            }
                            cntr.incrementAndGet();
                        } catch (TransactionOptimisticException ignore) {
                        // Retry.
                        } catch (IgniteException | CacheException e) {
                            assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
                        }
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
            assertTrue(cntr.get() > 0);
            checkValue(key, cntr.get(), cacheName, restart);
            if (U.currentTimeMillis() > stopTime)
                break;
        }
        stop.set(true);
        if (restartFut != null)
            restartFut.get();
    } finally {
        stop.set(true);
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 35 with Transaction

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

the class CacheSerializableTransactionsTest method testTxRollback.

/**
 * @throws Exception If failed.
 */
public void testTxRollback() throws Exception {
    Ignite ignite0 = ignite(0);
    Ignite ignite1 = ignite(1);
    final IgniteTransactions txs0 = ignite0.transactions();
    final IgniteTransactions txs1 = ignite1.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
            IgniteCache<Integer, Integer> cache1 = ignite1.cache(ccfg.getName());
            List<Integer> keys = testKeys(cache0);
            for (Integer key : keys) {
                log.info("Test key: " + key);
                Integer expVal = null;
                for (int i = 0; i < 100; i++) {
                    try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache0.get(key);
                        assertEquals(expVal, val);
                        cache0.put(key, i);
                        tx.rollback();
                    }
                    try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache0.get(key);
                        assertEquals(expVal, val);
                        cache0.put(key, i);
                        tx.commit();
                        expVal = i;
                    }
                    try (Transaction tx = txs1.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        Integer val = cache1.get(key);
                        assertEquals(expVal, val);
                        cache1.put(key, val);
                        tx.commit();
                    }
                }
                checkValue(key, expVal, cache0.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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