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.
*/
public 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 CacheUtils method sparseFold.
/**
* Sparse version of fold. This method also applicable to sparse zeroes.
*
* @param cacheName Cache name.
* @param folder Folder.
* @param keyFilter Key filter.
* @param accumulator Accumulator.
* @param zeroValSupp Zero value supplier.
* @param defVal Default value.
* @param defKey Default key.
* @param defValCnt Def value count.
* @param isNilpotent Is nilpotent.
*/
private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, IgniteSupplier<A> zeroValSupp, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
A defRes = zeroValSupp.get();
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 locNode = ignite.cluster().localNode();
A a = zeroValSupp.get();
// 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(entry, a);
}
return a;
});
return totalRes.stream().reduce(defRes, accumulator);
}
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;
});
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class IgniteCacheGroupsTest method testRebalance2.
/**
* @throws Exception If failed.
*/
@Test
public void testRebalance2() throws Exception {
Ignite srv0 = startGrid(0);
IgniteCache<Object, Object> srv0Cache1 = srv0.createCache(cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 0, false));
IgniteCache<Object, Object> srv0Cache2 = srv0.createCache(cacheConfiguration(GROUP1, "c2", PARTITIONED, ATOMIC, 0, false));
Affinity aff = srv0.affinity("c1");
final int ITEMS = 2_000;
Map<Integer, Integer> c1Data = new HashMap<>();
Map<Integer, Integer> c2Data = new HashMap<>();
for (int i = 0; i < ITEMS; i++) {
srv0Cache1.put(i, i);
c1Data.put(i, i);
if (i % 2 == 0) {
srv0Cache2.put(i, i);
c2Data.put(i, i);
}
}
assertEquals(ITEMS, srv0Cache1.size());
assertEquals(ITEMS / 2, srv0Cache2.size());
Ignite srv1 = startGrid(1);
awaitPartitionMapExchange();
assertEquals(ITEMS, srv0Cache1.size());
assertEquals(ITEMS / 2, srv0Cache2.size());
checkCacheData(c1Data, "c1");
checkCacheData(c2Data, "c2");
Set<Integer> srv1Parts = new HashSet<>();
for (Integer p : aff.primaryPartitions(srv1.cluster().localNode())) srv1Parts.add(p);
CacheGroupContext grpSrv0 = cacheGroup(srv0, GROUP1);
CacheGroupContext grpSrv1 = cacheGroup(srv1, GROUP1);
for (int p = 0; p < aff.partitions(); p++) {
if (srv1Parts.contains(p)) {
GridIterator<CacheDataRow> it = grpSrv0.offheap().partitionIterator(p);
assertFalse(it.hasNext());
it = grpSrv1.offheap().partitionIterator(p);
assertTrue(it.hasNext());
} else {
GridIterator<CacheDataRow> it = grpSrv0.offheap().partitionIterator(p);
assertTrue(it.hasNext());
it = grpSrv1.offheap().partitionIterator(p);
assertFalse(it.hasNext());
}
}
c1Data = new HashMap<>();
c2Data = new HashMap<>();
for (int i = 0; i < ITEMS; i++) {
srv0Cache1.put(i, i + 1);
c1Data.put(i, i + 1);
if (i % 2 == 0) {
srv0Cache2.put(i, i + 1);
c2Data.put(i, i + 1);
}
}
checkCacheData(c1Data, "c1");
checkCacheData(c2Data, "c2");
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class IgniteCacheGroupsTest method checkLocalData.
/**
* @param idx Node index.
* @param cacheName Cache name.
* @param data Expected data.
* @throws Exception If failed.
*/
private void checkLocalData(int idx, String cacheName, Integer[] data) throws Exception {
Ignite ignite = ignite(idx);
ClusterNode node = ignite.cluster().localNode();
IgniteCache cache = ignite.<Integer, Integer>cache(cacheName);
Affinity aff = affinity(cache);
Set<Integer> locKeys = new TreeSet<>();
for (int key = 0; key < data.length; key++) {
if (aff.isPrimaryOrBackup(node, key))
locKeys.add(key);
}
Iterable<Cache.Entry<Integer, Integer>> locEntries = cache.localEntries(CachePeekMode.OFFHEAP);
for (Cache.Entry<Integer, Integer> entry : locEntries) {
assertTrue(locKeys.remove(entry.getKey()));
assertEquals(data[entry.getKey()], entry.getValue());
}
assertTrue(locKeys.isEmpty());
}
Aggregations