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();
}
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));
}
}
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);
}
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);
}
}
}
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();
}
Aggregations