Search in sources :

Example 6 with QueryCursor

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

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

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

the class IgniteCacheAbstractQuerySelfTest method testMixedCustomTableName.

/**
     * JUnit.
     *
     * @throws Exception In case of error.
     */
public void testMixedCustomTableName() throws Exception {
    final IgniteCache<Integer, Object> cache = jcache(Integer.class, Object.class);
    cache.put(10, new Type1(1, "Type1 record #1"));
    cache.put(20, new Type1(2, "Type1 record #2"));
    cache.put(30, new Type2(1, "Type2 record #1"));
    cache.put(40, new Type2(2, "Type2 record #2"));
    cache.put(50, new Type2(3, "Type2 record #3"));
    QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type2"));
    List<Cache.Entry<Integer, Type1>> all = qry1.getAll();
    assertEquals(2, all.size());
    QueryCursor<Cache.Entry<Integer, Type2>> qry2 = cache.query(new SqlQuery<Integer, Type2>(Type2.class, "FROM Type1"));
    assertEquals(3, qry2.getAll().size());
    QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type1"));
    assertEquals(3, qry.getAll().size());
    qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type2"));
    assertEquals(2, qry.getAll().size());
    GridTestUtils.assertThrows(log, new GridPlainCallable<Void>() {

        @Override
        public Void call() throws Exception {
            QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type1"));
            qry1.getAll().size();
            return null;
        }
    }, CacheException.class, null);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IOException(java.io.IOException) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 9 with QueryCursor

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

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

the class IgniteCachePartitionedQueryMultiThreadedSelfTest method testLuceneAndSqlMultithreaded.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
public void testLuceneAndSqlMultithreaded() throws Exception {
    // ---------- Test parameters ---------- //
    int luceneThreads = 10;
    int sqlThreads = 10;
    long duration = 10 * 1000;
    final int logMod = 100;
    final PersonObj p1 = new PersonObj("Jon", 1500, "Master");
    final PersonObj p2 = new PersonObj("Jane", 2000, "Master");
    final PersonObj p3 = new PersonObj("Mike", 1800, "Bachelor");
    final PersonObj p4 = new PersonObj("Bob", 1900, "Bachelor");
    final IgniteCache<UUID, PersonObj> cache0 = grid(0).cache(DEFAULT_CACHE_NAME);
    cache0.put(p1.id(), p1);
    cache0.put(p2.id(), p2);
    cache0.put(p3.id(), p3);
    cache0.put(p4.id(), p4);
    assertEquals(4, cache0.localSize(CachePeekMode.ALL));
    assert grid(0).cluster().nodes().size() == GRID_CNT;
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicLong luceneCnt = new AtomicLong();
    // Start lucene query threads.
    IgniteInternalFuture<?> futLucene = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> master = cache0.query(new TextQuery(PersonObj.class, "Master"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = master.getAll();
                checkResult(entries, p1, p2);
                long cnt = luceneCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed LUCENE queries: " + cnt);
            }
        }
    }, luceneThreads, "LUCENE-THREAD");
    final AtomicLong sqlCnt = new AtomicLong();
    // Start sql query threads.
    IgniteInternalFuture<?> futSql = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> bachelors = cache0.query(new SqlQuery(PersonObj.class, "degree = 'Bachelor'"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = bachelors.getAll();
                checkResult(entries, p3, p4);
                long cnt = sqlCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed SQL queries: " + cnt);
            }
        }
    }, sqlThreads, "SQL-THREAD");
    Thread.sleep(duration);
    done.set(true);
    futLucene.get();
    futSql.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collection(java.util.Collection) CAX(org.apache.ignite.internal.util.typedef.CAX) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

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