Search in sources :

Example 1 with ComputeTask

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

the class DeploymentExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Deployment example started.");
        // This task will be deployed on local node and then peer-loaded
        // onto remote nodes on demand. For this example this task is
        // available on the classpath, however in real life that may not
        // always be the case. In those cases you should use explicit
        // 'IgniteCompute.localDeployTask(Class, ClassLoader) apply and
        // then use 'IgniteCompute.execute(String, Object)' method
        // passing your task name as first parameter.
        ignite.compute().localDeployTask(ExampleTask.class, ExampleTask.class.getClassLoader());
        for (Map.Entry<String, Class<? extends ComputeTask<?, ?>>> e : ignite.compute().localTasks().entrySet()) System.out.println(">>> Found locally deployed task [alias=" + e.getKey() + ", taskCls=" + e.getValue());
        // Execute the task passing its name as a parameter. The system will find
        // the deployed task by its name and execute it.
        ignite.compute().execute(TASK_NAME, null);
        // Execute the task passing class name as a parameter. The system will find
        // the deployed task by its class name and execute it.
        // g.compute().execute(ExampleTask.class.getName(), null).get();
        // Undeploy task
        ignite.compute().undeployTask(TASK_NAME);
        System.out.println();
        System.out.println(">>> Finished executing Ignite Direct Deployment Example.");
        System.out.println(">>> Check participating nodes output.");
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) Ignite(org.apache.ignite.Ignite) Map(java.util.Map)

Example 2 with ComputeTask

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

the class GridDeploymentLocalStore method recordDeployFailed.

/**
     * Records deploy event.
     *
     * @param cls Deployed class.
     * @param clsLdr Class loader.
     * @param recordEvt Flag indicating whether to record events.
     */
@SuppressWarnings({ "unchecked" })
private void recordDeployFailed(Class<?> cls, ClassLoader clsLdr, boolean recordEvt) {
    assert cls != null;
    assert clsLdr != null;
    boolean isTask = isTask(cls);
    String msg = "Failed to deploy " + (isTask ? "task" : "class") + " [cls=" + cls + ", clsLdr=" + clsLdr + ']';
    if (recordEvt && ctx.event().isRecordable(isTask ? EVT_CLASS_DEPLOY_FAILED : EVT_TASK_DEPLOY_FAILED)) {
        String taskName = isTask ? U.getTaskName((Class<? extends ComputeTask<?, ?>>) cls) : null;
        DeploymentEvent evt = new DeploymentEvent();
        evt.message(msg);
        evt.node(ctx.discovery().localNode());
        evt.type(isTask(cls) ? EVT_CLASS_DEPLOY_FAILED : EVT_TASK_DEPLOY_FAILED);
        evt.alias(taskName);
        ctx.event().record(evt);
    }
    if (log.isInfoEnabled())
        log.info(msg);
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) DeploymentEvent(org.apache.ignite.events.DeploymentEvent)

Example 3 with ComputeTask

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

the class GridTaskWorker method body.

/**
     * Maps this task's jobs to nodes and sends them out.
     */
@SuppressWarnings({ "unchecked" })
@Override
protected void body() {
    evtLsnr.onTaskStarted(this);
    try {
        // Use either user task or deployed one.
        if (task == null) {
            assert taskCls != null;
            assert ComputeTask.class.isAssignableFrom(taskCls);
            try {
                task = newTask((Class<? extends ComputeTask<T, R>>) taskCls);
            } catch (IgniteCheckedException e) {
                // If cannot instantiate task, then assign internal flag based
                // on information available.
                internal = dep.internalTask(null, taskCls);
                recordTaskEvent(EVT_TASK_STARTED, "Task started.");
                throw e;
            }
        }
        internal = ses.isInternal();
        recordTaskEvent(EVT_TASK_STARTED, "Task started.");
        initializeSpis();
        ses.setClassLoader(dep.classLoader());
        // Nodes are ignored by affinity tasks.
        final List<ClusterNode> shuffledNodes = affCacheIds == null ? getTaskTopology() : Collections.<ClusterNode>emptyList();
        // Load balancer.
        ComputeLoadBalancer balancer = ctx.loadBalancing().getLoadBalancer(ses, shuffledNodes);
        continuous = ctx.resource().isAnnotationPresent(dep, task, TaskContinuousMapperResource.class);
        if (log.isDebugEnabled())
            log.debug("Injected task resources [continuous=" + continuous + ']');
        // Inject resources.
        ctx.resource().inject(dep, task, ses, balancer, mapper);
        Map<? extends ComputeJob, ClusterNode> mappedJobs = U.wrapThreadLoader(dep.classLoader(), new Callable<Map<? extends ComputeJob, ClusterNode>>() {

            @Override
            public Map<? extends ComputeJob, ClusterNode> call() {
                return task.map(shuffledNodes, arg);
            }
        });
        if (log.isDebugEnabled())
            log.debug("Mapped task jobs to nodes [jobCnt=" + (mappedJobs != null ? mappedJobs.size() : 0) + ", mappedJobs=" + mappedJobs + ", ses=" + ses + ']');
        if (F.isEmpty(mappedJobs)) {
            synchronized (mux) {
                // Check if some jobs are sent from continuous mapper.
                if (F.isEmpty(jobRes))
                    throw new IgniteCheckedException("Task map operation produced no mapped jobs: " + ses);
            }
        } else
            processMappedJobs(mappedJobs);
        synchronized (mux) {
            lockRespProc = false;
        }
        processDelayedResponses();
    } catch (ClusterGroupEmptyCheckedException e) {
        U.warn(log, "Failed to map task jobs to nodes (topology projection is empty): " + ses);
        finishTask(null, e);
    } catch (IgniteException | IgniteCheckedException e) {
        if (!fut.isCancelled()) {
            if (!(e instanceof VisorClusterGroupEmptyException))
                U.error(log, "Failed to map task jobs to nodes: " + ses, e);
            finishTask(null, e);
        } else if (log.isDebugEnabled())
            log.debug("Failed to map task jobs to nodes due to task cancellation: " + ses);
    }// Catch throwable to protect against bad user code.
     catch (Throwable e) {
        String errMsg = "Failed to map task jobs to nodes due to undeclared user exception" + " [cause=" + e.getMessage() + ", ses=" + ses + "]";
        U.error(log, errMsg, e);
        finishTask(null, new ComputeUserUndeclaredException(errMsg, e));
        if (e instanceof Error)
            throw e;
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeTask(org.apache.ignite.compute.ComputeTask) ComputeUserUndeclaredException(org.apache.ignite.compute.ComputeUserUndeclaredException) TaskContinuousMapperResource(org.apache.ignite.resources.TaskContinuousMapperResource) ComputeJob(org.apache.ignite.compute.ComputeJob) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) ComputeLoadBalancer(org.apache.ignite.compute.ComputeLoadBalancer) VisorClusterGroupEmptyException(org.apache.ignite.internal.visor.util.VisorClusterGroupEmptyException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with ComputeTask

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

the class GridP2PClassLoadingSelfTest method testClassLoading.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "serial", "ConstantConditions" })
public void testClassLoading() throws Exception {
    ComputeTask<?, ?> task = (ComputeTask<?, ?>) tstClsLdr.loadClass(GridP2PTestTask.class.getName()).newInstance();
    byte[] rawTask = GridTestIoUtils.serializeJdk(task);
    ComputeTask<Object, Integer> res = GridTestIoUtils.deserializeJdk(rawTask, tstClsLdr);
    ClusterNode fakeNode = new TestGridNode();
    List<ClusterNode> nodes = Collections.singletonList(fakeNode);
    ComputeJob p2pJob = res.map(nodes, 1).entrySet().iterator().next().getKey();
    assert p2pJob.getClass().getClassLoader() instanceof GridTestClassLoader : "Class loader = " + res.getClass().getClassLoader();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader)

Example 5 with ComputeTask

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

the class GridP2PDoubleDeploymentSelfTest method processTestBothNodesDeploy.

/**
     * @param depMode deployment mode.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private void processTestBothNodesDeploy(DeploymentMode depMode) throws Exception {
    try {
        this.depMode = depMode;
        Ignite ignite1 = startGrid(1);
        Ignite ignite2 = startGrid(2);
        ClassLoader ldr = new GridTestClassLoader(Collections.singletonMap("org/apache/ignite/p2p/p2p.properties", "resource=loaded"), GridP2PTestTask.class.getName(), GridP2PTestJob.class.getName());
        Class<? extends ComputeTask<?, ?>> taskCls = (Class<? extends ComputeTask<?, ?>>) ldr.loadClass(GridP2PTestTask.class.getName());
        ignite1.compute().localDeployTask(taskCls, ldr);
        Integer res1 = (Integer) ignite1.compute().execute(taskCls.getName(), 1);
        ignite1.compute().undeployTask(taskCls.getName());
        // Wait here 1 sec before the deployment as we have async undeploy.
        Thread.sleep(1000);
        ignite1.compute().localDeployTask(taskCls, ldr);
        ignite2.compute().localDeployTask(taskCls, ldr);
        Integer res2 = (Integer) ignite2.compute().execute(taskCls.getName(), 2);
        info("Checking results...");
        assert res1 == 10 : "Invalid res1 value: " + res1;
        assert res2 == 20 : "Invalid res1 value: " + res1;
        info("Tests passed.");
    } finally {
        stopGrid(2);
        stopGrid(1);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) Ignite(org.apache.ignite.Ignite)

Aggregations

ComputeTask (org.apache.ignite.compute.ComputeTask)25 Ignite (org.apache.ignite.Ignite)17 GridTestClassLoader (org.apache.ignite.testframework.GridTestClassLoader)14 URLClassLoader (java.net.URLClassLoader)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 ComputeJob (org.apache.ignite.compute.ComputeJob)2 ComputeTaskFuture (org.apache.ignite.compute.ComputeTaskFuture)2 LocalDeploymentSpi (org.apache.ignite.spi.deployment.local.LocalDeploymentSpi)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1