Search in sources :

Example 46 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery 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 {
        AbstractContinuousQuery<QueryTestKey, QueryTestValue> qry = createQuery();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        if (qry instanceof ContinuousQuery) {
            ((ContinuousQuery<QueryTestKey, QueryTestValue>) 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);
                }
            });
        } else if (qry instanceof ContinuousQueryWithTransformer)
            initQueryWithTransformer((ContinuousQueryWithTransformer<QueryTestKey, QueryTestValue, CacheEntryEvent>) qry, evts);
        else
            fail("Unknown query type");
        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 : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) TreeMap(java.util.TreeMap) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 47 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery 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) {
            AbstractContinuousQuery<Object, Object> qry = createQuery();
            final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
            if (qry instanceof ContinuousQuery) {
                ((ContinuousQuery<Object, Object>) qry).setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

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

                    @Override
                    public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                    }
                });
            } else if (qry instanceof ContinuousQueryWithTransformer)
                initQueryWithTransformer((ContinuousQueryWithTransformer<Object, Object, CacheEntryEvent>) qry, evtsQueue);
            else
                fail("Unknown query type");
            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++) {
                AbstractContinuousQuery<Object, Object> qry = createQuery();
                final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
                if (qry instanceof ContinuousQuery) {
                    ((ContinuousQuery<Object, Object>) qry).setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                        @Override
                        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                            for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
                        }
                    });
                } else if (qry instanceof ContinuousQueryWithTransformer)
                    initQueryWithTransformer((ContinuousQueryWithTransformer<Object, Object, CacheEntryEvent>) qry, evtsQueue);
                else
                    fail("Unknown query type");
                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 : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Random(java.util.Random) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 48 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery 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 {
        AbstractContinuousQuery<QueryTestKey, QueryTestValue> qry = createQuery();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        if (qry instanceof ContinuousQuery) {
            ((ContinuousQuery<QueryTestKey, QueryTestValue>) 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);
                }
            });
        } else if (qry instanceof ContinuousQueryWithTransformer)
            initQueryWithTransformer((ContinuousQueryWithTransformer<QueryTestKey, QueryTestValue, CacheEntryEvent>) qry, evts);
        else
            fail("Unknown query type");
        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, new QueryTestValue(1), new QueryTestValue(1));
                checkSingleEvent(evts.get(2), CREATED, new QueryTestValue(2), null);
                checkSingleEvent(evts.get(3), REMOVED, new QueryTestValue(2), 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, new QueryTestValue(4), 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 : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 49 with ContinuousQuery

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueEvict.

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

        @Override
        public boolean apply() {
            return backupQueue(ignite(0)).isEmpty();
        }
    }, 5000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(0)), backupQueue(ignite(0)).size() < BACKUP_ACK_THRESHOLD);
    boolean wait = waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cache0.localPeek(keys.get(0)) == null;
        }
    }, ttl + 1000);
    assertTrue("Entry evicted.", wait);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(0)).isEmpty();
        }
    }, 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(0)), backupQueue(ignite(0)).size() < BACKUP_ACK_THRESHOLD);
    if (!backupQueue(ignite(0)).isEmpty()) {
        for (Object o : backupQueue(ignite(0))) {
            CacheContinuousQueryEntry e = (CacheContinuousQueryEntry) o;
            assertNotSame("Evicted entry added to backup queue.", -1L, e.updateCounter());
        }
    }
    cur.close();
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Duration(javax.cache.expiry.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Ignite(org.apache.ignite.Ignite) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 50 with ContinuousQuery

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueCleanupClientQuery.

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

        @Override
        public boolean apply() {
            return backupQueue(ignite(1)).isEmpty();
        }
    }, 2000);
    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() + ']');
    keys = primaryKeys(cache0, BACKUP_ACK_THRESHOLD / 2);
    latch = new CountDownLatch(keys.size());
    lsnr.latch = latch;
    for (Integer key : keys) cache0.put(key, key);
    final long ACK_FREQ = 5000;
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(1)).isEmpty();
        }
    }, ACK_FREQ + 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(1)), backupQueue(ignite(1)).isEmpty());
    if (!latch.await(5, SECONDS))
        fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
    cur.close();
    assertFalse("Unexpected error during test, see log for details.", err);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

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