Search in sources :

Example 21 with ContinuousQuery

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

the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.

/**
     * @throws Exception If failed.
     */
public void testContinuousQuery() throws Exception {
    runInAllDataModes(new TestRunnable() {

        @Override
        public void run() throws Exception {
            final AtomicInteger updCnt = new AtomicInteger();
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {

                @Override
                public boolean apply(Object key, Object val) {
                    return valueOf(key) >= 3;
                }
            }));
            qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Object, ? extends Object>> evts) throws CacheEntryListenerException {
                    for (CacheEntryEvent<? extends Object, ? extends Object> evt : evts) {
                        int v = valueOf(evt.getKey());
                        // Check filter.
                        assertTrue("v=" + v, v >= 10 && v < 15);
                        updCnt.incrementAndGet();
                    }
                }
            });
            qry.setRemoteFilter(new TestCacheEntryEventSerializableFilter());
            IgniteCache<Object, Object> cache = jcache();
            for (int i = 0; i < 10; i++) cache.put(key(i), value(i));
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry)) {
                int cnt = 0;
                for (Cache.Entry<Object, Object> e : cur) {
                    cnt++;
                    int val = valueOf(e.getKey());
                    assertTrue("v=" + val, val >= 3);
                }
                assertEquals(7, cnt);
                for (int i = 10; i < 20; i++) cache.put(key(i), value(i));
                GridTestUtils.waitForCondition(new GridAbsPredicateX() {

                    @Override
                    public boolean applyx() throws IgniteCheckedException {
                        return updCnt.get() == 5;
                    }
                }, 30_000);
            }
        }
    });
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) MutableEntry(javax.cache.processor.MutableEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 22 with ContinuousQuery

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

the class CacheContinuousBatchAckTest method checkBackupAcknowledgeMessage.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void checkBackupAcknowledgeMessage(CacheConfiguration<Object, Object> ccfg) throws Exception {
    QueryCursor qry = null;
    IgniteCache<Object, Object> cache = null;
    try {
        ContinuousQuery q = new ContinuousQuery();
        q.setLocalListener(new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
            // No-op.
            }
        });
        cache = grid(SERVER).getOrCreateCache(ccfg);
        qry = cache.query(q);
        for (int i = 0; i < 10000; i++) cache.put(i, i);
        assert !GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return fail.get();
            }
        }, 1300L);
    } finally {
        if (qry != null)
            qry.close();
        if (cache != null)
            grid(SERVER).destroyCache(cache.getName());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 23 with ContinuousQuery

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

the class IgniteClientReconnectContinuousProcessorTest method testCacheContinuousQueryReconnectNewServer.

/**
     * @throws Exception If failed.
     */
public void testCacheContinuousQueryReconnectNewServer() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    IgniteCache<Object, Object> clientCache = client.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
    CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setAutoUnsubscribe(true);
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = clientCache.query(qry);
    continuousQueryReconnect(client, clientCache, lsnr);
    // Check new server registers listener for reconnected client.
    try (Ignite newSrv = startGrid(serverCount() + 1)) {
        awaitPartitionMapExchange();
        lsnr.latch = new CountDownLatch(10);
        IgniteCache<Object, Object> newSrvCache = newSrv.cache(DEFAULT_CACHE_NAME);
        for (Integer key : primaryKeys(newSrvCache, 10)) newSrvCache.put(key, key);
        assertTrue(lsnr.latch.await(5000, MILLISECONDS));
    }
    cur.close();
    // Check new server does not register listener for closed query.
    try (Ignite newSrv = startGrid(serverCount() + 1)) {
        awaitPartitionMapExchange();
        lsnr.latch = new CountDownLatch(5);
        IgniteCache<Object, Object> newSrvCache = newSrv.cache(DEFAULT_CACHE_NAME);
        for (Integer key : primaryKeys(newSrvCache, 5)) newSrvCache.put(key, key);
        assertFalse(lsnr.latch.await(3000, MILLISECONDS));
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with ContinuousQuery

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

the class BinaryMetadataUpdatesFlowTest method startListening.

/**
     * @param idx Index.
     * @param deafClient Deaf client.
     * @param observedIds Observed ids.
     */
private void startListening(int idx, boolean deafClient, Set<Integer> observedIds) throws Exception {
    clientMode = true;
    ContinuousQuery qry = new ContinuousQuery();
    qry.setLocalListener(new CQListener(observedIds));
    if (deafClient) {
        applyDiscoveryHook = true;
        discoveryHook = new DiscoveryHook() {

            @Override
            public void handleDiscoveryMessage(DiscoverySpiCustomMessage msg) {
                DiscoveryCustomMessage customMsg = msg == null ? null : (DiscoveryCustomMessage) IgniteUtils.field(msg, "delegate");
                if (customMsg instanceof MetadataUpdateProposedMessage) {
                    if (((MetadataUpdateProposedMessage) customMsg).typeId() == BINARY_TYPE_ID)
                        GridTestUtils.setFieldValue(customMsg, "typeId", 1);
                } else if (customMsg instanceof MetadataUpdateAcceptedMessage) {
                    if (((MetadataUpdateAcceptedMessage) customMsg).typeId() == BINARY_TYPE_ID)
                        GridTestUtils.setFieldValue(customMsg, "typeId", 1);
                }
            }
        };
        IgniteEx client = startGrid(idx);
        client.cache(DEFAULT_CACHE_NAME).withKeepBinary().query(qry);
    } else {
        applyDiscoveryHook = false;
        IgniteEx client = startGrid(idx);
        client.cache(DEFAULT_CACHE_NAME).withKeepBinary().query(qry);
    }
}
Also used : DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) DiscoveryHook(org.apache.ignite.testframework.GridTestUtils.DiscoveryHook)

Example 25 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery 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)

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