/** * 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); }
/** {@inheritDoc} */ @Override public void close() throws SQLException { if (closed) return; closed = true; GridClientFactory.stop(client.id(), false); }
/** * Print cluster state. * * @param client Client. * @throws Throwable If failed to print state. */ private void state(GridClient client) throws Throwable { try { GridClientClusterState state = client.state(); log("Cluster is " + (state.active() ? "active" : "inactive")); } catch (Throwable e) { log("Failed to get cluster state."); throw e; } }
/** * @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."); }
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; futs.put("refreshTop", compute.refreshTopologyAsync(true, true)); GridClientFactory.stop(client.id(), false);
final CountDownLatch rmvLatch = new CountDownLatch(1); assertEquals(NODES_CNT, client.compute().refreshTopology(false, false).size()); client.addTopologyListener(lsnr); client.removeTopologyListener(lsnr);
/** * Decrements the usages of the client and closes it if this is the last usage. * * @return {@code True} if client can be closed safely by the called. */ public boolean release() { int cnt0 = cnt.decrementAndGet(); assert cnt0 >= 0; if (cnt0 == 0) { if (cnt.compareAndSet(0, -1)) { GridClient cli0 = cli; if (cli0 != null) cli0.close(); return true; } } return false; } }
/** {@inheritDoc} */ @Override public void run() { try { GridClientData data = client.data("partitioned"); for (int i = 0; i < iterationCnt; i++) performIteration(data); } catch (GridClientException e) { e.printStackTrace(); } }
/** * @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()); }
/** {@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; } }
/** * Events for class tasks that was started from external clients should contain * client subject id instead of the node where it was started. This test checks it. * * @throws Exception If failed. */ @Test public void testClient() throws Exception { latch = new CountDownLatch(3); client.compute().execute(SimpleTask.class.getName(), null); assert latch.await(1000, MILLISECONDS); assertEquals(3, evts.size()); Iterator<TaskEvent> it = evts.iterator(); assert it.hasNext(); TaskEvent evt = it.next(); assert evt != null; assertEquals(EVT_TASK_STARTED, evt.type()); assertEquals(client.id(), evt.subjectId()); assert it.hasNext(); evt = it.next(); assert evt != null; assertEquals(EVT_TASK_REDUCED, evt.type()); assertEquals(client.id(), evt.subjectId()); assert it.hasNext(); evt = it.next(); assert evt != null; assertEquals(EVT_TASK_FINISHED, evt.type()); assertEquals(client.id(), evt.subjectId()); assert !it.hasNext(); }
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { if (client != null) { GridClientFactory.stop(client.id(), false); client = null; } }
/** * Deactivate cluster. * * @param client Client. * @throws Throwable If failed to deactivate. */ private void deactivate(GridClient client) throws Throwable { try { GridClientClusterState state = client.state(); state.active(false); log("Cluster deactivated"); } catch (Throwable e) { log("Failed to deactivate cluster."); throw e; } }
/** * @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()))); }
/** * @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); }
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { GridClientFactory.stop(client.id(), false); client = null; }
/** * Activate cluster. * * @param client Client. * @throws GridClientException If failed to activate. */ private void activate(GridClient client) throws Throwable { try { GridClientClusterState state = client.state(); state.active(true); log("Cluster activated"); } catch (Throwable e) { log("Failed to activate cluster."); throw e; } }
/** * @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()) ) ); }
/** * 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); }
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { GridClientFactory.stop(client.id()); }