Search in sources :

Example 21 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class CacheVectorExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> CacheVector example started.");
        CacheConfiguration<Integer, Double> cfg = new CacheConfiguration<>();
        cfg.setName(CACHE_NAME);
        try (IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(cfg)) {
            double[] testValues1 = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            double[] testValues2 = { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            ValueMapper valMapper = new IdentityValueMapper();
            // Map vector element index to cache keys.
            VectorKeyMapper<Integer> keyMapper1 = new VectorKeyMapper<Integer>() {

                @Override
                public Integer apply(int i) {
                    return i;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && CARDINALITY > integer;
                }
            };
            // Map vector element index to cache keys with shift.
            VectorKeyMapper<Integer> keyMapper2 = new VectorKeyMapper<Integer>() {

                @Override
                public Integer apply(int i) {
                    return i + CARDINALITY;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && CARDINALITY > integer;
                }
            };
            // Create two cache vectors over one cache.
            CacheVector cacheVector1 = new CacheVector(CARDINALITY, cache, keyMapper1, valMapper);
            System.out.println(">>> First cache vector created.");
            CacheVector cacheVector2 = new CacheVector(CARDINALITY, cache, keyMapper2, valMapper);
            System.out.println(">>> Second cache vector created.");
            cacheVector1.assign(testValues1);
            cacheVector2.assign(testValues2);
            // Dot product for orthogonal vectors is 0.0.
            assert cacheVector1.dot(cacheVector2) == 0.0;
            System.out.println(">>>");
            System.out.println(">>> Finished executing Ignite \"CacheVector\" example.");
            System.out.println(">>> Dot product is 0.0 for orthogonal vectors.");
            System.out.println(">>>");
        }
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) ValueMapper(org.apache.ignite.ml.math.ValueMapper) IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) CacheVector(org.apache.ignite.ml.math.impls.vector.CacheVector) VectorKeyMapper(org.apache.ignite.ml.math.VectorKeyMapper) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 22 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class GridifyAspectJAspect 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", "CatchGenericClass", "unchecked" })
@Around("execution(@org.apache.ignite.compute.gridify.Gridify * *(..)) && !cflow(call(* org.apache.ignite.compute.ComputeJob.*(..)))")
public Object gridify(ProceedingJoinPoint joinPnt) throws Throwable {
    Method mtd = ((MethodSignature) joinPnt.getSignature()).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(), joinPnt.getArgs(), joinPnt.getTarget());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return joinPnt.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 task class was specified.
        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(joinPnt.getSignature().getDeclaringType()), arg);
        }
        // If task name was specified.
        return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
    } 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) 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) Around(org.aspectj.lang.annotation.Around)

Example 23 with Ignite

use of org.apache.ignite.Ignite 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", "CatchGenericClass" })
@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 24 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class GridifySetToSetSpringAspect method invoke.

/**
     * Aspect implementation which executes grid-enabled methods on remote
     * nodes.
     *
     * {@inheritDoc}
     */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "CatchGenericClass" })
@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 25 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class SparseDistributedMatrixExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> Sparse distributed matrix API usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
        // because we create ignite cache internally.
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
            double[][] testValues = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } };
            System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
            // Create SparseDistributedMatrix, new cache will be created automagically.
            SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(testValues.length, testValues[0].length, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
            distributedMatrix.assign(testValues);
            System.out.println("Sum of all matrix elements is " + distributedMatrix.sum());
            System.out.println(">>> Destroy SparseDistributedMatrix after using.");
            // Destroy internal cache.
            distributedMatrix.destroy();
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread)

Aggregations

Ignite (org.apache.ignite.Ignite)1560 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)275 CountDownLatch (java.util.concurrent.CountDownLatch)188 IgniteException (org.apache.ignite.IgniteException)187 Transaction (org.apache.ignite.transactions.Transaction)166 IgniteCache (org.apache.ignite.IgniteCache)161 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)161 ArrayList (java.util.ArrayList)135 ClusterNode (org.apache.ignite.cluster.ClusterNode)121 UUID (java.util.UUID)104 Event (org.apache.ignite.events.Event)104 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)98 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)97 CacheException (javax.cache.CacheException)94 HashMap (java.util.HashMap)78 IgniteKernal (org.apache.ignite.internal.IgniteKernal)71 Map (java.util.Map)61 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)61 Callable (java.util.concurrent.Callable)60 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)60