Search in sources :

Example 21 with IgniteCompute

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

the class IgniteClientReconnectApiExceptionTest method igniteOperationsTest.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
public void igniteOperationsTest() throws Exception {
    clientMode = true;
    final Ignite client = startGrid(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;
        }
    })));
    clientMode = false;
}
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 22 with IgniteCompute

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

the class IgniteClientReconnectFailoverTest method testReconnectComputeApi.

/**
 * @throws Exception If failed.
 */
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)

Example 23 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.
 */
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 ConcurrentHashSet nodes = new ConcurrentHashSet();
    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 * 3
    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()) <= 6);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 24 with IgniteCompute

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

the class TaskNodeRestartTest method testTaskNodeRestart.

/**
 * @throws Exception If failed.
 */
public void testTaskNodeRestart() throws Exception {
    final AtomicBoolean finished = new AtomicBoolean();
    final AtomicInteger stopIdx = new AtomicInteger();
    IgniteInternalFuture<?> restartFut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            int idx = stopIdx.getAndIncrement();
            int node = NODES + idx;
            while (!finished.get()) {
                log.info("Start node: " + node);
                startGrid(node);
                U.sleep(300);
                log.info("Stop node: " + node);
                stopGrid(node);
            }
            return null;
        }
    }, 2, "stop-thread");
    IgniteInternalFuture<?> fut = null;
    try {
        final long stopTime = System.currentTimeMillis() + 60_000;
        final AtomicInteger idx = new AtomicInteger();
        fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                int node = idx.getAndIncrement() % NODES;
                Ignite ignite = ignite(node);
                log.info("Start thread: " + ignite.name());
                IgniteCompute compute = ignite.compute();
                while (U.currentTimeMillis() < stopTime) {
                    try {
                        compute.broadcast(new TestCallable());
                        compute.call(new TestCallable());
                        compute.execute(new TestTask1(), null);
                        compute.execute(new TestTask2(), null);
                    } catch (IgniteException e) {
                        log.info("Error: " + e);
                    }
                }
                return null;
            }
        }, 20, "test-thread");
        fut.get(90_000);
        finished.set(true);
        restartFut.get();
    } finally {
        finished.set(true);
        if (fut != null)
            fut.cancel();
        restartFut.get(5000);
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 25 with IgniteCompute

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

the class IgniteComputeTopologyExceptionTest method testCorrectException.

/**
 * @throws Exception If failed.
 */
public void testCorrectException() throws Exception {
    Ignite ignite = ignite(0);
    IgniteCompute comp = ignite.compute(ignite.cluster().forRemotes()).withNoFailover();
    stopGrid(1);
    try {
        comp.call(new IgniteCallable<Object>() {

            @Override
            public Object call() throws Exception {
                fail("Should not be called.");
                return null;
            }
        });
        fail();
    } catch (ClusterTopologyException e) {
        log.info("Expected exception: " + e);
    }
}
Also used : Ignite(org.apache.ignite.Ignite) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteCompute(org.apache.ignite.IgniteCompute) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException)

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