use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.
the class GridCacheCommandHandler method executeCommand.
/**
* Executes command on flagged cache projection. Checks {@code destId} to find if command could be performed locally
* or routed to a remote node.
*
* @param destId Target node Id for the operation. If {@code null} - operation could be executed anywhere.
* @param clientId Client ID.
* @param cacheName Cache name.
* @param skipStore Skip store.
* @param key Key to set affinity mapping in the response.
* @param op Operation to perform.
* @return Operation result in future.
* @throws IgniteCheckedException If failed
*/
private IgniteInternalFuture<GridRestResponse> executeCommand(@Nullable UUID destId, UUID clientId, final String cacheName, final boolean skipStore, final Object key, final CacheProjectionCommand op) throws IgniteCheckedException {
final boolean locExec = destId == null || destId.equals(ctx.localNodeId()) || replicatedCacheAvailable(cacheName);
if (locExec) {
IgniteInternalCache<?, ?> prj = localCache(cacheName).forSubjectId(clientId).setSkipStore(skipStore);
return op.apply((IgniteInternalCache<Object, Object>) prj, ctx).chain(resultWrapper((IgniteInternalCache<Object, Object>) prj, key));
} else {
ClusterGroup prj = ctx.grid().cluster().forPredicate(F.nodeForNodeId(destId));
ctx.task().setThreadContext(TC_NO_FAILOVER, true);
return ctx.closure().callAsync(BALANCE, new FlaggedCacheOperationCallable(clientId, cacheName, skipStore, op, key), prj.nodes());
}
}
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 BinaryMetadataUpdatesFlowTest method startComputation.
/**
* Starts new ignite node and submits computation job to it.
* @param idx Index.
* @param stopFlag Stop flag.
*/
private void startComputation(int idx, AtomicBoolean stopFlag) throws Exception {
clientMode = false;
final IgniteEx ignite0 = startGrid(idx);
ClusterGroup cg = ignite0.cluster().forNodeId(ignite0.localNode().id());
ignite0.compute(cg).withAsync().call(new BinaryObjectAdder(ignite0, updatesQueue, 30, stopFlag));
}
use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.
the class TcpCommunicationSpiHalfOpenedConnectionTest method checkReconnect.
/**
* @throws Exception If failed.
*/
private void checkReconnect() throws Exception {
Ignite srv = startGrid("server");
Ignite client = startGrid("client");
UUID nodeId = srv.cluster().localNode().id();
System.out.println(">> Server ID: " + nodeId);
ClusterGroup srvGrp = client.cluster().forNodeId(nodeId);
System.out.println(">> Send job");
// Establish connection
client.compute(srvGrp).run(F.noop());
ConcurrentMap<UUID, GridCommunicationClient[]> clients = U.field(clientSpi, "clients");
ConcurrentMap<?, GridNioRecoveryDescriptor> recoveryDescs = U.field(clientSpi, "recoveryDescs");
ConcurrentMap<?, GridNioRecoveryDescriptor> outRecDescs = U.field(clientSpi, "outRecDescs");
ConcurrentMap<?, GridNioRecoveryDescriptor> inRecDescs = U.field(clientSpi, "inRecDescs");
GridNioServerListener<Message> lsnr = U.field(clientSpi, "srvLsnr");
Iterator<GridNioRecoveryDescriptor> it = F.concat(recoveryDescs.values().iterator(), outRecDescs.values().iterator(), inRecDescs.values().iterator());
while (it.hasNext()) {
GridNioRecoveryDescriptor desc = it.next();
// Need to simulate connection close in GridNioServer as it
// releases descriptors on disconnect.
desc.release();
}
// Remove client to avoid calling close(), in that case server
// will close connection too, but we want to keep the server
// uninformed and force ping old connection.
GridCommunicationClient[] clients0 = clients.remove(nodeId);
for (GridCommunicationClient commClient : clients0) lsnr.onDisconnected(((GridTcpNioCommunicationClient) commClient).session(), new IOException("Test exception"));
info(">> Removed client");
// Reestablish connection
client.compute(srvGrp).run(F.noop());
info(">> Sent second job");
}
use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.
the class ClusterAPI method example.
@Test
void example(Ignite ignite) {
// tag::group-examples[]
IgniteCluster cluster = ignite.cluster();
// All nodes on which the cache with name "myCache" is deployed,
// either in client or server mode.
ClusterGroup cacheGroup = cluster.forCacheNodes("myCache");
// All data nodes responsible for caching data for "myCache".
ClusterGroup dataGroup = cluster.forDataNodes("myCache");
// All client nodes that can access "myCache".
ClusterGroup clientGroup = cluster.forClientNodes("myCache");
// end::group-examples[]
}
Aggregations