/** * Get any runnable node that is not one of the excluded nodes * * @param excluded the list of nodes to ignore * @return the runnable node, null if no runnable node can be found */ public ClusterNode getRunnableNodeForAny(Set<String> excluded) { double avgLoad = loadManager.getAverageLoad(type); // Make two passes over the nodes. In the first pass, try to find a // node that has lower than average number of grants on it. If that does // not find a node, try looking at all nodes. for (int pass = 0; pass < 2; pass++) { for (Map.Entry<String, NodeContainer> e : hostToRunnableNodes.entrySet()) { NodeContainer nodeContainer = e.getValue(); if (nodeContainer == null) { continue; } synchronized (nodeContainer) { if (nodeContainer.isEmpty()) { continue; } for (ClusterNode node : nodeContainer) { if (excluded == null || !excluded.contains(node.getHost())) { if (resourceLimit.hasEnoughResource(node)) { // When pass == 0, try to average out the load. if (pass == 0) { if (node.getGrantCount(type) < avgLoad) { return node; } } else { return node; } } } } } } } return null; }
/** * Get a runnable node in the given rack that is not present in the excluded list * * @param requestedNode the node to look up rack locality for * @param excluded the list of nodes to ignore * @return the runnable node from the rack satisfying conditions, null if the node was not found */ public ClusterNode getRunnableNodeForRack(RequestedNode requestedNode, Set<String> excluded) { NodeContainer nodeContainer = requestedNode.getRackNodes(); getRunnableNodeForRackCounter += 1; if (nodeContainer == null) { return null; } synchronized (nodeContainer) { if (nodeContainer.isEmpty()) { return null; } if (getRunnableNodeForRackCounter % RACK_SHUFFLE_PERIOD == 0) { // This balances more evenly across nodes in a rack nodeContainer.shuffle(); } for (ClusterNode node : nodeContainer) { if (excluded == null || !excluded.contains(node.getHost())) { if (resourceLimit.hasEnoughResource(node)) { return node; } } } } return null; }
/** * Create a snapshot of runnable nodes. * * @return The snapshot. */ public NodeSnapshot getNodeSnapshot() { int nodeCount = 0; Map<String, NodeContainer> hostRunnables = new HashMap<String, NodeContainer>(); for (Map.Entry<String, NodeContainer> entry : hostToRunnableNodes.entrySet()) { NodeContainer value = entry.getValue(); synchronized (value) { if (!value.isEmpty()) { hostRunnables.put(entry.getKey(), value.copy()); nodeCount += value.size(); } } } Map<Node, NodeContainer> rackRunnables = new HashMap<Node, NodeContainer>(); for (Map.Entry<Node, NodeContainer> entry : rackToRunnableNodes.entrySet()) { NodeContainer value = entry.getValue(); synchronized (value) { if (!value.isEmpty()) { rackRunnables.put(entry.getKey(), value.copy()); } } } return new NodeSnapshot(topologyCache, hostRunnables, rackRunnables, nodeCount); }
/** * Get runnable node local to the given host * * @param requestedNode the requested node that needs local scheduling * @return the node that is local to the host, null if there are no runnable nodes local to the * host */ public ClusterNode getRunnableNodeForHost(RequestedNode requestedNode) { // there should only be one node per host in the common case NodeContainer nodeContainer = requestedNode.getHostNodes(); if (nodeContainer == null) { return null; } synchronized (nodeContainer) { if (nodeContainer.isEmpty()) { return null; } for (ClusterNode node : nodeContainer) { if (resourceLimit.hasEnoughResource(node)) { return node; } } } return null; }
/** * Checks if a node is present as runnable in this index. Should be called while holding the * node lock. * * @param clusterNode The node. * @return A boolean indicating if the node is present. */ public boolean hasRunnable(ClusterNode clusterNode) { String host = clusterNode.getHost(); NodeContainer nodeContainer = hostToRunnableNodes.get(host); return (nodeContainer != null) && !nodeContainer.isEmpty(); }