Search in sources :

Example 11 with ComputeJobResult

use of org.apache.ignite.compute.ComputeJobResult in project ignite by apache.

the class VisorNodeGcTask method reduce0.

/** {@inheritDoc} */
@Nullable
@Override
protected Map<UUID, VisorNodeGcTaskResult> reduce0(List<ComputeJobResult> results) {
    Map<UUID, VisorNodeGcTaskResult> total = new HashMap<>();
    for (ComputeJobResult res : results) {
        VisorNodeGcTaskResult jobRes = res.getData();
        total.put(res.getNode().id(), jobRes);
    }
    return total;
}
Also used : HashMap(java.util.HashMap) UUID(java.util.UUID) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with ComputeJobResult

use of org.apache.ignite.compute.ComputeJobResult in project ignite by apache.

the class VisorNodeSuppressedErrorsTask method reduce0.

/** {@inheritDoc} */
@Nullable
@Override
protected Map<UUID, VisorNodeSuppressedErrors> reduce0(List<ComputeJobResult> results) {
    Map<UUID, VisorNodeSuppressedErrors> taskRes = new HashMap<>(results.size());
    for (ComputeJobResult res : results) {
        VisorNodeSuppressedErrors jobRes = res.getData();
        taskRes.put(res.getNode().id(), jobRes);
    }
    return taskRes;
}
Also used : HashMap(java.util.HashMap) UUID(java.util.UUID) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ComputeJobResult

use of org.apache.ignite.compute.ComputeJobResult in project ignite by apache.

the class VisorLogSearchTask method reduce0.

/** {@inheritDoc} */
@Nullable
@Override
protected VisorLogSearchTaskResult reduce0(List<ComputeJobResult> results) {
    List<VisorLogSearchResult> searchRes = new ArrayList<>();
    Map<Exception, UUID> exRes = U.newHashMap(0);
    // Separate successfully executed results and exceptions.
    for (ComputeJobResult result : results) {
        if (result.getException() != null)
            exRes.put(result.getException(), result.getNode().id());
        else if (result.getData() != null) {
            Collection<VisorLogSearchResult> data = result.getData();
            searchRes.addAll(data);
        }
    }
    return new VisorLogSearchTaskResult(exRes.isEmpty() ? null : exRes, searchRes.isEmpty() ? null : searchRes);
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) UUID(java.util.UUID) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with ComputeJobResult

use of org.apache.ignite.compute.ComputeJobResult in project ignite by apache.

the class GridTaskWorker method onResponse.

/**
     * @param msg Job execution response.
     */
void onResponse(GridJobExecuteResponse msg) {
    assert msg != null;
    if (fut.isDone()) {
        if (log.isDebugEnabled())
            log.debug("Ignoring job response since task has finished: " + msg);
        return;
    }
    GridJobExecuteResponse res = msg;
    while (res != null) {
        GridJobResultImpl jobRes = null;
        // Flag indicating whether occupied flag for
        // job response was changed in this method apply.
        boolean selfOccupied = false;
        IgniteInternalFuture<?> affFut = null;
        boolean waitForAffTop = false;
        final GridJobExecuteResponse failoverRes = res;
        try {
            synchronized (mux) {
                // then there is no point to proceed.
                if (state != State.WAITING) {
                    if (log.isDebugEnabled())
                        log.debug("Ignoring response since task is already reducing or finishing [res=" + res + ", job=" + ses + ", state=" + state + ']');
                    return;
                }
                jobRes = this.jobRes.get(res.getJobId());
                if (jobRes == null) {
                    if (log.isDebugEnabled())
                        U.warn(log, "Received response for unknown child job (was job presumed failed?): " + res);
                    res = delayedRess.poll();
                    // We can not return here because there can be more delayed messages in the queue.
                    continue;
                }
                // was created from discovery listener and when sending request failed.
                if (jobRes.hasResponse()) {
                    if (log.isDebugEnabled())
                        log.debug("Received redundant response for a job (will ignore): " + res);
                    res = delayedRess.poll();
                    // We can not return here because there can be more delayed messages in the queue.
                    continue;
                }
                if (!jobRes.getNode().id().equals(res.getNodeId())) {
                    if (log.isDebugEnabled())
                        log.debug("Ignoring stale response as job was already resent to other node [res=" + res + ", jobRes=" + jobRes + ']');
                    // Prevent processing 2 responses for the same job simultaneously.
                    jobRes.setOccupied(true);
                    selfOccupied = true;
                    // We can not return here because there can be more delayed messages in the queue.
                    continue;
                }
                if (jobRes.isOccupied()) {
                    if (log.isDebugEnabled())
                        log.debug("Adding response to delayed queue (job is either being sent or processing " + "another response): " + res);
                    delayedRess.offer(res);
                    return;
                }
                if (lockRespProc) {
                    delayedRess.offer(res);
                    return;
                }
                lockRespProc = true;
                selfOccupied = true;
                // Prevent processing 2 responses for the same job simultaneously.
                jobRes.setOccupied(true);
                // We don't keep reference to job if results are not cached.
                if (!resCache)
                    this.jobRes.remove(res.getJobId());
            }
            if (res.getFakeException() != null)
                jobRes.onResponse(null, res.getFakeException(), null, false);
            else {
                ClassLoader clsLdr = dep.classLoader();
                try {
                    boolean loc = ctx.localNodeId().equals(res.getNodeId()) && !ctx.config().isMarshalLocalJobs();
                    Object res0 = loc ? res.getJobResult() : U.unmarshal(marsh, res.getJobResultBytes(), U.resolveClassLoader(clsLdr, ctx.config()));
                    IgniteException ex = loc ? res.getException() : U.<IgniteException>unmarshal(marsh, res.getExceptionBytes(), U.resolveClassLoader(clsLdr, ctx.config()));
                    Map<Object, Object> attrs = loc ? res.getJobAttributes() : U.<Map<Object, Object>>unmarshal(marsh, res.getJobAttributesBytes(), U.resolveClassLoader(clsLdr, ctx.config()));
                    jobRes.onResponse(res0, ex, attrs, res.isCancelled());
                    if (loc)
                        ctx.resource().invokeAnnotated(dep, jobRes.getJob(), ComputeJobAfterSend.class);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Error deserializing job response: " + res, e);
                    finishTask(null, e);
                }
            }
            List<ComputeJobResult> results;
            if (!resCache)
                results = Collections.emptyList();
            else {
                synchronized (mux) {
                    results = getRemoteResults();
                }
            }
            ComputeJobResultPolicy plc = result(jobRes, results);
            if (plc == null) {
                String errMsg = "Failed to obtain remote job result policy for result from ComputeTask.result(..) " + "method that returned null (will fail the whole task): " + jobRes;
                finishTask(null, new IgniteCheckedException(errMsg));
                return;
            }
            boolean retry = false;
            synchronized (mux) {
                // then there is no point to proceed.
                if (state != State.WAITING) {
                    if (log.isDebugEnabled())
                        log.debug("Ignoring ComputeTask.result(..) value since task is already reducing or" + "finishing [res=" + res + ", job=" + ses + ", state=" + state + ']');
                    return;
                }
                if (res.retry()) {
                    // Retry is used only with affinity call / run.
                    assert affCacheIds != null;
                    retry = true;
                    mapTopVer = U.max(res.getRetryTopologyVersion(), ctx.discovery().topologyVersionEx());
                    affFut = ctx.cache().context().exchange().affinityReadyFuture(mapTopVer);
                    if (affFut != null && !affFut.isDone()) {
                        waitForAffTop = true;
                        jobRes.resetResponse();
                    }
                } else {
                    switch(plc) {
                        // Start reducing all results received so far.
                        case REDUCE:
                            {
                                state = State.REDUCING;
                                break;
                            }
                        // otherwise, reduce.
                        case WAIT:
                            {
                                assert results.size() <= this.jobRes.size();
                                // when both collections are empty.
                                if (results.size() == this.jobRes.size()) {
                                    plc = ComputeJobResultPolicy.REDUCE;
                                    // All results are received, proceed to reduce method.
                                    state = State.REDUCING;
                                }
                                break;
                            }
                        case FAILOVER:
                            {
                                if (affCacheIds != null) {
                                    mapTopVer = ctx.discovery().topologyVersionEx();
                                    affFut = ctx.cache().context().exchange().affinityReadyFuture(mapTopVer);
                                }
                                if (affFut != null && !affFut.isDone()) {
                                    waitForAffTop = true;
                                    jobRes.resetResponse();
                                } else if (!failover(res, jobRes, getTaskTopology()))
                                    plc = null;
                                break;
                            }
                    }
                }
            }
            // Outside of synchronization.
            if (retry && !waitForAffTop) {
                // Handle retry
                retryAttemptCnt++;
                final long wait = retryAttemptCnt * RETRY_DELAY_MS;
                sendRetryRequest(wait, jobRes, res);
            } else if (plc != null && !waitForAffTop && !retry) {
                // Handle failover.
                if (plc == FAILOVER)
                    sendFailoverRequest(jobRes);
                else {
                    evtLsnr.onJobFinished(this, jobRes.getSibling());
                    if (plc == ComputeJobResultPolicy.REDUCE)
                        reduce(results);
                }
            }
        } catch (IgniteCheckedException e) {
            U.error(log, "Failed to obtain topology [ses=" + ses + ", err=" + e + ']', e);
            finishTask(null, e);
            waitForAffTop = false;
        } finally {
            // set in this method.
            if (selfOccupied) {
                assert jobRes != null;
                synchronized (mux) {
                    jobRes.setOccupied(false);
                    lockRespProc = false;
                }
                // Process delayed responses if there are any.
                res = delayedRess.poll();
            }
        }
        if (waitForAffTop && affFut != null) {
            affFut.listen(new IgniteInClosure<IgniteInternalFuture<?>>() {

                @Override
                public void apply(IgniteInternalFuture<?> fut0) {
                    ctx.closure().runLocalSafe(new Runnable() {

                        @Override
                        public void run() {
                            onResponse(failoverRes);
                        }
                    }, false);
                }
            });
        }
    }
}
Also used : GridJobResultImpl(org.apache.ignite.internal.GridJobResultImpl) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ComputeJobResultPolicy(org.apache.ignite.compute.ComputeJobResultPolicy) GridJobExecuteResponse(org.apache.ignite.internal.GridJobExecuteResponse) IgniteException(org.apache.ignite.IgniteException) ComputeJobAfterSend(org.apache.ignite.compute.ComputeJobAfterSend) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult)

Aggregations

ComputeJobResult (org.apache.ignite.compute.ComputeJobResult)14 Nullable (org.jetbrains.annotations.Nullable)8 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 IgniteException (org.apache.ignite.IgniteException)4 HashMap (java.util.HashMap)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 GridTestJobResult (org.apache.ignite.GridTestJobResult)2 GridTestTaskSession (org.apache.ignite.GridTestTaskSession)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridFailoverTestContext (org.apache.ignite.spi.failover.GridFailoverTestContext)2 GridTestNode (org.apache.ignite.testframework.GridTestNode)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ClusterGroupEmptyException (org.apache.ignite.cluster.ClusterGroupEmptyException)1