public static List<Long> getAllPorts(List<Resource> resources) { Ranges ranges = getRanges(resources, PORTS); final List<Long> ports = Lists.newArrayList(); if (ranges != null) { for (Range range : ranges.getRangeList()) { for (long port = range.getBegin(); port <= range.getEnd(); port++) { ports.add(port); } } } return ports; }
public Map<String, String> getTextAttributes(Offer offer) { Map<String, String> textAttributes = new HashMap<>(); for (Attribute attribute : offer.getAttributesList()) { if (!attribute.getName().equals(rackIdAttributeKey)) { if (attribute.hasText()) { textAttributes.put(attribute.getName(), attribute.getText().getValue()); } else if (attribute.hasScalar()) { textAttributes.put(attribute.getName(), Double.toString(attribute.getScalar().getValue())); } else if (attribute.hasRanges()) { textAttributes.put(attribute.getName(), attribute.getRanges().getRangeList().toString()); } } } return textAttributes; }
public static List<Resource> combineResources(List<List<Resource>> resourcesList) { List<Resource> resources = new ArrayList<>(); for (List<Resource> resourcesToAdd : resourcesList) { for (Resource resource : resourcesToAdd) { Optional<Resource> matched = getMatchingResource(resource, resources); if (!matched.isPresent()) { resources.add(resource); } else { int index = resources.indexOf(matched.get()); Resource.Builder resourceBuilder = resource.toBuilder().clone(); if (resource.hasScalar()) { resourceBuilder.setScalar(resource.toBuilder().getScalarBuilder().setValue(resource.getScalar().getValue() + matched.get().getScalar().getValue()).build()); resources.set(index, resourceBuilder.build()); } else if (resource.hasRanges()) { Ranges.Builder newRanges = Ranges.newBuilder(); resource.getRanges().getRangeList().forEach(newRanges::addRange); matched.get().getRanges().getRangeList().forEach(newRanges::addRange); resourceBuilder.setRanges(newRanges); resources.set(index, resourceBuilder.build()); } else { throw new IllegalStateException(String.format("Can't subtract non-scalar or range resources %s", formatForLogging(resource))); } } } } return resources; }
List<Range> offerRangeList = Lists.newArrayList(ranges.getRangeList());
public static List<Resource> combineResources(List<List<Resource>> resourcesList) { List<Resource> resources = new ArrayList<>(); for (List<Resource> resourcesToAdd : resourcesList) { for (Resource resource : resourcesToAdd) { Optional<Resource> matched = getMatchingResource(resource, resources); if (!matched.isPresent()) { resources.add(resource); } else { int index = resources.indexOf(matched.get()); Resource.Builder resourceBuilder = resource.toBuilder().clone(); if (resource.hasScalar()) { resourceBuilder.setScalar(resource.toBuilder().getScalarBuilder().setValue(resource.getScalar().getValue() + matched.get().getScalar().getValue()).build()); resources.set(index, resourceBuilder.build()); } else if (resource.hasRanges()) { Ranges.Builder newRanges = Ranges.newBuilder(); resource.getRanges().getRangeList().forEach(newRanges::addRange); matched.get().getRanges().getRangeList().forEach(newRanges::addRange); resourceBuilder.setRanges(newRanges); resources.set(index, resourceBuilder.build()); } else { throw new IllegalStateException(String.format("Can't subtract non-scalar or range resources %s", formatForLogging(resource))); } } } } return resources; }
private static Ranges subtractRanges(Ranges ranges, Ranges toSubtract) { Ranges.Builder newRanges = Ranges.newBuilder(); List<Range> sortedRanges = Lists.newArrayList(ranges.getRangeList()); Collections.sort(sortedRanges, RANGE_COMPARATOR); List<Range> subtractRanges = Lists.newArrayList(toSubtract.getRangeList()); Collections.sort(subtractRanges, RANGE_COMPARATOR); int s = 0; for (Range range : ranges.getRangeList()) { Range.Builder currentRange = range.toBuilder(); for (int i = s; i < subtractRanges.size(); i++) { Range matchedRange = subtractRanges.get(i); if (matchedRange.getBegin() < currentRange.getBegin() || matchedRange.getEnd() > currentRange.getEnd()) { s = i; break; } currentRange.setEnd(matchedRange.getBegin() - 1); if (currentRange.getEnd() >= currentRange.getBegin()) { newRanges.addRange(currentRange.build()); } currentRange = Range.newBuilder(); currentRange.setBegin(matchedRange.getEnd() + 1); currentRange.setEnd(range.getEnd()); } if (currentRange.getEnd() >= currentRange.getBegin()) { newRanges.addRange(currentRange.build()); } } return newRanges.build(); }
public static long[] getPorts(Resource portsResource, int numPorts) { long[] ports = new long[numPorts]; if (numPorts == 0) { return ports; } int idx = 0; for (Range r : portsResource.getRanges().getRangeList()) { for (long port = r.getBegin(); port <= r.getEnd(); port++) { ports[idx++] = port; if (idx >= numPorts) { return ports; } } } return ports; }
public Map<String, String> getTextAttributes(Offer offer) { Map<String, String> textAttributes = new HashMap<>(); for (Attribute attribute : offer.getAttributesList()) { if (!attribute.getName().equals(rackIdAttributeKey)) { if (attribute.hasText()) { textAttributes.put(attribute.getName(), attribute.getText().getValue()); } else if (attribute.hasScalar()) { textAttributes.put(attribute.getName(), Double.toString(attribute.getScalar().getValue())); } else if (attribute.hasRanges()) { textAttributes.put(attribute.getName(), attribute.getRanges().getRangeList().toString()); } } } return textAttributes; }
public static List<Long> getAllPorts(List<Resource> resources) { Ranges ranges = getRanges(resources, PORTS); final List<Long> ports = Lists.newArrayList(); if (ranges != null) { for (Range range : ranges.getRangeList()) { for (long port = range.getBegin(); port <= range.getEnd(); port++) { ports.add(port); } } } return ports; }
private static int getNumRanges(List<Resource> resources, String name) { int totalRanges = 0; for (Range range : getRanges(resources, name).getRangeList()) { totalRanges += (range.getEnd() - range.getBegin()) + 1; } return totalRanges; }
List<Range> offerRangeList = Lists.newArrayList(ranges.getRangeList());
private static Ranges subtractRanges(Ranges ranges, Ranges toSubtract) { Ranges.Builder newRanges = Ranges.newBuilder(); List<Range> sortedRanges = Lists.newArrayList(ranges.getRangeList()); Collections.sort(sortedRanges, RANGE_COMPARATOR); List<Range> subtractRanges = Lists.newArrayList(toSubtract.getRangeList()); Collections.sort(subtractRanges, RANGE_COMPARATOR); int s = 0; for (Range range : ranges.getRangeList()) { Range.Builder currentRange = range.toBuilder(); for (int i = s; i < subtractRanges.size(); i++) { Range matchedRange = subtractRanges.get(i); if (matchedRange.getBegin() < currentRange.getBegin() || matchedRange.getEnd() > currentRange.getEnd()) { s = i; break; } currentRange.setEnd(matchedRange.getBegin() - 1); if (currentRange.getEnd() >= currentRange.getBegin()) { newRanges.addRange(currentRange.build()); } currentRange = Range.newBuilder(); currentRange.setBegin(matchedRange.getEnd() + 1); currentRange.setEnd(range.getEnd()); } if (currentRange.getEnd() >= currentRange.getBegin()) { newRanges.addRange(currentRange.build()); } } return newRanges.build(); }
public static long[] getPorts(Resource portsResource, int numPorts) { long[] ports = new long[numPorts]; if (numPorts == 0) { return ports; } int idx = 0; for (Range r : portsResource.getRanges().getRangeList()) { for (long port = r.getBegin(); port <= r.getEnd(); port++) { ports[idx++] = port; if (idx >= numPorts) { return ports; } } } return ports; }
private static int getNumRanges(List<Resource> resources, String name) { int totalRanges = 0; for (Range range : getRanges(resources, name).getRangeList()) { totalRanges += (range.getEnd() - range.getBegin()) + 1; } return totalRanges; }