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;
}
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));
}
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));
}
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() + ']');
}
}
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);
}
}
Aggregations