Search in sources :

Example 36 with IgniteCache

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

the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInListener.

/**
 * @param ccfg Cache configuration.
 * @param asyncFltr Async filter.
 * @param asyncLsnr Async listener.
 * @param jcacheApi Use JCache api for registration entry update listener.
 * @throws Exception If failed.
 */
private void testNonDeadLockInListener(CacheConfiguration ccfg, final boolean asyncFltr, boolean asyncLsnr, boolean jcacheApi) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    try {
        for (int i = 0; i < ITERATION_CNT; i++) {
            log.info("Start iteration: " + i);
            int nodeIdx = i % NODES;
            final String cacheName = ccfg.getName();
            final IgniteCache cache = grid(nodeIdx).cache(cacheName);
            final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
            final QueryTestValue val0 = new QueryTestValue(1);
            final QueryTestValue newVal = new QueryTestValue(2);
            final CountDownLatch latch = new CountDownLatch(1);
            final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    if (asyncFltr) {
                        assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
                        assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
                    }
                }
            };
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
                    QueryTestValue val = e.getValue();
                    if (val == null)
                        return;
                    else if (val.equals(newVal)) {
                        evtFromLsnrLatch.countDown();
                        return;
                    } else if (!val.equals(val0))
                        return;
                    Transaction tx = null;
                    try {
                        if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
                            tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
                        assertEquals(val, val0);
                        cache0.put(key, newVal);
                        if (tx != null)
                            tx.commit();
                        latch.countDown();
                    } catch (Exception exp) {
                        log.error("Failed: ", exp);
                        throw new IgniteException(exp);
                    } finally {
                        if (tx != null)
                            tx.close();
                    }
                }
            };
            QueryCursor qry = null;
            MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
            CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
            CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFltr ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
            if (jcacheApi) {
                lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
                cache.registerCacheEntryListener(lsnrCfg);
            } else {
                ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
                conQry.setLocalListener(locLsnr);
                conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
                qry = cache.query(conQry);
            }
            try {
                if (rnd.nextBoolean())
                    cache.put(key, val0);
                else {
                    cache.invoke(key, new CacheEntryProcessor() {

                        @Override
                        public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
                            entry.setValue(val0);
                            return null;
                        }
                    });
                }
                assertTrue("Failed to waiting event.", U.await(latch, 3, SECONDS));
                assertEquals(cache.get(key), new QueryTestValue(2));
                assertTrue("Failed to waiting event from listener.", U.await(latch, 3, SECONDS));
            } finally {
                if (qry != null)
                    qry.close();
                if (lsnrCfg != null)
                    cache.deregisterCacheEntryListener(lsnrCfg);
            }
            log.info("Iteration finished: " + i);
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) Transaction(org.apache.ignite.transactions.Transaction) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Example 37 with IgniteCache

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

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatePartition.

/**
 * @param atomicityMode Cache atomicity mode.
 * @param cacheGrp {@code True} if test cache multiple caches in the same group.
 * @throws Exception If failed.
 */
private void concurrentUpdatePartition(CacheAtomicityMode atomicityMode, boolean cacheGrp) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    List<AtomicInteger> cntrs = new ArrayList<>();
    List<String> caches = new ArrayList<>();
    if (cacheGrp) {
        for (int i = 0; i < 3; i++) {
            CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME + i);
            ccfg.setGroupName("testGroup");
            ccfg.setWriteSynchronizationMode(FULL_SYNC);
            ccfg.setAtomicityMode(atomicityMode);
            IgniteCache<Object, Object> cache = client.createCache(ccfg);
            caches.add(cache.getName());
            cntrs.add(startListener(cache).get1());
        }
    } else {
        CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
        ccfg.setWriteSynchronizationMode(FULL_SYNC);
        ccfg.setAtomicityMode(atomicityMode);
        IgniteCache<Object, Object> cache = client.createCache(ccfg);
        caches.add(cache.getName());
        cntrs.add(startListener(cache).get1());
    }
    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 < 15; i++) {
        log.info("Iteration: " + i);
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) {
                    for (int c = 0; c < srvCaches.size(); c++) srvCaches.get(c).put(keys.get(rnd.nextInt(KEYS)), i);
                }
                return null;
            }
        }, THREADS, "update");
        for (final AtomicInteger evtCnt : cntrs) {
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    log.info("Events: " + evtCnt.get());
                    return evtCnt.get() >= THREADS * UPDATES;
                }
            }, 5000);
            assertEquals(THREADS * UPDATES, evtCnt.get());
            evtCnt.set(0);
        }
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 38 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 39 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 40 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)

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