Search in sources :

Example 1 with ClusterGroup

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());
    }
}
Also used : IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup)

Example 2 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 3 with ClusterGroup

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));
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) ClusterGroup(org.apache.ignite.cluster.ClusterGroup)

Example 4 with ClusterGroup

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");
}
Also used : Message(org.apache.ignite.plugin.extensions.communication.Message) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IOException(java.io.IOException) GridCommunicationClient(org.apache.ignite.internal.util.nio.GridCommunicationClient) GridTcpNioCommunicationClient(org.apache.ignite.internal.util.nio.GridTcpNioCommunicationClient) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 5 with ClusterGroup

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[]
}
Also used : IgniteCluster(org.apache.ignite.IgniteCluster) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Test(org.junit.jupiter.api.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