Search in sources :

Example 1 with GridJobSiblingImpl

use of org.apache.ignite.internal.GridJobSiblingImpl 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 2 with GridJobSiblingImpl

use of org.apache.ignite.internal.GridJobSiblingImpl in project ignite by apache.

the class GridTaskProcessor method sendSessionAttributes.

/**
     * This method will make the best attempt to send attributes to all jobs.
     *
     * @param attrs Deserialized session attributes.
     * @param ses Task session.
     * @throws IgniteCheckedException If send to any of the jobs failed.
     */
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter", "BusyWait" })
private void sendSessionAttributes(Map<?, ?> attrs, GridTaskSessionImpl ses) throws IgniteCheckedException {
    assert attrs != null;
    assert ses != null;
    Collection<ComputeJobSibling> siblings = ses.getJobSiblings();
    GridIoManager commMgr = ctx.io();
    long timeout = ses.getEndTime() - U.currentTimeMillis();
    if (timeout <= 0) {
        U.warn(log, "Session attributes won't be set due to task timeout: " + attrs);
        return;
    }
    Set<UUID> rcvrs = new HashSet<>();
    UUID locNodeId = ctx.localNodeId();
    synchronized (ses) {
        if (ses.isClosed()) {
            if (log.isDebugEnabled())
                log.debug("Setting session attributes on closed session (will ignore): " + ses);
            return;
        }
        ses.setInternal(attrs);
        // ID will be associated with a certain session state.
        for (ComputeJobSibling s : siblings) {
            GridJobSiblingImpl sib = (GridJobSiblingImpl) s;
            UUID nodeId = sib.nodeId();
            if (!nodeId.equals(locNodeId) && !sib.isJobDone() && !rcvrs.contains(nodeId))
                rcvrs.add(nodeId);
        }
    }
    if (ctx.event().isRecordable(EVT_TASK_SESSION_ATTR_SET)) {
        Event evt = new TaskEvent(ctx.discovery().localNode(), "Changed attributes: " + attrs, EVT_TASK_SESSION_ATTR_SET, ses.getId(), ses.getTaskName(), ses.getTaskClassName(), false, null);
        ctx.event().record(evt);
    }
    IgniteCheckedException ex = null;
    // Every job gets an individual message to keep track of ghost requests.
    for (ComputeJobSibling s : ses.getJobSiblings()) {
        GridJobSiblingImpl sib = (GridJobSiblingImpl) s;
        UUID nodeId = sib.nodeId();
        // Pair can be null if job is finished.
        if (rcvrs.remove(nodeId)) {
            ClusterNode node = ctx.discovery().node(nodeId);
            // Check that node didn't change (it could happen in case of failover).
            if (node != null) {
                boolean loc = node.id().equals(ctx.localNodeId()) && !ctx.config().isMarshalLocalJobs();
                GridTaskSessionRequest req = new GridTaskSessionRequest(ses.getId(), null, loc ? null : U.marshal(marsh, attrs), attrs);
                // should be preserved here.
                try {
                    commMgr.sendOrderedMessage(node, sib.jobTopic(), req, SYSTEM_POOL, timeout, false);
                } catch (IgniteCheckedException e) {
                    node = ctx.discovery().node(nodeId);
                    if (node != null) {
                        try {
                            // Since communication on remote node may stop before
                            // we get discovery notification, we give ourselves the
                            // best effort to detect it.
                            Thread.sleep(DISCO_TIMEOUT);
                        } catch (InterruptedException ignore) {
                            U.warn(log, "Got interrupted while sending session attributes.");
                        }
                        node = ctx.discovery().node(nodeId);
                    }
                    String err = "Failed to send session attribute request message to node " + "(normal case if node left grid) [node=" + node + ", req=" + req + ']';
                    if (node != null)
                        U.warn(log, err);
                    else if (log.isDebugEnabled())
                        log.debug(err);
                    if (ex == null)
                        ex = e;
                }
            }
        }
    }
    if (ex != null)
        throw ex;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridJobSiblingImpl(org.apache.ignite.internal.GridJobSiblingImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) GridTaskSessionRequest(org.apache.ignite.internal.GridTaskSessionRequest) TaskEvent(org.apache.ignite.events.TaskEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) ComputeJobSibling(org.apache.ignite.compute.ComputeJobSibling) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 ComputeJobSibling (org.apache.ignite.compute.ComputeJobSibling)2 GridJobSiblingImpl (org.apache.ignite.internal.GridJobSiblingImpl)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ComputeJob (org.apache.ignite.compute.ComputeJob)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 Event (org.apache.ignite.events.Event)1 TaskEvent (org.apache.ignite.events.TaskEvent)1 GridJobResultImpl (org.apache.ignite.internal.GridJobResultImpl)1 GridTaskSessionRequest (org.apache.ignite.internal.GridTaskSessionRequest)1 GridIoManager (org.apache.ignite.internal.managers.communication.GridIoManager)1 IgniteUuid (org.apache.ignite.lang.IgniteUuid)1