Search in sources :

Example 71 with ClusterNode

use of org.apache.ignite.cluster.ClusterNode in project ignite by apache.

the class AgentClusterDemo method tryStart.

/**
 * Start ignite node with cacheEmployee and populate it with data.
 */
public static CountDownLatch tryStart() {
    if (initGuard.compareAndSet(false, true)) {
        log.info("DEMO: Starting embedded nodes for demo...");
        System.setProperty(IGNITE_NO_ASCII, "true");
        System.setProperty(IGNITE_QUIET, "false");
        System.setProperty(IGNITE_UPDATE_NOTIFIER, "false");
        System.setProperty(IGNITE_ATOMIC_CACHE_DELETE_HISTORY_SIZE, "20");
        System.setProperty(IGNITE_PERFORMANCE_SUGGESTIONS_DISABLED, "true");
        final AtomicInteger basePort = new AtomicInteger(60700);
        final AtomicInteger cnt = new AtomicInteger(-1);
        final ScheduledExecutorService execSrv = newScheduledThreadPool(1, "demo-nodes-start");
        execSrv.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                int idx = cnt.incrementAndGet();
                int port = basePort.get();
                boolean first = idx == 0;
                try {
                    IgniteConfiguration cfg = igniteConfiguration(port, idx, false);
                    if (first) {
                        U.delete(Paths.get(cfg.getWorkDirectory()));
                        U.resolveWorkDirectory(cfg.getWorkDirectory(), cfg.getDataStorageConfiguration().getStoragePath(), true);
                    }
                    Ignite ignite = Ignition.start(cfg);
                    if (first) {
                        ClusterNode node = ignite.cluster().localNode();
                        Collection<String> jettyAddrs = node.attribute(ATTR_REST_JETTY_ADDRS);
                        if (jettyAddrs == null) {
                            Ignition.stopAll(true);
                            throw new IgniteException("DEMO: Failed to start Jetty REST server on embedded node");
                        }
                        String jettyHost = jettyAddrs.iterator().next();
                        Integer jettyPort = node.attribute(ATTR_REST_JETTY_PORT);
                        if (F.isEmpty(jettyHost) || jettyPort == null)
                            throw new IgniteException("DEMO: Failed to start Jetty REST handler on embedded node");
                        log.info("DEMO: Started embedded node for demo purpose [TCP binary port={}, Jetty REST port={}]", port, jettyPort);
                        demoUrl = String.format("http://%s:%d", jettyHost, jettyPort);
                        initLatch.countDown();
                    }
                } catch (Throwable e) {
                    if (first) {
                        basePort.getAndAdd(50);
                        log.warn("DEMO: Failed to start embedded node.", e);
                    } else
                        log.error("DEMO: Failed to start embedded node.", e);
                } finally {
                    if (idx == NODE_CNT) {
                        Ignite ignite = Ignition.ignite(SRV_NODE_NAME + 0);
                        if (ignite != null) {
                            ignite.cluster().active(true);
                            deployServices(ignite.services(ignite.cluster().forServers()));
                        }
                        log.info("DEMO: All embedded nodes for demo successfully started");
                        execSrv.shutdown();
                    }
                }
            }
        }, 1, 5, TimeUnit.SECONDS);
    }
    return initLatch;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite)

Example 72 with ClusterNode

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

Example 73 with ClusterNode

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);
}
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 74 with ClusterNode

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;
    });
}
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 75 with ClusterNode

use of org.apache.ignite.cluster.ClusterNode in project ignite by apache.

the class HadoopTestRoundRobinMrPlanner method preparePlan.

/**
 * {@inheritDoc}
 */
@Override
public HadoopMapReducePlan preparePlan(HadoopJob job, Collection<ClusterNode> top, @Nullable HadoopMapReducePlan oldPlan) throws IgniteCheckedException {
    if (top.isEmpty())
        throw new IllegalArgumentException("Topology is empty");
    // Has at least one element.
    Iterator<ClusterNode> it = top.iterator();
    Map<UUID, Collection<HadoopInputSplit>> mappers = new HashMap<>();
    for (HadoopInputSplit block : job.input()) {
        ClusterNode node = it.next();
        Collection<HadoopInputSplit> nodeBlocks = mappers.get(node.id());
        if (nodeBlocks == null) {
            nodeBlocks = new ArrayList<>();
            mappers.put(node.id(), nodeBlocks);
        }
        nodeBlocks.add(block);
        if (!it.hasNext())
            it = top.iterator();
    }
    int[] rdc = new int[job.reducers()];
    for (int i = 0; i < rdc.length; i++) rdc[i] = i;
    return new HadoopDefaultMapReducePlan(mappers, Collections.singletonMap(it.next().id(), rdc));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) HadoopInputSplit(org.apache.ignite.hadoop.HadoopInputSplit) HadoopDefaultMapReducePlan(org.apache.ignite.internal.processors.hadoop.planner.HadoopDefaultMapReducePlan) Collection(java.util.Collection) UUID(java.util.UUID)

Aggregations

ClusterNode (org.apache.ignite.cluster.ClusterNode)1104 UUID (java.util.UUID)281 ArrayList (java.util.ArrayList)280 Test (org.junit.Test)276 Ignite (org.apache.ignite.Ignite)239 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)239 HashMap (java.util.HashMap)184 Map (java.util.Map)182 List (java.util.List)165 IgniteException (org.apache.ignite.IgniteException)147 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)147 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)143 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)126 Collection (java.util.Collection)113 Message (org.apache.ignite.plugin.extensions.communication.Message)106 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)87 HashSet (java.util.HashSet)85 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)82 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)81 IgniteEx (org.apache.ignite.internal.IgniteEx)81