/** @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); }
/** @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. */ 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. */ @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 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. */ 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); }