Search in sources :

Example 21 with TaskEvent

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

the class VisorManagementEventSelfTest method doTestNotManagementVisorTask.

/**
 * @param cls class of the task.
 * @param arg argument.
 * @param ignite instance of Ignite.
 *
 * @throws Exception If failed.
 */
private <T, R> void doTestNotManagementVisorTask(Class<? extends ComputeTask<VisorTaskArgument<T>, R>> cls, T arg, IgniteEx ignite) throws Exception {
    final AtomicReference<TaskEvent> evt = new AtomicReference<>();
    final CountDownLatch evtLatch = new CountDownLatch(1);
    ignite.events().localListen(new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent e) {
            evt.set(e);
            evtLatch.countDown();
            return false;
        }
    }, EventType.EVT_MANAGEMENT_TASK_STARTED);
    for (ClusterNode node : ignite.cluster().forServers().nodes()) ignite.compute().executeAsync(cls, new VisorTaskArgument<>(node.id(), arg, true));
    assertFalse(evtLatch.await(10000, TimeUnit.MILLISECONDS));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) TaskEvent(org.apache.ignite.events.TaskEvent) VisorTaskArgument(org.apache.ignite.internal.visor.VisorTaskArgument) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with TaskEvent

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

the class VisorManagementEventSelfTest method doTestVisorTask.

/**
 * @param cls class of the task.
 * @param arg argument.
 * @param ignite instance of Ignite.
 *
 * @throws Exception If failed.
 */
private <T, R> void doTestVisorTask(Class<? extends ComputeTask<VisorTaskArgument<T>, R>> cls, T arg, IgniteEx ignite) throws Exception {
    final AtomicReference<TaskEvent> evt = new AtomicReference<>();
    final CountDownLatch evtLatch = new CountDownLatch(1);
    ignite.events().localListen(new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent e) {
            evt.set(e);
            evtLatch.countDown();
            return false;
        }
    }, EventType.EVT_MANAGEMENT_TASK_STARTED);
    for (ClusterNode node : ignite.cluster().forServers().nodes()) ignite.compute().executeAsync(cls, new VisorTaskArgument<>(node.id(), arg, true));
    assertTrue(evtLatch.await(10000, TimeUnit.MILLISECONDS));
    assertNotNull(evt.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) TaskEvent(org.apache.ignite.events.TaskEvent) VisorTaskArgument(org.apache.ignite.internal.visor.VisorTaskArgument) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 23 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)23 Event (org.apache.ignite.events.Event)9 UUID (java.util.UUID)8 IgniteException (org.apache.ignite.IgniteException)8 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Ignite (org.apache.ignite.Ignite)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)6 JobEvent (org.apache.ignite.events.JobEvent)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)6 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 Ignition (org.apache.ignite.Ignition)4 EVTS_TASK_EXECUTION (org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION)3 ExampleNodeStartup (org.apache.ignite.examples.ExampleNodeStartup)3 VisorTaskArgument (org.apache.ignite.internal.visor.VisorTaskArgument)3 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 TaskSessionResource (org.apache.ignite.resources.TaskSessionResource)3 ArrayList (java.util.ArrayList)2