Search in sources :

Example 31 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 32 with IgniteCompute

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

the class CacheManualRebalancingTest method testRebalance.

/**
 * @throws Exception If failed.
 */
public void testRebalance() throws Exception {
    // Fill cache with large dataset to make rebalancing slow.
    try (IgniteDataStreamer<Object, Object> streamer = grid(0).dataStreamer(MYCACHE)) {
        for (int i = 0; i < 100_000; i++) streamer.addData(i, i);
    }
    // Start new node.
    final IgniteEx newNode = startGrid(NODES_CNT);
    int newNodeCacheSize;
    // Start manual rebalancing.
    IgniteCompute compute = newNode.compute().withAsync();
    compute.broadcast(new MyCallable());
    final ComputeTaskFuture<Object> rebalanceTaskFuture = compute.future();
    boolean rebalanceFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return rebalanceTaskFuture.isDone();
        }
    }, 10_000);
    assertTrue(rebalanceFinished);
    assertTrue(newNode.context().cache().cache(MYCACHE).context().preloader().rebalanceFuture().isDone());
    newNodeCacheSize = newNode.cache(MYCACHE).localSize(CachePeekMode.ALL);
    System.out.println("New node cache local size: " + newNodeCacheSize);
    assertTrue(newNodeCacheSize > 0);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 33 with IgniteCompute

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

the class GridClosureProcessorSelfTest method callAsync.

/**
 * @param idx Node index.
 * @param jobs Callable job.
 * @param p Optional node predicate.
 * @return Future object.
 */
private IgniteFuture<Collection<Integer>> callAsync(int idx, Collection<ClosureTestCallable> jobs, @Nullable IgnitePredicate<ClusterNode> p) {
    assert idx >= 0 && idx < NODES_CNT;
    assert !F.isEmpty(jobs);
    execCntr.set(0);
    IgniteCompute comp = p != null ? compute(grid(idx).cluster().forPredicate(p)) : grid(idx).compute();
    return comp.callAsync(jobs);
}
Also used : IgniteCompute(org.apache.ignite.IgniteCompute)

Example 34 with IgniteCompute

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

the class GridClosureProcessorSelfTest method testCallAsyncErrorNoFailover.

/**
 * @throws Exception If failed.
 */
public void testCallAsyncErrorNoFailover() throws Exception {
    IgniteCompute comp = compute(grid(0).cluster().forPredicate(F.notEqualTo(grid(0).localNode())));
    IgniteFuture<Integer> fut = comp.withNoFailover().callAsync(new ClosureTestCallableError());
    try {
        fut.get();
        assert false : "Exception should have been thrown.";
    } catch (IgniteException e) {
        info("Caught expected exception: " + e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 35 with IgniteCompute

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

the class GridClosureProcessorSelfTest method broadcast.

/**
 * @param idx Node index.
 * @param job Callable job.
 * @param p Optional node predicate.
 * @return Future object.
 */
private IgniteFuture<Collection<Integer>> broadcast(int idx, IgniteCallable<Integer> job, @Nullable IgnitePredicate<ClusterNode> p) {
    assert idx >= 0 && idx < NODES_CNT;
    assert job != null;
    execCntr.set(0);
    IgniteCompute comp = p != null ? compute(grid(idx).cluster().forPredicate(p)) : grid(idx).compute();
    return comp.broadcastAsync(job);
}
Also used : IgniteCompute(org.apache.ignite.IgniteCompute)

Aggregations

IgniteCompute (org.apache.ignite.IgniteCompute)41 Ignite (org.apache.ignite.Ignite)19 IgniteException (org.apache.ignite.IgniteException)14 ArrayList (java.util.ArrayList)10 IgniteFuture (org.apache.ignite.lang.IgniteFuture)9 UUID (java.util.UUID)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 Nullable (org.jetbrains.annotations.Nullable)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)3 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)3 Collection (java.util.Collection)2 Callable (java.util.concurrent.Callable)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2