Search in sources :

Example 56 with IgniteCompute

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

the class GridClosureProcessorSelfTest method testCallAsyncErrorNoFailover.

/**
 * @throws Exception If failed.
 */
@Test
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) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 57 with IgniteCompute

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

the class IgniteComputeCustomExecutorSelfTest method testAllComputeApiByCustomExecutor.

/**
 * @throws Exception If fails.
 */
@Test
public void testAllComputeApiByCustomExecutor() throws Exception {
    IgniteCompute comp = grid(0).compute().withExecutor(EXEC_NAME0);
    comp.affinityRun(CACHE_NAME, primaryKey(grid(1).cache(CACHE_NAME)), new IgniteRunnable() {

        @Override
        public void run() {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
        }
    });
    comp.affinityCall(CACHE_NAME, 0, new IgniteCallable<Object>() {

        @Override
        public Object call() throws Exception {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    });
    comp.broadcast(new IgniteRunnable() {

        @Override
        public void run() {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
        }
    });
    comp.broadcast(new IgniteCallable<Object>() {

        @Override
        public Object call() throws Exception {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    });
    comp.broadcast(new IgniteClosure<Object, Object>() {

        @Override
        public Object apply(Object o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, 0);
    comp.apply(new IgniteClosure<Object, Object>() {

        @Override
        public Object apply(Object o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, 0);
    comp.apply(new IgniteClosure<Integer, Object>() {

        @Override
        public Object apply(Integer o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, Collections.singletonList(0));
    comp.apply(new IgniteClosure<Integer, Object>() {

        @Override
        public Object apply(Integer o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, Collections.singletonList(0), new IgniteReducer<Object, Object>() {

        @Override
        public boolean collect(@Nullable Object o) {
            return true;
        }

        @Override
        public Object reduce() {
            return null;
        }
    });
    List<IgniteCallable<Object>> calls = new ArrayList<>();
    for (int i = 0; i < GRID_CNT * 2; ++i) {
        calls.add(new IgniteCallable<Object>() {

            @Override
            public Object call() throws Exception {
                assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
                return null;
            }
        });
    }
    comp.call(calls.get(0));
    comp.call(calls);
    comp.call(calls, new IgniteReducer<Object, Object>() {

        @Override
        public boolean collect(@Nullable Object o) {
            return true;
        }

        @Override
        public Object reduce() {
            return null;
        }
    });
    List<IgniteRunnable> runs = new ArrayList<>();
    for (int i = 0; i < GRID_CNT * 2; ++i) {
        runs.add(new IgniteRunnable() {

            @Override
            public void run() {
                assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            }
        });
    }
    comp.run(runs.get(0));
    comp.run(runs);
    comp.execute(TestTask.class, null);
}
Also used : ArrayList(java.util.ArrayList) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteException(org.apache.ignite.IgniteException) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteCompute(org.apache.ignite.IgniteCompute) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 58 with IgniteCompute

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

the class IgniteClientReconnectApiExceptionTest method igniteOperationsTest.

/**
 * @throws Exception If failed.
 */
private void igniteOperationsTest() throws Exception {
    final Ignite client = startClientGrid(serverCount());
    final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
    final CountDownLatch recvLatch = new CountDownLatch(1);
    assertNotNull(dfltCache);
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check compute.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.compute();
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.compute();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCompute comp = (IgniteCompute) o;
            Collection<UUID> uuids = comp.broadcast(new IgniteCallable<UUID>() {

                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public UUID call() throws Exception {
                    return ignite.cluster().localNode().id();
                }
            });
            assertFalse(uuids.isEmpty());
            for (UUID uuid : uuids) assertNotNull(uuid);
            return true;
        }
    }), // Check ping node.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.cluster().pingNode(new UUID(0, 0));
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.cluster().pingNode(new UUID(0, 0));
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            Boolean pingNode = (Boolean) o;
            assertFalse(pingNode);
            return true;
        }
    }), // Check register remote listener.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.events().remoteListen(null, new IgnitePredicate<Event>() {

                    @Override
                    public boolean apply(Event event) {
                        return true;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.events().remoteListen(null, new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event event) {
                    return true;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            UUID remoteId = (UUID) o;
            assertNotNull(remoteId);
            client.events().stopRemoteListen(remoteId);
            return true;
        }
    }), // Check message operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {

                    @Override
                    public boolean apply(UUID uuid, Object o) {
                        if (o.equals("Test message."))
                            recvLatch.countDown();
                        return true;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {

                @Override
                public boolean apply(UUID uuid, Object o) {
                    if (o.equals("Test message."))
                        recvLatch.countDown();
                    return true;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteMessaging msg = client.message();
            msg.send(null, "Test message.");
            try {
                assertTrue(recvLatch.await(2, SECONDS));
            } catch (InterruptedException ignored) {
                fail("Message wasn't received.");
            }
            return true;
        }
    }), // Check executor.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.executorService().submit(new Callable<Integer>() {

                    @Override
                    public Integer call() throws Exception {
                        return 42;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.executorService().submit(new Callable<Integer>() {

                @Override
                public Integer call() throws Exception {
                    return 42;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            Future<Integer> fut = (Future<Integer>) o;
            try {
                assertEquals(42, (int) fut.get());
            } catch (Exception ignored) {
                fail("Failed submit task.");
            }
            return true;
        }
    })));
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteMessaging(org.apache.ignite.IgniteMessaging) IgniteCallable(org.apache.ignite.lang.IgniteCallable) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 59 with IgniteCompute

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

the class GridMultithreadedJobStealingSelfTest method testJoinedNodeCanStealJobs.

/**
 * Test newly joined node can steal jobs.
 *
 * @throws Exception If test failed.
 */
@Test
public void testJoinedNodeCanStealJobs() throws Exception {
    final AtomicReference<Exception> fail = new AtomicReference<>(null);
    final AtomicInteger stolen = new AtomicInteger(0);
    final AtomicInteger noneStolen = new AtomicInteger(0);
    final GridConcurrentHashSet nodes = new GridConcurrentHashSet();
    int threadsNum = 10;
    final int jobsPerTask = 4;
    jobExecutedLatch = new CountDownLatch(threadsNum);
    final IgniteInternalFuture<Long> future = GridTestUtils.runMultiThreadedAsync(new Runnable() {

        /**
         */
        @Override
        public void run() {
            try {
                final IgniteCompute compute = ignite.compute().withAsync();
                compute.execute(new JobStealingTask(jobsPerTask), null);
                JobStealingResult res = (JobStealingResult) compute.future().get();
                info("Task result: " + res);
                stolen.addAndGet(res.stolen);
                noneStolen.addAndGet(res.nonStolen);
                nodes.addAll(res.nodes);
            } catch (IgniteException e) {
                log.error("Failed to execute task.", e);
                fail.getAndSet(e);
            }
        }
    }, threadsNum, "JobStealingThread");
    // Wait for first job begin execution.
    jobExecutedLatch.await();
    startGrid(2);
    for (Ignite g : G.allGrids()) info("Metrics [nodeId=" + g.cluster().localNode().id() + ", metrics=" + g.cluster().localNode().metrics() + ']');
    future.get();
    assertNull("Test failed with exception: ", fail.get());
    // Total jobs number is threadsNum * 4
    assertEquals("Incorrect processed jobs number", threadsNum * jobsPerTask, stolen.get() + noneStolen.get());
    assertFalse("No jobs were stolen.", stolen.get() == 0);
    for (Ignite g : G.allGrids()) assertTrue("Node get no jobs.", nodes.contains(g.name()));
    assertTrue("Stats [stolen=" + stolen + ", noneStolen=" + noneStolen + ']', Math.abs(stolen.get() - 2 * noneStolen.get()) <= 8);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 60 with IgniteCompute

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

the class IgniteClientReconnectFailoverTest method testReconnectComputeApi.

/**
 * @throws Exception If failed.
 */
@Test
public void testReconnectComputeApi() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCompute comp = client.compute();
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            comp.call(new DummyClosure());
            comp.broadcast(new DummyClosure());
            return null;
        }
    });
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) Test(org.junit.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