use of org.apache.ignite.cluster.ClusterNode in project ignite by apache.
the class IgnitePutTxPrimaryOnlyBenchmark method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
IgniteCache<Integer, Object> cache = cacheForOperation();
int key;
Affinity<Object> aff = ignite().affinity("tx");
ClusterNode locNode = ignite().cluster().localNode();
for (; ; ) {
key = nextRandom(args.range());
// Exit only if primary.
if (aff.isPrimary(locNode, key))
break;
}
// Implicit transaction is used.
cache.put(key, new SampleValue(key));
return true;
}
use of org.apache.ignite.cluster.ClusterNode 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, IgniteConsumer<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<>();
for (K k : ks) {
V v = cache.localPeek(k);
fun.accept(new CacheEntryImpl<>(k, v));
m.put(k, v);
}
cache.putAll(m);
});
}
use of org.apache.ignite.cluster.ClusterNode 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.cluster.ClusterNode 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.cluster.ClusterNode in project ignite by apache.
the class GridJobStealingCollisionSpiCustomTopologySelfTest method testThiefNodeNotInTopology.
/**
* @throws Exception If test failed.
*/
public void testThiefNodeNotInTopology() throws Exception {
List<CollisionJobContext> waitCtxs = new ArrayList<>(2);
final ClusterNode node = getSpiContext().nodes().iterator().next();
Collections.addAll(waitCtxs, new GridTestCollisionJobContext(createTaskSession(node), IgniteUuid.randomUuid()), new GridTestCollisionJobContext(createTaskSession(node), IgniteUuid.randomUuid()), new GridTestCollisionJobContext(createTaskSession(node), IgniteUuid.randomUuid()));
Collection<CollisionJobContext> activeCtxs = new ArrayList<>(1);
// Add active.
Collections.addAll(activeCtxs, new GridTestCollisionJobContext(createTaskSession(node), IgniteUuid.randomUuid()));
// Emulate message to steal 2 jobs.
getSpiContext().triggerMessage(rmtNode2, new JobStealingRequest(2));
getSpi().onCollision(new GridCollisionTestContext(activeCtxs, waitCtxs));
// Check no action.
checkNoAction((GridTestCollisionJobContext) waitCtxs.get(0));
checkNoAction((GridTestCollisionJobContext) waitCtxs.get(1));
checkNoAction((GridTestCollisionJobContext) waitCtxs.get(2));
// Make sure that no message was sent.
Serializable msg1 = getSpiContext().removeSentMessage(getSpiContext().localNode());
assert msg1 == null;
Serializable mgs2 = getSpiContext().removeSentMessage(rmtNode1);
assert mgs2 == null;
Serializable msg3 = getSpiContext().removeSentMessage(rmtNode2);
assert msg3 == null;
}
Aggregations