use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project camel by apache.
the class IgniteCacheContinuousQueryTest method createRegistry.
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry answer = super.createRegistry();
ScanQuery<Integer, Person> scanQuery1 = new ScanQuery<>(new IgniteBiPredicate<Integer, Person>() {
private static final long serialVersionUID = 1L;
@Override
public boolean apply(Integer key, Person person) {
return person.getId() > 50;
}
});
CacheEntryEventSerializableFilter<Integer, Person> remoteFilter = new CacheEntryEventSerializableFilter<Integer, IgniteCacheContinuousQueryTest.Person>() {
private static final long serialVersionUID = 5624973479995548199L;
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Person> event) throws CacheEntryListenerException {
return event.getValue().getId() > 150;
}
};
answer.bind("query1", scanQuery1);
answer.bind("remoteFilter1", remoteFilter);
return answer;
}
use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.
the class CacheEntryListenersTest method testListenersWithRemoteFilter.
/**
* Test continuous queries and JCache entry listeners with remote filters.
*/
@Test
@SuppressWarnings("deprecation")
public void testListenersWithRemoteFilter() throws Exception {
try (IgniteClient client = startClient(0, 1, 2)) {
ClientCache<Integer, Integer> cache = client.getOrCreateCache("testListenersWithRmtFilter");
CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = evt -> (evt.getKey() & 1) == 0;
ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1).setRemoteFilterFactory(() -> rmtFilter));
cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2).setRemoteFilter(rmtFilter));
JCacheEntryListener<Integer, Integer> lsnr3 = new JCacheEntryListener<>();
cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, () -> rmtFilter, true, false));
for (int i = 0; i < 10; i++) cache.put(i, i);
Map<EventType, Map<Integer, Integer>> expRes = F.asMap(EventType.CREATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.put(i, -i);
expRes = F.asMap(EventType.UPDATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
for (int i = 0; i < 10; i++) cache.remove(i);
expRes = F.asMap(EventType.REMOVED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
assertTrue(lsnr1.isQueueEmpty());
assertTrue(lsnr2.isQueueEmpty());
assertTrue(lsnr3.isQueueEmpty());
}
}
use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.
the class ContinuousQueryReassignmentTest method testContinuousQueryWithRemoteFilterNotCalledOnReassignment.
/**
* @throws Exception If failed.
*/
@Test
public void testContinuousQueryWithRemoteFilterNotCalledOnReassignment() throws Exception {
Ignite lsnrNode = startGrid(1);
Ignite victim = startGrid(2);
awaitPartitionMapExchange();
CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<>("cache");
cacheCfg.setBackups(1);
IgniteCache<Integer, String> cache = lsnrNode.getOrCreateCache(cacheCfg);
AtomicInteger updCntr = new AtomicInteger();
CacheEntryEventSerializableFilter<Integer, String> filter = (e) -> e.getKey() % 2 == 0;
listenToUpdates(cache, false, updCntr, filter);
int updates = 1000;
for (int i = 0; i < updates; i++) cache.put(i, Integer.toString(i));
assertTrue("Failed to wait for continuous query updates. Exp: " + updates + "; actual: " + updCntr.get(), waitForCondition(() -> updCntr.get() == updates / 2, 10000));
victim.close();
assertFalse("Continuous query is called on reassignment.", waitForCondition(() -> updCntr.get() > updates / 2, 2000));
}
use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.
the class CacheContinuousQueryExecuteInPrimaryTest method doTestWithEventsEntries.
/**
* @throws Exception If failed.
*/
public void doTestWithEventsEntries(CacheConfiguration<Integer, String> ccfg) throws Exception {
try (IgniteCache<Integer, String> cache = grid(0).createCache(ccfg)) {
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
final CountDownLatch latch = new CountDownLatch(16);
final AtomicInteger cnt = new AtomicInteger(0);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> iterable) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Integer, ? extends String> e : iterable) {
cnt.incrementAndGet();
latch.countDown();
}
}
});
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, String>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) throws CacheEntryListenerException {
return e.getKey() % 2 == 0;
}
}));
// Execute query.
executeQuery(cache, qry, ccfg.getAtomicityMode() != ATOMIC);
assertTrue(latch.await(LATCH_TIMEOUT, MILLISECONDS));
assertEquals(16, cnt.get());
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.cache.CacheEntryEventSerializableFilter in project ignite by apache.
the class IgniteCacheProxyImpl method queryContinuous.
/**
* Executes continuous query.
*
* @param qry Query.
* @param loc Local flag.
* @param keepBinary Keep binary flag.
* @return Initial iteration cursor.
*/
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(AbstractContinuousQuery qry, boolean loc, boolean keepBinary) {
GridCacheContext<K, V> ctx = getContextSafe();
assert qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer;
if (qry.getInitialQuery() instanceof ContinuousQuery || qry.getInitialQuery() instanceof ContinuousQueryWithTransformer) {
throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
}
CacheEntryUpdatedListener locLsnr = null;
EventListener locTransLsnr = null;
CacheEntryEventSerializableFilter rmtFilter = null;
Factory<? extends IgniteClosure> rmtTransFactory = null;
if (qry instanceof ContinuousQuery) {
ContinuousQuery<K, V> qry0 = (ContinuousQuery<K, V>) qry;
if (qry0.getLocalListener() == null && qry0.getRemoteFilterFactory() == null && qry0.getRemoteFilter() == null) {
throw new IgniteException("LocalListener, RemoterFilter " + "or RemoteFilterFactory must be specified for the query: " + qry);
}
if (qry0.getRemoteFilter() != null && qry0.getRemoteFilterFactory() != null)
throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
locLsnr = qry0.getLocalListener();
rmtFilter = qry0.getRemoteFilter();
} else {
ContinuousQueryWithTransformer<K, V, ?> qry0 = (ContinuousQueryWithTransformer<K, V, ?>) qry;
if (qry0.getLocalListener() == null && qry0.getRemoteFilterFactory() == null) {
throw new IgniteException("LocalListener " + "or RemoteFilterFactory must be specified for the query: " + qry);
}
if (qry0.getRemoteTransformerFactory() == null)
throw new IgniteException("Mandatory RemoteTransformerFactory is not set for the query: " + qry);
Collection<ClusterNode> nodes = context().grid().cluster().nodes();
for (ClusterNode node : nodes) {
if (node.version().compareTo(CONT_QRY_WITH_TRANSFORMER_SINCE) < 0) {
throw new IgniteException("Can't start ContinuousQueryWithTransformer, " + "because some nodes in cluster doesn't support this feature: " + node);
}
}
locTransLsnr = qry0.getLocalListener();
rmtTransFactory = qry0.getRemoteTransformerFactory();
}
try {
final UUID routineId = ctx.continuousQueries().executeQuery(locLsnr, locTransLsnr, rmtFilter, qry.getRemoteFilterFactory(), rmtTransFactory, qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
try {
final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
return new QueryCursorEx<Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
}
@Override
public List<Cache.Entry<K, V>> getAll() {
return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
}
@Override
public void close() {
if (cur != null)
cur.close();
try {
ctx.kernalContext().continuous().stopRoutine(routineId).get();
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public void getAll(Consumer<Entry<K, V>> c) {
// No-op.
}
@Override
public List<GridQueryFieldMetadata> fieldsMeta() {
// noinspection rawtypes
return cur instanceof QueryCursorEx ? ((QueryCursorEx) cur).fieldsMeta() : null;
}
};
} catch (Throwable t) {
// Initial query failed: stop the routine.
ctx.kernalContext().continuous().stopRoutine(routineId).get();
throw t;
}
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
Aggregations