use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteSinkTest method testSink.
/**
* @throws Exception {@link Exception}.
*/
public void testSink() throws Exception {
IgniteConfiguration cfg = loadConfiguration("modules/flume/src/test/resources/example-ignite.xml");
cfg.setClientMode(false);
final Ignite grid = startGrid("igniteServerNode", cfg);
Context channelContext = new Context();
channelContext.put("capacity", String.valueOf(EVENT_CNT));
channelContext.put("transactionCapacity", String.valueOf(EVENT_CNT));
Channel memoryChannel = new MemoryChannel();
Configurables.configure(memoryChannel, channelContext);
final CountDownLatch latch = new CountDownLatch(EVENT_CNT);
final IgnitePredicate<Event> putLsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt != null;
latch.countDown();
return true;
}
};
IgniteSink sink = new IgniteSink() {
// Setting the listener on cache before sink processing starts.
@Override
public synchronized void start() {
super.start();
grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(putLsnr, EVT_CACHE_OBJECT_PUT);
}
};
sink.setName("IgniteSink");
sink.setChannel(memoryChannel);
Context ctx = new Context();
ctx.put(IgniteSinkConstants.CFG_CACHE_NAME, CACHE_NAME);
ctx.put(IgniteSinkConstants.CFG_PATH, "example-ignite.xml");
ctx.put(IgniteSinkConstants.CFG_EVENT_TRANSFORMER, "org.apache.ignite.stream.flume.TestEventTransformer");
Configurables.configure(sink, ctx);
sink.start();
try {
Transaction tx = memoryChannel.getTransaction();
tx.begin();
for (int i = 0; i < EVENT_CNT; i++) memoryChannel.put(EventBuilder.withBody((String.valueOf(i) + ": " + i).getBytes()));
tx.commit();
tx.close();
Sink.Status status = Sink.Status.READY;
while (status != Sink.Status.BACKOFF) {
status = sink.process();
}
} finally {
sink.stop();
}
// Checks that 10000 events successfully processed in 10 seconds.
assertTrue(latch.await(10, TimeUnit.SECONDS));
grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(putLsnr);
IgniteCache<String, Integer> cache = grid.cache(CACHE_NAME);
// Checks that each event was processed properly.
for (int i = 0; i < EVENT_CNT; i++) {
assertEquals(i, (int) cache.get(String.valueOf(i)));
}
assertEquals(EVENT_CNT, cache.size(CachePeekMode.PRIMARY));
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class CacheUtils method foreach.
/**
* @param cacheName Cache name.
* @param fun An operation that accepts a cache entry and processes it.
* @param keyFilter Cache keys filter.
* @param <K> Cache key object type.
* @param <V> Cache value object type.
*/
protected static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
int partsCnt = ignite.affinity(cacheName).partitions();
// Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
Affinity affinity = ignite.affinity(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
// Iterate over all partitions. Some of them will be stored on that local node.
for (int part = 0; part < partsCnt; part++) {
int p = part;
// Query returns an empty cursor if this partition is not stored on this node.
for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) fun.accept(new CacheEntry<>(entry, cache));
}
});
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridEventConsumeSelfTest method testNodeJoinWithP2P.
/**
* TODO: IGNITE-585.
*
* @throws Exception If failed.
*/
public void testNodeJoinWithP2P() throws Exception {
fail("https://issues.apache.org/jira/browse/IGNITE-585");
final Collection<UUID> nodeIds = new HashSet<>();
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(GRID_CNT + 1);
ClassLoader ldr = getExternalClassLoader();
IgnitePredicate<ClusterNode> prjPred = (IgnitePredicate<ClusterNode>) ldr.loadClass(PRJ_PRED_CLS_NAME).newInstance();
IgnitePredicate<Event> filter = (IgnitePredicate<Event>) ldr.loadClass(FILTER_CLS_NAME).newInstance();
UUID consumeId = events(grid(0).cluster().forPredicate(prjPred)).remoteListen(new P2<UUID, Event>() {
@Override
public boolean apply(UUID nodeId, Event evt) {
info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
assertEquals(EVT_JOB_STARTED, evt.type());
nodeIds.add(nodeId);
cnt.incrementAndGet();
latch.countDown();
return true;
}
}, filter, EVT_JOB_STARTED);
try {
assertNotNull(consumeId);
startGrid("anotherGrid");
grid(0).compute().broadcast(F.noop());
assert latch.await(2, SECONDS);
assertEquals(GRID_CNT + 1, nodeIds.size());
assertEquals(GRID_CNT + 1, cnt.get());
} finally {
stopGrid("anotherGrid");
grid(0).events().stopRemoteListen(consumeId);
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteCacheAbstractQuerySelfTest method testFieldsQueryEvents.
/**
* @throws Exception If failed.
*/
public void testFieldsQueryEvents() throws Exception {
final IgniteCache<UUID, Person> cache = jcache(UUID.class, Person.class);
final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
for (int i = 0; i < gridCount(); i++) {
IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryExecutedEvent;
if (evtsDisabled)
fail("Cache events are disabled");
CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
assertEquals(cache.getName(), qe.cacheName());
assertNotNull(qe.clause());
assertNull(qe.scanQueryFilter());
assertNull(qe.continuousQueryFilter());
assertArrayEquals(new Integer[] { 10 }, qe.arguments());
execLatch.countDown();
return true;
}
};
grid(i).events().localListen(pred, EVT_CACHE_QUERY_EXECUTED);
qryExecLsnrs[i] = pred;
}
try {
for (int i = 1; i <= 20; i++) cache.put(UUID.randomUUID(), new Person("Person " + i, i));
QueryCursor<List<?>> q = cache.query(new SqlFieldsQuery("select _key, name from Person where salary > ?").setArgs(10));
q.getAll();
assert execLatch.await(1000, MILLISECONDS);
} finally {
for (int i = 0; i < gridCount(); i++) grid(i).events().stopLocalListen(qryExecLsnrs[i]);
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteCacheAbstractQuerySelfTest method testScanQueryEvents.
/**
* @throws Exception If failed.
*/
public void testScanQueryEvents() throws Exception {
final Map<Integer, Integer> map = new ConcurrentHashMap<>();
final IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
final CountDownLatch latch = new CountDownLatch(evtsDisabled ? 0 : 10);
final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
IgnitePredicate[] objReadLsnrs = new IgnitePredicate[gridCount()];
IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
for (int i = 0; i < gridCount(); i++) {
IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryReadEvent;
if (evtsDisabled)
fail("Cache events are disabled");
CacheQueryReadEvent<Integer, Integer> qe = (CacheQueryReadEvent<Integer, Integer>) evt;
assertEquals(SCAN.name(), qe.queryType());
assertEquals(cache.getName(), qe.cacheName());
assertNull(qe.className());
assertNull(null, qe.clause());
assertNotNull(qe.scanQueryFilter());
assertNull(qe.continuousQueryFilter());
assertNull(qe.arguments());
map.put(qe.key(), qe.value());
latch.countDown();
return true;
}
};
grid(i).events().localListen(pred, EVT_CACHE_QUERY_OBJECT_READ);
objReadLsnrs[i] = pred;
IgnitePredicate<Event> execPred = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryExecutedEvent;
if (evtsDisabled)
fail("Cache events are disabled");
CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
assertEquals(SCAN.name(), qe.queryType());
assertEquals(cache.getName(), qe.cacheName());
assertNull(qe.className());
assertNull(null, qe.clause());
assertNotNull(qe.scanQueryFilter());
assertNull(qe.continuousQueryFilter());
assertNull(qe.arguments());
execLatch.countDown();
return true;
}
};
grid(i).events().localListen(execPred, EVT_CACHE_QUERY_EXECUTED);
qryExecLsnrs[i] = execPred;
}
try {
for (int i = 0; i < 20; i++) cache.put(i, i);
IgniteBiPredicate<Integer, Integer> filter = new IgniteBiPredicate<Integer, Integer>() {
@Override
public boolean apply(Integer k, Integer v) {
return k >= 10;
}
};
QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new ScanQuery<>(filter));
q.getAll();
assert latch.await(1000, MILLISECONDS);
assert execLatch.await(1000, MILLISECONDS);
if (!evtsDisabled) {
assertEquals(10, map.size());
for (int i = 10; i < 20; i++) assertEquals(i, map.get(i).intValue());
}
} finally {
for (int i = 0; i < gridCount(); i++) {
grid(i).events().stopLocalListen(objReadLsnrs[i]);
grid(i).events().stopLocalListen(qryExecLsnrs[i]);
}
}
}
Aggregations