use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testRestartQuery.
/**
* @throws Exception If failed.
*/
@Test
public void testRestartQuery() throws Exception {
if (cacheMode() == LOCAL)
return;
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
final int parts = grid(0).affinity(DEFAULT_CACHE_NAME).partitions();
final int keyCnt = parts * 2;
for (int i = 0; i < parts / 2; i++) cachePut(cache, i, i);
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
final AtomicInteger cntr = new AtomicInteger(0);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
qry.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();
}
});
QueryCursor<Cache.Entry<Integer, Integer>> qryCur = cache.query(qry);
for (int key = 0; key < keyCnt; key++) cachePut(cache, key, key);
try {
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return cntr.get() == keyCnt;
}
}, 2000L);
} finally {
qryCur.close();
}
} else {
for (int key = 0; key < keyCnt; key++) cachePut(cache, key, key);
}
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testInternalKey.
/**
* @throws Exception If failed.
*/
@Test
public void testInternalKey() throws Exception {
if (atomicityMode() == ATOMIC)
return;
IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final Map<Object, Object> map = new ConcurrentHashMap<>();
final CountDownLatch latch = new CountDownLatch(2);
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> e : evts) {
map.put(e.getKey(), e.getValue());
latch.countDown();
}
}
});
try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
cache.put(new GridCacheInternalKeyImpl("test", "test"), 1);
cachePut(cache, 1, 1);
cachePut(cache, 2, 2);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(2, map.size());
assertEquals(1, (int) map.get(1));
assertEquals(2, (int) map.get(2));
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class GridCacheContinuousQueryNodesFilteringTest method startNodeWithCache.
/**
* Start first, attribute-bearing, node, create new cache and launch continuous query on it.
*
* @return Node.
* @throws Exception if failed.
*/
private Ignite startNodeWithCache() throws Exception {
Ignite node1 = startGrid("node1", getConfiguration("node1", true, null));
CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
ccfg.setName("attrsTestCache");
ccfg.setNodeFilter(new IgnitePredicate<ClusterNode>() {
/**
* {@inheritDoc}
*/
@Override
public boolean apply(ClusterNode node) {
return "data".equals(node.attribute("node-type"));
}
});
IgniteCache<Integer, Integer> cache = node1.createCache(ccfg);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
qry.setRemoteFilterFactory(new RemoteFilterFactory());
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
/**
* {@inheritDoc}
*/
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
// No-op.
}
});
RemoteFilterFactory.clsLdr = getExternalClassLoader();
cache.query(qry);
// Switch class loader before starting the second node.
RemoteFilterFactory.clsLdr = getClass().getClassLoader();
return node1;
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class ContinuousQueryMarshallerTest method check.
/**
* @param node1Name Node 1 name.
* @param node2Name Node 2 name.
*/
private void check(String node1Name, String node2Name) throws Exception {
final Ignite node1 = startGrid(node1Name);
final IgniteCache<Integer, MarshallerCheckingEntry> cache = node1.getOrCreateCache(CACHE_NAME);
for (int i = 0; i < 10; i++) cache.put(i, new MarshallerCheckingEntry(String.valueOf(i)));
final Ignite node2 = "client".equals(node2Name) ? startClientGrid(node2Name) : startGrid(node2Name);
final ContinuousQuery<Integer, MarshallerCheckingEntry> qry = new ContinuousQuery<>();
ScanQuery<Integer, MarshallerCheckingEntry> scanQry = new ScanQuery<>(new IgniteBiPredicate<Integer, MarshallerCheckingEntry>() {
@Override
public boolean apply(Integer key, MarshallerCheckingEntry val) {
return key % 2 == 0;
}
});
qry.setInitialQuery(scanQry);
qry.setRemoteFilterFactory(new DummyEventFilterFactory<>());
final CountDownLatch latch = new CountDownLatch(15);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, MarshallerCheckingEntry>() {
@Override
public void onUpdated(final Iterable<CacheEntryEvent<? extends Integer, ? extends MarshallerCheckingEntry>> evts) throws CacheEntryListenerException {
System.out.println(">> Client 1 events " + evts);
for (CacheEntryEvent<? extends Integer, ? extends MarshallerCheckingEntry> evt : evts) latch.countDown();
}
});
final IgniteCache<Integer, MarshallerCheckingEntry> cache1 = node2.cache(CACHE_NAME);
for (Cache.Entry<Integer, MarshallerCheckingEntry> entry : cache1.query(qry)) {
latch.countDown();
System.out.println(">> Initial entry " + entry);
}
for (int i = 10; i < 20; i++) cache1.put(i, new MarshallerCheckingEntry(i));
assertTrue(Long.toString(latch.getCount()), latch.await(5, TimeUnit.SECONDS));
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testAllEntries.
/**
* @throws Exception If failed.
*/
@Test
public void testAllEntries() throws Exception {
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
final Map<Integer, List<Integer>> map = new HashMap<>();
final CountDownLatch latch = new CountDownLatch(5);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
synchronized (map) {
List<Integer> vals = map.get(e.getKey());
if (vals == null) {
vals = new ArrayList<>();
map.put(e.getKey(), vals);
}
vals.add(e.getValue());
}
latch.countDown();
}
}
});
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
cachePut(cache, 1, 1);
cachePut(cache, 2, 2);
cachePut(cache, 3, 3);
cacheRemove(cache, 2);
cachePut(cache, 1, 10);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(3, map.size());
List<Integer> vals = map.get(1);
assertNotNull(vals);
assertEquals(2, vals.size());
assertEquals(1, (int) vals.get(0));
assertEquals(10, (int) vals.get(1));
vals = map.get(2);
assertNotNull(vals);
assertEquals(2, vals.size());
assertEquals(2, (int) vals.get(0));
assertEquals(2, (int) vals.get(1));
vals = map.get(3);
assertNotNull(vals);
assertEquals(1, vals.size());
assertEquals(3, (int) vals.get(0));
}
}
Aggregations