Search in sources :

Example 86 with IgniteCache

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

the class IgniteCacheNearOnlyTxTest method testConcurrentTx.

/**
 * @throws Exception If failed.
 */
public void testConcurrentTx() throws Exception {
    final Ignite ignite1 = ignite(1);
    assertTrue(ignite1.configuration().isClientMode());
    ignite1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
    final Integer key = 1;
    IgniteInternalFuture<?> fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
            for (int i = 0; i < 100; i++) cache.put(key, 1);
            return null;
        }
    }, 5, "put1-thread");
    IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
            IgniteTransactions txs = ignite1.transactions();
            for (int i = 0; i < 100; i++) {
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache.get(key);
                    cache.put(key, 1);
                    tx.commit();
                }
            }
            return null;
        }
    }, 5, "put2-thread");
    fut1.get();
    fut2.get();
}
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 87 with IgniteCache

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

the class NearCachePutAllMultinodeTest method testMultithreadedPutAll.

/**
 * @throws Exception If failed.
 */
public void testMultithreadedPutAll() throws Exception {
    final AtomicInteger idx = new AtomicInteger();
    runMultiThreaded(new Callable<Object>() {

        private final Random rnd = new Random();

        @Override
        public Object call() throws Exception {
            int threadIdx = idx.getAndIncrement();
            int node = threadIdx % 2 + (GRID_CNT - 2);
            IgniteCache<Object, Object> cache = jcache(node);
            for (int i = 0; i < TX_CNT; i++) {
                Map<Integer, String> map = new TreeMap<>();
                for (int j = 0; j < 3; j++) map.put(rnd.nextInt(10), "value");
                cache.putAll(map);
                if (i % 100 == 0)
                    log.info("Iteration: " + i + " " + node);
            }
            return null;
        }
    }, 8, "putAll");
}
Also used : Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCache(org.apache.ignite.IgniteCache) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 88 with IgniteCache

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

the class WalModeChangeAdvancedSelfTest method testCacheCleanup.

/**
 * Test cache cleanup on restart.
 *
 * @throws Exception If failed.
 */
public void testCacheCleanup() throws Exception {
    Ignite srv = startGrid(config(SRV_1, false, false));
    srv.cluster().active(true);
    IgniteCache cache1 = srv.getOrCreateCache(cacheConfig(CACHE_NAME, PARTITIONED, TRANSACTIONAL));
    IgniteCache cache2 = srv.getOrCreateCache(cacheConfig(CACHE_NAME_2, PARTITIONED, TRANSACTIONAL));
    assertForAllNodes(CACHE_NAME, true);
    assertForAllNodes(CACHE_NAME_2, true);
    for (int i = 0; i < 10; i++) {
        cache1.put(i, i);
        cache2.put(i, i);
    }
    srv.cluster().disableWal(CACHE_NAME);
    assertForAllNodes(CACHE_NAME, false);
    assertForAllNodes(CACHE_NAME_2, true);
    for (int i = 10; i < 20; i++) {
        cache1.put(i, i);
        cache2.put(i, i);
    }
    srv.cluster().disableWal(CACHE_NAME_2);
    assertForAllNodes(CACHE_NAME, false);
    assertForAllNodes(CACHE_NAME_2, false);
    for (int i = 20; i < 30; i++) {
        cache1.put(i, i);
        cache2.put(i, i);
    }
    assertEquals(cache1.size(), 30);
    assertEquals(cache2.size(), 30);
    srv.cluster().enableWal(CACHE_NAME);
    assertForAllNodes(CACHE_NAME, true);
    assertForAllNodes(CACHE_NAME_2, false);
    assertEquals(cache1.size(), 30);
    assertEquals(cache2.size(), 30);
    stopAllGrids(true);
    srv = startGrid(config(SRV_1, false, false));
    srv.cluster().active(true);
    cache1 = srv.cache(CACHE_NAME);
    cache2 = srv.cache(CACHE_NAME_2);
    assertForAllNodes(CACHE_NAME, true);
    assertForAllNodes(CACHE_NAME_2, false);
    assertEquals(30, cache1.size());
    assertEquals(0, cache2.size());
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite)

Example 89 with IgniteCache

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

the class WalModeChangeAdvancedSelfTest method testConcurrentOperations.

/**
 * Test that concurrent enable/disable events doesn't leave to hangs.
 *
 * @throws Exception If failed.
 */
public void testConcurrentOperations() throws Exception {
    final Ignite srv1 = startGrid(config(SRV_1, false, false));
    final Ignite srv2 = startGrid(config(SRV_2, false, false));
    final Ignite srv3 = startGrid(config(SRV_3, false, true));
    final Ignite cli = startGrid(config(CLI, true, false));
    final Ignite cacheCli = startGrid(config(CLI_2, true, false));
    cacheCli.cluster().active(true);
    final IgniteCache cache = cacheCli.getOrCreateCache(cacheConfig(PARTITIONED));
    for (int i = 1; i <= 3; i++) {
        // Start pushing requests.
        Collection<Ignite> walNodes = new ArrayList<>();
        walNodes.add(srv1);
        walNodes.add(srv2);
        walNodes.add(srv3);
        walNodes.add(cli);
        final AtomicBoolean done = new AtomicBoolean();
        final CountDownLatch latch = new CountDownLatch(5);
        for (Ignite node : walNodes) {
            final Ignite node0 = node;
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    checkConcurrentOperations(done, node0);
                    latch.countDown();
                }
            });
            t.setName("wal-load-" + node0.name());
            t.start();
        }
        // Do some cache loading in the mean time.
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                int i = 0;
                while (!done.get()) cache.put(i, i++);
                latch.countDown();
            }
        });
        t.setName("cache-load");
        t.start();
        Thread.sleep(20_000);
        done.set(true);
        X.println(">>> Stopping iteration: " + i);
        latch.await();
        X.println(">>> Iteration finished: " + i);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 90 with IgniteCache

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

the class GridCacheQueryTransformerSelfTest method testLocalFiltered.

/**
 * @throws Exception If failed.
 */
public void testLocalFiltered() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteBiPredicate<Integer, Value> filter = new IgniteBiPredicate<Integer, Value>() {

                    @Override
                    public boolean apply(Integer k, Value v) {
                        return v.idx % 1000 == 0;
                    }
                };
                IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, Value> e) {
                        return e.getValue().idx;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<>(filter).setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(5, res.size());
        Collections.sort(res);
        for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)402 Ignite (org.apache.ignite.Ignite)232 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