use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class ComputeFibonacciContinuationExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws IgniteException If example execution failed.
*/
public static void main(String[] args) throws IgniteException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println("Compute Fibonacci continuation example started.");
long N = 100;
final UUID exampleNodeId = ignite.cluster().localNode().id();
// Filter to exclude this node from execution.
final IgnitePredicate<ClusterNode> nodeFilter = new IgnitePredicate<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
// Give preference to remote nodes.
return ignite.cluster().forRemotes().nodes().isEmpty() || !n.id().equals(exampleNodeId);
}
};
long start = System.currentTimeMillis();
BigInteger fib = ignite.compute(ignite.cluster().forPredicate(nodeFilter)).apply(new ContinuationFibonacciClosure(nodeFilter), N);
long duration = System.currentTimeMillis() - start;
System.out.println();
System.out.println(">>> Finished executing Fibonacci for '" + N + "' in " + duration + " ms.");
System.out.println(">>> Fibonacci sequence for input number '" + N + "' is '" + fib + "'.");
System.out.println(">>> If you re-run this example w/o stopping remote nodes - the performance will");
System.out.println(">>> increase since intermediate results are pre-cache on remote nodes.");
System.out.println(">>> You should see prints out every recursive Fibonacci execution on cluster nodes.");
System.out.println(">>> Check remote nodes for output.");
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class EventsExample method localListen.
/**
* Listen to events that happen only on local node.
*
* @throws IgniteException If failed.
*/
private static void localListen() throws IgniteException {
System.out.println();
System.out.println(">>> Local event listener example.");
Ignite ignite = Ignition.ignite();
IgnitePredicate<TaskEvent> lsnr = evt -> {
System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
return true;
};
// Register event listener for all local task execution events.
ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);
// Generate task events.
ignite.compute().withName("example-event-task").run(() -> System.out.println("Executing sample job."));
// Unsubscribe local task event listener.
ignite.events().stopLocalListen(lsnr);
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class EventsExample method remoteListen.
/**
* Listen to events coming from all cluster nodes.
*
* @throws IgniteException If failed.
*/
private static void remoteListen() throws IgniteException {
System.out.println();
System.out.println(">>> Remote event listener example.");
// This optional local callback is called for each event notification
// that passed remote predicate listener.
IgniteBiPredicate<UUID, TaskEvent> locLsnr = (nodeId, evt) -> {
// Remote filter only accepts tasks whose name being with "good-task" prefix.
assert evt.taskName().startsWith("good-task");
System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
// Return true to continue listening.
return true;
};
// Remote filter which only accepts tasks whose name begins with "good-task" prefix.
IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task");
Ignite ignite = Ignition.ignite();
// Register event listeners on all nodes to listen for task events.
ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
// Generate task events.
for (int i = 0; i < 10; i++) {
ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {
// Auto-inject task session.
@TaskSessionResource
private ComputeTaskSession ses;
@Override
public void run() {
System.out.println("Executing sample job for task: " + ses.getTaskName());
}
});
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class EventsExample method remoteListen.
/**
* Listen to events coming from all cluster nodes.
*
* @throws IgniteException If failed.
*/
private static void remoteListen() throws IgniteException {
System.out.println();
System.out.println(">>> Remote event listener example.");
// This optional local callback is called for each event notification
// that passed remote predicate listener.
IgniteBiPredicate<UUID, TaskEvent> locLsnr = new IgniteBiPredicate<UUID, TaskEvent>() {
@Override
public boolean apply(UUID nodeId, TaskEvent evt) {
// Remote filter only accepts tasks whose name being with "good-task" prefix.
assert evt.taskName().startsWith("good-task");
System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
// Return true to continue listening.
return true;
}
};
// Remote filter which only accepts tasks whose name begins with "good-task" prefix.
IgnitePredicate<TaskEvent> rmtLsnr = new IgnitePredicate<TaskEvent>() {
@Override
public boolean apply(TaskEvent evt) {
return evt.taskName().startsWith("good-task");
}
};
Ignite ignite = Ignition.ignite();
// Register event listeners on all nodes to listen for task events.
ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
// Generate task events.
for (int i = 0; i < 10; i++) {
ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {
// Auto-inject task session.
@TaskSessionResource
private ComputeTaskSession ses;
@Override
public void run() {
System.out.println("Executing sample job for task: " + ses.getTaskName());
}
});
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class CacheAbstractJdbcStore method loadCache.
/** {@inheritDoc} */
@Override
public void loadCache(final IgniteBiInClosure<K, V> clo, @Nullable Object... args) throws CacheLoaderException {
ExecutorService pool = null;
String cacheName = session().cacheName();
try {
pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
Collection<Future<?>> futs = new ArrayList<>();
Map<Object, EntryMapping> mappings = getOrCreateCacheMappings(cacheName);
if (args != null && args.length > 0) {
if (args.length % 2 != 0)
throw new CacheLoaderException("Expected even number of arguments, but found: " + args.length);
if (log.isDebugEnabled())
log.debug("Start loading entries from db using user queries from arguments...");
for (int i = 0; i < args.length; i += 2) {
final String keyType = args[i].toString();
if (!F.exist(mappings.values(), new IgnitePredicate<EntryMapping>() {
@Override
public boolean apply(EntryMapping em) {
return em.keyType().equals(keyType);
}
}))
throw new CacheLoaderException("Provided key type is not found in store or cache configuration " + "[cache=" + U.maskName(cacheName) + ", key=" + keyType + ']');
EntryMapping em = entryMapping(cacheName, typeIdForTypeName(kindForName(keyType), keyType));
Object arg = args[i + 1];
LoadCacheCustomQueryWorker<K, V> task;
if (arg instanceof PreparedStatement) {
PreparedStatement stmt = (PreparedStatement) arg;
if (log.isInfoEnabled())
log.info("Started load cache using custom statement [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", stmt=" + stmt + ']');
task = new LoadCacheCustomQueryWorker<>(em, stmt, clo);
} else {
String qry = arg.toString();
if (log.isInfoEnabled())
log.info("Started load cache using custom query [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", query=" + qry + ']');
task = new LoadCacheCustomQueryWorker<>(em, qry, clo);
}
futs.add(pool.submit(task));
}
} else {
Collection<String> processedKeyTypes = new HashSet<>();
for (EntryMapping em : mappings.values()) {
String keyType = em.keyType();
if (processedKeyTypes.contains(keyType))
continue;
processedKeyTypes.add(keyType);
if (log.isInfoEnabled())
log.info("Started load cache [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
if (parallelLoadCacheMinThreshold > 0) {
Connection conn = null;
try {
conn = connection();
PreparedStatement stmt = conn.prepareStatement(em.loadCacheSelRangeQry);
stmt.setInt(1, parallelLoadCacheMinThreshold);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
if (log.isDebugEnabled())
log.debug("Multithread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
int keyCnt = em.keyCols.size();
Object[] upperBound = new Object[keyCnt];
for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
futs.add(pool.submit(loadCacheRange(em, clo, null, upperBound, 0)));
while (rs.next()) {
Object[] lowerBound = upperBound;
upperBound = new Object[keyCnt];
for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
futs.add(pool.submit(loadCacheRange(em, clo, lowerBound, upperBound, 0)));
}
futs.add(pool.submit(loadCacheRange(em, clo, upperBound, null, 0)));
continue;
}
} catch (SQLException e) {
log.warning("Failed to load entries from db in multithreaded mode, will try in single thread " + "[cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']', e);
} finally {
U.closeQuiet(conn);
}
}
if (log.isDebugEnabled())
log.debug("Single thread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
futs.add(pool.submit(loadCacheFull(em, clo)));
}
}
for (Future<?> fut : futs) U.get(fut);
if (log.isInfoEnabled())
log.info("Finished load cache: " + U.maskName(cacheName));
} catch (IgniteCheckedException e) {
throw new CacheLoaderException("Failed to load cache: " + U.maskName(cacheName), e.getCause());
} finally {
U.shutdownNow(getClass(), pool, log);
}
}
Aggregations