org.apache.mesos.Protos.Value.Range Java Examples

The following examples show how to use org.apache.mesos.Protos.Value.Range. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MesosUtilities.java    From oodt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates string out an offer in a nice format.
 * @param resource - mesos resource to make into string.
 * @return string representing a resource.
 */
public static String getResourceMessage(Resource resource) {
    Type type = resource.getType();
    String ret = resource.getName() +" "+resource.getRole()+ ": ";
    switch (type) {
        case SCALAR:
            ret += resource.getScalar().getValue();
            break;
        case RANGES:
            for (Range range : resource.getRanges().getRangeList())
                ret += range.getBegin() + " - "+range.getEnd()+",";
            break;
        case TEXT:
            ret += " TEXT type...cannot find.";
            break;
        case SET:
            for (String string : resource.getSet().getItemList())
                ret += string + ",";
            break;
    }
    return ret;
}
 
Example #2
Source File: MesosNimbus.java    From storm with Apache License 2.0 6 votes vote down vote up
private Resource createMesosRangeResource(ResourceType resourceType, RangeResourceEntry rangeResourceEntry) {
  Ranges.Builder rangesBuilder = Ranges.newBuilder();
  Range rangeBuilder = Range.newBuilder()
                            .setBegin(rangeResourceEntry.getBegin())
                            .setEnd(rangeResourceEntry.getEnd()).build();
  rangesBuilder.addRange(rangeBuilder);

  Resource.Builder resourceBuilder = Resource.newBuilder()
                                             .setName(resourceType.toString())
                                             .setType(Protos.Value.Type.RANGES)
                                             .setRanges(rangesBuilder.build());
  if (rangeResourceEntry.getReservationType() != null && rangeResourceEntry.getReservationType().equals(ReservationType.STATIC)) {
    resourceBuilder.setRole(MesosCommon.getRole(mesosStormConf));
  }
  return resourceBuilder.build();
}
 
Example #3
Source File: ComposeRewriteHelper.java    From docker-compose-executor with Apache License 2.0 6 votes vote down vote up
private Iterator<Long> getPortMappingIterator(TaskInfo taskInfo) {
    List<Resource> list = taskInfo.getResourcesList();
    List<Long> ports = new ArrayList<Long>();
    for (Resource resource : list) {
        String name = resource.getName();
        if ("ports".equals(name)) {
            Ranges ranges = resource.getRanges();
            for (Range range : ranges.getRangeList()) {
                long startPort = range.getBegin();
                long endPort = range.getEnd();
                for (int i = 0; i <= endPort - startPort; i++) {
                    ports.add(startPort + i);
                }
            }
        }
    }
    return ports.iterator();
}
 
Example #4
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testRangeIsInAny() {
    List<Range> r1 = Arrays.asList(getRange(1, 3), getRange(5, 7));
    assertFalse(RangeUtils.isInAny(r1, 0));
    assertTrue(RangeUtils.isInAny(r1, 1));
    assertTrue(RangeUtils.isInAny(r1, 2));
    assertTrue(RangeUtils.isInAny(r1, 3));
    assertFalse(RangeUtils.isInAny(r1, 4));
    assertTrue(RangeUtils.isInAny(r1, 5));
    assertTrue(RangeUtils.isInAny(r1, 6));
    assertTrue(RangeUtils.isInAny(r1, 7));
    assertFalse(RangeUtils.isInAny(r1, 8));

    List<Range> r2 = Arrays.asList(getRange(2, 2));
    assertFalse(RangeUtils.isInAny(r2, 1));
    assertTrue(RangeUtils.isInAny(r2, 2));
    assertFalse(RangeUtils.isInAny(r2, 3));
}
 
Example #5
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergingOverlappingRanges() {
    List<Range> r1 = Arrays.asList(getRange(1, 3));
    List<Range> r2 = Arrays.asList(getRange(2, 4));

    List<Range> mergedRanges = RangeUtils.mergeRanges(r1, r2);

    assertEquals(1, mergedRanges.size());
    assertEquals(1, mergedRanges.get(0).getBegin());
    assertEquals(4, mergedRanges.get(0).getEnd());
}
 
Example #6
Source File: PrettyProtobuf.java    From storm with Apache License 2.0 5 votes vote down vote up
/**
 * Create String representation of mesos protobuf Range type.
 */
private static String rangeToString(Range range) {
  String beginStr = String.valueOf(range.getBegin());
  String endStr = String.valueOf(range.getEnd());
  /*
   * A Range representing a single number still has both Range.begin
   * and Range.end populated, but they are set to the same value.
   * In that case we just return "N" instead of "N-N".
   */
  if (range.getBegin() == range.getEnd()) {
    return beginStr;
  } else {
    return String.format("%s-%s", beginStr, endStr);
  }
}
 
Example #7
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubtractRangesForNilResult() {
    List<Range> r1 = Arrays.asList(getRange(2, 3));
    List<Range> r2 = Arrays.asList(getRange(1, 5));

    List<Range> difference = RangeUtils.subtractRanges(r1, r2);

    assertTrue(difference.isEmpty());
}
 
Example #8
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubtractRanges() {
    List<Range> r1 = Arrays.asList(getRange(1, 3), getRange(5, 7));
    List<Range> r2 = Arrays.asList(getRange(1, 3));

    List<Range> difference = RangeUtils.subtractRanges(r1, r2);

    assertEquals(1, difference.size());
    assertEquals(5, difference.get(0).getBegin());
    assertEquals(7, difference.get(0).getEnd());
}
 
Example #9
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergingInternallyOverlappingRanges() {
    List<Range> r1 = Arrays.asList(getRange(1, 5));
    List<Range> r2 = Arrays.asList(getRange(2, 4));

    List<Range> mergedRanges = RangeUtils.mergeRanges(r1, r2);

    assertEquals(1, mergedRanges.size());
    assertEquals(1, mergedRanges.get(0).getBegin());
    assertEquals(5, mergedRanges.get(0).getEnd());
}
 
Example #10
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergingNonOverlappingRanges() {
    List<Range> r1 = Arrays.asList(getRange(1, 3));
    List<Range> r2 = Arrays.asList(getRange(5, 7));

    List<Range> mergedRanges = RangeUtils.mergeRanges(r1, r2);

    assertEquals(2, mergedRanges.size());
    assertEquals(1, mergedRanges.get(0).getBegin());
    assertEquals(3, mergedRanges.get(0).getEnd());
    assertEquals(5, mergedRanges.get(1).getBegin());
    assertEquals(7, mergedRanges.get(1).getEnd());
}
 
Example #11
Source File: ValueUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
private static Integer compare(Value.Ranges ranges1, Value.Ranges ranges2) {
  List<Range> list1 = ranges1.getRangeList();
  List<Range> list2 = ranges2.getRangeList();

  if (RangeUtils.rangesEqual(list1, list2)) {
    return 0;
  } else if (RangeUtils.subtractRanges(list1, list2).size() == 0) {
    return -1;
  } else {
    return 1;
  }
}
 
Example #12
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
private static List<Range> intervalsToRanges(List<Interval> intervals) {
  List<Range> ranges = new ArrayList<Range>();
  for (Interval interval : intervals) {
    ranges.add(Range.newBuilder().setBegin(interval.a).setEnd(interval.b).build());
  }
  return ranges;
}
 
Example #13
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
private static List<Interval> rangesToIntervals(List<Range> ranges) {
  List<Interval> intervals = new ArrayList<Interval>();
  for (Range range : ranges) {
    intervals.add(rangeToInterval(range));
  }
  return intervals;
}
 
Example #14
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the provided value is encompassed by any of the provided ranges.
 */
public static boolean isInAny(List<Range> ranges, long value) {
  for (Interval interval : rangesToIntervals(ranges)) {
    if (interval.a <= value && value <= interval.b) {
      return true;
    }
  }
  return false;
}
 
Example #15
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the range intervals listed in {@code subtrahend} from {@code minuend}.
 */
@SuppressWarnings("checkstyle:LocalVariableName")
public static List<Range> subtractRanges(List<Range> minuend, List<Range> subtrahend) {
  IntervalSet iMinuend = intervalsToIntervalSet(rangesToIntervals(minuend));
  IntervalSet iSubtrahend = intervalsToIntervalSet(rangesToIntervals(subtrahend));
  IntervalSet iDifference = IntervalSet.subtract(iMinuend, iSubtrahend);
  return intervalSetToRanges(iDifference);
}
 
Example #16
Source File: ValueUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
private static Value.Ranges subtract(Value.Ranges ranges1, Value.Ranges ranges2) {
  List<Range> list1 = ranges1.getRangeList();
  List<Range> list2 = ranges2.getRangeList();
  List<Range> list = RangeUtils.subtractRanges(list1, list2);
  return Value.Ranges.newBuilder().addAllRange(list).build();
}
 
Example #17
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Combines and flattens the provided sets of ranges into a unified set.
 */
public static List<Range> mergeRanges(List<Range> r1, List<Range> r2) {
  List<Interval> intervals = rangesToIntervals(r1);
  intervals.addAll(rangesToIntervals(r2));
  return intervalsToRanges(intervalsToIntervalSet(intervals).getIntervals());
}
 
Example #18
Source File: ValueUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
private static Value.Ranges add(Value.Ranges ranges1, Value.Ranges ranges2) {
  List<Range> list1 = ranges1.getRangeList();
  List<Range> list2 = ranges2.getRangeList();
  List<Range> list = RangeUtils.mergeRanges(list1, list2);
  return Value.Ranges.newBuilder().addAllRange(list).build();
}
 
Example #19
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
private static List<Range> intervalSetToRanges(IntervalSet intervalSet) {
  return intervalSet.isNil()
      ? new ArrayList<Range>()
      : intervalsToRanges(intervalSet.getIntervals());
}
 
Example #20
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
private static Interval rangeToInterval(Range range) {
  return Interval.of((int) range.getBegin(), (int) range.getEnd());
}
 
Example #21
Source File: RangeUtilsTest.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
private static Range getRange(int begin, int end) {
    Range.Builder builder = Range.newBuilder();
    builder.setBegin(begin);
    builder.setEnd(end);
    return builder.build();
}
 
Example #22
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
public static Protos.Value.Ranges fromRangeList(List<Range> ranges) {
  return Protos.Value.Ranges.newBuilder().addAllRange(ranges).build();
}
 
Example #23
Source File: PrettyProtobuf.java    From storm with Apache License 2.0 4 votes vote down vote up
public String apply(Range r) {
  return rangeToString(r);
}
 
Example #24
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Returns whether the provided sets of ranges are equivalent when any overlaps are flattened.
 */
public static boolean rangesEqual(List<Range> list1, List<Range> list2) {
  IntervalSet i1 = intervalsToIntervalSet(rangesToIntervals(list1));
  IntervalSet i2 = intervalsToIntervalSet(rangesToIntervals(list2));
  return i1.equals(i2);
}