Search in sources :

Example 26 with ContinuousQuery

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

the class GridCacheContinuousQueryMultiNodesFilteringTest method testWithNodeFilter.

/**
     * @throws Exception If failed.
     */
public void testWithNodeFilter() throws Exception {
    List<QueryCursor> qryCursors = new ArrayList<>();
    final int nodesCnt = 3;
    startGridsMultiThreaded(nodesCnt);
    awaitPartitionMapExchange();
    CacheConfiguration ccfg = cacheConfiguration(new NodeFilterByRegexp(".*(0|1)$"));
    grid(0).createCache(ccfg);
    final AtomicInteger cntr = new AtomicInteger();
    final ConcurrentMap<ClusterNode, Set<Integer>> maps = new ConcurrentHashMap<>();
    final AtomicBoolean doubleNtfFail = new AtomicBoolean(false);
    CacheEntryUpdatedListener<Integer, Integer> lsnr = new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                cntr.incrementAndGet();
                ClusterNode node = ((Ignite) e.getSource().unwrap(Ignite.class)).cluster().localNode();
                Set<Integer> set = maps.get(node);
                if (set == null) {
                    set = new ConcurrentSkipListSet<>();
                    Set<Integer> oldVal = maps.putIfAbsent(node, set);
                    set = oldVal != null ? oldVal : set;
                }
                if (!set.add(e.getValue()))
                    doubleNtfFail.set(false);
            }
        }
    };
    for (int i = 0; i < nodesCnt; i++) {
        ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
        qry.setLocalListener(lsnr);
        Ignite ignite = grid(i);
        log.info("Try to start CQ on node: " + ignite.cluster().localNode().id());
        qryCursors.add(ignite.cache(ccfg.getName()).query(qry));
        log.info("CQ started on node: " + ignite.cluster().localNode().id());
    }
    client = true;
    startGrid(nodesCnt);
    awaitPartitionMapExchange();
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    qryCursors.add(grid(nodesCnt).cache(ccfg.getName()).query(qry));
    for (int i = 0; i <= nodesCnt; i++) {
        for (int key = 0; key < KEYS; key++) {
            int val = (i * KEYS) + key;
            grid(i).cache(ccfg.getName()).put(val, val);
        }
    }
    assertTrue(GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return cntr.get() >= 2 * (nodesCnt + 1) * KEYS;
        }
    }, 5000L));
    assertFalse("Got duplicate", doubleNtfFail.get());
    for (int i = 0; i < (nodesCnt + 1) * KEYS; i++) {
        for (Map.Entry<ClusterNode, Set<Integer>> e : maps.entrySet()) assertTrue("Lost event on node: " + e.getKey().id() + ", event: " + i, e.getValue().remove(i));
    }
    for (Map.Entry<ClusterNode, Set<Integer>> e : maps.entrySet()) assertTrue("Unexpected event on node: " + e.getKey(), e.getValue().isEmpty());
    assertEquals("Not expected count of CQ", nodesCnt + 1, qryCursors.size());
    for (QueryCursor cur : qryCursors) cur.close();
}
Also used : Set(java.util.Set) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PA(org.apache.ignite.internal.util.typedef.PA) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with ContinuousQuery

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

the class IgniteCacheContinuousQueryClientReconnectTest method testReconnectClientAndLeftRouter.

/**
     * @throws Exception If failed.
     */
public void testReconnectClientAndLeftRouter() throws Exception {
    Ignite client = grid(serverCount());
    final Ignite srv = clientRouter(client);
    final String clnRouterName = srv.name();
    assertTrue(client.cluster().localNode().isClient());
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    IgniteCache<Object, Object> clnCache = client.cache(DEFAULT_CACHE_NAME);
    QueryCursor<?> cur = clnCache.query(qry);
    int keyCnt = 100;
    lsnr.latch = new CountDownLatch(keyCnt);
    for (int key = 0; key < keyCnt; key++) clnCache.put(key, key);
    assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            stopGrid(clnRouterName);
        }
    });
    assertFalse("Client connected to the same server node.", clnRouterName.equals(clientRouter(client).name()));
    lsnr.latch = new CountDownLatch(keyCnt);
    for (int key = 0; key < keyCnt; key++) clnCache.put(key, key);
    assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
    cur.close();
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 28 with ContinuousQuery

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

the class IgniteCacheContinuousQueryClientTest method testNodeJoinsRestartQuery.

/**
     * @throws Exception If failed.
     */
public void testNodeJoinsRestartQuery() throws Exception {
    startGrids(2);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clientNode = startGrid(CLIENT_ID);
    client = false;
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        final CacheEventListener lsnr = new CacheEventListener();
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        qry.setLocalListener(lsnr);
        QueryCursor<?> cur = clientNode.cache(DEFAULT_CACHE_NAME).query(qry);
        lsnr.latch = new CountDownLatch(1);
        Ignite joined1 = startGrid(4);
        IgniteCache<Object, Object> joinedCache1 = joined1.cache(DEFAULT_CACHE_NAME);
        joinedCache1.put(primaryKey(joinedCache1), 1);
        assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
        cur.close();
        lsnr.latch = new CountDownLatch(1);
        Ignite joined2 = startGrid(5);
        IgniteCache<Object, Object> joinedCache2 = joined2.cache(DEFAULT_CACHE_NAME);
        joinedCache2.put(primaryKey(joinedCache2), 2);
        assertFalse("Unexpected event received.", GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return 1 != lsnr.latch.getCount();
            }
        }, 1000));
        stopGrid(4);
        stopGrid(5);
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 29 with ContinuousQuery

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

the class IgniteCacheContinuousQueryClientTest method testServerNodeLeft.

/**
     * @throws Exception If failed.
     */
public void testServerNodeLeft() throws Exception {
    startGrids(3);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clnNode = startGrid(CLIENT_ID);
    client = false;
    IgniteOutClosure<IgniteCache<Integer, Integer>> rndCache = new IgniteOutClosure<IgniteCache<Integer, Integer>>() {

        int cnt = 0;

        @Override
        public IgniteCache<Integer, Integer> apply() {
            ++cnt;
            return grid(CLIENT_ID).cache(DEFAULT_CACHE_NAME);
        }
    };
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = clnNode.cache(DEFAULT_CACHE_NAME).query(qry);
    boolean first = true;
    int keyCnt = 1;
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        if (first)
            first = false;
        else {
            for (int srv = 0; srv < CLIENT_ID - 1; srv++) startGrid(srv);
        }
        lsnr.latch = new CountDownLatch(keyCnt);
        for (int key = 0; key < keyCnt; key++) rndCache.apply().put(key, key);
        assertTrue("Failed to wait for event. Left events: " + lsnr.latch.getCount(), lsnr.latch.await(10, SECONDS));
        for (int srv = 0; srv < CLIENT_ID - 1; srv++) stopGrid(srv);
    }
    tryClose(cur);
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite)

Example 30 with ContinuousQuery

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

the class IgniteCacheContinuousQueryClientTest method testNodeJoins.

/**
     * @throws Exception If failed.
     */
public void testNodeJoins() throws Exception {
    startGrids(2);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clientNode = startGrid(CLIENT_ID);
    client = false;
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = clientNode.cache(DEFAULT_CACHE_NAME).query(qry);
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        lsnr.latch = new CountDownLatch(1);
        Ignite joined1 = startGrid(4);
        IgniteCache<Object, Object> joinedCache1 = joined1.cache(DEFAULT_CACHE_NAME);
        joinedCache1.put(primaryKey(joinedCache1), 1);
        assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
        lsnr.latch = new CountDownLatch(1);
        Ignite joined2 = startGrid(5);
        IgniteCache<Object, Object> joinedCache2 = joined2.cache(DEFAULT_CACHE_NAME);
        joinedCache2.put(primaryKey(joinedCache2), 2);
        assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
        stopGrid(4);
        stopGrid(5);
    }
    cur.close();
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)82 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)47 Ignite (org.apache.ignite.Ignite)42 CacheEntryEvent (javax.cache.event.CacheEntryEvent)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 ArrayList (java.util.ArrayList)26 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)19 QueryCursor (org.apache.ignite.cache.query.QueryCursor)19 IgniteCache (org.apache.ignite.IgniteCache)18 IgniteException (org.apache.ignite.IgniteException)15 HashMap (java.util.HashMap)14 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 List (java.util.List)12 PA (org.apache.ignite.internal.util.typedef.PA)12 T2 (org.apache.ignite.internal.util.typedef.T2)12 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)11 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9