/** @throws Exception If failed. */ @SuppressWarnings({"ObjectEquality"}) public void testDifferentTasks() throws Exception { Grid grid1 = null; Grid grid2 = null; try { grid1 = startGrid(1, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid2 = startGrid(2, new GridSpringResourceContextImpl(new GenericApplicationContext())); // Execute different tasks. grid1.compute().execute(SharedResourceTask1.class, null).get(); grid1.compute().execute(SharedResourceTask2.class, null).get(); // In ISOLATED_CLASSLOADER mode tasks should have the class // loaders because they have the same CL locally and thus the same // resources. // So 1 resource locally and 1 remotely assert task1Rsrc1 == task2Rsrc1; assert task1Rsrc2 == task2Rsrc2; assert task1Rsrc3 == task2Rsrc3; assert task1Rsrc4 == task2Rsrc4; checkUsageCount(createClss, UserResource1.class, 4); checkUsageCount(createClss, UserResource2.class, 4); checkUsageCount(deployClss, UserResource1.class, 4); checkUsageCount(deployClss, UserResource2.class, 4); } finally { GridTestUtils.close(grid1, log()); GridTestUtils.close(grid2, log()); } checkUsageCount(undeployClss, UserResource1.class, 4); checkUsageCount(undeployClss, UserResource2.class, 4); }
/** @throws Exception If failed. */ @SuppressWarnings({"ObjectEquality"}) public void testUndeployedTask() throws Exception { Grid grid1 = null; Grid grid2 = null; try { grid1 = startGrid(1, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid2 = startGrid(2, new GridSpringResourceContextImpl(new GenericApplicationContext())); // Execute tasks. grid1.compute().execute(SharedResourceTask1.class, null).get(); grid1.compute().execute(SharedResourceTask2.class, null).get(); grid1.compute().undeployTask(SharedResourceTask1.class.getName()); // Wait until resources get undeployed remotely // because undeploy is asynchronous apply. Thread.sleep(3000); // 1 local and 1 remote resource instances checkUsageCount(createClss, UserResource1.class, 4); checkUsageCount(deployClss, UserResource1.class, 4); checkUsageCount(createClss, UserResource2.class, 4); checkUsageCount(deployClss, UserResource2.class, 4); checkUsageCount(undeployClss, UserResource1.class, 4); checkUsageCount(undeployClss, UserResource2.class, 4); grid1.compute().undeployTask(SharedResourceTask2.class.getName()); // Wait until resources get undeployed remotely // because undeploy is asynchronous apply. Thread.sleep(3000); // We undeployed last task for this class loader and resources. // All resources should be undeployed. checkUsageCount(undeployClss, UserResource1.class, 4); checkUsageCount(undeployClss, UserResource2.class, 4); // Execute the same tasks. grid1.compute().execute(SharedResourceTask1.class, null).get(); grid1.compute().execute(SharedResourceTask2.class, null).get(); // 2 new resources. checkUsageCount(createClss, UserResource1.class, 8); checkUsageCount(deployClss, UserResource1.class, 8); checkUsageCount(createClss, UserResource2.class, 8); checkUsageCount(deployClss, UserResource2.class, 8); } finally { GridTestUtils.close(grid1, log()); GridTestUtils.close(grid2, log()); } checkUsageCount(undeployClss, UserResource1.class, 8); checkUsageCount(undeployClss, UserResource2.class, 8); }
/** {@inheritDoc} */ @Override protected GridConfiguration getConfiguration(String gridName) throws Exception { GridConfiguration c = super.getConfiguration(gridName); c.setLocalHost(HOST); assert c.getClientConnectionConfiguration() == null; GridClientConnectionConfiguration clientCfg = new GridClientConnectionConfiguration(); clientCfg.setRestTcpPort(REST_TCP_PORT_BASE); GridSslContextFactory sslCtxFactory = sslContextFactory(); if (sslCtxFactory != null) { clientCfg.setRestTcpSslEnabled(true); clientCfg.setRestTcpSslContextFactory(sslCtxFactory); } c.setClientConnectionConfiguration(clientCfg); GridTcpDiscoverySpi disco = new GridTcpDiscoverySpi(); disco.setIpFinder(IP_FINDER); c.setDiscoverySpi(disco); TestCommunicationSpi spi = new TestCommunicationSpi(); spi.setLocalPort(GridTestUtils.getNextCommPort(getClass())); c.setCommunicationSpi(spi); c.setCacheConfiguration( cacheConfiguration(null), cacheConfiguration(PARTITIONED_CACHE_NAME), cacheConfiguration(REPLICATED_CACHE_NAME), cacheConfiguration(REPLICATED_ASYNC_CACHE_NAME)); ThreadPoolExecutor exec = new ThreadPoolExecutor(40, 40, 0, MILLISECONDS, new LinkedBlockingQueue<Runnable>()); exec.prestartAllCoreThreads(); c.setExecutorService(exec); c.setExecutorServiceShutdown(true); ThreadPoolExecutor sysExec = new ThreadPoolExecutor(40, 40, 0, MILLISECONDS, new LinkedBlockingQueue<Runnable>()); sysExec.prestartAllCoreThreads(); c.setSystemExecutorService(sysExec); c.setSystemExecutorServiceShutdown(true); return c; }
/** @throws Exception If failed. */ public void testSameTaskFromTwoNodesLeft() throws Exception { Grid grid1 = null; Grid grid2 = null; Grid grid3 = null; try { grid1 = startGrid(1, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid2 = startGrid(2, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid3 = startGrid(3, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid1.compute().execute(SharedResourceTask1.class, null).get(); grid2.compute().execute(SharedResourceTask1.class, null).get(); checkUsageCount(createClss, UserResource1.class, 6); checkUsageCount(deployClss, UserResource1.class, 6); checkUsageCount(createClss, UserResource2.class, 6); checkUsageCount(deployClss, UserResource2.class, 6); checkUsageCount(undeployClss, UserResource1.class, 0); checkUsageCount(undeployClss, UserResource2.class, 0); GridTestUtils.close(grid1, log()); // Wait until other nodes get notified // this grid1 left. Thread.sleep(1000); // Undeployment happened only on Grid1. checkUsageCount(undeployClss, UserResource1.class, 2); checkUsageCount(undeployClss, UserResource2.class, 2); GridTestUtils.close(grid2, log()); // Wait until resources get undeployed remotely // because undeploy is asynchronous apply. Thread.sleep(1000); // Grid1 and Grid2 checkUsageCount(undeployClss, UserResource1.class, 4); checkUsageCount(undeployClss, UserResource2.class, 4); } finally { GridTestUtils.close(grid1, log()); GridTestUtils.close(grid2, log()); GridTestUtils.close(grid3, log()); } }
/** @throws Exception If failed. */ public void testSameTaskFromTwoNodesUndeploy() throws Exception { Grid grid1 = null; Grid grid2 = null; Grid grid3 = null; try { grid1 = startGrid(1, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid2 = startGrid(2, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid3 = startGrid(3, new GridSpringResourceContextImpl(new GenericApplicationContext())); grid1.compute().execute(SharedResourceTask1.class, null).get(); grid2.compute().execute(SharedResourceTask1.class, null).get(); checkUsageCount(createClss, UserResource1.class, 6); checkUsageCount(deployClss, UserResource1.class, 6); checkUsageCount(createClss, UserResource2.class, 6); checkUsageCount(deployClss, UserResource2.class, 6); checkUsageCount(undeployClss, UserResource1.class, 0); checkUsageCount(undeployClss, UserResource2.class, 0); grid1.compute().undeployTask(SharedResourceTask1.class.getName()); // Wait until resources get undeployed remotely // because undeploy is asynchronous apply. Thread.sleep(3000); checkUsageCount(undeployClss, UserResource1.class, 6); checkUsageCount(undeployClss, UserResource2.class, 6); grid2.compute().undeployTask(SharedResourceTask1.class.getName()); // Wait until resources get undeployed remotely // because undeploy is asynchronous apply. Thread.sleep(3000); // All Tasks from originating nodes were undeployed. All resources should be cleaned up. checkUsageCount(undeployClss, UserResource1.class, 6); checkUsageCount(undeployClss, UserResource2.class, 6); } finally { GridTestUtils.close(grid1, log()); GridTestUtils.close(grid2, log()); GridTestUtils.close(grid3, log()); } }
/** @throws Exception If failed. */ @SuppressWarnings("unchecked") public void testRedeployedTask() throws Exception { Grid grid = startGrid(0, new GridSpringResourceContextImpl(new GenericApplicationContext())); try { // Execute same task with different class loaders. Second execution should redeploy first one. grid.compute().execute(SharedResourceTask1.class, null).get(); checkUsageCount(createClss, UserResource1.class, 2); checkUsageCount(createClss, UserResource2.class, 2); checkUsageCount(deployClss, UserResource1.class, 2); checkUsageCount(deployClss, UserResource2.class, 2); // Change class loader of the task. So it's just implicit redeploy. ClassLoader tstClsLdr = new GridTestClassLoader( null, getClass().getClassLoader(), SharedResourceTask1.class.getName(), GridResourceSharedUndeploySelfTest.SharedResourceTask1.GridSharedJob1.class.getName(), GridResourceSharedUndeploySelfTest.class.getName()); Class<? extends GridComputeTask<Object, Object>> taskCls = (Class<? extends GridComputeTask<Object, Object>>) tstClsLdr.loadClass(SharedResourceTask1.class.getName()); grid.compute().execute(taskCls, null).get(); // Old resources should be undeployed at this point. checkUsageCount(undeployClss, UserResource1.class, 2); checkUsageCount(undeployClss, UserResource2.class, 2); // We should detect redeployment and create new resources. checkUsageCount(createClss, UserResource1.class, 4); checkUsageCount(createClss, UserResource2.class, 4); checkUsageCount(deployClss, UserResource1.class, 4); checkUsageCount(deployClss, UserResource2.class, 4); } finally { GridTestUtils.close(grid, log()); } checkUsageCount(undeployClss, UserResource1.class, 4); checkUsageCount(undeployClss, UserResource2.class, 4); }
/** @throws Exception If failed. */ public void testDifferentTaskNameLocally() throws Exception { Grid grid = startGrid(0, new GridSpringResourceContextImpl(new GenericApplicationContext())); // Versions are different - should not share // 2 resource created locally try { grid.compute().execute(SharedResourceTask1Version1.class, null).get(); try { grid.compute().execute(SharedResourceTask1Version2.class, null).get(); assert false : "SharedResourceTask4 should not be allowed to deploy."; } catch (GridException e) { info("Received expected exception: " + e); } } finally { GridTestUtils.close(grid, log()); } }
/** @throws Exception If failed. */ public void testSameTaskLocally() throws Exception { Grid grid = startGrid(0, new GridSpringResourceContextImpl(new GenericApplicationContext())); try { // Execute the same task twice. // 1 resource created locally grid.compute().execute(SharedResourceTask1.class, null).get(); grid.compute().execute(SharedResourceTask1.class, null).get(); checkUsageCount(createClss, UserResource1.class, 2); checkUsageCount(createClss, UserResource2.class, 2); checkUsageCount(deployClss, UserResource1.class, 2); checkUsageCount(deployClss, UserResource2.class, 2); } finally { GridTestUtils.close(grid, log()); } checkUsageCount(undeployClss, UserResource1.class, 2); checkUsageCount(undeployClss, UserResource2.class, 2); }
/** @throws Exception If failed. */ public void testEmptyProjections() throws Exception { final GridClientCompute dflt = client.compute(); Collection<? extends GridClientNode> nodes = dflt.nodes(); assertEquals(NODES_CNT, nodes.size()); Iterator<? extends GridClientNode> iter = nodes.iterator(); final GridClientCompute singleNodePrj = dflt.projection(Collections.singletonList(iter.next())); final GridClientNode second = iter.next(); final GridClientPredicate<GridClientNode> noneFilter = new GridClientPredicate<GridClientNode>() { @Override public boolean apply(GridClientNode node) { return false; } }; final GridClientPredicate<GridClientNode> targetFilter = new GridClientPredicate<GridClientNode>() { @Override public boolean apply(GridClientNode node) { return node.nodeId().equals(second.nodeId()); } }; GridTestUtils.assertThrows( log(), new Callable<Object>() { @Override public Object call() throws Exception { return dflt.projection(noneFilter).log(-1, -1); } }, GridServerUnreachableException.class, null); GridTestUtils.assertThrows( log(), new Callable<Object>() { @Override public Object call() throws Exception { return singleNodePrj.projection(second); } }, GridClientException.class, null); GridTestUtils.assertThrows( log(), new Callable<Object>() { @Override public Object call() throws Exception { return singleNodePrj.projection(targetFilter); } }, GridClientException.class, null); }