Search in sources :

Example 91 with ContinuousQuery

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

the class GridCacheContinuousQueryAbstractSelfTest method testRestartQuery.

/**
 * @throws Exception If failed.
 */
@Test
public void testRestartQuery() throws Exception {
    if (cacheMode() == LOCAL)
        return;
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    final int parts = grid(0).affinity(DEFAULT_CACHE_NAME).partitions();
    final int keyCnt = parts * 2;
    for (int i = 0; i < parts / 2; i++) cachePut(cache, i, i);
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            final AtomicInteger cntr = new AtomicInteger(0);
            ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
            qry.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();
                }
            });
            QueryCursor<Cache.Entry<Integer, Integer>> qryCur = cache.query(qry);
            for (int key = 0; key < keyCnt; key++) cachePut(cache, key, key);
            try {
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return cntr.get() == keyCnt;
                    }
                }, 2000L);
            } finally {
                qryCur.close();
            }
        } else {
            for (int key = 0; key < keyCnt; key++) cachePut(cache, key, key);
        }
    }
}
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)

Example 92 with ContinuousQuery

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

the class GridCacheContinuousQueryAbstractSelfTest method testInternalKey.

/**
 * @throws Exception If failed.
 */
@Test
public void testInternalKey() throws Exception {
    if (atomicityMode() == ATOMIC)
        return;
    IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final Map<Object, Object> map = new ConcurrentHashMap<>();
    final CountDownLatch latch = new CountDownLatch(2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent<?, ?> e : evts) {
                map.put(e.getKey(), e.getValue());
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
        cache.put(new GridCacheInternalKeyImpl("test", "test"), 1);
        cachePut(cache, 1, 1);
        cachePut(cache, 2, 2);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, map.size());
        assertEquals(1, (int) map.get(1));
        assertEquals(2, (int) map.get(2));
    }
}
Also used : GridCacheInternalKeyImpl(org.apache.ignite.internal.processors.datastructures.GridCacheInternalKeyImpl) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 93 with ContinuousQuery

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

the class GridCacheContinuousQueryNodesFilteringTest method startNodeWithCache.

/**
 * Start first, attribute-bearing, node, create new cache and launch continuous query on it.
 *
 * @return Node.
 * @throws Exception if failed.
 */
private Ignite startNodeWithCache() throws Exception {
    Ignite node1 = startGrid("node1", getConfiguration("node1", true, null));
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setName("attrsTestCache");
    ccfg.setNodeFilter(new IgnitePredicate<ClusterNode>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean apply(ClusterNode node) {
            return "data".equals(node.attribute("node-type"));
        }
    });
    IgniteCache<Integer, Integer> cache = node1.createCache(ccfg);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setRemoteFilterFactory(new RemoteFilterFactory());
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
        // No-op.
        }
    });
    RemoteFilterFactory.clsLdr = getExternalClassLoader();
    cache.query(qry);
    // Switch class loader before starting the second node.
    RemoteFilterFactory.clsLdr = getClass().getClassLoader();
    return node1;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 94 with ContinuousQuery

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

the class ContinuousQueryMarshallerTest method check.

/**
 * @param node1Name Node 1 name.
 * @param node2Name Node 2 name.
 */
private void check(String node1Name, String node2Name) throws Exception {
    final Ignite node1 = startGrid(node1Name);
    final IgniteCache<Integer, MarshallerCheckingEntry> cache = node1.getOrCreateCache(CACHE_NAME);
    for (int i = 0; i < 10; i++) cache.put(i, new MarshallerCheckingEntry(String.valueOf(i)));
    final Ignite node2 = "client".equals(node2Name) ? startClientGrid(node2Name) : startGrid(node2Name);
    final ContinuousQuery<Integer, MarshallerCheckingEntry> qry = new ContinuousQuery<>();
    ScanQuery<Integer, MarshallerCheckingEntry> scanQry = new ScanQuery<>(new IgniteBiPredicate<Integer, MarshallerCheckingEntry>() {

        @Override
        public boolean apply(Integer key, MarshallerCheckingEntry val) {
            return key % 2 == 0;
        }
    });
    qry.setInitialQuery(scanQry);
    qry.setRemoteFilterFactory(new DummyEventFilterFactory<>());
    final CountDownLatch latch = new CountDownLatch(15);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, MarshallerCheckingEntry>() {

        @Override
        public void onUpdated(final Iterable<CacheEntryEvent<? extends Integer, ? extends MarshallerCheckingEntry>> evts) throws CacheEntryListenerException {
            System.out.println(">> Client 1 events " + evts);
            for (CacheEntryEvent<? extends Integer, ? extends MarshallerCheckingEntry> evt : evts) latch.countDown();
        }
    });
    final IgniteCache<Integer, MarshallerCheckingEntry> cache1 = node2.cache(CACHE_NAME);
    for (Cache.Entry<Integer, MarshallerCheckingEntry> entry : cache1.query(qry)) {
        latch.countDown();
        System.out.println(">> Initial entry " + entry);
    }
    for (int i = 10; i < 20; i++) cache1.put(i, new MarshallerCheckingEntry(i));
    assertTrue(Long.toString(latch.getCount()), latch.await(5, TimeUnit.SECONDS));
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 95 with ContinuousQuery

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

the class GridCacheContinuousQueryAbstractSelfTest method testAllEntries.

/**
 * @throws Exception If failed.
 */
@Test
public void testAllEntries() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, List<Integer>> map = new HashMap<>();
    final CountDownLatch latch = new CountDownLatch(5);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                synchronized (map) {
                    List<Integer> vals = map.get(e.getKey());
                    if (vals == null) {
                        vals = new ArrayList<>();
                        map.put(e.getKey(), vals);
                    }
                    vals.add(e.getValue());
                }
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cachePut(cache, 1, 1);
        cachePut(cache, 2, 2);
        cachePut(cache, 3, 3);
        cacheRemove(cache, 2);
        cachePut(cache, 1, 10);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(3, map.size());
        List<Integer> vals = map.get(1);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(1, (int) vals.get(0));
        assertEquals(10, (int) vals.get(1));
        vals = map.get(2);
        assertNotNull(vals);
        assertEquals(2, vals.size());
        assertEquals(2, (int) vals.get(0));
        assertEquals(2, (int) vals.get(1));
        vals = map.get(3);
        assertNotNull(vals);
        assertEquals(1, vals.size());
        assertEquals(3, (int) vals.get(0));
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) List(java.util.List) ArrayList(java.util.ArrayList) 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