/** {@inheritDoc} */ @Override public boolean isValid(int timeout) throws SQLException { ensureNotClosed(); if (timeout < 0) throw new SQLException("Invalid timeout: " + timeout); try { return client.compute().<Boolean>executeAsync(VALID_TASK_NAME, cacheName).get(timeout, SECONDS); } catch (GridClientDisconnectedException | GridClientFutureTimeoutException e) { throw new SQLException("Failed to establish connection.", e); } catch (GridClientException ignored) { return false; } }
/** * Execute task. * * @param taskCls Task class. * @param args Arguments. * @return Result. * @throws IOException If failed. * @throws GridClientException If failed. */ private <T> T execute(Class taskCls, Object... args) throws IOException, GridClientException { HadoopProtocolTaskArguments args0 = args != null ? new HadoopProtocolTaskArguments(args) : null; return cli.client().compute().execute(taskCls.getName(), args0); }
/** * @param client Client. * @return List of hosts. */ private Stream<IgniteBiTuple<GridClientNode, String>> listHosts(GridClient client) throws GridClientException { return client.compute() .nodes(GridClientNode::connectable) .stream() .flatMap(node -> Stream.concat( node.tcpAddresses() == null ? Stream.empty() : node.tcpAddresses().stream(), node.tcpHostNames() == null ? Stream.empty() : node.tcpHostNames().stream() ) .map(addr -> new IgniteBiTuple<>(node, addr + ":" + node.tcpPort()))); }
/** * @param client Client. * @return List of hosts. */ private Stream<IgniteBiTuple<GridClientNode, List<String>>> listHostsByClientNode( GridClient client ) throws GridClientException { return client.compute().nodes(GridClientNode::connectable).stream() .map( node -> new IgniteBiTuple<>( node, Stream.concat( node.tcpAddresses() == null ? Stream.empty() : node.tcpAddresses().stream(), node.tcpHostNames() == null ? Stream.empty() : node.tcpHostNames().stream() ) .map(addr -> addr + ":" + node.tcpPort()).collect(Collectors.toList()) ) ); }
@Override public void run() { long processed; while ((processed = cnt.getAndIncrement()) < taskExecutionCount()) { try { if (processed > 0 && processed % STATISTICS_PRINT_STEP == 0) info(">>>>>>> " + processed + " tasks finished."); String res = client.compute().execute(TestTask.class.getName(), null); execQueue.add(res); } catch (GridClientException e) { err.compareAndSet(null, e); } } } }, THREAD_CNT, "client-task-request");
/** * @throws Exception If failed. */ @SuppressWarnings("TypeMayBeWeakened") private void executeTest(GridClientLoadBalancer b) throws Exception { try (GridClient c = client(b)) { Set<String> executions = new HashSet<>(); for (int i = 0; i < NODES_CNT * 10; i++) executions.add( c.compute().<String>execute(TestTask.class.getName(), null)); assertEquals(NODES_CNT / 2, executions.size()); for (int i = 0; i < NODES_CNT / 2; i++) executions.contains(grid(i).localNode().id().toString()); } }
@Override public Object call() throws Exception { GridClient client = GridClientFactory.start(getClientConfiguration()); client.compute().refreshTopology(false, false); return null; } },
/** * @throws Exception If failed. */ @Test public void testTaskAfterRestart() throws Exception { startGrids(1); GridClientConfiguration cfg = new GridClientConfiguration(); cfg.setProtocol(GridClientProtocol.TCP); cfg.setServers(Collections.singleton("127.0.0.1:" + PORT)); GridClient cli = GridClientFactory.start(cfg); cli.compute().execute(ClientTcpTask.class.getName(), Collections.singletonList("arg")); stopAllGrids(); startGrid(); cli.compute().execute(ClientTcpTask.class.getName(), Collections.singletonList("arg")); } }
/** * Tests correct behavior in case of shutdown node used to refresh topology state. * * @throws Exception If error occurs. */ @Test public void testRefreshTopologyOnNodeLeft() throws Exception { startRestNode("grid1", null, null); startRestNode("grid2", null, null); GridClient cli = startClient(LOOPBACK_IP, defaultRestPort()); List<GridClientNode> nodes = cli.compute().refreshTopology(true, false); assertEquals(2, nodes.size()); stopGrid("grid1"); nodes = cli.compute().refreshTopology(true, false); assertEquals(1, nodes.size()); startRestNode("grid3", null, null); nodes = cli.compute().refreshTopology(true, false); assertEquals(2, nodes.size()); stopGrid("grid2"); nodes = cli.compute().refreshTopology(true, false); assertEquals(1, nodes.size()); }
/** * @throws Exception If failed. */ @Test public void testExecute() throws Exception { String taskName = getTaskName(); Object taskArg = getTaskArgument(); GridClientCompute compute = client.compute(); assertEquals(Integer.valueOf(17), compute.execute(taskName, taskArg)); assertEquals(Integer.valueOf(17), compute.executeAsync(taskName, taskArg).get()); }
/** * @param cipherSuites list of cipher suites * @param protocols list of protocols * @throws Exception If failed. */ private void checkSuccessfulClientStart(String[] cipherSuites, String[] protocols) throws Exception { this.cipherSuites = F.isEmpty(cipherSuites) ? null : cipherSuites; this.protocols = F.isEmpty(protocols) ? null : protocols; try (GridClient client = GridClientFactory.start(getClientConfiguration())) { List<GridClientNode> top = client.compute().refreshTopology(false, false); assertEquals(1, top.size()); } }
/** * @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()); }
/** * @throws Exception If failed. */ @Test public void testConnectable() throws Exception { GridClient client = client(); List<GridClientNode> nodes = client.compute().refreshTopology(false, false); assertTrue(F.first(nodes).connectable()); }
/** * @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."); }
/** * @param connectIp IP to test. * @param nodeIp Expected IP reported to client. * @throws GridClientException If failed. */ private void checkConnectivityByIp(String connectIp, IgniteBiTuple<Collection<String>, Collection<String>> nodeIp) throws GridClientException { GridClient cli = startClient(connectIp, defaultRestPort()); List<GridClientNode> nodes = cli.compute().refreshTopology(true, false); assertEquals(1, nodes.size()); GridClientNode node = F.first(nodes); assertNotNull(node); assertTrue(eqAddresses(nodeIp, node)); }
/** * @throws Exception If failed. */ @Test @Override public void testConnectable() throws Exception { GridClient client = client(); List<GridClientNode> nodes = client.compute().refreshTopology(false, false); assertFalse(F.first(nodes).connectable()); } }
/** * @throws Exception If failed. */ @Test public void testManyTasksRun() throws Exception { GridClientCompute compute = client.compute(); for (int i = 0; i < 1000; i++) assertEquals(new Integer("executing".length()), compute.execute(TestTask.class.getName(), "executing")); GridClientFactory.stop(client.id(), true); IgniteKernal g = (IgniteKernal)grid(0); Map<GridRestCommand, GridRestCommandHandler> handlers = U.field(g.context().rest(), "handlers"); GridTaskCommandHandler taskHnd = (GridTaskCommandHandler)F.find(handlers.values(), null, new P1<GridRestCommandHandler>() { @Override public boolean apply(GridRestCommandHandler e) { return e instanceof GridTaskCommandHandler; } }); assertNotNull("GridTaskCommandHandler was not found", taskHnd); ConcurrentLinkedHashMap taskDesc = U.field(taskHnd, "taskDescs"); assertTrue("Task result map size exceeded max value [mapSize=" + taskDesc.sizex() + ", " + "maxSize=" + MAX_TASK_RESULTS + ']', taskDesc.sizex() <= MAX_TASK_RESULTS); }
/** * @throws Exception If failed. */ @Test public void testProjectionRun() throws Exception { GridClientCompute dflt = client.compute(); Collection<? extends GridClientNode> nodes = dflt.nodes(); assertEquals(NODES_CNT, nodes.size()); for (int i = 0; i < NODES_CNT; i++) { Ignite g = grid(i); assert g != null; GridClientNode clientNode = dflt.node(g.cluster().localNode().id()); assertNotNull("Client node for " + g.cluster().localNode().id() + " was not found", clientNode); GridClientCompute prj = dflt.projection(clientNode); String res = prj.execute(TestTask.class.getName(), null); assertNotNull(res); assertEquals(g.cluster().localNode().id().toString(), res); } }
/** * Test that thin client will be able to connect to node with custom binary serializer and custom consistent ID. * * @throws Exception If failed. */ @Test public void testThinClientConnected() throws Exception { UUID nid = ignite(0).cluster().localNode().id(); GridClientConfiguration clnCfg = new GridClientConfiguration(); clnCfg.setProtocol(GridClientProtocol.TCP); clnCfg.setServers(Collections.singleton("127.0.0.1:11211")); clnCfg.setBalancer(new GridClientRoundRobinBalancer()); // Start client. GridClient client = GridClientFactory.start(clnCfg); // Execute some task. client.compute().execute(VisorNodePingTask.class.getName(), new VisorTaskArgument<>(nid, new VisorNodePingTaskArg(nid), false)); GridClientFactory.stop(client.id(), false); }
/** * @throws Exception If failed. */ @Test public void testFailedInit() throws Exception { for (int i = 0; i < ClientTestRestServer.SERVERS_CNT; i++) runServer(i, true); GridClient c = client(); try { c.compute().execute("fake", "arg"); fail("Client operation should fail when server resets connections."); } catch (GridClientDisconnectedException e) { assertTrue("Thrown exception doesn't have an expected cause: " + X.getFullStackTrace(e), X.hasCause(e, GridClientConnectionResetException.class, ClosedChannelException.class)); } for (int i = 0; i < ClientTestRestServer.SERVERS_CNT; i++) // Connection manager does 3 attempts to get topology before failure. assertEquals("Server: " + i, 3, srvs[i].getConnectCount()); }