use of org.apache.ignite.Ignite 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.Ignite 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.Ignite 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.Ignite in project ignite by apache.
the class TcpClientDiscoverySpiFailureTimeoutSelfTest method testFailureTimeoutServerClient.
/**
* Test failure detection time between server and client if client fail with failure detection.
*
* @throws Exception in case of error.
*/
public void testFailureTimeoutServerClient() throws Exception {
failureThreshold = 3000;
clientFailureDetectionTimeout = 2000;
try {
startServerNodes(1);
startClientNodes(1);
checkNodes(1, 1);
Ignite srvNode = G.ignite("server-0");
final TcpDiscoverySpi srvSpi = (TcpDiscoverySpi) srvNode.configuration().getDiscoverySpi();
Ignite clientNode = G.ignite("client-0");
final TcpDiscoverySpi clientSpi = (TcpDiscoverySpi) clientNode.configuration().getDiscoverySpi();
long failureTime = U.currentTimeMillis();
final long[] failureDetectTime = new long[1];
final CountDownLatch latch = new CountDownLatch(1);
clientSpi.simulateNodeFailure();
srvNode.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
failureDetectTime[0] = U.currentTimeMillis();
latch.countDown();
return true;
}
}, EVT_NODE_FAILED);
assertTrue("Can't get node failure event", latch.await(15000, TimeUnit.MILLISECONDS));
long detectTime = failureDetectTime[0] - failureTime;
assertTrue("Client node failure detected too fast: " + detectTime + "ms", detectTime > clientFailureDetectionTimeout - 200);
assertTrue("Client node failure detected too slow: " + detectTime + "ms", detectTime < clientFailureDetectionTimeout + 5000);
} finally {
failureThreshold = FAILURE_THRESHOLD;
}
}
use of org.apache.ignite.Ignite in project ignite by apache.
the class GAGridFunction method getChromosomes.
/**
* Helper routine to return 'pivoted' results using the provided query param
*
* @param query Sql
* @return Result set
*/
private static SimpleResultSet getChromosomes(String query) {
Ignite ignite = Ignition.localIgnite();
List<Chromosome> chromosomes = GAGridUtils.getChromosomes(ignite, query);
SimpleResultSet rs2 = new SimpleResultSet();
Chromosome aChrom = chromosomes.get(0);
int genesCount = aChrom.getGenes().length;
rs2.addColumn("Chromosome Id", Types.INTEGER, 0, 0);
rs2.addColumn("Fitness Score", Types.DOUBLE, 0, 0);
for (int i = 0; i < genesCount; i++) {
int columnIndex = i + 1;
rs2.addColumn("Gene " + columnIndex, Types.VARCHAR, 0, 0);
}
for (Chromosome rowChrom : chromosomes) {
Object[] row = new Object[genesCount + 2];
row[0] = rowChrom.id();
row[1] = rowChrom.getFitnessScore();
List<Gene> genes = GAGridUtils.getGenesInOrderForChromosome(ignite, rowChrom);
int i = 2;
for (Gene gene : genes) {
row[i] = gene.getValue().toString();
i = i + 1;
}
// Add a row for an individual Chromosome
rs2.addRow(row);
}
return rs2;
}
Aggregations