Search in sources :

Example 1 with ClientCompute

use of org.apache.ignite.client.ClientCompute in project ignite by apache.

the class ComputeTaskRemoteSecurityContextTest method testIgniteClient.

/**
 * Tests task execution security context in case task was initiated from the {@link IgniteClient}.
 */
@Test
public void testIgniteClient() throws Exception {
    String login = "thin_client";
    ClientConfiguration cfg = new ClientConfiguration().setAddresses(Config.SERVER).setUserName(login).setUserPassword("");
    try (IgniteClient cli = Ignition.startClient(cfg)) {
        ClientCompute comp = cli.compute(cli.cluster().forNodes(cli.cluster().nodes()));
        if (failWithTimeout)
            comp = comp.withTimeout(TEST_TASK_TIMEOUT);
        String taskName = mapAsync ? MapAsyncTestTask.class.getName() : TestTask.class.getName();
        Throwable timeoutE = null;
        try {
            if (async)
                comp.executeAsync2(taskName, login).get();
            else
                comp.execute(taskName, login);
            checkTaskEvents("crd", login, REDUCER_SUCCEEDED_TASK_EVENTS, MAP_NODE_SUCCEEDED_TASK_EVENTS);
        } catch (Throwable e) {
            if (!failWithTimeout)
                throw e;
            timeoutE = e;
        }
        if (failWithTimeout) {
            assertNotNull(timeoutE);
            assertTrue(X.hasCause(timeoutE, "Task timed out", ClientServerError.class));
            checkTaskEvents("crd", login, REDUCER_FAILED_TASK_EVENTS, MAP_NODE_FAILED_TASK_EVENTS);
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientServerError(org.apache.ignite.internal.client.thin.ClientServerError) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) GridClientConfiguration(org.apache.ignite.internal.client.GridClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Example 2 with ClientCompute

use of org.apache.ignite.client.ClientCompute in project ignite by apache.

the class ComputeTaskTest method testTaskWithNoFailover.

/**
 */
@Test
public void testTaskWithNoFailover() throws Exception {
    try (IgniteClient client = startClient(0)) {
        ClientCompute computeWithFailover = client.compute();
        ClientCompute computeWithNoFailover = client.compute().withNoFailover();
        assertTrue(computeWithFailover.execute(TestFailoverTask.class.getName(), null));
        assertFalse(computeWithNoFailover.execute(TestFailoverTask.class.getName(), null));
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Example 3 with ClientCompute

use of org.apache.ignite.client.ClientCompute in project ignite by apache.

the class ComputeTaskTest method testExecuteTaskConcurrentLoad.

/**
 */
@Test
public void testExecuteTaskConcurrentLoad() throws Exception {
    try (IgniteClient client = startClient(0)) {
        int threadsCnt = 20;
        int iterations = 100;
        ClientCache<Integer, Integer> cache = client.getOrCreateCache(DEFAULT_CACHE_NAME);
        AtomicInteger threadIdxs = new AtomicInteger();
        CyclicBarrier barrier = new CyclicBarrier(threadsCnt);
        GridTestUtils.runMultiThreaded(() -> {
            int threadIdx = threadIdxs.incrementAndGet();
            Random rnd = new Random();
            try {
                barrier.await();
                for (int i = 0; i < iterations; i++) {
                    int nodeIdx = rnd.nextInt(GRIDS_CNT);
                    cache.put(threadIdx, i);
                    ClientCompute compute = client.compute(client.cluster().forNodeId(nodeId(nodeIdx)));
                    Future<T2<UUID, Set<UUID>>> fut = compute.executeAsync(TestTask.class.getName(), null);
                    boolean cancelled = (i % 3 == 0) && fut.cancel(true);
                    assertEquals((Integer) i, cache.get(threadIdx));
                    if (cancelled)
                        assertTrue(fut.isCancelled());
                    else
                        assertEquals(nodeIds(nodeIdx), fut.get().get2());
                }
            } catch (ExecutionException e) {
                log.error("Task failed: ", e);
                fail("Task failed");
            } catch (InterruptedException | BrokenBarrierException ignore) {
            // No-op.
            }
        }, threadsCnt, "run-task-async");
        assertTrue(GridTestUtils.waitForCondition(() -> ((ClientComputeImpl) client.compute()).activeTasksCount() == 0, TIMEOUT));
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) T2(org.apache.ignite.internal.util.typedef.T2) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Example 4 with ClientCompute

use of org.apache.ignite.client.ClientCompute in project ignite by apache.

the class ComputeTaskTest method testActiveTasksLimit.

/**
 */
@Test
public void testActiveTasksLimit() throws Exception {
    try (IgniteClient client = startClient(0)) {
        ClientCompute compute = client.compute(client.cluster().forNodeId(nodeId(1)));
        CountDownLatch latch = TestLatchTask.latch = new CountDownLatch(1);
        List<Future<T2<UUID, Set<UUID>>>> futs = new ArrayList<>(ACTIVE_TASKS_LIMIT);
        for (int i = 0; i < ACTIVE_TASKS_LIMIT; i++) futs.add(compute.executeAsync(TestLatchTask.class.getName(), null));
        assertTrue(GridTestUtils.waitForCondition(() -> ((ClientComputeImpl) client.compute()).activeTasksCount() == ACTIVE_TASKS_LIMIT, TIMEOUT));
        // Check that we can't start more tasks.
        GridTestUtils.assertThrowsAnyCause(null, () -> compute.executeAsync(TestLatchTask.class.getName(), null).get(), ClientException.class, "limit");
        // Check that cancelled tasks restore limit.
        for (int i = 0; i < ACTIVE_TASKS_LIMIT / 2; i++) futs.get(i).cancel(true);
        latch.countDown();
        // Check that successfully complited tasks restore limit.
        for (int i = ACTIVE_TASKS_LIMIT / 2; i < ACTIVE_TASKS_LIMIT; i++) assertEquals(nodeIds(1), futs.get(i).get(TIMEOUT, TimeUnit.MILLISECONDS).get2());
        // Check that complited with error tasks restore limit.
        GridTestUtils.assertThrowsAnyCause(null, () -> compute.execute("NoSuchTask", null), ClientException.class, null);
        // Check that we can start up to ACTIVE_TASKS_LIMIT new active tasks again.
        latch = TestLatchTask.latch = new CountDownLatch(1);
        futs = new ArrayList<>(ACTIVE_TASKS_LIMIT);
        for (int i = 0; i < ACTIVE_TASKS_LIMIT; i++) futs.add(compute.executeAsync(TestLatchTask.class.getName(), null));
        latch.countDown();
        for (Future<T2<UUID, Set<UUID>>> fut : futs) assertEquals(nodeIds(1), fut.get(TIMEOUT, TimeUnit.MILLISECONDS).get2());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteClient(org.apache.ignite.client.IgniteClient) Future(java.util.concurrent.Future) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Example 5 with ClientCompute

use of org.apache.ignite.client.ClientCompute in project ignite by apache.

the class ComputeTaskTest method testExecuteTaskTwoClientsToOneNode.

/**
 */
@Test
public void testExecuteTaskTwoClientsToOneNode() throws Exception {
    try (IgniteClient client1 = startClient(0);
        IgniteClient client2 = startClient(0)) {
        ClientCompute compute1 = client1.compute(client1.cluster().forNodeId(nodeId(1)));
        ClientCompute compute2 = client2.compute(client2.cluster().forNodeId(nodeId(2)));
        CountDownLatch latch1 = TestLatchTask.latch = new CountDownLatch(1);
        TestLatchTask.startLatch = new CountDownLatch(1);
        Future<T2<UUID, Set<UUID>>> fut1 = compute1.executeAsync(TestLatchTask.class.getName(), null);
        TestLatchTask.startLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
        CountDownLatch latch2 = TestLatchTask.latch = new CountDownLatch(1);
        TestLatchTask.startLatch = new CountDownLatch(1);
        Future<T2<UUID, Set<UUID>>> fut2 = compute2.executeAsync(TestLatchTask.class.getName(), null);
        TestLatchTask.startLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
        latch2.countDown();
        assertEquals(nodeIds(2), fut2.get().get2());
        assertFalse(fut1.isDone());
        latch1.countDown();
        assertEquals(nodeIds(1), fut1.get().get2());
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Aggregations

ClientCompute (org.apache.ignite.client.ClientCompute)8 IgniteClient (org.apache.ignite.client.IgniteClient)8 Test (org.junit.Test)8 UUID (java.util.UUID)5 T2 (org.apache.ignite.internal.util.typedef.T2)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Random (java.util.Random)1 Set (java.util.Set)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ClientClusterGroup (org.apache.ignite.client.ClientClusterGroup)1 IgniteClientFuture (org.apache.ignite.client.IgniteClientFuture)1 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)1 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)1