Search in sources :

Example 1 with IgniteCompute

use of org.apache.ignite.IgniteCompute in project ignite by apache.

the class ComputeRunnableExample 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")) {
        System.out.println();
        System.out.println("Compute runnable example started.");
        IgniteCompute compute = ignite.compute();
        // Iterate through all words in the sentence and create runnable jobs.
        for (final String word : "Print words using runnable".split(" ")) {
            // Execute runnable on some node.
            compute.run(() -> {
                System.out.println();
                System.out.println(">>> Printing '" + word + "' on this node from ignite job.");
            });
        }
        System.out.println();
        System.out.println(">>> Finished printing words using runnable execution.");
        System.out.println(">>> Check all nodes for output (this node is also part of the cluster).");
    }
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 2 with IgniteCompute

use of org.apache.ignite.IgniteCompute in project ignite by apache.

the class GridClusterStateProcessor method sendComputeChangeGlobalState.

/**
 * @param activate New cluster state.
 * @param resFut State change future.
 */
private void sendComputeChangeGlobalState(boolean activate, BaselineTopology blt, boolean forceBlt, final GridFutureAdapter<Void> resFut) {
    AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
    if (log.isInfoEnabled()) {
        log.info("Sending " + prettyStr(activate) + " request from node [id=" + ctx.localNodeId() + ", topVer=" + topVer + ", client=" + ctx.clientNode() + ", daemon=" + ctx.isDaemon() + "]");
    }
    IgniteCompute comp = ((ClusterGroupAdapter) ctx.cluster().get().forServers()).compute();
    IgniteFuture<Void> fut = comp.runAsync(new ClientChangeGlobalStateComputeRequest(activate, blt, forceBlt));
    fut.listen(new CI1<IgniteFuture>() {

        @Override
        public void apply(IgniteFuture fut) {
            try {
                fut.get();
                resFut.onDone();
            } catch (Exception e) {
                resFut.onDone(e);
            }
        }
    });
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteCompute(org.apache.ignite.IgniteCompute) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) ClusterGroupAdapter(org.apache.ignite.internal.cluster.ClusterGroupAdapter)

Example 3 with IgniteCompute

use of org.apache.ignite.IgniteCompute in project ignite by apache.

the class LargeEntryUpdateTest method testEntryUpdate.

/**
 * @throws Exception If failed.
 */
@Test
public void testEntryUpdate() throws Exception {
    try (Ignite ignite = startGrid()) {
        for (int i = 0; i < CACHE_COUNT; ++i) {
            IgniteCache<Long, byte[]> cache = ignite.cache(CACHE_PREFIX + i);
            cache.put(0L, new byte[PAGE_SIZE * 2]);
        }
        IgniteCompute compute = ignite.compute().withAsync();
        long endTime = System.currentTimeMillis() + WAIT_TIMEOUT;
        int iter = 0;
        while (System.currentTimeMillis() < endTime) {
            log.info("Iteration: " + iter++);
            cacheUpdate.set(true);
            try {
                List<IgniteFuture> futs = new ArrayList<>();
                for (int i = 0; i < THREAD_COUNT; ++i) {
                    compute.run(new CacheUpdater());
                    futs.add(compute.future());
                }
                Thread.sleep(30_000);
                cacheUpdate.set(false);
                for (IgniteFuture fut : futs) fut.get();
            } finally {
                cacheUpdate.set(false);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with IgniteCompute

use of org.apache.ignite.IgniteCompute in project ignite by apache.

the class DistributedComputing method async.

void async(Ignite ignite) {
    // tag::async[]
    IgniteCompute compute = ignite.compute();
    Collection<IgniteCallable<Integer>> calls = new ArrayList<>();
    // Iterate through all words in the sentence and create callable jobs.
    for (String word : "Count characters using a callable".split(" ")) calls.add(word::length);
    IgniteFuture<Collection<Integer>> future = compute.callAsync(calls);
    future.listen(fut -> {
        // Total number of characters.
        int total = fut.get().stream().mapToInt(Integer::intValue).sum();
        System.out.println("Total number of characters: " + total);
    });
// end::async[]
}
Also used : IgniteCallable(org.apache.ignite.lang.IgniteCallable) ArrayList(java.util.ArrayList) Collection(java.util.Collection) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 5 with IgniteCompute

use of org.apache.ignite.IgniteCompute in project ignite by apache.

the class ClusterAPI method remoteNodes.

@Test
void remoteNodes() {
    // tag::remote-nodes[]
    Ignite ignite = Ignition.ignite();
    IgniteCluster cluster = ignite.cluster();
    // Get compute instance which will only execute
    // over remote nodes, i.e. all the nodes except for this one.
    IgniteCompute compute = ignite.compute(cluster.forRemotes());
    // Broadcast to all remote nodes and print the ID of the node
    // on which this closure is executing.
    compute.broadcast(() -> System.out.println("Hello Node: " + ignite.cluster().localNode().id()));
// end::remote-nodes[]
}
Also used : IgniteCluster(org.apache.ignite.IgniteCluster) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute) Test(org.junit.jupiter.api.Test)

Aggregations

IgniteCompute (org.apache.ignite.IgniteCompute)63 Ignite (org.apache.ignite.Ignite)30 Test (org.junit.Test)20 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)18 IgniteException (org.apache.ignite.IgniteException)15 ArrayList (java.util.ArrayList)12 IgniteFuture (org.apache.ignite.lang.IgniteFuture)10 UUID (java.util.UUID)9 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 IgniteEx (org.apache.ignite.internal.IgniteEx)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 Nullable (org.jetbrains.annotations.Nullable)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3