use of org.apache.ignite.compute.ComputeTaskSession in project ignite by apache.
the class SingleSplitTestTask method map.
/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, Integer arg) {
assert !subgrid.isEmpty() : "Subgrid cannot be empty.";
Map<ComputeJobAdapter, ClusterNode> jobs = new HashMap<>(subgrid.size());
taskSes.setAttribute("1st", "1");
taskSes.setAttribute("2nd", "2");
Collection<UUID> assigned = new ArrayList<>(subgrid.size());
for (int i = 0; i < arg; i++) {
ComputeJobAdapter job = new ComputeJobAdapter(1) {
/** */
@TaskSessionResource
private ComputeTaskSession jobSes;
/** {@inheritDoc} */
@Override
public Object execute() {
assert jobSes != null;
Integer arg = this.<Integer>argument(0);
assert arg != null;
return new SingleSplitTestJobTarget().executeLoadTestJob(arg, jobSes);
}
};
ClusterNode node = balancer.getBalancedNode(job, null);
assert node != null;
assigned.add(node.id());
jobs.put(job, node);
}
taskSes.setAttribute("nodes", assigned);
return jobs;
}
use of org.apache.ignite.compute.ComputeTaskSession 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.compute.ComputeTaskSession 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.compute.ComputeTaskSession in project ignite by apache.
the class ComputeTaskInternalFuture method finishedFuture.
/**
* @param ctx Context.
* @param taskCls Task class.
* @param e Error.
* @return Finished task future.
*/
public static <R> ComputeTaskInternalFuture<R> finishedFuture(final GridKernalContext ctx, final Class<?> taskCls, IgniteCheckedException e) {
assert ctx != null;
assert taskCls != null;
assert e != null;
final long time = U.currentTimeMillis();
final IgniteUuid id = IgniteUuid.fromUuid(ctx.localNodeId());
ComputeTaskSession ses = new ComputeTaskSession() {
@Override
public String getTaskName() {
return taskCls.getName();
}
@Override
public UUID getTaskNodeId() {
return ctx.localNodeId();
}
@Override
public long getStartTime() {
return time;
}
@Override
public long getEndTime() {
return time;
}
@Override
public IgniteUuid getId() {
return id;
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public Collection<ComputeJobSibling> getJobSiblings() throws IgniteException {
return Collections.emptyList();
}
@Override
public Collection<ComputeJobSibling> refreshJobSiblings() throws IgniteException {
return Collections.emptyList();
}
@Nullable
@Override
public ComputeJobSibling getJobSibling(IgniteUuid jobId) throws IgniteException {
return null;
}
@Override
public void setAttribute(Object key, @Nullable Object val) throws IgniteException {
}
@Nullable
@Override
public <K, V> V getAttribute(K key) {
return null;
}
@Override
public void setAttributes(Map<?, ?> attrs) throws IgniteException {
// No-op.
}
@Override
public Map<?, ?> getAttributes() {
return Collections.emptyMap();
}
@Override
public void addAttributeListener(ComputeTaskSessionAttributeListener lsnr, boolean rewind) {
// No-op.
}
@Override
public boolean removeAttributeListener(ComputeTaskSessionAttributeListener lsnr) {
return false;
}
@Override
public <K, V> V waitForAttribute(K key, long timeout) throws InterruptedException {
throw new InterruptedException("Session was closed.");
}
@Override
public <K, V> boolean waitForAttribute(K key, @Nullable V val, long timeout) throws InterruptedException {
throw new InterruptedException("Session was closed.");
}
@Override
public Map<?, ?> waitForAttributes(Collection<?> keys, long timeout) throws InterruptedException {
throw new InterruptedException("Session was closed.");
}
@Override
public boolean waitForAttributes(Map<?, ?> attrs, long timeout) throws InterruptedException {
throw new InterruptedException("Session was closed.");
}
@Override
public void saveCheckpoint(String key, Object state) {
throw new IgniteException("Session was closed.");
}
@Override
public void saveCheckpoint(String key, Object state, ComputeTaskSessionScope scope, long timeout) {
throw new IgniteException("Session was closed.");
}
@Override
public void saveCheckpoint(String key, Object state, ComputeTaskSessionScope scope, long timeout, boolean overwrite) {
throw new IgniteException("Session was closed.");
}
@Nullable
@Override
public <T> T loadCheckpoint(String key) throws IgniteException {
throw new IgniteException("Session was closed.");
}
@Override
public boolean removeCheckpoint(String key) throws IgniteException {
throw new IgniteException("Session was closed.");
}
@Override
public Collection<UUID> getTopology() {
return Collections.emptyList();
}
@Override
public IgniteFuture<?> mapFuture() {
return new IgniteFinishedFutureImpl<Object>();
}
};
ComputeTaskInternalFuture<R> fut = new ComputeTaskInternalFuture<>(ses, ctx);
fut.onDone(e);
return fut;
}
use of org.apache.ignite.compute.ComputeTaskSession in project ignite by apache.
the class PriorityQueueCollisionSpi method getJobPriority.
/**
* Gets job priority. At first tries to get from job context. If job context has no priority,
* then tries to get from task session. If task session has no priority default one will be used.
*
* @param ctx Collision job context.
* @return Job priority.
*/
private int getJobPriority(CollisionJobContext ctx) {
assert ctx != null;
Integer p = null;
ComputeJobContext jctx = ctx.getJobContext();
try {
p = (Integer) jctx.getAttribute(jobPriAttrKey);
} catch (ClassCastException e) {
LT.error(log, e, "Type of job context priority attribute '" + jobPriAttrKey + "' is not java.lang.Integer [type=" + jctx.getAttribute(jobPriAttrKey).getClass() + ']');
}
if (p == null) {
ComputeTaskSession ses = ctx.getTaskSession();
try {
p = (Integer) ses.getAttribute(taskPriAttrKey);
} catch (ClassCastException e) {
LT.error(log, e, "Type of task session priority attribute '" + taskPriAttrKey + "' is not java.lang.Integer [type=" + ses.getAttribute(taskPriAttrKey).getClass() + ']');
}
if (p == null) {
if (log.isDebugEnabled()) {
log.debug("Failed get priority from job context attribute '" + jobPriAttrKey + "' and task session attribute '" + taskPriAttrKey + "' (will use default priority): " + dfltPri);
}
p = dfltPri;
}
}
assert p != null;
return p;
}
Aggregations