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);
});
}
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));
}
});
}
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;
}
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;
}
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;
}
Aggregations