Search in sources :

Example 81 with IgniteCache

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

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.

/**
 * @param atomicityMode Cache atomicity mode.
 * @param cacheGrp {@code True} if test cache multiple caches in the same group.
 * @throws Exception If failed.
 */
private void concurrentUpdatesAndQueryStart(CacheAtomicityMode atomicityMode, boolean cacheGrp) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    List<String> caches = new ArrayList<>();
    if (cacheGrp) {
        for (int i = 0; i < 3; i++) {
            CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME + i);
            ccfg.setGroupName("testGroup");
            ccfg.setWriteSynchronizationMode(FULL_SYNC);
            ccfg.setAtomicityMode(atomicityMode);
            IgniteCache cache = client.createCache(ccfg);
            caches.add(cache.getName());
        }
    } else {
        CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        ccfg.setWriteSynchronizationMode(FULL_SYNC);
        ccfg.setAtomicityMode(atomicityMode);
        IgniteCache cache = client.createCache(ccfg);
        caches.add(cache.getName());
    }
    Affinity<Integer> aff = srv.affinity(caches.get(0));
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    final List<IgniteCache<Object, Object>> srvCaches = new ArrayList<>();
    for (String cacheName : caches) srvCaches.add(srv.cache(cacheName));
    for (int i = 0; i < 5; i++) {
        log.info("Iteration: " + i);
        final AtomicBoolean stop = new AtomicBoolean();
        List<T2<AtomicInteger, QueryCursor>> qrys = new ArrayList<>();
        try {
            IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (!stop.get()) {
                        for (IgniteCache<Object, Object> srvCache : srvCaches) srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
                    }
                    return null;
                }
            }, THREADS, "update");
            U.sleep(1000);
            for (String cache : caches) qrys.add(startListener(client.cache(cache)));
            U.sleep(1000);
            stop.set(true);
            fut.get();
        } finally {
            stop.set(true);
        }
        GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) {
                    for (IgniteCache<Object, Object> srvCache : srvCaches) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                }
                return null;
            }
        }, THREADS, "update");
        for (T2<AtomicInteger, QueryCursor> qry : qrys) {
            final AtomicInteger evtCnt = qry.get1();
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    log.info("Events: " + evtCnt.get());
                    return evtCnt.get() >= THREADS * UPDATES;
                }
            }, 5000);
            assertEquals(THREADS * UPDATES, qry.get1().get());
            qry.get2().close();
        }
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) T2(org.apache.ignite.internal.util.typedef.T2) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 82 with IgniteCache

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

the class CacheContinuousQueryOperationFromCallbackTest method doTest.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
protected void doTest(final CacheConfiguration ccfg, boolean fromLsnr) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    assertEquals(0, filterCbCntr.get());
    try {
        List<Set<T2<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(NODES);
        List<Set<T2<QueryTestKey, QueryTestValue>>> evtsFromCallbacks = new ArrayList<>(NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final AtomicInteger cbCntr = new AtomicInteger(0);
        final int threadCnt = SYSTEM_POOL_SIZE * 2;
        for (int idx = 0; idx < NODES; idx++) {
            Set<T2<QueryTestKey, QueryTestValue>> evts = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            Set<T2<QueryTestKey, QueryTestValue>> evtsFromCb = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            IgniteCache<Object, Object> cache = grid(idx).getOrCreateCache(ccfg.getName());
            ContinuousQuery qry = new ContinuousQuery();
            qry.setLocalListener(new TestCacheAsyncEventListener(evts, evtsFromCb, fromLsnr ? cache : null, qryCntr, cbCntr));
            if (!fromLsnr)
                qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
            rcvdEvts.add(evts);
            evtsFromCallbacks.add(evtsFromCb);
            QueryCursor qryCursor = cache.query(qry);
            qries.add(qryCursor);
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
                    Transaction tx = null;
                    if (startTx)
                        tx = cache.unwrap(Ignite.class).transactions().txStart();
                    try {
                        if ((cache.get(key) == null) || rnd.nextBoolean())
                            cache.invoke(key, new IncrementTestEntryProcessor());
                        else {
                            QueryTestValue val;
                            QueryTestValue newVal;
                            do {
                                val = cache.get(key);
                                newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                            } while (!cache.replace(key, val, newVal));
                        }
                    } finally {
                        if (tx != null)
                            tx.commit();
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(30, TimeUnit.SECONDS);
        assert GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * NODES;
            }
        }, TimeUnit.MINUTES.toMillis(2));
        for (Set<T2<QueryTestKey, QueryTestValue>> set : rcvdEvts) checkEvents(set, ITERATION_CNT * threadCnt, grid(0).cache(ccfg.getName()), false);
        if (fromLsnr) {
            final int expCnt = qryCntr.get() * NODES * KEYS_FROM_CALLBACK;
            boolean res = GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return cbCntr.get() >= expCnt;
                }
            }, TimeUnit.SECONDS.toMillis(60));
            assertTrue("Failed to wait events [exp=" + expCnt + ", act=" + cbCntr.get() + "]", res);
            assertEquals(expCnt, cbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, qryCntr.get() * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        } else {
            final int expInvkCnt = ITERATION_CNT * threadCnt * (ccfg.getCacheMode() != REPLICATED ? (ccfg.getBackups() + 1) : NODES - 1) * NODES;
            GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return filterCbCntr.get() >= expInvkCnt;
                }
            }, TimeUnit.SECONDS.toMillis(60));
            assertEquals(expInvkCnt, filterCbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, expInvkCnt * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        }
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 83 with IgniteCache

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

the class IgnitePdsContinuousRestartTest method checkRebalancingDuringLoad.

/**
 * @throws Exception if failed.
 */
private void checkRebalancingDuringLoad(int restartDelay, int checkpointDelay, int threads, final int batch) throws Exception {
    this.checkpointDelay = checkpointDelay;
    startGrids(GRID_CNT);
    final Ignite load = ignite(0);
    load.active(true);
    try (IgniteDataStreamer<Object, Object> s = load.dataStreamer(CACHE_NAME)) {
        s.allowOverwrite(true);
        for (int i = 0; i < ENTRIES_COUNT; i++) s.addData(i, i);
    }
    final AtomicBoolean done = new AtomicBoolean(false);
    IgniteInternalFuture<?> busyFut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Object call() throws Exception {
            IgniteCache<Object, Object> cache = load.cache(CACHE_NAME);
            Random rnd = ThreadLocalRandom.current();
            while (!done.get()) {
                Map<Integer, Integer> map = new TreeMap<>();
                for (int i = 0; i < batch; i++) map.put(rnd.nextInt(ENTRIES_COUNT), rnd.nextInt());
                cache.putAll(map);
            }
            return null;
        }
    }, threads, "updater");
    long end = System.currentTimeMillis() + 90_000;
    Random rnd = ThreadLocalRandom.current();
    while (System.currentTimeMillis() < end) {
        int idx = rnd.nextInt(GRID_CNT - 1) + 1;
        stopGrid(idx, cancel);
        U.sleep(restartDelay);
        startGrid(idx);
        U.sleep(restartDelay);
    }
    done.set(true);
    busyFut.get();
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 84 with IgniteCache

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

the class MemoryPolicyInitializationTest method testCachesOnUserDefinedDefaultMemoryPolicy.

/**
 * Test for verification that caches with not specified memory policy name,
 * with specified default memory policy name and specified custom memory policy name
 * all started with correct memory policy.
 */
public void testCachesOnUserDefinedDefaultMemoryPolicy() throws Exception {
    prepareCustomConfigWithOverriddenDefaultName();
    IgniteEx ignite = startGrid(0);
    CacheConfiguration cache1Cfg = new CacheConfiguration().setName("cache1");
    IgniteCache cache1 = ignite.createCache(cache1Cfg);
    verifyCacheMemoryPolicy(cache1, CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
    CacheConfiguration cache2Cfg = new CacheConfiguration().setName("cache2").setMemoryPolicyName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
    IgniteCache cache2 = ignite.createCache(cache2Cfg);
    verifyCacheMemoryPolicy(cache2, CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
    CacheConfiguration cache3Cfg = new CacheConfiguration().setName("cache3").setMemoryPolicyName(DFLT_MEM_PLC_DEFAULT_NAME);
    IgniteCache cache3 = ignite.createCache(cache3Cfg);
    verifyCacheMemoryPolicy(cache3, DFLT_MEM_PLC_DEFAULT_NAME);
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCache(org.apache.ignite.IgniteCache) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 85 with IgniteCache

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

the class MemoryPolicyInitializationTest method testCachesOnOverriddenMemoryPolicy.

/**
 * Test for verification that caches with not specified memory policy name,
 * with specified default memory policy name and specified custom memory policy name
 * all started with correct memory policy.
 */
public void testCachesOnOverriddenMemoryPolicy() throws Exception {
    prepareCustomConfigWithOverridingDefaultAndCustom();
    IgniteEx ignite = startGrid(0);
    CacheConfiguration cache1Cfg = new CacheConfiguration().setName("cache1");
    IgniteCache cache1 = ignite.createCache(cache1Cfg);
    verifyCacheMemoryPolicy(cache1, DFLT_MEM_PLC_DEFAULT_NAME);
    CacheConfiguration cache2Cfg = new CacheConfiguration().setName("cache2").setMemoryPolicyName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
    IgniteCache cache2 = ignite.createCache(cache2Cfg);
    verifyCacheMemoryPolicy(cache2, CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
    CacheConfiguration cache3Cfg = new CacheConfiguration().setName("cache3").setMemoryPolicyName(DFLT_MEM_PLC_DEFAULT_NAME);
    IgniteCache cache3 = ignite.createCache(cache3Cfg);
    verifyCacheMemoryPolicy(cache3, DFLT_MEM_PLC_DEFAULT_NAME);
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCache(org.apache.ignite.IgniteCache) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

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