Search in sources :

Example 26 with Affinity

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

the class CacheUtils method update.

/**
 * @param cacheName Cache name.
 * @param fun An operation that accepts a cache entry and processes it.
 * @param ignite Ignite.
 * @param keysGen Keys generator.
 * @param <K> Cache key object type.
 * @param <V> Cache value object type.
 */
public static <K, V> void update(String cacheName, Ignite ignite, IgniteBiFunction<Ignite, Cache.Entry<K, V>, Stream<Cache.Entry<K, V>>> fun, IgniteSupplier<Set<K>> keysGen) {
    bcast(cacheName, ignite, () -> {
        Ignite ig = Ignition.localIgnite();
        IgniteCache<K, V> cache = ig.getOrCreateCache(cacheName);
        Affinity<K> affinity = ig.affinity(cacheName);
        ClusterNode locNode = ig.cluster().localNode();
        Collection<K> ks = affinity.mapKeysToNodes(keysGen.get()).get(locNode);
        if (ks == null)
            return;
        Map<K, V> m = new ConcurrentHashMap<>();
        ks.parallelStream().forEach(k -> {
            V v = cache.localPeek(k);
            if (v != null)
                (fun.apply(ignite, new CacheEntryImpl<>(k, v))).forEach(ent -> m.put(ent.getKey(), ent.getValue()));
        });
        cache.putAll(m);
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) IgniteCallable(org.apache.ignite.lang.IgniteCallable) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) DataStructureCacheKey(org.apache.ignite.ml.math.distributed.keys.DataStructureCacheKey) ClusterNode(org.apache.ignite.cluster.ClusterNode) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Map(java.util.Map) Cache(javax.cache.Cache) IgniteBinaryOperator(org.apache.ignite.ml.math.functions.IgniteBinaryOperator) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) A(org.apache.ignite.internal.util.typedef.internal.A) IgniteSupplier(org.apache.ignite.ml.math.functions.IgniteSupplier) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Set(java.util.Set) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDoubleFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with Affinity

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

the class CacheUtils method foreach.

/**
 * @param cacheName Cache name.
 * @param fun An operation that accepts a cache entry and processes it.
 * @param keyFilter Cache keys filter.
 * @param <K> Cache key object type.
 * @param <V> Cache value object type.
 */
protected static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
    bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        // 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))))) fun.accept(new CacheEntry<>(entry, cache));
        }
    });
}
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) IgniteCallable(org.apache.ignite.lang.IgniteCallable) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) DataStructureCacheKey(org.apache.ignite.ml.math.distributed.keys.DataStructureCacheKey) ClusterNode(org.apache.ignite.cluster.ClusterNode) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Map(java.util.Map) Cache(javax.cache.Cache) IgniteBinaryOperator(org.apache.ignite.ml.math.functions.IgniteBinaryOperator) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) A(org.apache.ignite.internal.util.typedef.internal.A) IgniteSupplier(org.apache.ignite.ml.math.functions.IgniteSupplier) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Set(java.util.Set) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDoubleFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 28 with Affinity

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

the class CrossOverTask method map.

/**
 * Map Jobs to nodes using data affinity.
 *
 * @param nodes Cluster Nodes
 * @param chromosomeKeys Primary keys for respective chromosomes
 * @return A map of nodes to jobs.
 */
public Map map(List<ClusterNode> nodes, List<Long> chromosomeKeys) throws IgniteException {
    Map<ComputeJob, ClusterNode> map = new HashMap<>();
    Affinity affinity = ignite.affinity(GAGridConstants.POPULATION_CACHE);
    Map<ClusterNode, Collection<Long>> nodeKeys = affinity.mapKeysToNodes(chromosomeKeys);
    for (Map.Entry<ClusterNode, Collection<Long>> entry : nodeKeys.entrySet()) {
        ClusterNode aNode = entry.getKey();
        map = setupCrossOver(aNode, (List<Long>) entry.getValue(), map);
    }
    return map;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) HashMap(java.util.HashMap) Affinity(org.apache.ignite.cache.affinity.Affinity) Collection(java.util.Collection) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with Affinity

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

the class FitnessTask method map.

/**
 * @param nodes List of ClusterNode
 * @param chromosomeKeys List of chromosome keys
 * @return Map of jobs to nodes
 */
public Map map(List<ClusterNode> nodes, List<Long> chromosomeKeys) throws IgniteException {
    Map<ComputeJob, ClusterNode> map = new HashMap<>();
    Affinity affinity = ignite.affinity(GAGridConstants.POPULATION_CACHE);
    for (Long key : chromosomeKeys) {
        FitnessJob ajob = new FitnessJob(key, this.config.getFitnessFunction());
        ClusterNode primary = affinity.mapKeyToNode(key);
        map.put(ajob, primary);
    }
    return map;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) HashMap(java.util.HashMap) Affinity(org.apache.ignite.cache.affinity.Affinity)

Example 30 with Affinity

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

the class MutateTask method map.

/**
 * @param nodes List of ClusterNode
 * @param chromosomeKeys Primary keys for respective chromosomes
 */
public Map map(List<ClusterNode> nodes, List<Long> chromosomeKeys) throws IgniteException {
    Map<ComputeJob, ClusterNode> map = new HashMap<>();
    Affinity affinity = ignite.affinity(GAGridConstants.POPULATION_CACHE);
    for (Long key : chromosomeKeys) {
        MutateJob ajob = new MutateJob(key, getMutatedGenes(), this.config.getMutationRate());
        ClusterNode primary = affinity.mapKeyToNode(key);
        map.put(ajob, primary);
    }
    return map;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) HashMap(java.util.HashMap) Affinity(org.apache.ignite.cache.affinity.Affinity)

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