private long getNumInstancesWithAttribute(List<SingularityTaskId> taskIds, String attrKey, String attrValue) { return taskIds.stream() .map(id -> leaderCache.getSlave(taskManager.getTask(id).get().getMesosTask().getSlaveId().getValue()).get().getAttributes().get(attrKey)) .filter(Objects::nonNull) .filter(x -> x.equals(attrValue)) .count(); }
for (Entry<String, String> entry : slave.getAttributes().entrySet()) { attributeTaskMap .computeIfAbsent(entry.getKey(), key -> new HashMap<>())
private boolean areSlaveAttributeMinimumsFeasible(SingularityOfferHolder offerHolder, SingularityTaskRequest taskRequest, List<SingularityTaskId> activeTaskIdsForRequest) { if (!taskRequest.getRequest().getSlaveAttributeMinimums().isPresent()) { return true; } Map<String, String> offerAttributes = slaveManager.getSlave(offerHolder.getSlaveId()).get().getAttributes(); Integer numDesiredInstances = taskRequest.getRequest().getInstancesSafe(); Integer numActiveInstances = activeTaskIdsForRequest.size(); for (Entry<String, Map<String, Integer>> keyEntry : taskRequest.getRequest().getSlaveAttributeMinimums().get().entrySet()) { String attrKey = keyEntry.getKey(); for (Entry<String, Integer> valueEntry : keyEntry.getValue().entrySet()) { Integer percentInstancesWithAttr = valueEntry.getValue(); Integer minInstancesWithAttr = Math.max(1, (int) ((percentInstancesWithAttr / 100.0) * numDesiredInstances)); if (offerAttributes.containsKey(attrKey) && offerAttributes.get(attrKey).equals(valueEntry.getKey())) { // Accepting this offer would add an instance of the needed attribute, so it's okay. continue; } // Would accepting this offer prevent meeting the necessary attribute in the future? long numInstancesWithAttr = getNumInstancesWithAttribute(activeTaskIdsForRequest, attrKey, valueEntry.getKey()); long numInstancesWithoutAttr = numActiveInstances - numInstancesWithAttr + 1; long maxPotentialInstancesWithAttr = numDesiredInstances - numInstancesWithoutAttr; if (maxPotentialInstancesWithAttr < minInstancesWithAttr) { return false; } } } return true; }
private long getNumInstancesWithAttribute(List<SingularityTaskId> taskIds, String attrKey, String attrValue) { return taskIds.stream() .map(id -> leaderCache.getSlave(taskManager.getTask(id).get().getMesosTask().getSlaveId().getValue()).get().getAttributes().get(attrKey)) .filter(Objects::nonNull) .filter(x -> x.equals(attrValue)) .count(); }
for (Entry<String, String> entry : slave.getAttributes().entrySet()) { attributeTaskMap .computeIfAbsent(entry.getKey(), key -> new HashMap<>())
private boolean areSlaveAttributeMinimumsFeasible(SingularityOfferHolder offerHolder, SingularityTaskRequest taskRequest, List<SingularityTaskId> activeTaskIdsForRequest) { if (!taskRequest.getRequest().getSlaveAttributeMinimums().isPresent()) { return true; } Map<String, String> offerAttributes = slaveManager.getSlave(offerHolder.getSlaveId()).get().getAttributes(); Integer numDesiredInstances = taskRequest.getRequest().getInstancesSafe(); Integer numActiveInstances = activeTaskIdsForRequest.size(); for (Entry<String, Map<String, Integer>> keyEntry : taskRequest.getRequest().getSlaveAttributeMinimums().get().entrySet()) { String attrKey = keyEntry.getKey(); for (Entry<String, Integer> valueEntry : keyEntry.getValue().entrySet()) { Integer percentInstancesWithAttr = valueEntry.getValue(); Integer minInstancesWithAttr = Math.max(1, (int) ((percentInstancesWithAttr / 100.0) * numDesiredInstances)); if (offerAttributes.containsKey(attrKey) && offerAttributes.get(attrKey).equals(valueEntry.getKey())) { // Accepting this offer would add an instance of the needed attribute, so it's okay. continue; } // Would accepting this offer prevent meeting the necessary attribute in the future? long numInstancesWithAttr = getNumInstancesWithAttribute(activeTaskIdsForRequest, attrKey, valueEntry.getKey()); long numInstancesWithoutAttr = numActiveInstances - numInstancesWithAttr + 1; long maxPotentialInstancesWithAttr = numDesiredInstances - numInstancesWithoutAttr; if (maxPotentialInstancesWithAttr < minInstancesWithAttr) { return false; } } } return true; }