Search in sources :

Example 11 with QueryCursor

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

the class GridServiceContinuousQueryRedeployTest method testServiceRedeploymentAfterCancel.

/**
     * @throws Exception If failed.
     */
public void testServiceRedeploymentAfterCancel() throws Exception {
    final Ignite ignite = startGrid(0);
    final IgniteCache<Object, Object> managementCache = ignite.getOrCreateCache(CACHE_NAME);
    final ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    final List<Object> evts = Collections.synchronizedList(new ArrayList<>());
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> iterable) throws CacheEntryListenerException {
            for (CacheEntryEvent<?, ?> event : iterable) evts.add(event);
        }
    });
    int iterations = 100;
    while (iterations-- > 0) {
        QueryCursor quorumCursor = managementCache.query(qry);
        IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                System.out.println("Deploy " + SERVICE_NAME);
                deployService(ignite);
                return null;
            }
        });
        IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                System.out.println("Undeploy " + SERVICE_NAME);
                ignite.services().cancel(SERVICE_NAME);
                return null;
            }
        });
        fut1.get();
        fut2.get();
        U.sleep(100);
        assert evts.size() <= 1 : evts.size();
        ignite.services().cancel("service1");
        evts.clear();
        quorumCursor.close();
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 12 with QueryCursor

use of org.apache.ignite.cache.query.QueryCursor 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 13 with QueryCursor

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

the class CacheContinuousQueryRandomOperationsTest method doTestContinuousQuery.

/**
     * @param ccfg Cache configuration.
     * @param deploy The place where continuous query will be started.
     * @throws Exception If failed.
     */
protected void doTestContinuousQuery(CacheConfiguration<Object, Object> ccfg, ContinuousDeploy deploy) throws Exception {
    ignite(0).createCache(ccfg);
    try {
        long seed = System.currentTimeMillis();
        Random rnd = new Random(seed);
        log.info("Random seed: " + seed);
        List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues = new ArrayList<>();
        Collection<QueryCursor<?>> curs = new ArrayList<>();
        if (deploy == CLIENT) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                    for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                }
            });
            evtsQueues.add(evtsQueue);
            QueryCursor<?> cur = grid(getClientIndex()).cache(ccfg.getName()).query(qry);
            curs.add(cur);
        } else if (deploy == SERVER) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                    for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                }
            });
            evtsQueues.add(evtsQueue);
            QueryCursor<?> cur = grid(rnd.nextInt(getServerNodeCount())).cache(ccfg.getName()).query(qry);
            curs.add(cur);
        } else {
            for (int i = 0; i <= getServerNodeCount(); i++) {
                ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
                qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                    @Override
                    public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                    }
                });
                evtsQueues.add(evtsQueue);
                QueryCursor<?> cur = ignite(i).cache(ccfg.getName()).query(qry);
                curs.add(cur);
            }
        }
        ConcurrentMap<Object, Object> expData = new ConcurrentHashMap<>();
        Map<Integer, Long> partCntr = new ConcurrentHashMap<>();
        try {
            for (int i = 0; i < ITERATION_CNT; i++) {
                if (i % 20 == 0)
                    log.info("Iteration: " + i);
                for (int idx = 0; idx < getServerNodeCount(); idx++) randomUpdate(rnd, evtsQueues, expData, partCntr, grid(idx).cache(ccfg.getName()));
            }
        } finally {
            for (QueryCursor<?> cur : curs) cur.close();
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Random(java.util.Random) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 14 with QueryCursor

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

the class CacheContinuousQueryRandomOperationsTest method batchOperation.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void batchOperation(CacheConfiguration ccfg) throws Exception {
    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
    try {
        ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
            }
        });
        Map<QueryTestKey, QueryTestValue> map = new TreeMap<>();
        for (int i = 0; i < KEYS; i++) map.put(new QueryTestKey(i), new QueryTestValue(i));
        try (QueryCursor qryCur = cache.query(qry)) {
            for (int i = 0; i < ITERATION_CNT / 2; i++) {
                log.info("Start iteration: " + i);
                // Not events.
                cache.removeAll(map.keySet());
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.putAll(map);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == KEYS;
                    }
                }, 5_000);
                checkEvents(evts, CREATED);
                evts.clear();
                // Not events.
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                U.sleep(100);
                assertEquals(0, evts.size());
                // Get events.
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                // Not events.
                cache.removeAll(map.keySet());
                cache.removeAll(map.keySet());
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == KEYS;
                    }
                }, 5_000);
                checkEvents(evts, REMOVED);
                evts.clear();
                log.info("Finish iteration: " + i);
            }
        }
    } finally {
        grid(getClientIndex()).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) TreeMap(java.util.TreeMap) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 15 with QueryCursor

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

the class CacheContinuousQueryRandomOperationsTest method singleOperation.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void singleOperation(CacheConfiguration ccfg) throws Exception {
    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
    try {
        ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
            }
        });
        QueryTestKey key = new QueryTestKey(1);
        try (QueryCursor qryCur = cache.query(qry)) {
            for (int i = 0; i < ITERATION_CNT; i++) {
                log.info("Start iteration: " + i);
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.put(key, new QueryTestValue(1));
                cache.remove(key);
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.remove(key);
                // Get events.
                cache.put(key, new QueryTestValue(2));
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                // Not events.
                cache.remove(key);
                // Get events.
                cache.put(key, new QueryTestValue(3));
                cache.put(key, new QueryTestValue(4));
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.remove(key, new QueryTestValue(5));
                // Get events.
                cache.remove(key, new QueryTestValue(4));
                cache.putIfAbsent(key, new QueryTestValue(5));
                // Not events.
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                // Get events.
                cache.replace(key, new QueryTestValue(5), new QueryTestValue(6));
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == 9;
                    }
                }, 5_000);
                checkSingleEvent(evts.get(0), CREATED, new QueryTestValue(1), null);
                checkSingleEvent(evts.get(1), REMOVED, null, new QueryTestValue(1));
                checkSingleEvent(evts.get(2), CREATED, new QueryTestValue(2), null);
                checkSingleEvent(evts.get(3), REMOVED, null, new QueryTestValue(2));
                checkSingleEvent(evts.get(4), CREATED, new QueryTestValue(3), null);
                checkSingleEvent(evts.get(5), EventType.UPDATED, new QueryTestValue(4), new QueryTestValue(3));
                checkSingleEvent(evts.get(6), REMOVED, null, new QueryTestValue(4));
                checkSingleEvent(evts.get(7), CREATED, new QueryTestValue(5), null);
                checkSingleEvent(evts.get(8), EventType.UPDATED, new QueryTestValue(6), new QueryTestValue(5));
                evts.clear();
                cache.remove(key);
                cache.remove(key);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == 1;
                    }
                }, 5_000);
                evts.clear();
                log.info("Finish iteration: " + i);
            }
        }
    } finally {
        grid(getClientIndex()).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

QueryCursor (org.apache.ignite.cache.query.QueryCursor)34 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)20 IgniteCache (org.apache.ignite.IgniteCache)16 Ignite (org.apache.ignite.Ignite)14 Cache (javax.cache.Cache)13 ArrayList (java.util.ArrayList)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteException (org.apache.ignite.IgniteException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 CacheException (javax.cache.CacheException)6 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)6 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)6 SqlQuery (org.apache.ignite.cache.query.SqlQuery)6 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 PA (org.apache.ignite.internal.util.typedef.PA)5 EntryProcessorException (javax.cache.processor.EntryProcessorException)4