Search in sources :

Example 1 with ComputeLoadBalancer

use of org.apache.ignite.compute.ComputeLoadBalancer 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;
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeTask(org.apache.ignite.compute.ComputeTask) ComputeUserUndeclaredException(org.apache.ignite.compute.ComputeUserUndeclaredException) TaskContinuousMapperResource(org.apache.ignite.resources.TaskContinuousMapperResource) ComputeJob(org.apache.ignite.compute.ComputeJob) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) ComputeLoadBalancer(org.apache.ignite.compute.ComputeLoadBalancer) VisorClusterGroupEmptyException(org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ComputeJob (org.apache.ignite.compute.ComputeJob)1 ComputeLoadBalancer (org.apache.ignite.compute.ComputeLoadBalancer)1 ComputeTask (org.apache.ignite.compute.ComputeTask)1 ComputeUserUndeclaredException (org.apache.ignite.compute.ComputeUserUndeclaredException)1 ClusterGroupEmptyCheckedException (org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException)1 VisorClusterGroupEmptyException (org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException)1 TaskContinuousMapperResource (org.apache.ignite.resources.TaskContinuousMapperResource)1