Search in sources :

Example 16 with ContinuousQuery

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

the class CacheContinuousQueryCounterAbstractTest method testLoadCache.

/**
     * @throws Exception If failed.
     */
public void testLoadCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, T2<Integer, Long>> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(10);
    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) {
                map.put(e.getKey(), new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cache.loadCache(null, 0);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : "Count: " + latch.getCount();
        assertEquals(10, map.size());
        for (int i = 0; i < 10; i++) {
            assertEquals(i, (int) map.get(i).get1());
            assertEquals((long) 1, (long) map.get(i).get2());
        }
    }
}
Also used : ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) T2(org.apache.ignite.internal.util.typedef.T2)

Example 17 with ContinuousQuery

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

the class CacheContinuousQueryCounterAbstractTest method testRestartQuery.

/**
     * @throws Exception If failed.
     */
public void testRestartQuery() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    final int keyCnt = 300;
    final int updateKey = 1;
    for (int i = 0; i < keyCnt; i++) cache.put(updateKey, i);
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            final AtomicInteger cntr = new AtomicInteger(0);
            ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
            final List<T2<Integer, Long>> vals = new ArrayList<>();
            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 (vals) {
                            cntr.incrementAndGet();
                            vals.add(new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                        }
                    }
                }
            });
            try (QueryCursor<Cache.Entry<Integer, Integer>> ignore = cache.query(qry)) {
                for (int key = 0; key < keyCnt; key++) cache.put(updateKey, cache.get(updateKey) + 1);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return cntr.get() == keyCnt;
                    }
                }, 2000L);
                synchronized (vals) {
                    for (T2<Integer, Long> val : vals) assertEquals((long) val.get1() + 1, (long) val.get2());
                }
            }
        } else {
            for (int key = 0; key < keyCnt; key++) cache.put(updateKey, cache.get(updateKey) + 1);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) 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) T2(org.apache.ignite.internal.util.typedef.T2)

Example 18 with ContinuousQuery

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueCleanupServerQuery.

/**
     * @throws Exception If failed.
     */
public void testBackupQueueCleanupServerQuery() throws Exception {
    Ignite qryClient = startGridsMultiThreaded(2);
    CacheEventListener1 lsnr = new CacheEventListener1(false);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    IgniteCache<Object, Object> cache = qryClient.cache(DEFAULT_CACHE_NAME);
    QueryCursor<?> cur = cache.query(qry);
    assertEquals(0, backupQueue(ignite(1)).size());
    List<Integer> keys = primaryKeys(cache, BACKUP_ACK_THRESHOLD);
    CountDownLatch latch = new CountDownLatch(keys.size());
    lsnr.latch = latch;
    for (Integer key : keys) {
        log.info("Put: " + key);
        cache.put(key, key);
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(1)).isEmpty();
        }
    }, 3000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(1)), backupQueue(ignite(1)).size() < BACKUP_ACK_THRESHOLD);
    if (!latch.await(5, SECONDS))
        fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
    cur.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 19 with ContinuousQuery

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

the class CacheContinuousQueryOperationFromCallbackTest method doTest.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
protected void doTest(final CacheConfiguration ccfg, boolean fromLsnr) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    assertEquals(0, filterCbCntr.get());
    try {
        List<Set<T2<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(NODES);
        List<Set<T2<QueryTestKey, QueryTestValue>>> evtsFromCallbacks = new ArrayList<>(NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final AtomicInteger cbCntr = new AtomicInteger(0);
        final int threadCnt = SYSTEM_POOL_SIZE * 2;
        for (int idx = 0; idx < NODES; idx++) {
            Set<T2<QueryTestKey, QueryTestValue>> evts = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            Set<T2<QueryTestKey, QueryTestValue>> evtsFromCb = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            IgniteCache<Object, Object> cache = grid(idx).getOrCreateCache(ccfg.getName());
            ContinuousQuery qry = new ContinuousQuery();
            qry.setLocalListener(new TestCacheAsyncEventListener(evts, evtsFromCb, fromLsnr ? cache : null, qryCntr, cbCntr));
            if (!fromLsnr)
                qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
            rcvdEvts.add(evts);
            evtsFromCallbacks.add(evtsFromCb);
            QueryCursor qryCursor = cache.query(qry);
            qries.add(qryCursor);
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
                    Transaction tx = null;
                    if (startTx)
                        tx = cache.unwrap(Ignite.class).transactions().txStart();
                    try {
                        if ((cache.get(key) == null) || rnd.nextBoolean())
                            cache.invoke(key, new IncrementTestEntryProcessor());
                        else {
                            QueryTestValue val;
                            QueryTestValue newVal;
                            do {
                                val = cache.get(key);
                                newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                            } while (!cache.replace(key, val, newVal));
                        }
                    } finally {
                        if (tx != null)
                            tx.commit();
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(30, TimeUnit.SECONDS);
        assert GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * NODES;
            }
        }, TimeUnit.MINUTES.toMillis(2));
        for (Set<T2<QueryTestKey, QueryTestValue>> set : rcvdEvts) checkEvents(set, ITERATION_CNT * threadCnt, grid(0).cache(ccfg.getName()), false);
        if (fromLsnr) {
            final int expCnt = qryCntr.get() * NODES * KEYS_FROM_CALLBACK;
            boolean res = GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return cbCntr.get() >= expCnt;
                }
            }, TimeUnit.SECONDS.toMillis(60));
            assertTrue("Failed to wait events [exp=" + expCnt + ", act=" + cbCntr.get() + "]", res);
            assertEquals(expCnt, cbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, qryCntr.get() * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        } else {
            final int expInvkCnt = ITERATION_CNT * threadCnt * (ccfg.getCacheMode() != REPLICATED ? (ccfg.getBackups() + 1) : NODES - 1) * NODES;
            GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return filterCbCntr.get() >= expInvkCnt;
                }
            }, TimeUnit.SECONDS.toMillis(60));
            assertEquals(expInvkCnt, filterCbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, expInvkCnt * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        }
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 20 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.
     * @throws Exception If failed.
     */
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    QueryCursor<?> cur = null;
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    final CountDownLatch latch = new CountDownLatch(10);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    TestLocalListener localLsnr = new TestLocalListener() {

        @Override
        public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();
                log.info("Received event: " + evt);
            }
        }
    };
    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
    qry.setLocalListener(localLsnr);
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
    IgniteCache<Integer, Integer> cache = null;
    try {
        if (isClient)
            cache = grid(NODES - 1).cache(ccfg.getName());
        else
            cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
        cur = cache.query(qry);
        cache.registerCacheEntryListener(lsnrCfg);
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertTrue(latch.await(3, TimeUnit.SECONDS));
    } finally {
        if (cur != null)
            cur.close();
        if (cache != null)
            cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

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