Search in sources :

Example 1 with GridifyArgumentBuilder

use of org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder in project ignite by apache.

the class GridifySetToValueAspectJAspect method gridify.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * @param joinPnt Join point provided by AspectJ AOP.
 * @return Method execution result.
 * @throws Throwable If execution failed.
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown" })
@Around("execution(@org.apache.ignite.compute.gridify.GridifySetToValue * *(..)) && " + "!cflow(call(* org.apache.ignite.compute.ComputeJob.*(..)))")
public Object gridify(ProceedingJoinPoint joinPnt) throws Throwable {
    Method mtd = ((MethodSignature) joinPnt.getSignature()).getMethod();
    GridifySetToValue ann = mtd.getAnnotation(GridifySetToValue.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 Igninte 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);
    GridifyNodeFilter nodeFilter = null;
    if (!ann.nodeFilter().equals(GridifyNodeFilter.class))
        nodeFilter = ann.nodeFilter().newInstance();
    // Check is method allowed for gridify.
    checkMethodSignature(mtd);
    GridifyArgumentBuilder argBuilder = new GridifyArgumentBuilder();
    // Creates task argument.
    GridifyRangeArgument arg = argBuilder.createTaskArgument(mtd.getDeclaringClass(), mtd.getName(), mtd.getReturnType(), mtd.getParameterTypes(), mtd.getParameterAnnotations(), joinPnt.getArgs(), joinPnt.getTarget());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return joinPnt.proceed();
    }
    // Proceed locally for negative threshold parameter.
    if (ann.threshold() < 0)
        return joinPnt.proceed();
    // Analyse where to execute method (remotely or locally).
    if (arg.getInputSize() != UNKNOWN_SIZE && arg.getInputSize() <= ann.threshold())
        return joinPnt.proceed();
    // Check is split to jobs allowed for input method argument with declared splitSize.
    checkIsSplitToJobsAllowed(arg, ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        return execute(mtd, ignite.compute(), joinPnt.getSignature().getDeclaringType(), arg, nodeFilter, ann.threshold(), ann.splitSize(), ann.timeout());
    } catch (Exception e) {
        for (Class<?> ex : ((MethodSignature) joinPnt.getSignature()).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 : MethodSignature(org.aspectj.lang.reflect.MethodSignature) GridifyNodeFilter(org.apache.ignite.compute.gridify.GridifyNodeFilter) GridifyArgumentBuilder(org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder) Method(java.lang.reflect.Method) GridifySetToValue(org.apache.ignite.compute.gridify.GridifySetToValue) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Ignite(org.apache.ignite.Ignite) GridifyRangeArgument(org.apache.ignite.internal.util.gridify.GridifyRangeArgument) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Around(org.aspectj.lang.annotation.Around)

Example 2 with GridifyArgumentBuilder

use of org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder in project ignite by apache.

the class GridifySetToSetSpringAspect method invoke.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * {@inheritDoc}
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown" })
@Override
public Object invoke(MethodInvocation invoc) throws Throwable {
    Method mtd = invoc.getMethod();
    GridifySetToSet ann = mtd.getAnnotation(GridifySetToSet.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);
    GridifyNodeFilter nodeFilter = null;
    if (!ann.nodeFilter().equals(GridifyNodeFilter.class))
        nodeFilter = ann.nodeFilter().newInstance();
    GridifyArgumentBuilder argBuilder = new GridifyArgumentBuilder();
    // Creates task argument.
    GridifyRangeArgument arg = argBuilder.createTaskArgument(mtd.getDeclaringClass(), mtd.getName(), mtd.getReturnType(), mtd.getParameterTypes(), mtd.getParameterAnnotations(), invoc.getArguments(), invoc.getThis());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return invoc.proceed();
    }
    // Proceed locally for negative threshold parameter.
    if (ann.threshold() < 0)
        return invoc.proceed();
    // Analyse where to execute method (remotely or locally).
    if (arg.getInputSize() != UNKNOWN_SIZE && arg.getInputSize() <= ann.threshold())
        return invoc.proceed();
    // Check is split to jobs allowed for input method argument with declared splitSize.
    checkIsSplitToJobsAllowed(arg, ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        return execute(ignite.compute(), invoc.getMethod().getDeclaringClass(), arg, nodeFilter, ann.threshold(), ann.splitSize(), ann.timeout());
    } 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 : GridifyNodeFilter(org.apache.ignite.compute.gridify.GridifyNodeFilter) GridifyArgumentBuilder(org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder) Method(java.lang.reflect.Method) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Ignite(org.apache.ignite.Ignite) GridifyRangeArgument(org.apache.ignite.internal.util.gridify.GridifyRangeArgument) GridifySetToSet(org.apache.ignite.compute.gridify.GridifySetToSet) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException)

Example 3 with GridifyArgumentBuilder

use of org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder in project ignite by apache.

the class GridifyDefaultRangeTask method map.

/**
 * {@inheritDoc}
 */
@NotNull
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, GridifyRangeArgument arg) {
    assert !subgrid.isEmpty() : "Subgrid should not be empty: " + subgrid;
    assert ignite != null : "Grid instance could not be injected";
    if (splitSize < threshold && splitSize != 0 && threshold != 0) {
        throw new IgniteException("Incorrect Gridify annotation parameters. Value for parameter " + "'splitSize' should not be less than parameter 'threshold' [splitSize=" + splitSize + ", threshold=" + threshold + ']');
    }
    Collection<ClusterNode> exclNodes = new LinkedList<>();
    // Filter nodes.
    if (nodeFilter != null) {
        for (ClusterNode node : subgrid) {
            if (!nodeFilter.apply(node, ses))
                exclNodes.add(node);
        }
        if (exclNodes.size() == subgrid.size())
            throw new IgniteException("Failed to execute on grid where all nodes excluded.");
    }
    int inputPerNode = splitSize;
    // Calculate input elements size per node for default annotation splitSize parameter.
    if (splitSize <= 0) {
        // For iterable input splitSize will be assigned with threshold value.
        if (threshold > 0 && arg.getInputSize() == UNKNOWN_SIZE)
            inputPerNode = threshold;
        else // Otherwise, splitSize equals (inputSize / nodesCount)
        {
            assert arg.getInputSize() != UNKNOWN_SIZE;
            int gridSize = subgrid.size() - exclNodes.size();
            gridSize = (gridSize <= 0 ? subgrid.size() : gridSize);
            inputPerNode = calculateInputSizePerNode(gridSize, arg.getInputSize(), threshold, limitedSplit);
            if (log.isDebugEnabled()) {
                log.debug("Calculated input elements size per node [inputSize=" + arg.getInputSize() + ", gridSize=" + gridSize + ", threshold=" + threshold + ", limitedSplit=" + limitedSplit + ", inputPerNode=" + inputPerNode + ']');
            }
        }
    }
    GridifyArgumentBuilder argBuilder = new GridifyArgumentBuilder();
    Iterator<?> inputIter = arg.getInputIterator();
    while (inputIter.hasNext()) {
        Collection<Object> nodeInput = new LinkedList<>();
        for (int i = 0; i < inputPerNode && inputIter.hasNext(); i++) nodeInput.add(inputIter.next());
        // Create job argument.
        GridifyArgument jobArg = argBuilder.createJobArgument(arg, nodeInput);
        ComputeJob job = new GridifyJobAdapter(jobArg);
        mapper.send(job, balancer.getBalancedNode(job, exclNodes));
    }
    // Map method can return null because job already sent by continuous mapper.
    return null;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridifyArgument(org.apache.ignite.compute.gridify.GridifyArgument) ComputeJob(org.apache.ignite.compute.ComputeJob) IgniteException(org.apache.ignite.IgniteException) GridifyJobAdapter(org.apache.ignite.internal.util.gridify.GridifyJobAdapter) GridifyArgumentBuilder(org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GridifyArgumentBuilder

use of org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder in project ignite by apache.

the class GridifySetToSetAspectJAspect method gridify.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * @param joinPnt Join point provided by AspectJ AOP.
 * @return Method execution result.
 * @throws Throwable If execution failed.
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown" })
@Around("execution(@org.apache.ignite.compute.gridify.GridifySetToSet * *(..)) && " + "!cflow(call(* org.apache.ignite.compute.ComputeJob.*(..)))")
public Object gridify(ProceedingJoinPoint joinPnt) throws Throwable {
    Method mtd = ((MethodSignature) joinPnt.getSignature()).getMethod();
    GridifySetToSet ann = mtd.getAnnotation(GridifySetToSet.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);
    GridifyNodeFilter nodeFilter = null;
    if (!ann.nodeFilter().equals(GridifyNodeFilter.class))
        nodeFilter = ann.nodeFilter().newInstance();
    // Check method return type.
    checkMethodSignature(mtd);
    GridifyArgumentBuilder argBuilder = new GridifyArgumentBuilder();
    // Creates task argument.
    GridifyRangeArgument arg = argBuilder.createTaskArgument(mtd.getDeclaringClass(), mtd.getName(), mtd.getReturnType(), mtd.getParameterTypes(), mtd.getParameterAnnotations(), joinPnt.getArgs(), joinPnt.getTarget());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return joinPnt.proceed();
    }
    // Proceed locally for negative threshold parameter.
    if (ann.threshold() < 0)
        return joinPnt.proceed();
    // Analyse where to execute method (remotely or locally).
    if (arg.getInputSize() != UNKNOWN_SIZE && arg.getInputSize() <= ann.threshold())
        return joinPnt.proceed();
    // Check is split to jobs allowed for input method argument with declared splitSize.
    checkIsSplitToJobsAllowed(arg, ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        return execute(ignite.compute(), joinPnt.getSignature().getDeclaringType(), arg, nodeFilter, ann.threshold(), ann.splitSize(), ann.timeout());
    } catch (Exception e) {
        for (Class<?> ex : ((MethodSignature) joinPnt.getSignature()).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 : MethodSignature(org.aspectj.lang.reflect.MethodSignature) GridifyNodeFilter(org.apache.ignite.compute.gridify.GridifyNodeFilter) GridifyArgumentBuilder(org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder) Method(java.lang.reflect.Method) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Ignite(org.apache.ignite.Ignite) GridifyRangeArgument(org.apache.ignite.internal.util.gridify.GridifyRangeArgument) GridifySetToSet(org.apache.ignite.compute.gridify.GridifySetToSet) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Around(org.aspectj.lang.annotation.Around)

Example 5 with GridifyArgumentBuilder

use of org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder in project ignite by apache.

the class GridifySetToValueSpringAspect method invoke.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * {@inheritDoc}
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown" })
@Override
public Object invoke(MethodInvocation invoc) throws Throwable {
    Method mtd = invoc.getMethod();
    GridifySetToValue ann = mtd.getAnnotation(GridifySetToValue.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);
    GridifyNodeFilter nodeFilter = null;
    if (!ann.nodeFilter().equals(GridifyNodeFilter.class))
        nodeFilter = ann.nodeFilter().newInstance();
    GridifyArgumentBuilder argBuilder = new GridifyArgumentBuilder();
    // Creates task argument.
    GridifyRangeArgument arg = argBuilder.createTaskArgument(mtd.getDeclaringClass(), mtd.getName(), mtd.getReturnType(), mtd.getParameterTypes(), mtd.getParameterAnnotations(), invoc.getArguments(), invoc.getThis());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return invoc.proceed();
    }
    // Proceed locally for negative threshold parameter.
    if (ann.threshold() < 0)
        return invoc.proceed();
    // Analyse where to execute method (remotely or locally).
    if (arg.getInputSize() != UNKNOWN_SIZE && arg.getInputSize() <= ann.threshold())
        return invoc.proceed();
    // Check is split to jobs allowed for input method argument with declared splitSize.
    checkIsSplitToJobsAllowed(arg, ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        return execute(mtd, ignite.compute(), invoc.getMethod().getDeclaringClass(), arg, nodeFilter, ann.threshold(), ann.splitSize(), ann.timeout());
    } 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 : GridifyNodeFilter(org.apache.ignite.compute.gridify.GridifyNodeFilter) GridifyArgumentBuilder(org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder) Method(java.lang.reflect.Method) GridifySetToValue(org.apache.ignite.compute.gridify.GridifySetToValue) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Ignite(org.apache.ignite.Ignite) GridifyRangeArgument(org.apache.ignite.internal.util.gridify.GridifyRangeArgument) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException)

Aggregations

GridifyArgumentBuilder (org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder)5 Method (java.lang.reflect.Method)4 Ignite (org.apache.ignite.Ignite)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridifyInterceptor (org.apache.ignite.compute.gridify.GridifyInterceptor)4 GridifyNodeFilter (org.apache.ignite.compute.gridify.GridifyNodeFilter)4 GridifyRuntimeException (org.apache.ignite.compute.gridify.GridifyRuntimeException)4 GridifyRangeArgument (org.apache.ignite.internal.util.gridify.GridifyRangeArgument)4 GridifySetToSet (org.apache.ignite.compute.gridify.GridifySetToSet)2 GridifySetToValue (org.apache.ignite.compute.gridify.GridifySetToValue)2 Around (org.aspectj.lang.annotation.Around)2 MethodSignature (org.aspectj.lang.reflect.MethodSignature)2 LinkedList (java.util.LinkedList)1 IgniteException (org.apache.ignite.IgniteException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ComputeJob (org.apache.ignite.compute.ComputeJob)1 GridifyArgument (org.apache.ignite.compute.gridify.GridifyArgument)1 GridifyJobAdapter (org.apache.ignite.internal.util.gridify.GridifyJobAdapter)1 NotNull (org.jetbrains.annotations.NotNull)1