Search in sources :

Example 11 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 12 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 13 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)

Example 14 with ComputeTask

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

the class GridP2PHotRedeploymentSelfTest method processTestHotRedeployment.

/**
 * @param depMode deployment mode.
 * @throws Exception If failed.
 */
private void processTestHotRedeployment(DeploymentMode depMode) throws Exception {
    try {
        this.depMode = depMode;
        Ignite ignite1 = startGrid(1);
        Ignite ignite2 = startGrid(2);
        waitForDiscovery(ignite1, ignite2);
        ClassLoader ldr = getExternalClassLoader();
        Class<? extends ComputeTask<Object, Integer>> taskCls = (Class<? extends ComputeTask<Object, Integer>>) ldr.loadClass(TASK_NAME);
        Integer res1 = ignite1.compute().execute(taskCls, Collections.singletonList(ignite2.cluster().localNode().id()));
        info("Result1: " + res1);
        assert res1 != null;
        ignite1.compute().localDeployTask(taskCls, taskCls.getClassLoader());
        Integer res2 = (Integer) ignite1.compute().execute(taskCls.getName(), Collections.singletonList(ignite2.cluster().localNode().id()));
        info("Result2: " + res2);
        assert res2 != null;
        assert res1.equals(res2);
    } finally {
        stopGrid(2);
        stopGrid(1);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) Ignite(org.apache.ignite.Ignite)

Example 15 with ComputeTask

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

the class GridP2PRemoteClassLoadersSelfTest method processTestSameRemoteClassLoader.

/**
 * @param depMode deployment mode.
 * @throws Exception If failed..
 */
@SuppressWarnings("unchecked")
private void processTestSameRemoteClassLoader(DeploymentMode depMode) throws Exception {
    try {
        this.depMode = depMode;
        GridP2PTestStaticVariable.staticVar = 0;
        Ignite ignite1 = startGrid(1);
        startGrid(2);
        waitForRemoteNodes(ignite1, 1);
        ClassLoader tstClsLdr = new GridTestClassLoader(Collections.<String, String>emptyMap(), getClass().getClassLoader(), GridP2PRemoteTestTask.class.getName(), GridP2PRemoteTestTask1.class.getName(), GridP2PRemoteTestJob.class.getName());
        Class<? extends ComputeTask<?, ?>> task1 = (Class<? extends ComputeTask<?, ?>>) tstClsLdr.loadClass(GridP2PRemoteTestTask.class.getName());
        Class<? extends ComputeTask<?, ?>> task2 = (Class<? extends ComputeTask<?, ?>>) tstClsLdr.loadClass(GridP2PRemoteTestTask1.class.getName());
        Object res1 = ignite1.compute().execute(task1.newInstance(), null);
        Object res2 = ignite1.compute().execute(task2.newInstance(), null);
        info("Check results.");
        // One remote p2p class loader
        assert res1 != null : "res1 != null";
        assert res1 instanceof Long : "res1 instanceof Long != true";
        assert res1.equals(0L) : "Expected 0, got " + res1;
        // The same remote p2p class loader.
        assert res2 != null : "res2 != null";
        assert res2 instanceof Long : "res2 instanceof Long != true";
        assert res2.equals(1L) : "Expected 1 got " + res2;
        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