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());
}
}
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);
}
}
});
}
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);
}
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());
}
}
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();
}
Aggregations