use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class GridServiceContinuousQueryRedeployTest method testServiceRedeploymentAfterCancel.
/**
* @throws Exception If failed.
*/
public void testServiceRedeploymentAfterCancel() throws Exception {
final Ignite ignite = startGrid(0);
final IgniteCache<Object, Object> managementCache = ignite.getOrCreateCache(CACHE_NAME);
final ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final List<Object> evts = Collections.synchronizedList(new ArrayList<>());
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> iterable) throws CacheEntryListenerException {
for (CacheEntryEvent<?, ?> event : iterable) evts.add(event);
}
});
int iterations = 100;
while (iterations-- > 0) {
QueryCursor quorumCursor = managementCache.query(qry);
IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
System.out.println("Deploy " + SERVICE_NAME);
deployService(ignite);
return null;
}
});
IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
System.out.println("Undeploy " + SERVICE_NAME);
ignite.services().cancel(SERVICE_NAME);
return null;
}
});
fut1.get();
fut2.get();
U.sleep(100);
assert evts.size() <= 1 : evts.size();
ignite.services().cancel("service1");
evts.clear();
quorumCursor.close();
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class GridCacheContinuousQueryMultiNodesFilteringTest method testWithNodeFilter.
/**
* @throws Exception If failed.
*/
public void testWithNodeFilter() throws Exception {
List<QueryCursor> qryCursors = new ArrayList<>();
final int nodesCnt = 3;
startGridsMultiThreaded(nodesCnt);
awaitPartitionMapExchange();
CacheConfiguration ccfg = cacheConfiguration(new NodeFilterByRegexp(".*(0|1)$"));
grid(0).createCache(ccfg);
final AtomicInteger cntr = new AtomicInteger();
final ConcurrentMap<ClusterNode, Set<Integer>> maps = new ConcurrentHashMap<>();
final AtomicBoolean doubleNtfFail = new AtomicBoolean(false);
CacheEntryUpdatedListener<Integer, Integer> lsnr = new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
cntr.incrementAndGet();
ClusterNode node = ((Ignite) e.getSource().unwrap(Ignite.class)).cluster().localNode();
Set<Integer> set = maps.get(node);
if (set == null) {
set = new ConcurrentSkipListSet<>();
Set<Integer> oldVal = maps.putIfAbsent(node, set);
set = oldVal != null ? oldVal : set;
}
if (!set.add(e.getValue()))
doubleNtfFail.set(false);
}
}
};
for (int i = 0; i < nodesCnt; i++) {
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
Ignite ignite = grid(i);
log.info("Try to start CQ on node: " + ignite.cluster().localNode().id());
qryCursors.add(ignite.cache(ccfg.getName()).query(qry));
log.info("CQ started on node: " + ignite.cluster().localNode().id());
}
client = true;
startGrid(nodesCnt);
awaitPartitionMapExchange();
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
qryCursors.add(grid(nodesCnt).cache(ccfg.getName()).query(qry));
for (int i = 0; i <= nodesCnt; i++) {
for (int key = 0; key < KEYS; key++) {
int val = (i * KEYS) + key;
grid(i).cache(ccfg.getName()).put(val, val);
}
}
assertTrue(GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return cntr.get() >= 2 * (nodesCnt + 1) * KEYS;
}
}, 5000L));
assertFalse("Got duplicate", doubleNtfFail.get());
for (int i = 0; i < (nodesCnt + 1) * KEYS; i++) {
for (Map.Entry<ClusterNode, Set<Integer>> e : maps.entrySet()) assertTrue("Lost event on node: " + e.getKey().id() + ", event: " + i, e.getValue().remove(i));
}
for (Map.Entry<ClusterNode, Set<Integer>> e : maps.entrySet()) assertTrue("Unexpected event on node: " + e.getKey(), e.getValue().isEmpty());
assertEquals("Not expected count of CQ", nodesCnt + 1, qryCursors.size());
for (QueryCursor cur : qryCursors) cur.close();
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method doTestContinuousQuery.
/**
* @param ccfg Cache configuration.
* @param deploy The place where continuous query will be started.
* @throws Exception If failed.
*/
protected void doTestContinuousQuery(CacheConfiguration<Object, Object> ccfg, ContinuousDeploy deploy) throws Exception {
ignite(0).createCache(ccfg);
try {
long seed = System.currentTimeMillis();
Random rnd = new Random(seed);
log.info("Random seed: " + seed);
List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues = new ArrayList<>();
Collection<QueryCursor<?>> curs = new ArrayList<>();
if (deploy == CLIENT) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
}
});
evtsQueues.add(evtsQueue);
QueryCursor<?> cur = grid(getClientIndex()).cache(ccfg.getName()).query(qry);
curs.add(cur);
} else if (deploy == SERVER) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
}
});
evtsQueues.add(evtsQueue);
QueryCursor<?> cur = grid(rnd.nextInt(getServerNodeCount())).cache(ccfg.getName()).query(qry);
curs.add(cur);
} else {
for (int i = 0; i <= getServerNodeCount(); i++) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final BlockingQueue<CacheEntryEvent<?, ?>> evtsQueue = new ArrayBlockingQueue<>(50_000);
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> evt : evts) evtsQueue.add(evt);
}
});
evtsQueues.add(evtsQueue);
QueryCursor<?> cur = ignite(i).cache(ccfg.getName()).query(qry);
curs.add(cur);
}
}
ConcurrentMap<Object, Object> expData = new ConcurrentHashMap<>();
Map<Integer, Long> partCntr = new ConcurrentHashMap<>();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
if (i % 20 == 0)
log.info("Iteration: " + i);
for (int idx = 0; idx < getServerNodeCount(); idx++) randomUpdate(rnd, evtsQueues, expData, partCntr, grid(idx).cache(ccfg.getName()));
}
} finally {
for (QueryCursor<?> cur : curs) cur.close();
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method batchOperation.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void batchOperation(CacheConfiguration ccfg) throws Exception {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
try {
ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
if (noOpFilterFactory() != null)
qry.setRemoteFilterFactory(noOpFilterFactory());
qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
}
});
Map<QueryTestKey, QueryTestValue> map = new TreeMap<>();
for (int i = 0; i < KEYS; i++) map.put(new QueryTestKey(i), new QueryTestValue(i));
try (QueryCursor qryCur = cache.query(qry)) {
for (int i = 0; i < ITERATION_CNT / 2; i++) {
log.info("Start iteration: " + i);
// Not events.
cache.removeAll(map.keySet());
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
// Get events.
cache.putAll(map);
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == KEYS;
}
}, 5_000);
checkEvents(evts, CREATED);
evts.clear();
// Not events.
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
U.sleep(100);
assertEquals(0, evts.size());
// Get events.
cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
// Not events.
cache.removeAll(map.keySet());
cache.removeAll(map.keySet());
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == KEYS;
}
}, 5_000);
checkEvents(evts, REMOVED);
evts.clear();
log.info("Finish iteration: " + i);
}
}
} finally {
grid(getClientIndex()).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method singleOperation.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void singleOperation(CacheConfiguration ccfg) throws Exception {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
try {
ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
if (noOpFilterFactory() != null)
qry.setRemoteFilterFactory(noOpFilterFactory());
qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
}
});
QueryTestKey key = new QueryTestKey(1);
try (QueryCursor qryCur = cache.query(qry)) {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
// Get events.
cache.put(key, new QueryTestValue(1));
cache.remove(key);
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
cache.remove(key);
// Get events.
cache.put(key, new QueryTestValue(2));
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
// Get events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
// Not events.
cache.remove(key);
// Get events.
cache.put(key, new QueryTestValue(3));
cache.put(key, new QueryTestValue(4));
// Not events.
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
cache.putIfAbsent(key, new QueryTestValue(5));
cache.putIfAbsent(key, new QueryTestValue(5));
cache.putIfAbsent(key, new QueryTestValue(5));
cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
cache.remove(key, new QueryTestValue(5));
// Get events.
cache.remove(key, new QueryTestValue(4));
cache.putIfAbsent(key, new QueryTestValue(5));
// Not events.
cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
// Get events.
cache.replace(key, new QueryTestValue(5), new QueryTestValue(6));
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == 9;
}
}, 5_000);
checkSingleEvent(evts.get(0), CREATED, new QueryTestValue(1), null);
checkSingleEvent(evts.get(1), REMOVED, null, new QueryTestValue(1));
checkSingleEvent(evts.get(2), CREATED, new QueryTestValue(2), null);
checkSingleEvent(evts.get(3), REMOVED, null, new QueryTestValue(2));
checkSingleEvent(evts.get(4), CREATED, new QueryTestValue(3), null);
checkSingleEvent(evts.get(5), EventType.UPDATED, new QueryTestValue(4), new QueryTestValue(3));
checkSingleEvent(evts.get(6), REMOVED, null, new QueryTestValue(4));
checkSingleEvent(evts.get(7), CREATED, new QueryTestValue(5), null);
checkSingleEvent(evts.get(8), EventType.UPDATED, new QueryTestValue(6), new QueryTestValue(5));
evts.clear();
cache.remove(key);
cache.remove(key);
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return evts.size() == 1;
}
}, 5_000);
evts.clear();
log.info("Finish iteration: " + i);
}
}
} finally {
grid(getClientIndex()).destroyCache(ccfg.getName());
}
}
Aggregations