Search in sources :

Example 21 with ComputeTask

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

the class GridP2PUndeploySelfTest method processTestUndeployP2PTasks.

/**
 * @param depMode deployment mode.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void processTestUndeployP2PTasks(DeploymentMode depMode) throws Exception {
    try {
        this.depMode = depMode;
        Ignite ignite1 = startGrid(1);
        Ignite ignite2 = startGrid(2);
        ClassLoader ldr = new URLClassLoader(new URL[] { new URL(GridTestProperties.getProperty("p2p.uri.cls")) }, GridP2PSameClassLoaderSelfTest.class.getClassLoader());
        Class<? extends ComputeTask<?, ?>> task1 = (Class<? extends ComputeTask<?, ?>>) ldr.loadClass(TEST_TASK_NAME);
        ignite1.compute().localDeployTask(task1, ldr);
        ignite1.compute().execute(task1.getName(), ignite2.cluster().localNode().id());
        LocalDeploymentSpi spi1 = spis.get(ignite1.name());
        LocalDeploymentSpi spi2 = spis.get(ignite2.name());
        assert spi1.findResource(task1.getName()) != null;
        assert ignite1.compute().localTasks().containsKey(task1.getName());
        // P2P deployment will not deploy task into the SPI.
        assert spi2.findResource(task1.getName()) == null;
        ignite1.compute().undeployTask(task1.getName());
        // Wait for undeploy.
        Thread.sleep(1000);
        assert spi1.findResource(task1.getName()) == null;
        assert spi2.findResource(task1.getName()) == null;
        assert !ignite1.compute().localTasks().containsKey(task1.getName());
        assert !ignite2.compute().localTasks().containsKey(task1.getName());
        spis = null;
    } finally {
        stopGrid(2);
        stopGrid(1);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) URLClassLoader(java.net.URLClassLoader) Ignite(org.apache.ignite.Ignite) LocalDeploymentSpi(org.apache.ignite.spi.deployment.local.LocalDeploymentSpi) URL(java.net.URL)

Example 22 with ComputeTask

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

the class GridP2PUndeploySelfTest method processTestUndeployLocalTasks.

/**
 * @param depMode deployment mode.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void processTestUndeployLocalTasks(DeploymentMode depMode) throws Exception {
    try {
        this.depMode = depMode;
        Ignite ignite1 = startGrid(1);
        Ignite ignite2 = startGrid(2);
        ClassLoader tstClsLdr = new GridTestClassLoader(GridP2PTestTask.class.getName(), GridP2PTestJob.class.getName());
        Class<? extends ComputeTask<?, ?>> task1 = (Class<? extends ComputeTask<?, ?>>) tstClsLdr.loadClass(GridP2PTestTask.class.getName());
        ignite1.compute().localDeployTask(task1, tstClsLdr);
        ignite1.compute().execute(task1.getName(), 1);
        ignite2.compute().localDeployTask(task1, tstClsLdr);
        ignite2.compute().execute(task1.getName(), 2);
        LocalDeploymentSpi spi1 = spis.get(ignite1.name());
        LocalDeploymentSpi spi2 = spis.get(ignite2.name());
        assert spi1.findResource(task1.getName()) != null;
        assert spi2.findResource(task1.getName()) != null;
        assert ignite1.compute().localTasks().containsKey(task1.getName());
        assert ignite2.compute().localTasks().containsKey(task1.getName());
        ignite2.compute().undeployTask(task1.getName());
        // Wait for undeploy.
        Thread.sleep(1000);
        assert spi1.findResource(task1.getName()) == null;
        assert spi2.findResource(task1.getName()) == null;
        assert !ignite1.compute().localTasks().containsKey(task1.getName());
        assert !ignite2.compute().localTasks().containsKey(task1.getName());
    } 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) URLClassLoader(java.net.URLClassLoader) Ignite(org.apache.ignite.Ignite) LocalDeploymentSpi(org.apache.ignite.spi.deployment.local.LocalDeploymentSpi)

Example 23 with ComputeTask

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

the class GridUriDeploymentFileProcessor method processWithDescriptorFile.

/**
 * Processes given GAR file and returns back all tasks which are in
 * descriptor.
 *
 * @param doc GAR file descriptor.
 * @param file GAR file.
 * @param uri GAR file deployment URI.
 * @param log Logger.
 * @throws org.apache.ignite.spi.IgniteSpiException Thrown if it's impossible to open file.
 * @return List of tasks from descriptor.
 */
@SuppressWarnings({ "ClassLoader2Instantiation" })
private static GridUriDeploymentFileProcessorResult processWithDescriptorFile(GridUriDeploymentSpringDocument doc, File file, String uri, IgniteLogger log) throws IgniteSpiException {
    ClassLoader clsLdr = GridUriDeploymentClassLoaderFactory.create(U.gridClassLoader(), file, log);
    List<Class<? extends ComputeTask<?, ?>>> tasks = doc.getTasks(clsLdr);
    List<Class<? extends ComputeTask<?, ?>>> validTasks = null;
    if (!F.isEmpty(tasks)) {
        validTasks = new ArrayList<>();
        for (Class<? extends ComputeTask<?, ?>> task : tasks) {
            if (!isAllowedTaskClass(task)) {
                U.warn(log, "Failed to load task. Task should be public none-abstract class " + "(might be inner static one) that implements ComputeTask interface [taskCls=" + task + ']');
            } else {
                if (log.isDebugEnabled())
                    log.debug("Found grid deployment task: " + task.getName());
                validTasks.add(task);
            }
        }
    }
    GridUriDeploymentFileProcessorResult res = new GridUriDeploymentFileProcessorResult();
    res.setFile(file);
    res.setClassLoader(clsLdr);
    if (!F.isEmpty(validTasks))
        res.setTaskClasses(validTasks);
    else if (log.isDebugEnabled())
        log.debug("No tasks loaded from file [file=" + file.getAbsolutePath() + ", uri=" + U.hidePassword(uri) + ']');
    return res;
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) URLClassLoader(java.net.URLClassLoader)

Example 24 with ComputeTask

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

the class GridifySpringAspect method invoke.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * {@inheritDoc}
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "CatchGenericClass", "unchecked" })
@Override
public Object invoke(MethodInvocation invoc) throws Throwable {
    Method mtd = invoc.getMethod();
    Gridify ann = mtd.getAnnotation(Gridify.class);
    assert ann != null : "Intercepted method does not have gridify annotation.";
    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended Ignite instance name.
    // NOTE: the 'ann.igniteInstanceName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String igniteInstanceName = F.isEmpty(ann.igniteInstanceName()) ? ann.gridName() : ann.igniteInstanceName();
    if (F.isEmpty(igniteInstanceName))
        igniteInstanceName = null;
    if (G.state(igniteInstanceName) != STARTED)
        throw new IgniteCheckedException("Grid is not locally started: " + igniteInstanceName);
    // Initialize defaults.
    GridifyArgument arg = new GridifyArgumentAdapter(mtd.getDeclaringClass(), mtd.getName(), mtd.getParameterTypes(), invoc.getArguments(), invoc.getThis());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return invoc.proceed();
    }
    if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty())
        throw new IgniteCheckedException("Gridify annotation must specify either Gridify.taskName() or " + "Gridify.taskClass(), but not both: " + ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        if (!ann.taskClass().equals(GridifyDefaultTask.class))
            return ignite.compute().withTimeout(ann.timeout()).execute((Class<? extends ComputeTask<GridifyArgument, Object>>) ann.taskClass(), arg);
        // If task name was not specified.
        if (ann.taskName().isEmpty())
            return ignite.compute().withTimeout(ann.timeout()).execute(new GridifyDefaultTask(invoc.getMethod().getDeclaringClass()), arg);
        // If task name was specified.
        return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
    } catch (Exception e) {
        for (Class<?> ex : invoc.getMethod().getExceptionTypes()) {
            // Descend all levels down.
            Throwable cause = e.getCause();
            while (cause != null) {
                if (ex.isAssignableFrom(cause.getClass()))
                    throw cause;
                cause = cause.getCause();
            }
            if (ex.isAssignableFrom(e.getClass()))
                throw e;
        }
        throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) Method(java.lang.reflect.Method) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Gridify(org.apache.ignite.compute.gridify.Gridify) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) GridifyArgument(org.apache.ignite.compute.gridify.GridifyArgument) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyDefaultTask(org.apache.ignite.compute.gridify.aop.GridifyDefaultTask) GridifyArgumentAdapter(org.apache.ignite.compute.gridify.aop.GridifyArgumentAdapter) Ignite(org.apache.ignite.Ignite) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException)

Example 25 with ComputeTask

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

the class GridTaskProcessor method startTask.

/**
 * @param taskName Task name.
 * @param taskCls Task class.
 * @param task Task.
 * @param sesId Task session ID.
 * @param arg Optional task argument.
 * @param sys If {@code true}, then system pool will be used.
 * @param execName Name of the custom executor.
 * @return Task future.
 */
@SuppressWarnings("unchecked")
private <T, R> ComputeTaskInternalFuture<R> startTask(@Nullable String taskName, @Nullable Class<?> taskCls, @Nullable ComputeTask<T, R> task, IgniteUuid sesId, @Nullable T arg, boolean sys, @Nullable String execName) {
    assert sesId != null;
    String taskClsName;
    if (task != null)
        taskClsName = task.getClass().getName();
    else
        taskClsName = taskCls != null ? taskCls.getName() : taskName;
    // Get values from thread-local context.
    Map<GridTaskThreadContextKey, Object> map = thCtx.get();
    if (map == null)
        map = EMPTY_ENUM_MAP;
    else
        // Reset thread-local context.
        thCtx.set(null);
    if (map.get(TC_SKIP_AUTH) == null)
        ctx.security().authorize(taskClsName, SecurityPermission.TASK_EXECUTE, null);
    Long timeout = (Long) map.get(TC_TIMEOUT);
    long timeout0 = timeout == null || timeout == 0 ? Long.MAX_VALUE : timeout;
    long startTime = U.currentTimeMillis();
    long endTime = timeout0 + startTime;
    // Account for overflow.
    if (endTime < 0)
        endTime = Long.MAX_VALUE;
    IgniteCheckedException deployEx = null;
    GridDeployment dep = null;
    // User provided task name.
    if (taskName != null) {
        assert taskCls == null;
        assert task == null;
        try {
            dep = ctx.deploy().getDeployment(taskName);
            if (dep == null)
                throw new IgniteDeploymentCheckedException("Unknown task name or failed to auto-deploy " + "task (was task (re|un)deployed?): " + taskName);
            taskCls = dep.deployedClass(taskName);
            if (taskCls == null)
                throw new IgniteDeploymentCheckedException("Unknown task name or failed to auto-deploy " + "task (was task (re|un)deployed?) [taskName=" + taskName + ", dep=" + dep + ']');
            if (!ComputeTask.class.isAssignableFrom(taskCls))
                throw new IgniteCheckedException("Failed to auto-deploy task (deployed class is not a task) " + "[taskName=" + taskName + ", depCls=" + taskCls + ']');
        } catch (IgniteCheckedException e) {
            deployEx = e;
        }
    } else // Deploy user task class.
    if (taskCls != null) {
        assert task == null;
        try {
            // Implicit deploy.
            dep = ctx.deploy().deploy(taskCls, U.detectClassLoader(taskCls));
            if (dep == null)
                throw new IgniteDeploymentCheckedException("Failed to auto-deploy task " + "(was task (re|un)deployed?): " + taskCls);
            taskName = taskName(dep, taskCls, map);
        } catch (IgniteCheckedException e) {
            taskName = taskCls.getName();
            deployEx = e;
        }
    } else // Deploy user task.
    if (task != null) {
        try {
            ClassLoader ldr;
            Class<?> cls;
            if (task instanceof GridPeerDeployAware) {
                GridPeerDeployAware depAware = (GridPeerDeployAware) task;
                cls = depAware.deployClass();
                ldr = depAware.classLoader();
                // Set proper class name to make peer-loading possible.
                taskCls = cls;
            } else {
                taskCls = task.getClass();
                assert ComputeTask.class.isAssignableFrom(taskCls);
                cls = task.getClass();
                ldr = U.detectClassLoader(cls);
            }
            // Explicit deploy.
            dep = ctx.deploy().deploy(cls, ldr);
            if (dep == null)
                throw new IgniteDeploymentCheckedException("Failed to auto-deploy task " + "(was task (re|un)deployed?): " + cls);
            taskName = taskName(dep, taskCls, map);
        } catch (IgniteCheckedException e) {
            taskName = task.getClass().getName();
            deployEx = e;
        }
    }
    assert taskName != null;
    if (log.isDebugEnabled())
        log.debug("Task deployment: " + dep);
    boolean fullSup = dep != null && taskCls != null && dep.annotation(taskCls, ComputeTaskSessionFullSupport.class) != null;
    Collection<UUID> top = null;
    final IgnitePredicate<ClusterNode> topPred = (IgnitePredicate<ClusterNode>) map.get(TC_SUBGRID_PREDICATE);
    if (topPred == null) {
        final Collection<ClusterNode> nodes = (Collection<ClusterNode>) map.get(TC_SUBGRID);
        top = nodes != null ? F.nodeIds(nodes) : null;
    }
    UUID subjId = getThreadContext(TC_SUBJ_ID);
    if (subjId == null)
        subjId = ctx.localNodeId();
    boolean internal = false;
    if (dep == null || taskCls == null)
        assert deployEx != null;
    else
        internal = dep.internalTask(task, taskCls);
    // Creates task session with task name and task version.
    GridTaskSessionImpl ses = ctx.session().createTaskSession(sesId, ctx.localNodeId(), taskName, dep, taskCls == null ? null : taskCls.getName(), top, topPred, startTime, endTime, Collections.<ComputeJobSibling>emptyList(), Collections.emptyMap(), fullSup, internal, subjId, execName);
    ComputeTaskInternalFuture<R> fut = new ComputeTaskInternalFuture<>(ses, ctx);
    IgniteCheckedException securityEx = null;
    if (ctx.security().enabled() && deployEx == null && !dep.internalTask(task, taskCls)) {
        try {
            saveTaskMetadata(taskName);
        } catch (IgniteCheckedException e) {
            securityEx = e;
        }
    }
    if (deployEx == null && securityEx == null) {
        if (dep == null || !dep.acquire())
            handleException(new IgniteDeploymentCheckedException("Task not deployed: " + ses.getTaskName()), fut);
        else {
            GridTaskWorker<?, ?> taskWorker = new GridTaskWorker<>(ctx, arg, ses, fut, taskCls, task, dep, new TaskEventListener(), map, subjId);
            GridTaskWorker<?, ?> taskWorker0 = tasks.putIfAbsent(sesId, taskWorker);
            assert taskWorker0 == null : "Session ID is not unique: " + sesId;
            if (!ctx.clientDisconnected()) {
                if (dep.annotation(taskCls, ComputeTaskMapAsync.class) != null) {
                    try {
                        // Start task execution in another thread.
                        if (sys)
                            ctx.getSystemExecutorService().execute(taskWorker);
                        else
                            ctx.getExecutorService().execute(taskWorker);
                    } catch (RejectedExecutionException e) {
                        tasks.remove(sesId);
                        release(dep);
                        handleException(new ComputeExecutionRejectedException("Failed to execute task " + "due to thread pool execution rejection: " + taskName, e), fut);
                    }
                } else
                    taskWorker.run();
            } else
                taskWorker.finishTask(null, disconnectedError(null));
        }
    } else {
        if (deployEx != null)
            handleException(deployEx, fut);
        else
            handleException(securityEx, fut);
    }
    return fut;
}
Also used : ComputeTaskMapAsync(org.apache.ignite.compute.ComputeTaskMapAsync) ComputeTask(org.apache.ignite.compute.ComputeTask) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridTaskSessionImpl(org.apache.ignite.internal.GridTaskSessionImpl) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteDeploymentCheckedException(org.apache.ignite.internal.IgniteDeploymentCheckedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) GridDeployment(org.apache.ignite.internal.managers.deployment.GridDeployment) ComputeTaskInternalFuture(org.apache.ignite.internal.ComputeTaskInternalFuture) Collection(java.util.Collection) ComputeExecutionRejectedException(org.apache.ignite.compute.ComputeExecutionRejectedException)

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