Search in sources :

Example 21 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class CacheUtils method sparseFold.

private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, A zeroVal, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
    A defRes = zeroVal;
    if (!isNilpotent)
        for (int i = 0; i < defValCnt; i++) defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes);
    Collection<A> totalRes = bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode localNode = ignite.cluster().localNode();
        A a = zeroVal;
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == localNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
        }
        return a;
    });
    totalRes.add(defRes);
    return totalRes.stream().reduce(zeroVal, accumulator);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 22 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class CacheUtils method fold.

/**
     * <b>Currently fold supports only commutative operations.<b/>
     *
     * @param cacheName Cache name.
     * @param folder Fold function operating over cache entries.
     * @param <K> Cache key object type.
     * @param <V> Cache value object type.
     * @param <A> Fold result type.
     * @return Fold operation result.
     */
public static <K, V, A> Collection<A> fold(String cacheName, IgniteBiFunction<CacheEntry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter) {
    return bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        A a = null;
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(new CacheEntry<>(entry, cache), a);
        }
        return a;
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 23 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class IgniteCacheConcurrentPutGetRemove method putGetRemove.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void putGetRemove(final CacheConfiguration ccfg) throws Exception {
    ignite(0).createCache(ccfg);
    try {
        long stopTime = System.currentTimeMillis() + 30_000;
        int iter = 0;
        while (System.currentTimeMillis() < stopTime) {
            if (iter++ % 100 == 0)
                log.info("Iteration: " + iter);
            final AtomicInteger idx = new AtomicInteger();
            final int KEYS = 10;
            GridTestUtils.runMultiThreaded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int nodeIdx = idx.getAndIncrement() % NODES;
                    IgniteCache<Object, Object> cache = ignite(nodeIdx).cache(ccfg.getName());
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    for (int i = 0; i < 10; i++) {
                        for (int k = 0; k < KEYS; k++) {
                            switch(rnd.nextInt(3)) {
                                case 0:
                                    cache.put(k, rnd.nextInt(10_000));
                                    break;
                                case 1:
                                    cache.get(k);
                                    break;
                                case 2:
                                    cache.remove(k);
                                    break;
                                default:
                                    fail();
                            }
                        }
                    }
                    return null;
                }
            }, NODES * 10, "update-thread");
            Affinity aff = ignite(0).affinity(ccfg.getName());
            for (int k = 0; k < KEYS; k++) {
                Collection<ClusterNode> nodes = aff.mapKeyToPrimaryAndBackups(k);
                Object expVal = grid(nodes.iterator().next()).cache(ccfg.getName()).get(k);
                for (int n = 0; n < NODES; n++) {
                    Ignite ignite = ignite(n);
                    IgniteCache<Object, Object> cache = ignite.cache(ccfg.getName());
                    if (nodes.contains(ignite.cluster().localNode()))
                        assertEquals(expVal, cache.localPeek(k));
                    else {
                        assertNull(cache.localPeek(k));
                        assertEquals(expVal, cache.get(k));
                    }
                }
            }
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite)

Example 24 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class SplitCache method localEntries.

/**
 * Returns local entries for keys corresponding to {@code featureIndexes}.
 *
 * @param featureIndexes Index of features.
 * @param affinity Affinity function.
 * @param trainingUUID UUID of training.
 * @return local entries for keys corresponding to {@code featureIndexes}.
 */
public static Iterable<Cache.Entry<SplitKey, IgniteBiTuple<Integer, Double>>> localEntries(Set<Integer> featureIndexes, IgniteBiFunction<Integer, Ignite, Object> affinity, UUID trainingUUID) {
    Ignite ignite = Ignition.localIgnite();
    Set<SplitKey> keys = featureIndexes.stream().map(fIdx -> new SplitKey(trainingUUID, affinity.apply(fIdx, ignite), fIdx)).collect(Collectors.toSet());
    Collection<SplitKey> locKeys = affinity().mapKeysToNodes(keys).getOrDefault(ignite.cluster().localNode(), Collections.emptyList());
    return () -> {
        Function<SplitKey, Cache.Entry<SplitKey, IgniteBiTuple<Integer, Double>>> f = k -> (new CacheEntryImpl<>(k, getOrCreate(ignite).localPeek(k)));
        return locKeys.stream().map(f).iterator();
    };
}
Also used : IntStream(java.util.stream.IntStream) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) Affinity(org.apache.ignite.cache.affinity.Affinity) Set(java.util.Set) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) AffinityKeyMapped(org.apache.ignite.cache.affinity.AffinityKeyMapped) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) Cache(javax.cache.Cache) Collections(java.util.Collections) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) CacheMode(org.apache.ignite.cache.CacheMode) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Function(java.util.function.Function) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 25 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class MLPGroupUpdateTrainerCacheInput method batchSupplier.

/**
 * {@inheritDoc}
 */
@Override
public IgniteSupplier<IgniteBiTuple<Matrix, Matrix>> batchSupplier() {
    String cName = cache.getName();
    // This line is for prohibiting of 'this' object be caught into serialization context of lambda.
    int bs = batchSize;
    // This line is for prohibiting of 'this' object be caught into serialization context of lambda.
    Random r = rand;
    return () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<Integer, LabeledVector<Vector, Vector>> cache = ignite.getOrCreateCache(cName);
        int total = cache.size();
        Affinity<Integer> affinity = ignite.affinity(cName);
        List<Integer> allKeys = IntStream.range(0, total).boxed().collect(Collectors.toList());
        List<Integer> keys = new ArrayList<>(affinity.mapKeysToNodes(allKeys).get(ignite.cluster().localNode()));
        int locKeysCnt = keys.size();
        int[] selected = Utils.selectKDistinct(locKeysCnt, Math.min(bs, locKeysCnt), r);
        // Get dimensions of vectors in cache. We suppose that every feature vector has
        // same dimension d 1 and every label has the same dimension d2.
        LabeledVector<Vector, Vector> dimEntry = cache.get(keys.get(selected[0]));
        Matrix inputs = new DenseLocalOnHeapMatrix(dimEntry.features().size(), bs);
        Matrix groundTruth = new DenseLocalOnHeapMatrix(dimEntry.label().size(), bs);
        for (int i = 0; i < selected.length; i++) {
            LabeledVector<Vector, Vector> labeled = cache.get(keys.get(selected[i]));
            inputs.assignColumn(i, labeled.features());
            groundTruth.assignColumn(i, labeled.label());
        }
        return new IgniteBiTuple<>(inputs, groundTruth);
    };
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Random(java.util.Random) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) Vector(org.apache.ignite.ml.math.Vector)

Aggregations

Affinity (org.apache.ignite.cache.affinity.Affinity)49 Ignite (org.apache.ignite.Ignite)30 IgniteCache (org.apache.ignite.IgniteCache)29 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)18 Collection (java.util.Collection)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Transaction (org.apache.ignite.transactions.Transaction)16 List (java.util.List)15 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)14 Map (java.util.Map)13 Cache (javax.cache.Cache)13 HashMap (java.util.HashMap)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)10 Collections (java.util.Collections)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)8 Ignition (org.apache.ignite.Ignition)8 ScanQuery (org.apache.ignite.cache.query.ScanQuery)8