Search in sources :

Example 11 with GridClientCompute

use of org.apache.ignite.internal.client.GridClientCompute in project ignite by apache.

the class CommandHandler method executeTask.

/**
 * @param client Client
 * @return Task result.
 * @throws GridClientException If failed to execute task.
 */
private <R> R executeTask(GridClient client, Class<?> taskCls, Object taskArgs) throws GridClientException {
    GridClientCompute compute = client.compute();
    List<GridClientNode> nodes = new ArrayList<>();
    for (GridClientNode node : compute.nodes()) if (node.connectable())
        nodes.add(node);
    if (F.isEmpty(nodes))
        throw new GridClientDisconnectedException("Connectable node not found", null);
    GridClientNode node = compute.balancer().balancedNode(nodes);
    return compute.projection(node).execute(taskCls.getName(), new VisorTaskArgument<>(node.nodeId(), taskArgs, false));
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) ArrayList(java.util.ArrayList)

Example 12 with GridClientCompute

use of org.apache.ignite.internal.client.GridClientCompute in project ignite by apache.

the class ClientAbstractSelfTest method testNoAsyncExceptions.

/**
 * Check async API methods don't generate exceptions.
 *
 * @throws Exception If failed.
 */
@Test
public void testNoAsyncExceptions() throws Exception {
    GridClient client = client();
    GridClientData data = client.data(CACHE_NAME);
    GridClientCompute compute = client.compute().projection(new GridClientPredicate<GridClientNode>() {

        @Override
        public boolean apply(GridClientNode e) {
            return false;
        }
    });
    Map<String, GridClientFuture<?>> futs = new LinkedHashMap<>();
    futs.put("exec", compute.executeAsync("taskName", "taskArg"));
    futs.put("affExec", compute.affinityExecuteAsync("taskName", "cacheName", "affKey", "taskArg"));
    futs.put("refreshById", compute.refreshNodeAsync(UUID.randomUUID(), true, true));
    futs.put("refreshByIP", compute.refreshNodeAsync("nodeIP", true, true));
    futs.put("refreshTop", compute.refreshTopologyAsync(true, true));
    GridClientFactory.stop(client.id(), false);
    futs.put("put", data.putAsync("key", "val"));
    futs.put("putAll", data.putAllAsync(F.asMap("key", "val")));
    futs.put("get", data.getAsync("key"));
    futs.put("getAll", data.getAllAsync(Collections.singletonList("key")));
    futs.put("remove", data.removeAsync("key"));
    futs.put("removeAll", data.removeAllAsync(Collections.singletonList("key")));
    futs.put("replace", data.replaceAsync("key", "val"));
    futs.put("cas", data.casAsync("key", "val", "val2"));
    futs.put("metrics", data.metricsAsync());
    for (Map.Entry<String, GridClientFuture<?>> e : futs.entrySet()) {
        try {
            e.getValue().get();
            info("Expects '" + e.getKey() + "' fails with grid client exception.");
        } catch (GridServerUnreachableException | GridClientClosedException ignore) {
        // No op: compute projection is empty.
        }
    }
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientFuture(org.apache.ignite.internal.client.GridClientFuture) GridClient(org.apache.ignite.internal.client.GridClient) LinkedHashMap(java.util.LinkedHashMap) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) GridClientData(org.apache.ignite.internal.client.GridClientData) GridClientClosedException(org.apache.ignite.internal.client.GridClientClosedException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 13 with GridClientCompute

use of org.apache.ignite.internal.client.GridClientCompute in project ignite by apache.

the class ClientAbstractSelfTest method testForceShutdown.

/**
 * @throws Exception If failed.
 */
@Test
public void testForceShutdown() throws Exception {
    GridClientCompute compute = client.compute();
    Object taskArg = getTaskArgument();
    String taskName = getSleepTaskName();
    GridClientFuture<Object> fut = compute.executeAsync(taskName, taskArg);
    GridClientFactory.stop(client.id(), false);
    try {
        fut.get();
    } catch (GridClientClosedException ignored) {
        return;
    }
    Assert.fail("Expected GridClientClosedException.");
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientClosedException(org.apache.ignite.internal.client.GridClientClosedException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 14 with GridClientCompute

use of org.apache.ignite.internal.client.GridClientCompute in project ignite by apache.

the class ClientAbstractSelfTest method testGracefulShutdown.

/**
 * @throws Exception If failed.
 */
@Test
public void testGracefulShutdown() throws Exception {
    GridClientCompute compute = client.compute();
    Object taskArg = getTaskArgument();
    String taskName = getSleepTaskName();
    GridClientFuture<Object> fut = compute.executeAsync(taskName, taskArg);
    GridClientFuture<Object> fut2 = compute.executeAsync(taskName, taskArg);
    GridClientFactory.stop(client.id(), true);
    Assert.assertEquals(17, fut.get());
    Assert.assertEquals(17, fut2.get());
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 15 with GridClientCompute

use of org.apache.ignite.internal.client.GridClientCompute in project ignite by apache.

the class PropertyAbstractSubCommand method execute.

/**
 * {@inheritDoc}
 */
@Override
public final Object execute(GridClientConfiguration clientCfg, Logger log) throws Exception {
    try (GridClient client = Command.startClient(clientCfg)) {
        GridClientCompute compute = client.compute();
        // Try to find connectable server nodes.
        Collection<GridClientNode> nodes = compute.nodes((n) -> n.connectable() && !n.isClient());
        if (F.isEmpty(nodes)) {
            nodes = compute.nodes(GridClientNode::connectable);
            if (F.isEmpty(nodes))
                throw new GridClientDisconnectedException("Connectable nodes not found", null);
        }
        GridClientNode node = nodes.stream().findAny().orElse(null);
        if (node == null)
            node = compute.balancer().balancedNode(nodes);
        MetadataResultDto res = compute.projection(node).execute(taskName(), new VisorTaskArgument<>(node.nodeId(), arg(), false));
        printResult(res, log);
    } catch (Throwable e) {
        log.severe("Failed to execute metadata command='" + name() + "'");
        log.severe(CommandLogger.errorMessage(e));
        throw e;
    }
    return null;
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) GridClient(org.apache.ignite.internal.client.GridClient)

Aggregations

GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)15 Test (org.junit.Test)10 GridClientNode (org.apache.ignite.internal.client.GridClientNode)9 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)9 GridClient (org.apache.ignite.internal.client.GridClient)6 GridClientDisconnectedException (org.apache.ignite.internal.client.GridClientDisconnectedException)4 GridClientException (org.apache.ignite.internal.client.GridClientException)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 GridClientClosedException (org.apache.ignite.internal.client.GridClientClosedException)2 GridClientConfiguration (org.apache.ignite.internal.client.GridClientConfiguration)2 GridClientFuture (org.apache.ignite.internal.client.GridClientFuture)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1