use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridLocalEventListenerSelfTest method getConfiguration.
/** {@inheritDoc} */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
int idx = getTestIgniteInstanceIndex(igniteInstanceName);
if (idx == 0) {
Map<IgnitePredicate<? extends Event>, int[]> lsnrs = new HashMap<>();
lsnrs.put(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
fired.countDown();
return true;
}
}, new int[] { EventType.EVT_NODE_JOINED });
cfg.setLocalEventListeners(lsnrs);
}
return cfg;
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteClientReconnectAbstractTest method reconnectClientNodes.
/**
* Reconnect client node.
*
* @param log Logger.
* @param clients Clients.
* @param srv Server.
* @param disconnectedC Closure which will be run when client node disconnected.
* @throws Exception If failed.
*/
protected static void reconnectClientNodes(final IgniteLogger log, List<Ignite> clients, Ignite srv, @Nullable Runnable disconnectedC) throws Exception {
final TestTcpDiscoverySpi srvSpi = spi(srv);
final CountDownLatch disconnectLatch = new CountDownLatch(clients.size());
final CountDownLatch reconnectLatch = new CountDownLatch(clients.size());
log.info("Block reconnect.");
for (Ignite client : clients) spi(client).writeLatch = new CountDownLatch(1);
IgnitePredicate<Event> p = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
log.info("Disconnected: " + evt);
disconnectLatch.countDown();
} else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
log.info("Reconnected: " + evt);
reconnectLatch.countDown();
}
return true;
}
};
for (Ignite client : clients) client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
for (Ignite client : clients) srvSpi.failNode(client.cluster().localNode().id(), null);
waitReconnectEvent(log, disconnectLatch);
if (disconnectedC != null)
disconnectedC.run();
log.info("Allow reconnect.");
for (Ignite client : clients) spi(client).writeLatch.countDown();
waitReconnectEvent(log, reconnectLatch);
for (Ignite client : clients) client.events().stopLocalListen(p);
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridTestUtils method assertOneToOne.
/**
* Asserts that each element in iterable has one-to-one correspondence with a
* predicate from list.
*
* @param it Input iterable of elements.
* @param ps Array of predicates (by number of elements in iterable).
*/
@SuppressWarnings("ConstantConditions")
public static <T> void assertOneToOne(Iterable<T> it, IgnitePredicate<T>... ps) {
Collection<IgnitePredicate<T>> ps0 = new ArrayList<>(Arrays.asList(ps));
Collection<T2<IgnitePredicate<T>, T>> passed = new ArrayList<>();
for (T elem : it) {
for (T2<IgnitePredicate<T>, T> p : passed) {
if (p.get1().apply(elem))
throw new AssertionError("Two elements match one predicate [elem1=" + p.get2() + ", elem2=" + elem + ", pred=" + p.get1() + ']');
}
IgnitePredicate<T> matched = null;
for (IgnitePredicate<T> p : ps0) {
if (p.apply(elem)) {
if (matched != null)
throw new AssertionError("Element matches more than one predicate [elem=" + elem + ", pred1=" + p + ", pred2=" + matched + ']');
matched = p;
}
}
if (// None matched.
matched == null)
throw new AssertionError("The element does not match [elem=" + elem + ", numRemainingPreds=" + ps0.size() + ']');
ps0.remove(matched);
passed.add(new T2<>(matched, elem));
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridJobStealingSelfTest method testProjectionPredicateDifferentClassLoaders.
/**
* Tests job stealing with peer deployment and different class loaders.
*
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
public void testProjectionPredicateDifferentClassLoaders() throws Exception {
final Ignite ignite3 = startGrid(3);
URL[] clsLdrUrls;
try {
clsLdrUrls = new URL[] { new URL(GridTestProperties.getProperty("p2p.uri.cls")) };
} catch (MalformedURLException e) {
throw new RuntimeException("Define property p2p.uri.cls", e);
}
ClassLoader ldr1 = new URLClassLoader(clsLdrUrls, getClass().getClassLoader());
Class taskCls = ldr1.loadClass("org.apache.ignite.tests.p2p.JobStealingTask");
Class nodeFilterCls = ldr1.loadClass("org.apache.ignite.tests.p2p.ExcludeNodeFilter");
IgnitePredicate<ClusterNode> nodeFilter = (IgnitePredicate<ClusterNode>) nodeFilterCls.getConstructor(UUID.class).newInstance(ignite2.cluster().localNode().id());
Map<UUID, Integer> ret = (Map<UUID, Integer>) executeAsync(compute(ignite1.cluster().forPredicate(nodeFilter)), taskCls, null).get(TASK_EXEC_TIMEOUT_MS);
assert ret != null;
assert ret.get(ignite1.cluster().localNode().id()) != null && ret.get(ignite1.cluster().localNode().id()) == 2 : ret.get(ignite1.cluster().localNode().id());
assert ret.get(ignite3.cluster().localNode().id()) != null && ret.get(ignite3.cluster().localNode().id()) == 2 : ret.get(ignite3.cluster().localNode().id());
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testEvents.
/**
* @throws Exception If failed.
*/
public void testEvents() throws Exception {
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(50);
final CountDownLatch execLatch = new CountDownLatch(cacheMode() == REPLICATED ? 1 : gridCount());
IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryReadEvent;
CacheQueryReadEvent qe = (CacheQueryReadEvent) evt;
assertEquals(CONTINUOUS.name(), qe.queryType());
assertEquals(DEFAULT_CACHE_NAME, qe.cacheName());
assertEquals(grid(0).localNode().id(), qe.subjectId());
assertNull(qe.className());
assertNull(qe.clause());
assertNull(qe.scanQueryFilter());
assertNotNull(qe.continuousQueryFilter());
assertNull(qe.arguments());
cnt.incrementAndGet();
latch.countDown();
return true;
}
};
IgnitePredicate<Event> execLsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt instanceof CacheQueryExecutedEvent;
CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
assertEquals(CONTINUOUS.name(), qe.queryType());
assertEquals(DEFAULT_CACHE_NAME, qe.cacheName());
assertEquals(grid(0).localNode().id(), qe.subjectId());
assertNull(qe.className());
assertNull(qe.clause());
assertNull(qe.scanQueryFilter());
assertNotNull(qe.continuousQueryFilter());
assertNull(qe.arguments());
execLatch.countDown();
return true;
}
};
try {
for (int i = 0; i < gridCount(); i++) {
grid(i).events().localListen(lsnr, EVT_CACHE_QUERY_OBJECT_READ);
grid(i).events().localListen(execLsnr, EVT_CACHE_QUERY_EXECUTED);
}
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
// No-op.
}
});
qry.setRemoteFilter(new CacheEntryEventSerializableFilter<Integer, Integer>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends Integer> evt) {
return evt.getValue() >= 50;
}
});
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
for (int i = 0; i < 100; i++) cache.put(i, i);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assert execLatch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(50, cnt.get());
}
} finally {
for (int i = 0; i < gridCount(); i++) {
grid(i).events().stopLocalListen(lsnr, EVT_CACHE_QUERY_OBJECT_READ);
grid(i).events().stopLocalListen(execLsnr, EVT_CACHE_QUERY_EXECUTED);
}
}
}
Aggregations