use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.
/**
* @param atomicityMode Cache atomicity mode.
* @throws Exception If failed.
*/
private void concurrentUpdatesAndQueryStart(CacheAtomicityMode atomicityMode) throws Exception {
Ignite srv = startGrid(0);
client = true;
Ignite client = startGrid(1);
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setAtomicityMode(atomicityMode);
IgniteCache clientCache = client.createCache(ccfg);
Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
final List<Integer> keys = new ArrayList<>();
final int KEYS = 10;
for (int i = 0; i < 100_000; i++) {
if (aff.partition(i) == 0) {
keys.add(i);
if (keys.size() == KEYS)
break;
}
}
assertEquals(KEYS, keys.size());
final int THREADS = 10;
final int UPDATES = 1000;
for (int i = 0; i < 5; i++) {
log.info("Iteration: " + i);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final AtomicInteger evtCnt = new AtomicInteger();
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent evt : evts) {
assertNotNull(evt.getKey());
assertNotNull(evt.getValue());
if ((Integer) evt.getValue() >= 0)
evtCnt.incrementAndGet();
}
}
});
QueryCursor cur;
final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
final AtomicBoolean stop = new AtomicBoolean();
try {
IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (!stop.get()) srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
return null;
}
}, THREADS, "update");
U.sleep(1000);
cur = clientCache.query(qry);
U.sleep(1000);
stop.set(true);
fut.get();
} finally {
stop.set(true);
}
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
return null;
}
}, THREADS, "update");
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
log.info("Events: " + evtCnt.get());
return evtCnt.get() >= THREADS * UPDATES;
}
}, 5000);
assertEquals(THREADS * UPDATES, evtCnt.get());
cur.close();
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteRepositoryQuery method execute.
/**
* {@inheritDoc}
*/
@Override
public Object execute(Object[] prmtrs) {
Query qry = prepareQuery(prmtrs);
QueryCursor qryCursor = cache.query(qry);
return transformQueryCursor(prmtrs, qryCursor);
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class JmxExporterSpiTest method testContinuousQuery.
/**
*/
@Test
public void testContinuousQuery() throws Exception {
try (IgniteEx remoteNode = startGrid(1)) {
IgniteCache<Integer, Integer> cache = ignite.createCache("cache-1");
assertEquals(0, systemView(CQ_SYS_VIEW).size());
assertEquals(0, systemView(remoteNode, CQ_SYS_VIEW).size());
try (QueryCursor qry = cache.query(new ContinuousQuery<>().setInitialQuery(new ScanQuery<>()).setPageSize(100).setTimeInterval(1000).setLocalListener(evts -> {
// No-op.
}).setRemoteFilterFactory(() -> evt -> true))) {
for (int i = 0; i < 100; i++) cache.put(i, i);
checkContinuousQueryView(ignite, ignite);
checkContinuousQueryView(ignite, remoteNode);
}
assertEquals(0, systemView(CQ_SYS_VIEW).size());
assertTrue(waitForCondition(() -> systemView(remoteNode, CQ_SYS_VIEW).isEmpty(), getTestTimeout()));
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class SystemViewSelfTest method testContinuousQuery.
/**
*/
@Test
public void testContinuousQuery() throws Exception {
try (IgniteEx originNode = startGrid(0);
IgniteEx remoteNode = startGrid(1)) {
IgniteCache<Integer, Integer> cache = originNode.createCache("cache-1");
SystemView<ContinuousQueryView> origQrys = originNode.context().systemView().view(CQ_SYS_VIEW);
SystemView<ContinuousQueryView> remoteQrys = remoteNode.context().systemView().view(CQ_SYS_VIEW);
assertEquals(0, origQrys.size());
assertEquals(0, remoteQrys.size());
try (QueryCursor qry = cache.query(new ContinuousQuery<>().setInitialQuery(new ScanQuery<>()).setPageSize(100).setTimeInterval(1000).setLocalListener(evts -> {
// No-op.
}).setRemoteFilterFactory(() -> evt -> true))) {
for (int i = 0; i < 100; i++) cache.put(i, i);
checkContinuousQueryView(originNode, origQrys, true);
checkContinuousQueryView(originNode, remoteQrys, false);
}
assertEquals(0, origQrys.size());
assertTrue(waitForCondition(() -> remoteQrys.size() == 0, getTestTimeout()));
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.
/**
* @throws Exception If failed.
*/
@Test
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);
}
}
});
}
Aggregations