Search in sources :

Example 1 with GridJobSiblingsRequest

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

the class GridJobProcessor method requestJobSiblings.

/**
     * @param ses Session.
     * @return Siblings.
     * @throws IgniteCheckedException If failed.
     */
public Collection<ComputeJobSibling> requestJobSiblings(final ComputeTaskSession ses) throws IgniteCheckedException {
    assert ses != null;
    final UUID taskNodeId = ses.getTaskNodeId();
    ClusterNode taskNode = ctx.discovery().node(taskNodeId);
    if (taskNode == null)
        throw new IgniteCheckedException("Node that originated task execution has left grid: " + taskNodeId);
    // Tuple: error message-response.
    final IgniteBiTuple<String, GridJobSiblingsResponse> t = new IgniteBiTuple<>();
    final Lock lock = new ReentrantLock();
    final Condition cond = lock.newCondition();
    GridMessageListener msgLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            String err = null;
            GridJobSiblingsResponse res = null;
            if (!(msg instanceof GridJobSiblingsResponse))
                err = "Received unexpected message: " + msg;
            else if (!nodeId.equals(taskNodeId))
                err = "Received job siblings response from unexpected node [taskNodeId=" + taskNodeId + ", nodeId=" + nodeId + ']';
            else {
                // Sender and message type are fine.
                res = (GridJobSiblingsResponse) msg;
                if (res.jobSiblings() == null) {
                    try {
                        res.unmarshalSiblings(marsh);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to unmarshal job siblings.", e);
                        err = e.getMessage();
                    }
                }
            }
            lock.lock();
            try {
                if (t.isEmpty()) {
                    t.set(err, res);
                    cond.signalAll();
                }
            } finally {
                lock.unlock();
            }
        }
    };
    GridLocalEventListener discoLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent && (evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT) : "Unexpected event: " + evt;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            if (taskNodeId.equals(discoEvt.eventNode().id())) {
                lock.lock();
                try {
                    if (t.isEmpty()) {
                        t.set("Node that originated task execution has left grid: " + taskNodeId, null);
                        cond.signalAll();
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    };
    boolean loc = ctx.localNodeId().equals(taskNodeId);
    // 1. Create unique topic name.
    Object topic = TOPIC_JOB_SIBLINGS.topic(ses.getId(), topicIdGen.getAndIncrement());
    try {
        // 2. Register listener.
        ctx.io().addMessageListener(topic, msgLsnr);
        // 3. Send message.
        ctx.io().sendToGridTopic(taskNode, TOPIC_JOB_SIBLINGS, new GridJobSiblingsRequest(ses.getId(), loc ? topic : null, loc ? null : U.marshal(marsh, topic)), SYSTEM_POOL);
        // 4. Listen to discovery events.
        ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        // 5. Check whether node has left before disco listener has been installed.
        taskNode = ctx.discovery().node(taskNodeId);
        if (taskNode == null)
            throw new IgniteCheckedException("Node that originated task execution has left grid: " + taskNodeId);
        // 6. Wait for result.
        lock.lock();
        try {
            long netTimeout = ctx.config().getNetworkTimeout();
            if (t.isEmpty())
                cond.await(netTimeout, MILLISECONDS);
            if (t.isEmpty())
                throw new IgniteCheckedException("Timed out waiting for job siblings (consider increasing" + "'networkTimeout' configuration property) [ses=" + ses + ", netTimeout=" + netTimeout + ']');
            // Error is set?
            if (t.get1() != null)
                throw new IgniteCheckedException(t.get1());
            else
                // Return result
                return t.get2().jobSiblings();
        } catch (InterruptedException e) {
            throw new IgniteCheckedException("Interrupted while waiting for job siblings response: " + ses, e);
        } finally {
            lock.unlock();
        }
    } finally {
        ctx.io().removeMessageListener(topic, msgLsnr);
        ctx.event().removeLocalEventListener(discoLsnr);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridJobSiblingsResponse(org.apache.ignite.internal.GridJobSiblingsResponse) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridSpinReadWriteLock(org.apache.ignite.internal.util.GridSpinReadWriteLock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) GridJobSiblingsRequest(org.apache.ignite.internal.GridJobSiblingsRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)1 Condition (java.util.concurrent.locks.Condition)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 Event (org.apache.ignite.events.Event)1 JobEvent (org.apache.ignite.events.JobEvent)1 TaskEvent (org.apache.ignite.events.TaskEvent)1 GridJobSiblingsRequest (org.apache.ignite.internal.GridJobSiblingsRequest)1 GridJobSiblingsResponse (org.apache.ignite.internal.GridJobSiblingsResponse)1 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)1 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)1 GridSpinReadWriteLock (org.apache.ignite.internal.util.GridSpinReadWriteLock)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1