Search in sources :

Example 21 with IgniteCache

use of org.apache.ignite.IgniteCache 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 22 with IgniteCache

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

the class CacheSerializableTransactionsTest method testNoOptimisticExceptionOnChangingTopology.

/**
 * @throws Exception If failed.
 */
public void testNoOptimisticExceptionOnChangingTopology() throws Exception {
    if (FAST)
        return;
    final AtomicBoolean finished = new AtomicBoolean();
    final List<String> cacheNames = new ArrayList<>();
    Ignite srv = ignite(1);
    try {
        {
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
            ccfg.setName("cache1");
            ccfg.setRebalanceMode(SYNC);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        {
            // Store enabled.
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, true, false);
            ccfg.setName("cache2");
            ccfg.setRebalanceMode(SYNC);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        {
            // Eviction.
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
            ccfg.setName("cache3");
            ccfg.setRebalanceMode(SYNC);
            LruEvictionPolicy plc = new LruEvictionPolicy();
            plc.setMaxSize(100);
            ccfg.setEvictionPolicy(plc);
            ccfg.setOnheapCacheEnabled(true);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        IgniteInternalFuture<?> restartFut = restartFuture(finished, null);
        List<IgniteInternalFuture<?>> futs = new ArrayList<>();
        final int KEYS_PER_THREAD = 100;
        for (int i = 1; i < SRVS + CLIENTS; i++) {
            final Ignite node = ignite(i);
            final int minKey = i * KEYS_PER_THREAD;
            final int maxKey = minKey + KEYS_PER_THREAD;
            // Threads update non-intersecting keys, optimistic exception should not be thrown.
            futs.add(GridTestUtils.runAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        log.info("Started update thread [node=" + node.name() + ", minKey=" + minKey + ", maxKey=" + maxKey + ']');
                        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
                        for (String cacheName : cacheNames) caches.add(node.<Integer, Integer>cache(cacheName));
                        assertEquals(3, caches.size());
                        int iter = 0;
                        while (!finished.get()) {
                            int keyCnt = rnd.nextInt(1, 10);
                            final Set<Integer> keys = new LinkedHashSet<>();
                            while (keys.size() < keyCnt) keys.add(rnd.nextInt(minKey, maxKey));
                            for (final IgniteCache<Integer, Integer> cache : caches) {
                                doInTransaction(node, OPTIMISTIC, SERIALIZABLE, new Callable<Void>() {

                                    @Override
                                    public Void call() throws Exception {
                                        for (Integer key : keys) randomOperation(rnd, cache, key);
                                        return null;
                                    }
                                });
                            }
                            if (iter % 100 == 0)
                                log.info("Iteration: " + iter);
                            iter++;
                        }
                        return null;
                    } catch (Throwable e) {
                        log.error("Unexpected error: " + e, e);
                        throw e;
                    }
                }
            }, "update-thread-" + i));
        }
        U.sleep(60_000);
        finished.set(true);
        restartFut.get();
        for (IgniteInternalFuture<?> fut : futs) fut.get();
    } finally {
        finished.set(true);
        for (String cacheName : cacheNames) destroyCache(cacheName);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) LruEvictionPolicy(org.apache.ignite.cache.eviction.lru.LruEvictionPolicy)

Example 23 with IgniteCache

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

the class CacheSerializableTransactionsTest method testTxCommitReadOnly2.

/**
 * @throws Exception If failed.
 */
public void testTxCommitReadOnly2() 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 (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                        @Override
                        public Void apply(IgniteCache<Integer, Integer> cache) {
                            cache.get(key);
                            return null;
                        }
                    });
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    Integer val = cache.get(key);
                    assertNull(val);
                    txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                        @Override
                        public Void apply(IgniteCache<Integer, Integer> cache) {
                            cache.get(key);
                            return null;
                        }
                    });
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 24 with IgniteCache

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

the class CacheSerializableTransactionsTest method testTxConflictCasReplace.

/**
 * @throws Exception If failed.
 */
public void testTxConflictCasReplace() 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 replace = cache.replace(key, 1, 2);
                        assertFalse(replace);
                        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 replace = cache.replace(key, 1, 2);
                    assertTrue(replace);
                    tx.commit();
                }
                checkValue(key, 2, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 1, 2);
                    assertFalse(replace);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 2);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean replace = cache.replace(key, 2, 1);
                        assertTrue(replace);
                        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 replace = cache.replace(key, 3, 4);
                        assertTrue(replace);
                        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 replace = cache.replace(key, 2, 3);
                    assertFalse(replace);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean replace = cache.replace(key, 1, 3);
                    assertTrue(replace);
                    tx.commit();
                }
                checkValue(key, 3, 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 25 with IgniteCache

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

the class CacheSerializableTransactionsTest method checkReadWriteTransactionsNoDeadlock.

/**
 * @param multiNode Multi-node test flag.
 * @throws Exception If failed.
 */
private void checkReadWriteTransactionsNoDeadlock(final boolean multiNode) throws Exception {
    final Ignite ignite0 = ignite(0);
    for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        ignite0.createCache(ccfg);
        try {
            final long stopTime = U.currentTimeMillis() + 10_000;
            final AtomicInteger idx = new AtomicInteger();
            GridTestUtils.runMultiThreaded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Ignite ignite = multiNode ? ignite(idx.incrementAndGet() % (SRVS + CLIENTS)) : ignite0;
                    IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (U.currentTimeMillis() < stopTime) {
                        try {
                            try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                                for (int i = 0; i < 10; i++) {
                                    Integer key = rnd.nextInt(30);
                                    if (rnd.nextBoolean())
                                        cache.get(key);
                                    else
                                        cache.put(key, key);
                                }
                                tx.commit();
                            }
                        } catch (TransactionOptimisticException ignore) {
                        // No-op.
                        }
                    }
                    return null;
                }
            }, 32, "test-thread");
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)403 Ignite (org.apache.ignite.Ignite)233 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)137 Cache (javax.cache.Cache)98 Transaction (org.apache.ignite.transactions.Transaction)87 ArrayList (java.util.ArrayList)69 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)65 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)60 Map (java.util.Map)55 IgniteException (org.apache.ignite.IgniteException)55 List (java.util.List)51 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)48 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)46 HashMap (java.util.HashMap)45 CacheException (javax.cache.CacheException)43 Random (java.util.Random)37 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32 CountDownLatch (java.util.concurrent.CountDownLatch)30 ClusterNode (org.apache.ignite.cluster.ClusterNode)30 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)28