Search in sources :

Example 6 with ComputeJob

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;
    }
}
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)

Example 7 with ComputeJob

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();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridJobResultImpl(org.apache.ignite.internal.GridJobResultImpl) ComputeJob(org.apache.ignite.compute.ComputeJob) GridJobSiblingImpl(org.apache.ignite.internal.GridJobSiblingImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) ComputeJobSibling(org.apache.ignite.compute.ComputeJobSibling) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with ComputeJob

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());
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) UUID(java.util.UUID)

Example 9 with ComputeJob

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;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CountDownLatch(java.util.concurrent.CountDownLatch) ComputeJob(org.apache.ignite.compute.ComputeJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.apache.ignite.events.Event) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult)

Example 10 with ComputeJob

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader)

Aggregations

ComputeJob (org.apache.ignite.compute.ComputeJob)17 ClusterNode (org.apache.ignite.cluster.ClusterNode)15 HashMap (java.util.HashMap)7 UUID (java.util.UUID)5 Nullable (org.jetbrains.annotations.Nullable)5 IgniteException (org.apache.ignite.IgniteException)4 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)2 ComputeTask (org.apache.ignite.compute.ComputeTask)2 GridifyArgument (org.apache.ignite.compute.gridify.GridifyArgument)2 GridifyJobAdapter (org.apache.ignite.internal.util.gridify.GridifyJobAdapter)2 VisorClusterGroupEmptyException (org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException)2 Serializable (java.io.Serializable)1 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Ignite (org.apache.ignite.Ignite)1 IgniteFileSystem (org.apache.ignite.IgniteFileSystem)1