/** * Finds and returns the nodes of the given type in any of the given states. * * @param type the node type to return * @param inState the states to return nodes from. If no states are given, all nodes of the given type are returned * @return the node, or empty if it was not found in any of the given states */ public List<Node> getNodes(NodeType type, Node.State ... inState) { return db.getNodes(inState).stream().filter(node -> node.type().equals(type)).collect(Collectors.toList()); }
public ResourceCapacity getCapacityTotal() { return allNodes.asList().stream() .filter(n -> n.type().equals(NodeType.host)) .map(ResourceCapacity::new) .reduce(new ResourceCapacity(), ResourceCapacity::add); }
public int freeCapacityInFlavorEquivalence(Flavor flavor) { return allNodes.asList().stream() .filter(n -> n.type().equals(NodeType.host)) .map(n -> canFitNumberOf(n, flavor)) .reduce(0, (a, b) -> a + b); }
/** * Spare hosts are the two hosts in the system with the most free capacity. * * We do not count retired or inactive nodes as used capacity (as they could have been * moved to create space for the spare node in the first place). */ private static Set<Node> findSpareHosts(List<Node> nodes, int spares) { DockerHostCapacity capacity = new DockerHostCapacity(new ArrayList<>(nodes)); return nodes.stream() .filter(node -> node.type().equals(NodeType.host)) .filter(dockerHost -> dockerHost.state().equals(Node.State.active)) .filter(dockerHost -> capacity.freeIPs(dockerHost) > 0) .sorted(capacity::compareWithoutInactive) .limit(spares) .collect(Collectors.toSet()); }
public ResourceCapacity getFreeCapacityTotal() { return allNodes.asList().stream() .filter(n -> n.type().equals(NodeType.host)) .map(n -> freeCapacityOf(n, false)) .reduce(new ResourceCapacity(), ResourceCapacity::add); }
/** * Add nodes already provisioned, but not allocated to any application */ void addReadyNodes() { allNodes.stream() .filter(node -> node.type().equals(requestedNodes.type())) .filter(node -> node.state().equals(Node.State.ready)) .map(node -> toNodePriority(node, false, false)) .filter(n -> !n.violatesSpares || isAllocatingForReplacement) .forEach(prioritizableNode -> nodes.put(prioritizableNode.node, prioritizableNode)); }
public long getNofHostsAvailableFor(Flavor flavor) { return allNodes.asList().stream() .filter(n -> n.type().equals(NodeType.host)) .filter(n -> hasCapacity(n, ResourceCapacity.of(flavor))) .count(); }
/** * Add existing nodes allocated to the application */ void addApplicationNodes() { List<Node.State> legalStates = Arrays.asList(Node.State.active, Node.State.inactive, Node.State.reserved); allNodes.stream() .filter(node -> node.type().equals(requestedNodes.type())) .filter(node -> legalStates.contains(node.state())) .filter(node -> node.allocation().isPresent()) .filter(node -> node.allocation().get().owner().equals(appId)) .map(node -> toNodePriority(node, false, false)) .forEach(prioritizableNode -> nodes.put(prioritizableNode.node, prioritizableNode)); }
/** * Calculate the remaining capacity for the dockerHost. * @param dockerHost The host to find free capacity of. * * @return A default (empty) capacity if not a docker host, otherwise the free/unallocated/rest capacity */ public ResourceCapacity freeCapacityOf(Node dockerHost, boolean treatInactiveOrRetiredAsUnusedCapacity) { // Only hosts have free capacity if (!dockerHost.type().equals(NodeType.host)) return new ResourceCapacity(); ResourceCapacity hostCapacity = new ResourceCapacity(dockerHost); for (Node container : allNodes.childrenOf(dockerHost)) { boolean isUsedCapacity = !(treatInactiveOrRetiredAsUnusedCapacity && isInactiveOrRetired(container)); if (isUsedCapacity) { hostCapacity.subtract(container); } } return hostCapacity; }