Search in sources :

Example 76 with Ignite

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);
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 77 with Ignite

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);
}
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) A(org.apache.ignite.internal.util.typedef.internal.A) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 78 with Ignite

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;
    });
}
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) A(org.apache.ignite.internal.util.typedef.internal.A) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 79 with Ignite

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;
    }
}
Also used : DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 80 with Ignite

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;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) Gene(org.apache.ignite.ml.genetic.Gene) Chromosome(org.apache.ignite.ml.genetic.Chromosome) Ignite(org.apache.ignite.Ignite)

Aggregations

Ignite (org.apache.ignite.Ignite)2007 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)358 CountDownLatch (java.util.concurrent.CountDownLatch)238 IgniteCache (org.apache.ignite.IgniteCache)234 IgniteException (org.apache.ignite.IgniteException)216 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)215 Transaction (org.apache.ignite.transactions.Transaction)194 ArrayList (java.util.ArrayList)177 ClusterNode (org.apache.ignite.cluster.ClusterNode)152 UUID (java.util.UUID)137 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)135 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)128 CacheException (javax.cache.CacheException)112 Event (org.apache.ignite.events.Event)112 HashMap (java.util.HashMap)105 List (java.util.List)89 IgniteEx (org.apache.ignite.internal.IgniteEx)85 Map (java.util.Map)84 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)81 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)78