Search in sources :

Example 1 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class GridCacheDeploymentManager method registerClass.

/**
 * @param obj Object whose class to register.
 * @throws IgniteCheckedException If failed.
 */
public void registerClass(Object obj) throws IgniteCheckedException {
    if (obj == null)
        return;
    if (obj instanceof GridPeerDeployAware) {
        GridPeerDeployAware p = (GridPeerDeployAware) obj;
        registerClass(p.deployClass(), p.classLoader());
    } else
        registerClass(obj instanceof Class ? (Class) obj : obj.getClass());
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware)

Example 2 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class IgniteUtils method peerDeployAware0.

/**
 * Gets the peer deploy aware instance for the object with the widest class loader.
 * If array is {@code null}, empty or contains only {@code null}s - the peer
 * deploy aware object based on system class loader will be returned.
 *
 * @param c Objects.
 * @return Peer deploy aware object from this array with the widest class loader.
 * @throws IllegalArgumentException Thrown in case when common class loader for all
 *      elements in this array cannot be found. In such case - peer deployment
 *      is not possible.
 */
@SuppressWarnings({ "ZeroLengthArrayAllocation" })
public static GridPeerDeployAware peerDeployAware0(@Nullable Object... c) {
    if (!F.isEmpty(c)) {
        assert c != null;
        boolean notAllNulls = false;
        for (Object obj : c) {
            if (obj != null) {
                notAllNulls = true;
                ClassLoader ldr = obj instanceof GridPeerDeployAware ? ((GridPeerDeployAware) obj).classLoader() : obj.getClass().getClassLoader();
                boolean found = true;
                for (Object obj2 : c) {
                    if (obj2 == null || obj2 == obj)
                        continue;
                    // Obj2 class name.
                    String clsName = obj2 instanceof GridPeerDeployAware ? ((GridPeerDeployAware) obj2).deployClass().getName() : obj2.getClass().getName();
                    if (!isLoadableBy(clsName, ldr)) {
                        found = false;
                        break;
                    }
                }
                if (found)
                    return peerDeployAware0(obj);
            }
        }
        // If all are nulls - don't throw an exception.
        if (notAllNulls)
            throw new IllegalArgumentException("Failed to find common class loader for all elements in " + "given collection. Peer deployment cannot be performed for such collection.");
    }
    return peerDeployAware(new Object[0]);
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) URLClassLoader(java.net.URLClassLoader)

Example 3 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class IgniteUtilsSelfTest method testPeerDeployAware0.

/**
 * @throws Exception If test failed.
 */
@Test
public void testPeerDeployAware0() throws Exception {
    Collection<Object> col = new ArrayList<>();
    col.add(null);
    col.add(null);
    col.add(null);
    GridPeerDeployAware pda = U.peerDeployAware0(col);
    assert pda != null;
    col.clear();
    col.add(null);
    pda = U.peerDeployAware0(col);
    assert pda != null;
    col.clear();
    pda = U.peerDeployAware0(col);
    assert pda != null;
    col.clear();
    col.add(null);
    col.add("Test");
    col.add(null);
    pda = U.peerDeployAware0(col);
    assert pda != null;
    col.clear();
    col.add("Test");
    pda = U.peerDeployAware0(col);
    assert pda != null;
    col.clear();
    col.add("Test");
    col.add(this);
    pda = U.peerDeployAware0(col);
    assert pda != null;
    col.clear();
    col.add(null);
    col.add("Test");
    col.add(null);
    col.add(this);
    col.add(null);
    pda = U.peerDeployAware0(col);
    assert pda != null;
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) ArrayList(java.util.ArrayList) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware in project ignite by apache.

the class GridMessageListenHandler method p2pMarshal.

/**
 * {@inheritDoc}
 */
@Override
public void p2pMarshal(GridKernalContext ctx) throws IgniteCheckedException {
    assert ctx != null;
    assert ctx.config().isPeerClassLoadingEnabled();
    if (topic != null)
        topicBytes = U.marshal(ctx.config().getMarshaller(), topic);
    predBytes = U.marshal(ctx.config().getMarshaller(), pred);
    // Deploy only listener, as it is very likely to be of some user class.
    GridPeerDeployAware pda = U.peerDeployAware(pred);
    clsName = pda.deployClass().getName();
    GridDeployment dep = ctx.deploy().deploy(pda.deployClass(), pda.classLoader());
    if (dep == null)
        throw new IgniteDeploymentCheckedException("Failed to deploy message listener.");
    depInfo = new GridDeploymentInfoBean(dep);
    depEnabled = true;
}
Also used : GridPeerDeployAware(org.apache.ignite.internal.util.lang.GridPeerDeployAware) GridDeployment(org.apache.ignite.internal.managers.deployment.GridDeployment) GridDeploymentInfoBean(org.apache.ignite.internal.managers.deployment.GridDeploymentInfoBean)

Example 5 with GridPeerDeployAware

use of org.apache.ignite.internal.util.lang.GridPeerDeployAware 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.
 */
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) {
        if (task instanceof GridPeerDeployAware)
            taskClsName = ((GridPeerDeployAware) task).deployClass().getName();
        else
            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);
    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);
            IgniteBiTuple<Class<?>, Throwable> cls = dep.deployedClass(taskName);
            if (cls.get1() == null)
                throw new IgniteDeploymentCheckedException("Unknown task name or failed to auto-deploy " + "task (was task (re|un)deployed?) [taskName=" + taskName + ", dep=" + dep + ']', cls.get2());
            taskCls = cls.get1();
            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;
    }
    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.emptyList(), emptyMap(), fullSup, internal, execName, ctx.security().enabled() ? ctx.security().securityContext().subject().login() : null);
    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, securitySubjectId(ctx));
            GridTaskWorker<?, ?> taskWorker0 = tasks.putIfAbsent(sesId, taskWorker);
            assert taskWorker0 == null : "Session ID is not unique: " + sesId;
            if (ctx.event().isRecordable(EVT_MANAGEMENT_TASK_STARTED) && dep.visorManagementTask(task, taskCls)) {
                VisorTaskArgument visorTaskArgument = (VisorTaskArgument) arg;
                Event evt = new TaskEvent(ctx.discovery().localNode(), visorTaskArgument != null && visorTaskArgument.getArgument() != null ? visorTaskArgument.getArgument().toString() : "[]", EVT_MANAGEMENT_TASK_STARTED, ses.getId(), taskCls == null ? null : taskCls.getSimpleName(), "VisorManagementTask", false, securitySubjectId(ctx));
                ctx.event().record(evt);
            }
            if (!ctx.clientDisconnected()) {
                if (dep.annotation(taskCls, ComputeTaskMapAsync.class) != null) {
                    try {
                        // Start task execution in another thread.
                        if (sys)
                            ctx.pools().getSystemExecutorService().execute(taskWorker);
                        else
                            ctx.pools().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) TaskEvent(org.apache.ignite.events.TaskEvent) Collection(java.util.Collection) VisorTaskArgument(org.apache.ignite.internal.visor.VisorTaskArgument) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) ComputeExecutionRejectedException(org.apache.ignite.compute.ComputeExecutionRejectedException)

Aggregations

GridPeerDeployAware (org.apache.ignite.internal.util.lang.GridPeerDeployAware)8 URLClassLoader (java.net.URLClassLoader)2 Collection (java.util.Collection)2 GridDeployment (org.apache.ignite.internal.managers.deployment.GridDeployment)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ComputeExecutionRejectedException (org.apache.ignite.compute.ComputeExecutionRejectedException)1 ComputeTask (org.apache.ignite.compute.ComputeTask)1 ComputeTaskMapAsync (org.apache.ignite.compute.ComputeTaskMapAsync)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 Event (org.apache.ignite.events.Event)1 TaskEvent (org.apache.ignite.events.TaskEvent)1