Search in sources :

Example 86 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryFailoverAbstractSelfTest method testStartStop.

/**
 * @throws Exception If failed.
 */
@Test
public void testStartStop() throws Exception {
    this.backups = 2;
    final int SRV_NODES = 4;
    startGridsMultiThreaded(SRV_NODES);
    Ignite qryClient = startClientGrid(SRV_NODES);
    IgniteCache<Object, Object> qryClnCache = qryClient.cache(DEFAULT_CACHE_NAME);
    Affinity<Object> aff = qryClient.affinity(DEFAULT_CACHE_NAME);
    final CacheEventListener2 lsnr = new CacheEventListener2();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    qry.setRemoteFilter(new CacheEventFilter());
    QueryCursor<?> cur = qryClnCache.query(qry);
    for (int i = 0; i < 10; i++) {
        final int idx = i % (SRV_NODES - 1);
        log.info("Stop node: " + idx);
        stopGrid(idx);
        awaitPartitionMapExchange();
        List<T3<Object, Object, Object>> afterRestEvts = new ArrayList<>();
        for (int j = 0; j < aff.partitions(); j++) {
            Integer oldVal = (Integer) qryClnCache.get(j);
            qryClnCache.put(j, i);
            afterRestEvts.add(new T3<>((Object) j, (Object) i, (Object) oldVal));
        }
        checkEvents(new ArrayList<>(afterRestEvts), lsnr, false);
        log.info("Start node: " + idx);
        startGrid(idx);
    }
    cur.close();
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) T3(org.apache.ignite.internal.util.typedef.T3) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 87 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.

/**
 * @param ccfg Cache configuration.
 * @param isClient Client.
 * @param joinNode If a node should be added to topology after a query is started.
 * @param evtFilterFactoryCls Remote filter factory class.
 * @throws Exception If failed.
 */
private void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient, boolean joinNode, Class<Factory<CacheEntryEventFilter>> evtFilterFactoryCls) throws Exception {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final CountDownLatch latch = new CountDownLatch(UPDATES);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    AtomicReference<String> err = new AtomicReference<>();
    TestLocalListener locLsnr = new TestLocalListener() {

        @Override
        protected void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();
                log.info("Received event: " + evt);
                int key = evt.getKey();
                if (key % 2 == 0)
                    err.set("Event received on entry, that doesn't pass a filter: " + key);
            }
        }
    };
    qry.setLocalListener(locLsnr);
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactoryCls.newInstance());
    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(locLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactoryCls.newInstance(), true, true);
    IgniteCache<Integer, Integer> cache;
    cache = isClient ? grid(NODES - 1).cache(ccfg.getName()) : grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
    try (QueryCursor<?> cur = cache.query(qry)) {
        cache.registerCacheEntryListener(lsnrCfg);
        if (joinNode) {
            startGrid(NODES);
            awaitPartitionMapExchange();
        }
        for (int i = 0; i < UPDATES; i++) cache.put(i, i);
        assertTrue("Failed to wait for local listener invocations: " + latch.getCount(), latch.await(3, TimeUnit.SECONDS));
        assertNull(err.get(), err.get());
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 88 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryOperationP2PTest method testMultithreadedUpdatesNodeJoin.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultithreadedUpdatesNodeJoin() throws Exception {
    Ignite client = startGrid("client");
    CacheConfiguration<Object, Object> cacheCfg = cacheConfiguration(PARTITIONED, 0, ATOMIC);
    IgniteCache<Object, Object> cache = client.createCache(cacheCfg);
    int iterations = 50;
    int keysNum = 100;
    int threadsNum = Runtime.getRuntime().availableProcessors();
    CountDownLatch updatesLatch = new CountDownLatch(iterations * keysNum * threadsNum / 2);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactoryCls = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Object, Object>>) (Object) evtFilterFactoryCls.newInstance());
    qry.setLocalListener((evts) -> {
        for (CacheEntryEvent<?, ?> ignored : evts) updatesLatch.countDown();
    });
    cache.query(qry);
    for (int t = 0; t < threadsNum; t++) {
        int threadId = t;
        GridTestUtils.runAsync(() -> {
            for (int i = 0; i < iterations; i++) {
                log.info("Iteration #" + (i + 1));
                for (int k = 0; k < keysNum; k++) {
                    int key = keysNum * threadId + k;
                    cache.put(key, key);
                }
            }
        }, "cache-writer-thread-" + threadId);
    }
    startGrid(NODES);
    assertTrue("Failed to wait for all cache updates invocations. Latch: " + updatesLatch, updatesLatch.await(30, TimeUnit.SECONDS));
}
Also used : Factory(javax.cache.configuration.Factory) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 89 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class CacheContinuousQueryRandomOperationsTest method testFilterAndFactoryProvided.

/**
 * @throws Exception If failed.
 */
@Test
public void testFilterAndFactoryProvided() throws Exception {
    final CacheConfiguration<Object, Object> ccfg = cacheConfiguration(PARTITIONED, 1, ATOMIC, false);
    grid(0).createCache(ccfg);
    try {
        final ContinuousQuery qry = new ContinuousQuery();
        qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter>() {

            @Override
            public CacheEntryEventFilter create() {
                return null;
            }
        });
        qry.setRemoteFilter(new CacheEntryEventSerializableFilter() {

            @Override
            public boolean evaluate(CacheEntryEvent event) throws CacheEntryListenerException {
                return false;
            }
        });
        qry.setLocalListener(new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
            // No-op.
            }
        });
        GridTestUtils.assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                return grid(0).cache(ccfg.getName()).query(qry);
            }
        }, IgniteException.class, null);
    } finally {
        grid(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheLoaderException(javax.cache.integration.CacheLoaderException) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 90 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class GridCacheContinuousQueryAbstractSelfTest method testTwoQueryListener.

/**
 * @throws Exception If failed.
 */
@Test
public void testTwoQueryListener() throws Exception {
    if (cacheMode() == LOCAL)
        return;
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Integer> cache1 = grid(1).cache(DEFAULT_CACHE_NAME);
    final AtomicInteger cntr = new AtomicInteger(0);
    final AtomicInteger cntr1 = new AtomicInteger(0);
    ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<>();
    ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<>();
    qry1.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> ignore : evts) cntr.incrementAndGet();
        }
    });
    qry2.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> ignore : evts) cntr1.incrementAndGet();
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> qryCur2 = cache1.query(qry2);
        QueryCursor<Cache.Entry<Integer, Integer>> qryCur1 = cache.query(qry1)) {
        for (int i = 0; i < gridCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(DEFAULT_CACHE_NAME);
            cachePut(cache0, 1, 1);
            cachePut(cache0, 2, 2);
            cachePut(cache0, 3, 3);
            cacheRemove(cache0, 1);
            cacheRemove(cache0, 2);
            cacheRemove(cache0, 3);
            final int iter = i + 1;
            assert GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return iter * 6 * /* count operation */
                    2 == /* count continues queries*/
                    (cntr.get() + cntr1.get());
                }
            }, 5000L);
        }
    }
}
Also used : CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)123 Test (org.junit.Test)74 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)70 Ignite (org.apache.ignite.Ignite)60 CacheEntryEvent (javax.cache.event.CacheEntryEvent)58 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)53 CountDownLatch (java.util.concurrent.CountDownLatch)48 IgniteCache (org.apache.ignite.IgniteCache)43 QueryCursor (org.apache.ignite.cache.query.QueryCursor)38 ArrayList (java.util.ArrayList)32 IgniteException (org.apache.ignite.IgniteException)28 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)27 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)24 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)22 Cache (javax.cache.Cache)22 HashMap (java.util.HashMap)21 List (java.util.List)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 CacheException (javax.cache.CacheException)17 IgniteEx (org.apache.ignite.internal.IgniteEx)17