use of org.apache.ignite.cache.query.ContinuousQuery 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.ContinuousQuery in project ignite by apache.
the class IgniteCacheContinuousQueryClientReconnectTest method testReconnectClientAndLeftRouter.
/**
* @throws Exception If failed.
*/
public void testReconnectClientAndLeftRouter() throws Exception {
Ignite client = grid(serverCount());
final Ignite srv = clientRouter(client);
final String clnRouterName = srv.name();
assertTrue(client.cluster().localNode().isClient());
final CacheEventListener lsnr = new CacheEventListener();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
IgniteCache<Object, Object> clnCache = client.cache(DEFAULT_CACHE_NAME);
QueryCursor<?> cur = clnCache.query(qry);
int keyCnt = 100;
lsnr.latch = new CountDownLatch(keyCnt);
for (int key = 0; key < keyCnt; key++) clnCache.put(key, key);
assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
reconnectClientNode(client, srv, new Runnable() {
@Override
public void run() {
stopGrid(clnRouterName);
}
});
assertFalse("Client connected to the same server node.", clnRouterName.equals(clientRouter(client).name()));
lsnr.latch = new CountDownLatch(keyCnt);
for (int key = 0; key < keyCnt; key++) clnCache.put(key, key);
assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
cur.close();
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheContinuousQueryClientTest method testNodeJoinsRestartQuery.
/**
* @throws Exception If failed.
*/
public void testNodeJoinsRestartQuery() throws Exception {
startGrids(2);
client = true;
final int CLIENT_ID = 3;
Ignite clientNode = startGrid(CLIENT_ID);
client = false;
for (int i = 0; i < 10; i++) {
log.info("Start iteration: " + i);
final CacheEventListener lsnr = new CacheEventListener();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
QueryCursor<?> cur = clientNode.cache(DEFAULT_CACHE_NAME).query(qry);
lsnr.latch = new CountDownLatch(1);
Ignite joined1 = startGrid(4);
IgniteCache<Object, Object> joinedCache1 = joined1.cache(DEFAULT_CACHE_NAME);
joinedCache1.put(primaryKey(joinedCache1), 1);
assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
cur.close();
lsnr.latch = new CountDownLatch(1);
Ignite joined2 = startGrid(5);
IgniteCache<Object, Object> joinedCache2 = joined2.cache(DEFAULT_CACHE_NAME);
joinedCache2.put(primaryKey(joinedCache2), 2);
assertFalse("Unexpected event received.", GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return 1 != lsnr.latch.getCount();
}
}, 1000));
stopGrid(4);
stopGrid(5);
}
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheContinuousQueryClientTest method testServerNodeLeft.
/**
* @throws Exception If failed.
*/
public void testServerNodeLeft() throws Exception {
startGrids(3);
client = true;
final int CLIENT_ID = 3;
Ignite clnNode = startGrid(CLIENT_ID);
client = false;
IgniteOutClosure<IgniteCache<Integer, Integer>> rndCache = new IgniteOutClosure<IgniteCache<Integer, Integer>>() {
int cnt = 0;
@Override
public IgniteCache<Integer, Integer> apply() {
++cnt;
return grid(CLIENT_ID).cache(DEFAULT_CACHE_NAME);
}
};
final CacheEventListener lsnr = new CacheEventListener();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
QueryCursor<?> cur = clnNode.cache(DEFAULT_CACHE_NAME).query(qry);
boolean first = true;
int keyCnt = 1;
for (int i = 0; i < 10; i++) {
log.info("Start iteration: " + i);
if (first)
first = false;
else {
for (int srv = 0; srv < CLIENT_ID - 1; srv++) startGrid(srv);
}
lsnr.latch = new CountDownLatch(keyCnt);
for (int key = 0; key < keyCnt; key++) rndCache.apply().put(key, key);
assertTrue("Failed to wait for event. Left events: " + lsnr.latch.getCount(), lsnr.latch.await(10, SECONDS));
for (int srv = 0; srv < CLIENT_ID - 1; srv++) stopGrid(srv);
}
tryClose(cur);
}
use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.
the class IgniteCacheContinuousQueryClientTest method testNodeJoins.
/**
* @throws Exception If failed.
*/
public void testNodeJoins() throws Exception {
startGrids(2);
client = true;
final int CLIENT_ID = 3;
Ignite clientNode = startGrid(CLIENT_ID);
client = false;
final CacheEventListener lsnr = new CacheEventListener();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(lsnr);
QueryCursor<?> cur = clientNode.cache(DEFAULT_CACHE_NAME).query(qry);
for (int i = 0; i < 10; i++) {
log.info("Start iteration: " + i);
lsnr.latch = new CountDownLatch(1);
Ignite joined1 = startGrid(4);
IgniteCache<Object, Object> joinedCache1 = joined1.cache(DEFAULT_CACHE_NAME);
joinedCache1.put(primaryKey(joinedCache1), 1);
assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
lsnr.latch = new CountDownLatch(1);
Ignite joined2 = startGrid(5);
IgniteCache<Object, Object> joinedCache2 = joined2.cache(DEFAULT_CACHE_NAME);
joinedCache2.put(primaryKey(joinedCache2), 2);
assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
stopGrid(4);
stopGrid(5);
}
cur.close();
}
Aggregations