Search in sources :

Example 1 with GridClientDisconnectedException

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

the class MetadataAbstractSubCommand 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)

Example 2 with GridClientDisconnectedException

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

the class TaskExecutor method executeTaskByNameOnNode.

/**
 * @param client Client
 * @param taskClsName Task class name.
 * @param taskArgs Task args.
 * @param nodeId Node ID to execute task at (if null, random node will be chosen by balancer).
 * @param clientCfg
 * @return Task result.
 * @throws GridClientException If failed to execute task.
 */
public static <R> R executeTaskByNameOnNode(GridClient client, String taskClsName, Object taskArgs, UUID nodeId, GridClientConfiguration clientCfg) throws GridClientException {
    GridClientCompute compute = client.compute();
    if (nodeId == BROADCAST_UUID) {
        Collection<GridClientNode> nodes = compute.nodes(GridClientNode::connectable);
        if (F.isEmpty(nodes))
            throw new GridClientDisconnectedException("Connectable nodes not found", null);
        List<UUID> nodeIds = nodes.stream().map(GridClientNode::nodeId).collect(Collectors.toList());
        return client.compute().execute(taskClsName, new VisorTaskArgument<>(nodeIds, taskArgs, false));
    }
    GridClientNode node = null;
    if (nodeId == null) {
        // Prefer node from connect string.
        final String cfgAddr = clientCfg.getServers().iterator().next();
        String[] parts = cfgAddr.split(":");
        if (DFLT_HOST.equals(parts[0])) {
            InetAddress addr;
            try {
                addr = IgniteUtils.getLocalHost();
            } catch (IOException e) {
                throw new GridClientException("Can't get localhost name.", e);
            }
            if (addr.isLoopbackAddress())
                throw new GridClientException("Can't find localhost name.");
            String origAddr = addr.getHostName() + ":" + parts[1];
            node = listHosts(client).filter(tuple -> origAddr.equals(tuple.get2())).findFirst().map(IgniteBiTuple::get1).orElse(null);
            if (node == null)
                node = listHostsByClientNode(client).filter(tuple -> tuple.get2().size() == 1 && cfgAddr.equals(tuple.get2().get(0))).findFirst().map(IgniteBiTuple::get1).orElse(null);
        } else
            node = listHosts(client).filter(tuple -> cfgAddr.equals(tuple.get2())).findFirst().map(IgniteBiTuple::get1).orElse(null);
        // Otherwise choose random node.
        if (node == null)
            node = getBalancedNode(compute);
    } else {
        for (GridClientNode n : compute.nodes()) {
            if (n.connectable() && nodeId.equals(n.nodeId())) {
                node = n;
                break;
            }
        }
        if (node == null)
            throw new IllegalArgumentException("Node with id=" + nodeId + " not found");
    }
    return compute.projection(node).execute(taskClsName, new VisorTaskArgument<>(node.nodeId(), taskArgs, false));
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientException(org.apache.ignite.internal.client.GridClientException) F(org.apache.ignite.internal.util.typedef.F) GridClient(org.apache.ignite.internal.client.GridClient) GridClientConfiguration(org.apache.ignite.internal.client.GridClientConfiguration) Collection(java.util.Collection) IOException(java.io.IOException) UUID(java.util.UUID) VisorTaskArgument(org.apache.ignite.internal.visor.VisorTaskArgument) Collectors(java.util.stream.Collectors) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) InetAddress(java.net.InetAddress) List(java.util.List) Stream(java.util.stream.Stream) GridClientException(org.apache.ignite.internal.client.GridClientException) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) ComputeTask(org.apache.ignite.compute.ComputeTask) GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridClientNode(org.apache.ignite.internal.client.GridClientNode) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) IOException(java.io.IOException) UUID(java.util.UUID) InetAddress(java.net.InetAddress)

Example 3 with GridClientDisconnectedException

use of org.apache.ignite.internal.client.GridClientDisconnectedException 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 4 with GridClientDisconnectedException

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

the class GridClientImpl method tryInitTopology.

/**
 * Tries to init client topology using configured set of servers or routers.
 *
 * @throws GridClientException If initialisation failed.
 * @throws InterruptedException If initialisation was interrupted.
 */
private void tryInitTopology() throws GridClientException, InterruptedException {
    boolean hasSrvs = routers.isEmpty();
    final Collection<InetSocketAddress> connSrvs = (hasSrvs) ? new LinkedHashSet<>(srvs) : routers;
    if (hasSrvs) {
        // Add REST endpoints for all nodes from previous topology snapshot.
        try {
            for (GridClientNodeImpl node : top.nodes()) {
                Collection<InetSocketAddress> endpoints = node.availableAddresses(cfg.getProtocol(), true);
                List<InetSocketAddress> resolvedEndpoints = new ArrayList<>(endpoints.size());
                for (InetSocketAddress endpoint : endpoints) if (!endpoint.isUnresolved())
                    resolvedEndpoints.add(endpoint);
                boolean sameHost = node.attributes().isEmpty() || F.containsAny(U.allLocalMACs(), node.attribute(ATTR_MACS).toString().split(", "));
                if (sameHost) {
                    Collections.sort(resolvedEndpoints, U.inetAddressesComparator(true));
                    connSrvs.addAll(resolvedEndpoints);
                } else {
                    for (InetSocketAddress endpoint : resolvedEndpoints) if (!endpoint.getAddress().isLoopbackAddress())
                        connSrvs.add(endpoint);
                }
            }
        } catch (GridClientDisconnectedException ignored) {
        // Ignore if latest topology update failed.
        }
    }
    connMgr.init(connSrvs);
    Map<String, GridClientCacheMode> overallCaches = new HashMap<>();
    for (GridClientNodeImpl node : top.nodes()) overallCaches.putAll(node.caches());
    for (Map.Entry<String, GridClientCacheMode> entry : overallCaches.entrySet()) {
        GridClientDataAffinity affinity = affinity(entry.getKey());
        if (affinity instanceof GridClientPartitionAffinity && entry.getValue() != GridClientCacheMode.PARTITIONED)
            log.warning(GridClientPartitionAffinity.class.getSimpleName() + " is used for a cache configured " + "for non-partitioned mode [cacheName=" + entry.getKey() + ", cacheMode=" + entry.getValue() + ']');
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridClientPartitionAffinity(org.apache.ignite.internal.client.GridClientPartitionAffinity) InetSocketAddress(java.net.InetSocketAddress) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) ArrayList(java.util.ArrayList) GridClientDataAffinity(org.apache.ignite.internal.client.GridClientDataAffinity) GridClientCacheMode(org.apache.ignite.internal.client.GridClientCacheMode) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with GridClientDisconnectedException

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

the class ClientFailedInitSelfTest method doTestRouter.

/**
 * @param p Protocol.
 * @throws Exception If failed.
 */
@SuppressWarnings("BusyWait")
private void doTestRouter(GridClientProtocol p) throws Exception {
    startRouters();
    GridClient c = client(p, true);
    for (int i = 0; i < RECONN_CNT; i++) {
        try {
            c.compute().nodes();
            fail("Nodes list should fail while grid is stopped.");
        } catch (GridClientDisconnectedException e) {
            assertTrue(X.hasCause(e, GridClientException.class));
        }
        startGrid();
        try {
            Thread.sleep(TOP_REFRESH_PERIOD * 2);
            c.compute().nodes();
            assertEquals("arg", c.compute().execute(TestTask.class.getName(), "arg"));
        } finally {
            stopGrid();
        }
        Thread.sleep(TOP_REFRESH_PERIOD * 2);
    }
}
Also used : GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) GridClient(org.apache.ignite.internal.client.GridClient)

Aggregations

GridClientDisconnectedException (org.apache.ignite.internal.client.GridClientDisconnectedException)9 GridClient (org.apache.ignite.internal.client.GridClient)5 GridClientNode (org.apache.ignite.internal.client.GridClientNode)5 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)4 ArrayList (java.util.ArrayList)3 InetSocketAddress (java.net.InetSocketAddress)2 UUID (java.util.UUID)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ComputeTask (org.apache.ignite.compute.ComputeTask)1 GridClientCacheMode (org.apache.ignite.internal.client.GridClientCacheMode)1