Search in sources :

Example 61 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)

Example 62 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 63 with IgniteCache

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

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

the class CacheStoreUsageMultinodeDynamicStartAbstractTest method checkStoreWithDynamicStart.

/**
     * @param clientStart {@code True} if start cache from client node.
     * @throws Exception If failed.
     */
private void checkStoreWithDynamicStart(boolean clientStart) throws Exception {
    cacheStore = true;
    CacheConfiguration ccfg = cacheConfiguration();
    assertNotNull(ccfg.getCacheStoreFactory());
    Ignite srv = ignite(0);
    Ignite client = ignite(3);
    Ignite node = clientStart ? client : srv;
    IgniteCache cache = nearCache ? node.createCache(ccfg, new NearCacheConfiguration()) : node.createCache(ccfg);
    assertNotNull(cache);
    try {
        if (nearCache)
            client.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
        checkStoreUpdate(true);
    } finally {
        cache = srv.cache(DEFAULT_CACHE_NAME);
        if (cache != null)
            cache.destroy();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration)

Example 65 with IgniteCache

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

the class CacheStartupInDeploymentModesTest method doCheckStarted.

/**
     * @param mode Deployment mode.
     * @throws Exception If failed.
     */
private void doCheckStarted(DeploymentMode mode) throws Exception {
    startGridsMultiThreaded(2);
    checkTopology(2);
    assertEquals(mode, ignite(0).configuration().getDeploymentMode());
    assert ignite(0).configuration().getMarshaller() instanceof BinaryMarshaller;
    IgniteCache rCache = ignite(0).cache(REPLICATED_CACHE);
    checkPutCache(rCache);
    IgniteCache pCache = ignite(0).cache(PARTITIONED_CACHE);
    checkPutCache(pCache);
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)298 Ignite (org.apache.ignite.Ignite)161 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)104 Cache (javax.cache.Cache)72 Transaction (org.apache.ignite.transactions.Transaction)71 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 IgniteException (org.apache.ignite.IgniteException)50 ArrayList (java.util.ArrayList)47 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)40 CacheException (javax.cache.CacheException)37 Map (java.util.Map)34 List (java.util.List)33 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)33 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)31 HashMap (java.util.HashMap)27 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)26 CyclicBarrier (java.util.concurrent.CyclicBarrier)25 IgniteKernal (org.apache.ignite.internal.IgniteKernal)24 Random (java.util.Random)23 IgniteTransactions (org.apache.ignite.IgniteTransactions)22