use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class GridTaskWorker method body.
/**
* Maps this task's jobs to nodes and sends them out.
*/
@SuppressWarnings({ "unchecked" })
@Override
protected void body() {
evtLsnr.onTaskStarted(this);
try {
// Use either user task or deployed one.
if (task == null) {
assert taskCls != null;
assert ComputeTask.class.isAssignableFrom(taskCls);
try {
task = newTask((Class<? extends ComputeTask<T, R>>) taskCls);
} catch (IgniteCheckedException e) {
// If cannot instantiate task, then assign internal flag based
// on information available.
internal = dep.internalTask(null, taskCls);
recordTaskEvent(EVT_TASK_STARTED, "Task started.");
throw e;
}
}
internal = ses.isInternal();
recordTaskEvent(EVT_TASK_STARTED, "Task started.");
initializeSpis();
ses.setClassLoader(dep.classLoader());
// Nodes are ignored by affinity tasks.
final List<ClusterNode> shuffledNodes = affCacheIds == null ? getTaskTopology() : Collections.<ClusterNode>emptyList();
// Load balancer.
ComputeLoadBalancer balancer = ctx.loadBalancing().getLoadBalancer(ses, shuffledNodes);
continuous = ctx.resource().isAnnotationPresent(dep, task, TaskContinuousMapperResource.class);
if (log.isDebugEnabled())
log.debug("Injected task resources [continuous=" + continuous + ']');
// Inject resources.
ctx.resource().inject(dep, task, ses, balancer, mapper);
Map<? extends ComputeJob, ClusterNode> mappedJobs = U.wrapThreadLoader(dep.classLoader(), new Callable<Map<? extends ComputeJob, ClusterNode>>() {
@Override
public Map<? extends ComputeJob, ClusterNode> call() {
return task.map(shuffledNodes, arg);
}
});
if (log.isDebugEnabled())
log.debug("Mapped task jobs to nodes [jobCnt=" + (mappedJobs != null ? mappedJobs.size() : 0) + ", mappedJobs=" + mappedJobs + ", ses=" + ses + ']');
if (F.isEmpty(mappedJobs)) {
synchronized (mux) {
// Check if some jobs are sent from continuous mapper.
if (F.isEmpty(jobRes))
throw new IgniteCheckedException("Task map operation produced no mapped jobs: " + ses);
}
} else
processMappedJobs(mappedJobs);
synchronized (mux) {
lockRespProc = false;
}
processDelayedResponses();
} catch (ClusterGroupEmptyCheckedException e) {
U.warn(log, "Failed to map task jobs to nodes (topology projection is empty): " + ses);
finishTask(null, e);
} catch (IgniteException | IgniteCheckedException e) {
if (!fut.isCancelled()) {
if (!(e instanceof VisorClusterGroupEmptyException))
U.error(log, "Failed to map task jobs to nodes: " + ses, e);
finishTask(null, e);
} else if (log.isDebugEnabled())
log.debug("Failed to map task jobs to nodes due to task cancellation: " + ses);
}// Catch throwable to protect against bad user code.
catch (Throwable e) {
String errMsg = "Failed to map task jobs to nodes due to undeclared user exception" + " [cause=" + e.getMessage() + ", ses=" + ses + "]";
U.error(log, errMsg, e);
finishTask(null, new ComputeUserUndeclaredException(errMsg, e));
if (e instanceof Error)
throw e;
}
}
use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class GridTaskWorker method processMappedJobs.
/**
* @param jobs Map of jobs.
* @throws IgniteCheckedException Thrown in case of any error.
*/
private void processMappedJobs(Map<? extends ComputeJob, ClusterNode> jobs) throws IgniteCheckedException {
if (F.isEmpty(jobs))
return;
Collection<GridJobResultImpl> jobResList = new ArrayList<>(jobs.size());
Collection<ComputeJobSibling> sibs = new ArrayList<>(jobs.size());
// Map jobs to nodes for computation.
for (Map.Entry<? extends ComputeJob, ClusterNode> mappedJob : jobs.entrySet()) {
ComputeJob job = mappedJob.getKey();
ClusterNode node = mappedJob.getValue();
if (job == null)
throw new IgniteCheckedException("Job can not be null [mappedJob=" + mappedJob + ", ses=" + ses + ']');
if (node == null)
throw new IgniteCheckedException("Node can not be null [mappedJob=" + mappedJob + ", ses=" + ses + ']');
IgniteUuid jobId = IgniteUuid.fromUuid(ctx.localNodeId());
GridJobSiblingImpl sib = new GridJobSiblingImpl(ses.getId(), jobId, node.id(), ctx);
jobResList.add(new GridJobResultImpl(job, jobId, node, sib));
// Do not add siblings if result cache is disabled.
if (resCache)
sibs.add(sib);
recordJobEvent(EVT_JOB_MAPPED, jobId, node, "Job got mapped.");
}
synchronized (mux) {
if (state != State.WAITING)
throw new IgniteCheckedException("Task is not in waiting state [state=" + state + ", ses=" + ses + ']');
// Do not add siblings if result cache is disabled.
if (resCache)
ses.addJobSiblings(sibs);
if (jobRes == null)
jobRes = new HashMap<>();
// getting results while still sending out references.
for (GridJobResultImpl res : jobResList) {
if (jobRes.put(res.getJobContext().getJobId(), res) != null)
throw new IgniteCheckedException("Duplicate job ID for remote job found: " + res.getJobContext().getJobId());
res.setOccupied(true);
if (resCache && jobRes.size() > ctx.discovery().size() && jobRes.size() % SPLIT_WARN_THRESHOLD == 0)
LT.warn(log, "Number of jobs in task is too large for task: " + ses.getTaskName() + ". Consider reducing number of jobs or disabling job result cache with " + "@ComputeTaskNoResultCache annotation.");
}
}
// Set mapped flag.
ses.onMapped();
// Send out all remote mappedJobs.
for (GridJobResultImpl res : jobResList) {
evtLsnr.onJobSend(this, res.getSibling());
try {
sendRequest(res);
} finally {
// Open job for processing results.
synchronized (mux) {
res.setOccupied(false);
}
}
}
processDelayedResponses();
}
use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class VisorMultiNodeTask method map0.
/**
* Actual map logic.
*
* @param arg Task execution argument.
* @param subgrid Nodes available for this task execution.
* @return Map of grid jobs assigned to subgrid node.
* @throws IgniteException If mapping could not complete successfully.
*/
protected Map<? extends ComputeJob, ClusterNode> map0(List<ClusterNode> subgrid, VisorTaskArgument<A> arg) {
Collection<UUID> nodeIds = arg.getNodes();
Map<ComputeJob, ClusterNode> map = U.newHashMap(nodeIds.size());
try {
for (ClusterNode node : subgrid) if (nodeIds.contains(node.id()))
map.put(job(taskArg), node);
return map;
} finally {
if (debug)
logMapped(ignite.log(), getClass(), map.values());
}
}
use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class GridTaskJobRejectSelfTest method testReject.
/**
* @throws Exception If failed.
*/
public void testReject() throws Exception {
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
X.println("Task event: " + evt);
return true;
}
}, EVTS_TASK_EXECUTION);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
X.println("Job event: " + evt);
return true;
}
}, EVTS_JOB_EXECUTION);
final CountDownLatch startedLatch = new CountDownLatch(1);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
startedLatch.countDown();
return true;
}
}, EVT_JOB_STARTED);
final AtomicInteger failedOver = new AtomicInteger(0);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
failedOver.incrementAndGet();
return true;
}
}, EVT_JOB_FAILED_OVER);
final CountDownLatch finishedLatch = new CountDownLatch(1);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
finishedLatch.countDown();
return true;
}
}, EVT_TASK_FINISHED, EVT_TASK_FAILED);
final ClusterNode node = grid(1).localNode();
ComputeTaskFuture<?> fut = grid(1).compute().executeAsync(new ComputeTaskAdapter<Void, Void>() {
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable Void arg) {
return F.asMap(new SleepJob(), node, new SleepJob(), node);
}
/** {@inheritDoc} */
@Nullable
@Override
public Void reduce(List<ComputeJobResult> results) {
return null;
}
}, null);
assert startedLatch.await(2, SECONDS);
fut.cancel();
assert finishedLatch.await(2, SECONDS);
assert failedOver.get() == 0;
}
use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class GridP2PClassLoadingSelfTest method testClassLoading.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "serial", "ConstantConditions" })
public void testClassLoading() throws Exception {
ComputeTask<?, ?> task = (ComputeTask<?, ?>) tstClsLdr.loadClass(GridP2PTestTask.class.getName()).newInstance();
byte[] rawTask = GridTestIoUtils.serializeJdk(task);
ComputeTask<Object, Integer> res = GridTestIoUtils.deserializeJdk(rawTask, tstClsLdr);
ClusterNode fakeNode = new TestGridNode();
List<ClusterNode> nodes = Collections.singletonList(fakeNode);
ComputeJob p2pJob = res.map(nodes, 1).entrySet().iterator().next().getKey();
assert p2pJob.getClass().getClassLoader() instanceof GridTestClassLoader : "Class loader = " + res.getClass().getClassLoader();
}
Aggregations