use of javax.cache.event.CacheEntryUpdatedListener in project ignite by apache.
the class CacheContinuousQueryFactoryFilterRandomOperationTest method testInternalQuery.
/**
* @throws Exception If failed.
*/
public void testInternalQuery() throws Exception {
CacheConfiguration<Object, Object> ccfg = cacheConfiguration(REPLICATED, 1, ATOMIC, false);
final IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
UUID uuid = null;
try {
for (int i = 0; i < 10; i++) cache.put(i, i);
final CountDownLatch latch = new CountDownLatch(5);
CacheEntryUpdatedListener lsnr = new CacheEntryUpdatedListener() {
@Override
public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
for (Object evt : iterable) {
latch.countDown();
log.info("Received event: " + evt);
}
}
};
uuid = grid(0).context().cache().cache(cache.getName()).context().continuousQueries().executeInternalQuery(lsnr, new SerializableFilter(), false, true, true);
for (int i = 10; i < 20; i++) cache.put(i, i);
assertTrue(latch.await(3, SECONDS));
} finally {
if (uuid != null)
grid(0).context().cache().cache(cache.getName()).context().continuousQueries().cancelInternalQuery(uuid);
grid(0).destroyCache(ccfg.getName());
}
}
use of javax.cache.event.CacheEntryUpdatedListener in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.
/**
* @throws Exception If failed.
*/
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);
}
}
});
}
use of javax.cache.event.CacheEntryUpdatedListener in project ignite by apache.
the class CacheContinuousBatchAckTest method checkBackupAcknowledgeMessage.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void checkBackupAcknowledgeMessage(CacheConfiguration<Object, Object> ccfg) throws Exception {
QueryCursor qry = null;
IgniteCache<Object, Object> cache = null;
try {
ContinuousQuery q = new ContinuousQuery();
q.setLocalListener(new CacheEntryUpdatedListener() {
@Override
public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
// No-op.
}
});
cache = grid(SERVER).getOrCreateCache(ccfg);
qry = cache.query(q);
for (int i = 0; i < 10000; i++) cache.put(i, i);
assert !GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return fail.get();
}
}, 1300L);
} finally {
if (qry != null)
qry.close();
if (cache != null)
grid(SERVER).destroyCache(cache.getName());
}
}
use of javax.cache.event.CacheEntryUpdatedListener 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 javax.cache.event.CacheEntryUpdatedListener in project ignite by apache.
the class TcpDiscoveryMultiThreadedTest method _testClientContinuousQueryCoordinatorStop.
/**
* @throws Exception If failed.
*/
public void _testClientContinuousQueryCoordinatorStop() throws Exception {
for (int k = 0; k < 10; k++) {
log.info("Iteration: " + k);
clientFlagGlobal = false;
final int START_NODES = 5;
final int JOIN_NODES = 5;
startGrids(START_NODES);
ignite(0).createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
final AtomicInteger startIdx = new AtomicInteger(START_NODES);
final CyclicBarrier barrier = new CyclicBarrier(JOIN_NODES + 1);
clientFlagGlobal = true;
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int idx = startIdx.getAndIncrement();
Thread.currentThread().setName("start-thread-" + idx);
barrier.await();
Ignite ignite = startGrid(idx);
assertTrue(ignite.configuration().isClientMode());
log.info("Started node: " + ignite.name());
IgniteCache<Object, Object> cache = ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 10; i++) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
// No-op.
}
});
cache.query(qry);
}
return null;
}
}, JOIN_NODES, "start-thread");
barrier.await();
U.sleep(ThreadLocalRandom.current().nextInt(100, 500));
for (int i = 0; i < START_NODES - 1; i++) {
GridTestUtils.invoke(ignite(i).configuration().getDiscoverySpi(), "simulateNodeFailure");
stopGrid(i);
}
fut.get();
stopAllGrids();
}
}
Aggregations