Search in sources :

Example 56 with ClusterGroup

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

the class ComputeUtils method affinityCallWithRetries.

/**
 * Calls the specified {@code fun} function on all partitions so that is't guaranteed that partitions with the same
 * index of all specified caches will be placed on the same node and will not be moved before computation is
 * finished. If partitions are placed on different nodes then call will be retried, but not more than {@code
 * retries} times with {@code interval} interval specified in milliseconds.
 *
 * @param ignite Ignite instance.
 * @param cacheNames Collection of cache names.
 * @param fun Function to be applied on all partitions.
 * @param retries Number of retries for the case when one of partitions not found on the node.
 * @param interval Interval of retries for the case when one of partitions not found on the node.
 * @param <R> Type of a result.
 * @return Collection of results.
 */
public static <R> Collection<R> affinityCallWithRetries(Ignite ignite, Collection<String> cacheNames, IgniteFunction<Integer, R> fun, int retries, int interval) {
    assert cacheNames.size() > 0;
    assert interval >= 0;
    String primaryCache = cacheNames.iterator().next();
    Affinity<?> affinity = ignite.affinity(primaryCache);
    int partitions = affinity.partitions();
    BitSet completionFlags = new BitSet(partitions);
    Collection<R> results = new ArrayList<>();
    for (int t = 0; t <= retries; t++) {
        ClusterGroup clusterGrp = ignite.cluster().forDataNodes(primaryCache);
        // Sends jobs.
        Map<Integer, IgniteFuture<R>> futures = new HashMap<>();
        for (int part = 0; part < partitions; part++) if (!completionFlags.get(part)) {
            final int currPart = part;
            futures.put(currPart, ignite.compute(clusterGrp).affinityCallAsync(cacheNames, currPart, () -> fun.apply(currPart)));
        }
        // Collects results.
        for (int part : futures.keySet()) try {
            R res = futures.get(part).get();
            results.add(res);
            completionFlags.set(part);
        } catch (IgniteException ignore) {
        }
        if (completionFlags.cardinality() == partitions)
            return results;
        LockSupport.parkNanos(interval * 1_000_000);
    }
    throw new IllegalStateException();
}
Also used : HashMap(java.util.HashMap) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteException(org.apache.ignite.IgniteException)

Example 57 with ClusterGroup

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

the class ClusterGroupExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
            return;
        System.out.println();
        System.out.println("Compute example started.");
        IgniteCluster cluster = ignite.cluster();
        // Say hello to all nodes in the cluster, including local node.
        sayHello(ignite, cluster);
        // Say hello to all remote nodes.
        sayHello(ignite, cluster.forRemotes());
        // Pick random node out of remote nodes.
        ClusterGroup randomNode = cluster.forRemotes().forRandom();
        // Say hello to a random node.
        sayHello(ignite, randomNode);
        // Say hello to all nodes residing on the same host with random node.
        sayHello(ignite, cluster.forHost(randomNode.node()));
        // Say hello to all nodes that have current CPU load less than 50%.
        sayHello(ignite, cluster.forPredicate(n -> n.metrics().getCurrentCpuLoad() < 0.5));
    }
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignition(org.apache.ignite.Ignition) IgniteCluster(org.apache.ignite.IgniteCluster) ExamplesUtils(org.apache.ignite.examples.ExamplesUtils) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) IgniteCluster(org.apache.ignite.IgniteCluster) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 58 with ClusterGroup

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

the class GridCacheAdapter method sizeLongAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<Long> sizeLongAsync(final int part, final CachePeekMode[] peekModes) {
    assert peekModes != null;
    final PeekModes modes = parsePeekModes(peekModes, true);
    IgniteClusterEx cluster = ctx.grid().cluster();
    final GridCacheAffinityManager aff = ctx.affinity();
    final AffinityTopologyVersion topVer = aff.affinityTopologyVersion();
    ClusterGroup grp = cluster.forDataNodes(name());
    Collection<ClusterNode> nodes = new ArrayList<>(grp.forPredicate(new IgnitePredicate<ClusterNode>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean apply(ClusterNode clusterNode) {
            return ((modes.primary && aff.primaryByPartition(clusterNode, part, topVer)) || (modes.backup && aff.backupByPartition(clusterNode, part, topVer)));
        }
    }).nodes());
    if (nodes.isEmpty())
        return new GridFinishedFuture<>(0L);
    ctx.kernalContext().task().setThreadContext(TC_SUBGRID, nodes);
    return ctx.kernalContext().task().execute(new PartitionSizeLongTask(ctx.name(), ctx.affinity().affinityTopologyVersion(), peekModes, part), null);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteClusterNode(org.apache.ignite.internal.managers.discovery.IgniteClusterNode) IgniteClusterEx(org.apache.ignite.internal.cluster.IgniteClusterEx) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) ClusterGroup(org.apache.ignite.cluster.ClusterGroup)

Example 59 with ClusterGroup

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

the class MessagingPingPongExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    // Game is played over the default ignite.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
            return;
        System.out.println();
        System.out.println(">>> Messaging ping-pong example started.");
        // Pick random remote node as a partner.
        ClusterGroup nodeB = ignite.cluster().forRemotes().forRandom();
        // Note that both nodeA and nodeB will always point to
        // same nodes regardless of whether they were implicitly
        // serialized and deserialized on another node as part of
        // anonymous closure's state during its remote execution.
        // Set up remote player.
        ignite.message(nodeB).remoteListen(null, (nodeId, rcvMsg) -> {
            System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']');
            if ("PING".equals(rcvMsg)) {
                ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PONG");
                // Continue listening.
                return true;
            }
            // Unsubscribe.
            return false;
        });
        int MAX_PLAYS = 10;
        final CountDownLatch cnt = new CountDownLatch(MAX_PLAYS);
        // Set up local player.
        ignite.message().localListen(null, (nodeId, rcvMsg) -> {
            System.out.println("Received message [msg=" + rcvMsg + ", sender=" + nodeId + ']');
            if (cnt.getCount() == 1) {
                ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "STOP");
                cnt.countDown();
                // Stop listening.
                return false;
            } else if ("PONG".equals(rcvMsg))
                ignite.message(ignite.cluster().forNodeId(nodeId)).send(null, "PING");
            else
                throw new IgniteException("Received unexpected message: " + rcvMsg);
            cnt.countDown();
            // Continue listening.
            return true;
        });
        // Serve!
        ignite.message(nodeB).send(null, "PING");
        // Wait til the game is over.
        try {
            cnt.await();
        } catch (InterruptedException e) {
            System.err.println("Hm... let us finish the game!\n" + e);
        }
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 60 with ClusterGroup

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

the class GridProjectionForCachesOnDaemonNodeSelfTest method testForDataNodes.

/**
 * @throws Exception If failed.
 */
@Test
public void testForDataNodes() throws Exception {
    ClusterGroup grp = ignite.cluster().forDataNodes(DEFAULT_CACHE_NAME);
    assertFalse(grp.nodes().isEmpty());
    try {
        daemon.cluster().forDataNodes(DEFAULT_CACHE_NAME);
    } catch (IllegalStateException ignored) {
        return;
    }
    fail();
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

ClusterGroup (org.apache.ignite.cluster.ClusterGroup)99 Ignite (org.apache.ignite.Ignite)51 Test (org.junit.Test)49 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)36 UUID (java.util.UUID)26 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClusterNode (org.apache.ignite.cluster.ClusterNode)16 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)14 IgniteException (org.apache.ignite.IgniteException)12 ArrayList (java.util.ArrayList)9 IgniteCompute (org.apache.ignite.IgniteCompute)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IgniteCluster (org.apache.ignite.IgniteCluster)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)5 IgniteFuture (org.apache.ignite.lang.IgniteFuture)5 Map (java.util.Map)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 IgniteClusterEx (org.apache.ignite.internal.cluster.IgniteClusterEx)4 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)4