NodeQueueLoadMonitor implements ClusterMonitor { public RMNode selectAnyNode(Set<String> blacklist) { List<NodeId> nodeIds = getCandidatesForSelectAnyNode(); int size = nodeIds.size(); if (size <= 0) { return null; } Random rand = new Random(); int startIndex = rand.nextInt(size); for (int i = 0; i < size; ++i) { int index = i + startIndex; index %= size; NodeId nodeId = nodeIds.get(index); if (nodeId != null && !blacklist.contains(nodeId.getHost())) { ClusterNode node = clusterNodes.get(nodeId); if (node != null && comparator.compareAndIncrement(node, 1)) { return nodeByHostName.get(nodeId.getHost()); } } } return null; } @VisibleForTesting  NodeQueueLoadMonitor(LoadComparator comparator);  NodeQueueLoadMonitor(long nodeComputationInterval,
      LoadComparator comparator, int numNodes); QueueLimitCalculator getThresholdCalculator(); void stop(); void initThresholdCalculator(float sigma, int limitMin, int limitMax); @Override void addNode(List<NMContainerStatus> containerStatuses,
      RMNode rmNode); @Override void removeNode(RMNode removedRMNode); @Override void updateNode(RMNode rmNode); @Override void updateNodeResource(RMNode rmNode, ResourceOption resourceOption); List<NodeId> selectNodes(); List<NodeId> selectLeastLoadedNodes(int k); RMNode selectLocalNode(String hostName, Set<String> blacklist); RMNode selectRackLocalNode(String rackName, Set<String> blacklist); RMNode selectAnyNode(Set<String> blacklist);  }
@Test public void testSelectAnyNode() { NodeQueueLoadMonitor selector = new NodeQueueLoadMonitor( NodeQueueLoadMonitor.LoadComparator.QUEUE_LENGTH); RMNode h1 = createRMNode("h1", 1, "rack1", -1, 2, 5); RMNode h2 = createRMNode("h2", 2, "rack2", -1, 5, 5); RMNode h3 = createRMNode("h3", 3, "rack2", -1, 4, 10); selector.addNode(null, h1); selector.addNode(null, h2); selector.addNode(null, h3); selector.updateNode(h1); selector.updateNode(h2); selector.updateNode(h3); selector.computeTask.run(); Assert.assertEquals(2, selector.getSortedNodes().size()); Set<String> blacklist = new HashSet<>(); RMNode node = selector.selectAnyNode(blacklist); Assert.assertTrue(node.getHostName().equals("h1") || node.getHostName().equals("h3")); blacklist.add("h1"); node = selector.selectAnyNode(blacklist); Assert.assertEquals("h3", node.getHostName()); blacklist.add("h3"); node = selector.selectAnyNode(blacklist); Assert.assertNull(node); }