Search in sources :

Example 1 with ComputeTaskName

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

the class GridDeploymentManager method deploy.

/**
     * @param cls Class to deploy.
     * @param clsLdr Class loader.
     * @throws IgniteCheckedException If deployment failed.
     * @return Grid deployment.
     */
@Nullable
public GridDeployment deploy(Class<?> cls, ClassLoader clsLdr) throws IgniteCheckedException {
    if (clsLdr == null)
        clsLdr = getClass().getClassLoader();
    String clsName = cls.getName();
    String lambdaParent = U.lambdaEnclosingClassName(clsName);
    if (lambdaParent != null) {
        clsName = lambdaParent;
        // Need to override passed in class if class is Lambda.
        try {
            cls = Class.forName(clsName, true, clsLdr);
        } catch (ClassNotFoundException e) {
            throw new IgniteCheckedException("Cannot deploy parent class for lambda: " + clsName, e);
        }
    }
    if (clsLdr instanceof GridDeploymentClassLoader) {
        GridDeploymentInfo ldr = (GridDeploymentInfo) clsLdr;
        // Expecting that peer-deploy awareness handled on upper level.
        if ((ldr.deployMode() == ISOLATED || ldr.deployMode() == PRIVATE) && (ctx.config().getDeploymentMode() == SHARED || ctx.config().getDeploymentMode() == CONTINUOUS) && !U.hasAnnotation(cls, GridInternal.class))
            throw new IgniteCheckedException("Attempt to deploy class loaded in ISOLATED or PRIVATE mode on node with " + "SHARED or CONTINUOUS deployment mode [cls=" + cls + ", clsDeployMode=" + ldr.deployMode() + ", localDeployMode=" + ctx.config().getDeploymentMode() + ']');
        GridDeploymentMetadata meta = new GridDeploymentMetadata();
        meta.alias(clsName);
        meta.classLoader(clsLdr);
        // Check for nested execution. In that case, if task
        // is available locally by name, then we should ignore
        // class loader ID.
        GridDeployment dep = locStore.getDeployment(meta);
        if (dep == null) {
            dep = ldrStore.getDeployment(ldr.classLoaderId());
            if (dep == null)
                dep = verStore.getDeployment(ldr.classLoaderId());
        }
        return dep;
    } else if (locDep != null) {
        if (ComputeTask.class.isAssignableFrom(cls)) {
            ComputeTaskName taskNameAnn = locDep.annotation(cls, ComputeTaskName.class);
            if (taskNameAnn != null)
                locDep.addDeployedClass(cls, taskNameAnn.value());
        }
        return locDep;
    } else
        return locStore.explicitDeploy(cls, clsLdr);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ComputeTask(org.apache.ignite.compute.ComputeTask) ComputeTaskName(org.apache.ignite.compute.ComputeTaskName) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ComputeTaskName

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

the class LocalDeploymentSpi method addResource.

/**
     * Add new classes in class loader resource map.
     * Note that resource map may contain two entries for one added class:
     * task name -> class name and class name -> class name.
     *
     * @param ldr Registered class loader.
     * @param ldrRsrcs Class loader resources.
     * @param cls Registered classes collection.
     * @return Map of new resources added for registered class loader.
     * @throws org.apache.ignite.spi.IgniteSpiException If resource already registered. Exception thrown
     * if registered resources conflicts with rule when all task classes must be
     * annotated with different task names.
     */
@Nullable
private Map<String, String> addResource(ClassLoader ldr, ConcurrentMap<String, String> ldrRsrcs, Class<?> cls) throws IgniteSpiException {
    assert ldr != null;
    assert ldrRsrcs != null;
    assert cls != null;
    // Maps resources to classes.
    // Map may contain 2 entries for one class.
    Map<String, String> regRsrcs = U.newHashMap(2);
    // Check alias collision for added classes.
    String alias = null;
    if (ComputeTask.class.isAssignableFrom(cls)) {
        ComputeTaskName nameAnn = GridAnnotationsCache.getAnnotation(cls, ComputeTaskName.class);
        if (nameAnn != null)
            alias = nameAnn.value();
    }
    if (alias != null)
        regRsrcs.put(alias, cls.getName());
    regRsrcs.put(cls.getName(), cls.getName());
    if (log.isDebugEnabled())
        log.debug("Resources to register: " + regRsrcs);
    Map<String, String> newRsrcs = null;
    // Check collisions for added classes.
    for (Entry<String, String> entry : regRsrcs.entrySet()) {
        String oldCls = ldrRsrcs.putIfAbsent(entry.getKey(), entry.getValue());
        if (oldCls != null) {
            if (!oldCls.equals(entry.getValue()))
                throw new IgniteSpiException("Failed to register resources with given task name " + "(found another class with same task name in the same class loader) " + "[taskName=" + entry.getKey() + ", existingCls=" + oldCls + ", newCls=" + entry.getValue() + ", ldr=" + ldr + ']');
        } else {
            // New resource was added.
            if (newRsrcs == null)
                newRsrcs = U.newHashMap(regRsrcs.size());
            newRsrcs.put(entry.getKey(), entry.getValue());
        }
    }
    // New resources to register. Add it all.
    if (newRsrcs != null)
        ldrRsrcs.putAll(newRsrcs);
    if (log.isDebugEnabled())
        log.debug("New resources: " + newRsrcs);
    return newRsrcs;
}
Also used : IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ComputeTaskName(org.apache.ignite.compute.ComputeTaskName) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ComputeTaskName

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

the class GridTaskProcessor method taskName.

/**
     * Gets task name for a task class. It firstly checks
     * {@link @ComputeTaskName} annotation, then thread context
     * map. If both are empty, class name is returned.
     *
     * @param dep Deployment.
     * @param cls Class.
     * @param map Thread context map.
     * @return Task name.
     * @throws IgniteCheckedException If {@link @ComputeTaskName} annotation is found, but has empty value.
     */
private String taskName(GridDeployment dep, Class<?> cls, Map<GridTaskThreadContextKey, Object> map) throws IgniteCheckedException {
    assert dep != null;
    assert cls != null;
    assert map != null;
    String taskName;
    ComputeTaskName ann = dep.annotation(cls, ComputeTaskName.class);
    if (ann != null) {
        taskName = ann.value();
        if (F.isEmpty(taskName))
            throw new IgniteCheckedException("Task name specified by @ComputeTaskName annotation" + " cannot be empty for class: " + cls);
    } else
        taskName = map.containsKey(TC_TASK_NAME) ? (String) map.get(TC_TASK_NAME) : cls.getName();
    return taskName;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ComputeTaskName(org.apache.ignite.compute.ComputeTaskName)

Example 4 with ComputeTaskName

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

the class UriDeploymentSpi method addResources.

/**
     * Add new classes in class loader resource map.
     * Note that resource map may contain two entries for one added class:
     * task name -> class name and class name -> class name.
     *
     * @param ldr Registered class loader.
     * @param desc Deployment descriptor.
     * @param clss Registered classes array.
     * @return Map of new resources added for registered class loader.
     * @throws org.apache.ignite.spi.IgniteSpiException If resource already registered. Exception thrown
     * if registered resources conflicts with rule when all task classes must be
     * annotated with different task names.
     */
@Nullable
private Map<String, String> addResources(ClassLoader ldr, GridUriDeploymentUnitDescriptor desc, Class<?>[] clss) throws IgniteSpiException {
    assert ldr != null;
    assert desc != null;
    assert clss != null;
    // Maps resources to classes.
    // Map may contain 2 entries for one class.
    Map<String, Class<?>> alias2Cls = new HashMap<>(clss.length * 2, 1.0f);
    // Check alias collision between added classes.
    for (Class<?> cls : clss) {
        String alias = null;
        if (ComputeTask.class.isAssignableFrom(cls)) {
            ComputeTaskName nameAnn = U.getAnnotation(cls, ComputeTaskName.class);
            if (nameAnn != null)
                alias = nameAnn.value();
        }
        // If added classes maps to one alias.
        if (alias != null && alias2Cls.containsKey(alias) && !alias2Cls.get(alias).equals(cls))
            throw new IgniteSpiException("Failed to register resources with given task name " + "(found another class with same task name) [taskName=" + alias + ", cls1=" + cls.getName() + ", cls2=" + alias2Cls.get(alias).getName() + ", ldr=" + ldr + ']');
        if (alias != null) {
            alias2Cls.put(alias, cls);
            desc.addResource(alias, cls);
        } else
            desc.addResource(cls);
    }
    Map<String, String> newRsrcs = null;
    // Check collisions between added and exist classes.
    for (Entry<String, Class<?>> entry : alias2Cls.entrySet()) {
        String newAlias = entry.getKey();
        String newName = entry.getValue().getName();
        Class<?> cls = desc.getResourceByAlias(newAlias);
        if (cls != null) {
            // Different classes for the same resource name.
            if (!cls.getName().equals(newName))
                throw new IgniteSpiException("Failed to register resources with given task name " + "(found another class with same task name in the same class loader) [taskName=" + newAlias + ", existingCls=" + cls.getName() + ", newCls=" + newName + ", ldr=" + ldr + ']');
        } else // Add resources that should be removed for another class loaders.
        {
            if (newRsrcs == null)
                newRsrcs = U.newHashMap(alias2Cls.size() + clss.length);
            newRsrcs.put(newAlias, newName);
            newRsrcs.put(newName, newName);
        }
    }
    return newRsrcs;
}
Also used : HashMap(java.util.HashMap) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ComputeTaskName(org.apache.ignite.compute.ComputeTaskName) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ComputeTaskName (org.apache.ignite.compute.ComputeTaskName)4 Nullable (org.jetbrains.annotations.Nullable)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)2 HashMap (java.util.HashMap)1 ComputeTask (org.apache.ignite.compute.ComputeTask)1