use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryFailoverAbstractSelfTest method testStartStop.
/**
* @throws Exception If failed.
*/
@Test
public void testStartStop() throws Exception {
this.backups = 2;
final int SRV_NODES = 4;
startGridsMultiThreaded(SRV_NODES);
Ignite qryClient = startClientGrid(SRV_NODES);
IgniteCache<Object, Object> qryClnCache = qryClient.cache(DEFAULT_CACHE_NAME);
Affinity<Object> aff = qryClient.affinity(DEFAULT_CACHE_NAME);
final CacheEventListener2 lsnr = new CacheEventListener2();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
qry.setRemoteFilter(new CacheEventFilter());
QueryCursor<?> cur = qryClnCache.query(qry);
for (int i = 0; i < 10; i++) {
final int idx = i % (SRV_NODES - 1);
log.info("Stop node: " + idx);
stopGrid(idx);
awaitPartitionMapExchange();
List<T3<Object, Object, Object>> afterRestEvts = new ArrayList<>();
for (int j = 0; j < aff.partitions(); j++) {
Integer oldVal = (Integer) qryClnCache.get(j);
qryClnCache.put(j, i);
afterRestEvts.add(new T3<>((Object) j, (Object) i, (Object) oldVal));
}
checkEvents(new ArrayList<>(afterRestEvts), lsnr, false);
log.info("Start node: " + idx);
startGrid(idx);
}
cur.close();
}
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.
* @param joinNode If a node should be added to topology after a query is started.
* @param evtFilterFactoryCls Remote filter factory class.
* @throws Exception If failed.
*/
private void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient, boolean joinNode, Class<Factory<CacheEntryEventFilter>> evtFilterFactoryCls) throws Exception {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
final CountDownLatch latch = new CountDownLatch(UPDATES);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
AtomicReference<String> err = new AtomicReference<>();
TestLocalListener locLsnr = new TestLocalListener() {
@Override
protected void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
latch.countDown();
log.info("Received event: " + evt);
int key = evt.getKey();
if (key % 2 == 0)
err.set("Event received on entry, that doesn't pass a filter: " + key);
}
}
};
qry.setLocalListener(locLsnr);
qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactoryCls.newInstance());
MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(locLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactoryCls.newInstance(), true, true);
IgniteCache<Integer, Integer> cache;
cache = isClient ? grid(NODES - 1).cache(ccfg.getName()) : grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
try (QueryCursor<?> cur = cache.query(qry)) {
cache.registerCacheEntryListener(lsnrCfg);
if (joinNode) {
startGrid(NODES);
awaitPartitionMapExchange();
}
for (int i = 0; i < UPDATES; i++) cache.put(i, i);
assertTrue("Failed to wait for local listener invocations: " + latch.getCount(), latch.await(3, TimeUnit.SECONDS));
assertNull(err.get(), err.get());
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryOperationP2PTest method testMultithreadedUpdatesNodeJoin.
/**
* @throws Exception If failed.
*/
@Test
public void testMultithreadedUpdatesNodeJoin() throws Exception {
Ignite client = startGrid("client");
CacheConfiguration<Object, Object> cacheCfg = cacheConfiguration(PARTITIONED, 0, ATOMIC);
IgniteCache<Object, Object> cache = client.createCache(cacheCfg);
int iterations = 50;
int keysNum = 100;
int threadsNum = Runtime.getRuntime().availableProcessors();
CountDownLatch updatesLatch = new CountDownLatch(iterations * keysNum * threadsNum / 2);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final Class<Factory<CacheEntryEventFilter>> evtFilterFactoryCls = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Object, Object>>) (Object) evtFilterFactoryCls.newInstance());
qry.setLocalListener((evts) -> {
for (CacheEntryEvent<?, ?> ignored : evts) updatesLatch.countDown();
});
cache.query(qry);
for (int t = 0; t < threadsNum; t++) {
int threadId = t;
GridTestUtils.runAsync(() -> {
for (int i = 0; i < iterations; i++) {
log.info("Iteration #" + (i + 1));
for (int k = 0; k < keysNum; k++) {
int key = keysNum * threadId + k;
cache.put(key, key);
}
}
}, "cache-writer-thread-" + threadId);
}
startGrid(NODES);
assertTrue("Failed to wait for all cache updates invocations. Latch: " + updatesLatch, updatesLatch.await(30, TimeUnit.SECONDS));
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method testFilterAndFactoryProvided.
/**
* @throws Exception If failed.
*/
@Test
public void testFilterAndFactoryProvided() throws Exception {
final CacheConfiguration<Object, Object> ccfg = cacheConfiguration(PARTITIONED, 1, ATOMIC, false);
grid(0).createCache(ccfg);
try {
final ContinuousQuery qry = new ContinuousQuery();
qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter>() {
@Override
public CacheEntryEventFilter create() {
return null;
}
});
qry.setRemoteFilter(new CacheEntryEventSerializableFilter() {
@Override
public boolean evaluate(CacheEntryEvent event) throws CacheEntryListenerException {
return false;
}
});
qry.setLocalListener(new CacheEntryUpdatedListener() {
@Override
public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
// No-op.
}
});
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
return grid(0).cache(ccfg.getName()).query(qry);
}
}, IgniteException.class, null);
} finally {
grid(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testTwoQueryListener.
/**
* @throws Exception If failed.
*/
@Test
public void testTwoQueryListener() throws Exception {
if (cacheMode() == LOCAL)
return;
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Integer> cache1 = grid(1).cache(DEFAULT_CACHE_NAME);
final AtomicInteger cntr = new AtomicInteger(0);
final AtomicInteger cntr1 = new AtomicInteger(0);
ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<>();
ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<>();
qry1.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> ignore : evts) cntr.incrementAndGet();
}
});
qry2.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> ignore : evts) cntr1.incrementAndGet();
}
});
try (QueryCursor<Cache.Entry<Integer, Integer>> qryCur2 = cache1.query(qry2);
QueryCursor<Cache.Entry<Integer, Integer>> qryCur1 = cache.query(qry1)) {
for (int i = 0; i < gridCount(); i++) {
IgniteCache<Object, Object> cache0 = grid(i).cache(DEFAULT_CACHE_NAME);
cachePut(cache0, 1, 1);
cachePut(cache0, 2, 2);
cachePut(cache0, 3, 3);
cacheRemove(cache0, 1);
cacheRemove(cache0, 2);
cacheRemove(cache0, 3);
final int iter = i + 1;
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return iter * 6 * /* count operation */
2 == /* count continues queries*/
(cntr.get() + cntr1.get());
}
}, 5000L);
}
}
}
Aggregations