use of org.apache.mesos.v1.Protos.Value.Ranges in project Singularity by HubSpot.
the class MesosUtils method subtractRanges.
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();
}
use of org.apache.mesos.v1.Protos.Value.Ranges in project Singularity by HubSpot.
the class MesosUtilsTest method test.
private void test(int numPorts, String... ranges) {
Resource resource = MesosUtils.getPortsResource(numPorts, buildOffer(ranges));
Assert.assertEquals(numPorts, MesosUtils.getNumPorts(Collections.singletonList(resource)));
}
use of org.apache.mesos.v1.Protos.Value.Ranges in project Singularity by HubSpot.
the class MesosUtils method getPortsResource.
public static Resource getPortsResource(int numPorts, List<Resource> resources, List<Long> otherRequestedPorts) {
List<Long> requestedPorts = new ArrayList<>(otherRequestedPorts);
Ranges ranges = getRanges(resources, PORTS);
Preconditions.checkState(ranges.getRangeCount() > 0, "Ports %s should have existed in resources %s", PORTS, formatForLogging(resources));
Ranges.Builder rangesBldr = Ranges.newBuilder();
int portsSoFar = 0;
List<Range> offerRangeList = Lists.newArrayList(ranges.getRangeList());
Random random = new Random();
Collections.shuffle(offerRangeList, random);
if (numPorts > 0) {
for (Range range : offerRangeList) {
long rangeStartSelection = Math.max(range.getBegin(), range.getEnd() - (numPorts - portsSoFar + 1));
if (rangeStartSelection != range.getBegin()) {
int rangeDelta = (int) (rangeStartSelection - range.getBegin()) + 1;
rangeStartSelection = random.nextInt(rangeDelta) + range.getBegin();
}
long rangeEndSelection = Math.min(range.getEnd(), rangeStartSelection + (numPorts - portsSoFar - 1));
rangesBldr.addRange(Range.newBuilder().setBegin(rangeStartSelection).setEnd(rangeEndSelection));
portsSoFar += (rangeEndSelection - rangeStartSelection) + 1;
List<Long> toRemove = new ArrayList<>();
for (long port : requestedPorts) {
if (rangeStartSelection >= port && rangeEndSelection <= port) {
toRemove.add(port);
portsSoFar--;
}
}
requestedPorts.removeAll(toRemove);
if (portsSoFar == numPorts) {
break;
}
}
}
for (long port : requestedPorts) {
rangesBldr.addRange(Range.newBuilder().setBegin(port).setEnd(port).build());
}
return Resource.newBuilder().setType(Type.RANGES).setName(PORTS).setRanges(rangesBldr).build();
}
use of org.apache.mesos.v1.Protos.Value.Ranges in project Singularity by HubSpot.
the class MesosUtils method getAllPorts.
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;
}