use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer 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) {
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)
throw new IgniteException("Mandatory local listener is not set 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)
throw new IgniteException("Mandatory local transformed event listener is not set 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());
final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
return new QueryCursor<Cache.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);
}
}
};
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer in project ignite by apache.
the class CacheContinuousQueryWithTransformerExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Cache continuous query with transformer example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, Organization> cache = ignite.getOrCreateCache(CACHE_NAME)) {
// Create new continuous query with transformer.
ContinuousQueryWithTransformer<Integer, Organization, String> qry = new ContinuousQueryWithTransformer<>();
// Factory to create transformers.
Factory<IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>> factory = FactoryBuilder.factoryOf(// Only this field will be sent over to a local node over the network.
(IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>) event -> event.getValue().name());
qry.setRemoteTransformerFactory(factory);
// Listener that will receive transformed data.
qry.setLocalListener(names -> {
for (String name : names) System.out.println("New organization name: " + name);
});
// Execute query.
try (QueryCursor<Cache.Entry<Integer, Organization>> cur = cache.query(qry)) {
populateCache(cache);
// Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000);
}
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(CACHE_NAME);
}
}
}
use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer in project ignite by apache.
the class CacheContinuousWithTransformerFailoverTest 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>> cache = 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();
ContinuousQueryWithTransformer<Object, Object, String> qry = new ContinuousQueryWithTransformer<>();
qry.setLocalListener(lsnr);
qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<?, ?>, String>() {
@Override
public String apply(CacheEntryEvent<?, ?> evt) {
return "" + evt.getKey() + evt.getValue();
}
}));
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++) cache.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.ContinuousQueryWithTransformer in project ignite by apache.
the class CacheContinuousWithTransformerFailoverTest method testTransformerException.
/**
* @throws Exception If failed.
*/
public void testTransformerException() throws Exception {
try {
startGrids(1);
Ignite ignite = ignite(0);
IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
final CountDownLatch latch = new CountDownLatch(10);
ContinuousQueryWithTransformer<Integer, Integer, Integer> qry = new ContinuousQueryWithTransformer<>();
qry.setLocalListener(new EventListener<Integer>() {
/**
*/
@LoggerResource
private IgniteLogger log;
@Override
public void onUpdated(Iterable<? extends Integer> evts) throws CacheEntryListenerException {
for (Integer evt : evts) {
log.debug("" + evt);
}
}
});
qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Integer>, Integer>() {
@Override
public Integer apply(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
latch.countDown();
throw new RuntimeException("Test error.");
}
}));
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Integer>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
return true;
}
}));
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
for (int i = 0; i < 10; i++) cache.put(i, i);
assertTrue(latch.await(10, SECONDS));
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.cache.query.ContinuousQueryWithTransformer in project ignite by apache.
the class CacheContinuousWithTransformerReplicatedSelfTest method testTransformerReturnNull.
/**
* @throws Exception If failed.
*/
public void testTransformerReturnNull() throws Exception {
Ignite ignite = gridToRunQuery();
IgniteCache<Integer, Employee> cache = ignite.cache(DEFAULT_CACHE_NAME);
ContinuousQueryWithTransformer<Integer, Employee, String> qry = new ContinuousQueryWithTransformer<>();
final AtomicInteger cnt = new AtomicInteger(0);
qry.setLocalListener(new EventListener() {
@Override
public void onUpdated(Iterable events) throws CacheEntryListenerException {
for (Object e : events) {
assertNull(e);
cnt.incrementAndGet();
}
}
});
qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Employee>, String>() {
@Override
public String apply(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
return null;
}
}));
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheEntryEventSerializableFilter<Integer, Employee>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Employee> evt) {
return true;
}
}));
try (QueryCursor<Cache.Entry<Integer, Employee>> ignored = cache.query(qry)) {
for (int i = 0; i < 10; i++) cache.put(i, new Employee(JOHN_CONNOR, i));
boolean evtsReceived = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return cnt.get() == 10;
}
}, 20_000);
assertTrue(evtsReceived);
}
}
Aggregations