Search in sources :

Example 16 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class EventsExample method localListen.

/**
     * Listen to events that happen only on local node.
     *
     * @throws IgniteException If failed.
     */
private static void localListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Local event listener example.");
    Ignite ignite = Ignition.ignite();
    IgnitePredicate<TaskEvent> lsnr = new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent evt) {
            System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
            // Return true to continue listening.
            return true;
        }
    };
    // Register event listener for all local task execution events.
    ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    ignite.compute().withName("example-event-task").run(new IgniteRunnable() {

        @Override
        public void run() {
            System.out.println("Executing sample job.");
        }
    });
    // Unsubscribe local task event listener.
    ignite.events().stopLocalListen(lsnr);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 17 with TaskEvent

use of org.apache.ignite.events.TaskEvent 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)

Example 18 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class GridJobProcessor method processTaskSessionRequest.

/**
     * @param nodeId Node ID.
     * @param req Request.
     */
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter", "RedundantCast" })
private void processTaskSessionRequest(UUID nodeId, GridTaskSessionRequest req) {
    if (!rwLock.tryReadLock()) {
        if (log.isDebugEnabled())
            log.debug("Received job session request while stopping grid (will ignore): " + req);
        return;
    }
    try {
        GridTaskSessionImpl ses = ctx.session().getSession(req.getSessionId());
        if (ses == null) {
            if (log.isDebugEnabled())
                log.debug("Received job session request for non-existing session: " + req);
            return;
        }
        boolean loc = ctx.localNodeId().equals(nodeId) && !ctx.config().isMarshalLocalJobs();
        Map<?, ?> attrs = loc ? req.getAttributes() : (Map<?, ?>) U.unmarshal(marsh, req.getAttributesBytes(), U.resolveClassLoader(ses.getClassLoader(), ctx.config()));
        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);
        }
        synchronized (ses) {
            ses.setInternal(attrs);
        }
    } catch (IgniteCheckedException e) {
        U.error(log, "Failed to deserialize session attributes.", e);
    } finally {
        rwLock.readUnlock();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TaskEvent(org.apache.ignite.events.TaskEvent) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridTaskSessionImpl(org.apache.ignite.internal.GridTaskSessionImpl)

Example 19 with TaskEvent

use of org.apache.ignite.events.TaskEvent in project ignite by apache.

the class AdaptiveLoadBalancingSpi method onContextInitialized0.

/** {@inheritDoc} */
@Override
protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException {
    getSpiContext().addLocalEventListener(evtLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            switch(evt.type()) {
                case EVT_TASK_FINISHED:
                case EVT_TASK_FAILED:
                    {
                        TaskEvent taskEvt = (TaskEvent) evt;
                        taskTops.remove(taskEvt.taskSessionId());
                        if (log.isDebugEnabled())
                            log.debug("Removed task topology from topology cache for session: " + taskEvt.taskSessionId());
                        break;
                    }
                case EVT_JOB_MAPPED:
                    {
                        // We should keep topology and use cache in ComputeTask#map() method to
                        // avoid O(n*n/2) complexity, after that we can drop caches.
                        // Here we set mapped property and later cache will be ignored
                        JobEvent jobEvt = (JobEvent) evt;
                        IgniteBiTuple<Boolean, WeightedTopology> weightedTop = taskTops.get(jobEvt.taskSessionId());
                        if (weightedTop != null)
                            weightedTop.set1(true);
                        if (log.isDebugEnabled())
                            log.debug("Job has been mapped. Ignore cache for session: " + jobEvt.taskSessionId());
                        break;
                    }
                case EVT_NODE_METRICS_UPDATED:
                case EVT_NODE_FAILED:
                case EVT_NODE_JOINED:
                case EVT_NODE_LEFT:
                    {
                        DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
                        rwLock.writeLock().lock();
                        try {
                            switch(evt.type()) {
                                case EVT_NODE_JOINED:
                                    {
                                        nodeJobs.put(discoEvt.eventNode().id(), new AtomicInteger(0));
                                        break;
                                    }
                                case EVT_NODE_LEFT:
                                case EVT_NODE_FAILED:
                                    {
                                        nodeJobs.remove(discoEvt.eventNode().id());
                                        break;
                                    }
                                case EVT_NODE_METRICS_UPDATED:
                                    {
                                        // Reset counter.
                                        nodeJobs.put(discoEvt.eventNode().id(), new AtomicInteger(0));
                                        break;
                                    }
                            }
                        } finally {
                            rwLock.writeLock().unlock();
                        }
                    }
            }
        }
    }, EVT_NODE_METRICS_UPDATED, EVT_NODE_FAILED, EVT_NODE_JOINED, EVT_NODE_LEFT, EVT_TASK_FINISHED, EVT_TASK_FAILED, EVT_JOB_MAPPED);
    // Put all known nodes.
    rwLock.writeLock().lock();
    try {
        for (ClusterNode node : getSpiContext().nodes()) nodeJobs.put(node.id(), new AtomicInteger(0));
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) JobEvent(org.apache.ignite.events.JobEvent) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskEvent(org.apache.ignite.events.TaskEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent)

Aggregations

TaskEvent (org.apache.ignite.events.TaskEvent)19 Event (org.apache.ignite.events.Event)7 UUID (java.util.UUID)6 Ignite (org.apache.ignite.Ignite)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 IgniteException (org.apache.ignite.IgniteException)5 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)5 JobEvent (org.apache.ignite.events.JobEvent)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)4 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)4 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 TaskSessionResource (org.apache.ignite.resources.TaskSessionResource)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 GridTestTaskSession (org.apache.ignite.GridTestTaskSession)2 Ignition (org.apache.ignite.Ignition)2 ComputeTaskTimeoutException (org.apache.ignite.compute.ComputeTaskTimeoutException)2