/** @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); }
/** * Waits for all workers to finish. * * @param cancel Flag to indicate whether workers should be cancelled before waiting for them to * finish. */ public void join(boolean cancel) { if (cancel) U.cancel(workers); // Record current interrupted status of calling thread. boolean interrupted = Thread.interrupted(); try { U.join(workers, log); } finally { // Reset interrupted flag on calling thread. if (interrupted) Thread.currentThread().interrupt(); } }
/** {@inheritDoc} */ @Override public Serializable execute() { int arg = this.<Integer>argument(0); try { if (log.isInfoEnabled()) log.info("Executing job [job=" + this + ", arg=" + arg + ']'); startSignal.countDown(); try { if (!startSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS)) fail(); if (arg == 1) { if (log.isInfoEnabled()) log.info("Job one is proceeding."); } else Thread.sleep(WAIT_TIME); } catch (InterruptedException e) { if (log.isInfoEnabled()) log.info("Job got cancelled [arg=" + arg + ", ses=" + ses + ", e=" + e + ']'); return 0; } if (log.isInfoEnabled()) log.info("Completing job: " + ses); return argument(0); } finally { stopSignal.countDown(); processedCnt++; } }
/** @throws Exception If failed. */ public void testDisabledRest() throws Exception { restEnabled = false; final Grid g = startGrid("disabled-rest"); try { Thread.sleep(2 * TOP_REFRESH_FREQ); // As long as we have round robin load balancer this will cause every node to be queried. for (int i = 0; i < NODES_CNT + 1; i++) assertEquals(NODES_CNT + 1, client.compute().refreshTopology(false, false).size()); final GridClientData data = client.data(PARTITIONED_CACHE_NAME); // Check rest-disabled node is unavailable. try { String affKey; do { affKey = UUID.randomUUID().toString(); } while (!data.affinity(affKey).equals(g.localNode().id())); data.put(affKey, "asdf"); assertEquals("asdf", cache(0, PARTITIONED_CACHE_NAME).get(affKey)); } catch (GridServerUnreachableException e) { // Thrown for direct client-node connections. assertTrue( "Unexpected exception message: " + e.getMessage(), e.getMessage() .startsWith("No available endpoints to connect (is rest enabled for this node?)")); } catch (GridClientException e) { // Thrown for routed client-router-node connections. String msg = e.getMessage(); assertTrue( "Unexpected exception message: " + msg, protocol() == GridClientProtocol.TCP ? msg.contains("No available endpoints to connect (is rest enabled for this node?)") : // TCP router. msg.startsWith( "No available nodes on the router for destination node ID")); // HTTP router. } // Check rest-enabled nodes are available. String affKey; do { affKey = UUID.randomUUID().toString(); } while (data.affinity(affKey).equals(g.localNode().id())); data.put(affKey, "fdsa"); assertEquals("fdsa", cache(0, PARTITIONED_CACHE_NAME).get(affKey)); } finally { restEnabled = true; G.stop(g.name(), true); } }
/** @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. */ public void testAffinityPut() throws Exception { Thread.sleep(2 * TOP_REFRESH_FREQ); assertEquals(NODES_CNT, client.compute().refreshTopology(false, false).size()); Map<UUID, Grid> gridsByLocNode = new HashMap<>(NODES_CNT); GridClientData partitioned = client.data(PARTITIONED_CACHE_NAME); GridClientCompute compute = client.compute(); for (int i = 0; i < NODES_CNT; i++) gridsByLocNode.put(grid(i).localNode().id(), grid(i)); for (int i = 0; i < 100; i++) { String key = "key" + i; UUID primaryNodeId = grid(0).mapKeyToNode(PARTITIONED_CACHE_NAME, key).id(); assertEquals("Affinity mismatch for key: " + key, primaryNodeId, partitioned.affinity(key)); assertEquals(primaryNodeId, partitioned.affinity(key)); // Must go to primary node only. Since backup count is 0, value must present on // primary node only. partitioned.put(key, "val" + key); for (Map.Entry<UUID, Grid> entry : gridsByLocNode.entrySet()) { Object val = entry.getValue().cache(PARTITIONED_CACHE_NAME).peek(key); if (primaryNodeId.equals(entry.getKey())) assertEquals("val" + key, val); else assertNull(val); } } // Now check that we will see value in near cache in pinned mode. for (int i = 100; i < 200; i++) { String pinnedKey = "key" + i; UUID primaryNodeId = grid(0).mapKeyToNode(PARTITIONED_CACHE_NAME, pinnedKey).id(); UUID pinnedNodeId = F.first(F.view(gridsByLocNode.keySet(), F.notEqualTo(primaryNodeId))); GridClientNode node = compute.node(pinnedNodeId); partitioned.pinNodes(node).put(pinnedKey, "val" + pinnedKey); for (Map.Entry<UUID, Grid> entry : gridsByLocNode.entrySet()) { Object val = entry.getValue().cache(PARTITIONED_CACHE_NAME).peek(pinnedKey); if (primaryNodeId.equals(entry.getKey()) || pinnedNodeId.equals(entry.getKey())) assertEquals("val" + pinnedKey, val); else assertNull(val); } } }
/** {@inheritDoc} */ @SuppressWarnings("BusyWait") @Override public Boolean reduce(List<GridComputeJobResult> results) throws GridException { assert taskSes != null; assert results != null; assert params != null; assert !params.isEmpty(); assert results.size() == params.size(); Map<String, Integer> receivedParams = new HashMap<>(); boolean allAttrReceived = false; int cnt = 0; while (!allAttrReceived && cnt++ < 3) { allAttrReceived = true; for (Map.Entry<String, Integer> entry : params.entrySet()) { assert taskSes.getAttribute(entry.getKey()) != null; Integer newVal = (Integer) taskSes.getAttribute(entry.getKey()); assert newVal != null; receivedParams.put(entry.getKey(), newVal); if (newVal != entry.getValue() + 1) allAttrReceived = false; } if (!allAttrReceived) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new GridException("Thread interrupted.", e); } } } if (log.isDebugEnabled()) { for (Map.Entry<String, Integer> entry : receivedParams.entrySet()) { log.debug( "Received session attr value [name=" + entry.getKey() + ", val=" + entry.getValue() + ", expected=" + (params.get(entry.getKey()) + 1) + ']'); } } return allAttrReceived; }