Search in sources :

Example 6 with ValueRange

use of com.microsoft.frameworklauncher.common.model.ValueRange in project pai by Microsoft.

the class ValueRangeUtils method fitInRange.

/*
    verify if the bigRange include the small range
   */
public static boolean fitInRange(List<ValueRange> smallRange, List<ValueRange> bigRange) {
    if (smallRange == null) {
        return true;
    }
    if (bigRange == null) {
        return false;
    }
    List<ValueRange> result = coalesceRangeList(bigRange);
    List<ValueRange> smallRangeList = coalesceRangeList(smallRange);
    int i = 0;
    int j = 0;
    while (i < result.size() && j < smallRangeList.size()) {
        ValueRange big = result.get(i);
        ValueRange small = smallRangeList.get(j);
        if (small.getBegin() < big.getBegin()) {
            return false;
        }
        if (small.getBegin() <= big.getEnd()) {
            if (small.getEnd() > big.getEnd()) {
                return false;
            } else {
                big.setBegin(small.getEnd() + 1);
                j++;
            }
        } else {
            i++;
        }
    }
    return (j >= smallRangeList.size());
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange)

Example 7 with ValueRange

use of com.microsoft.frameworklauncher.common.model.ValueRange in project pai by Microsoft.

the class ValueRangeUtils method getValue.

/*
    get the value at "index" location in the Range list
   */
public static Integer getValue(List<ValueRange> list, int index) {
    if (list == null) {
        return -1;
    }
    List<ValueRange> ranges = coalesceRangeList(list);
    int i = index;
    for (ValueRange range : ranges) {
        if (range.getEnd() - range.getBegin() < i) {
            i -= (range.getEnd() - range.getBegin() + 1);
        } else {
            return (range.getBegin() + i);
        }
    }
    return -1;
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange)

Example 8 with ValueRange

use of com.microsoft.frameworklauncher.common.model.ValueRange in project pai by Microsoft.

the class ValueRangeUtils method coalesceRangeList.

/*
    coalesce the duplicate or overlap range in the range list.
   */
public static List<ValueRange> coalesceRangeList(List<ValueRange> rangeList) {
    if (rangeList == null || rangeList.isEmpty()) {
        return rangeList;
    }
    List<ValueRange> sortedList = SortRangeList(rangeList);
    List<ValueRange> resultList = new ArrayList<ValueRange>();
    ValueRange current = sortedList.get(0).clone();
    resultList.add(current);
    for (ValueRange range : sortedList) {
        // Skip if this range is equivalent to the current range.
        if (range.getBegin().intValue() == current.getBegin().intValue() && range.getEnd().intValue() == current.getEnd().intValue()) {
            continue;
        }
        // If the current range just needs to be extended on the right.
        if (range.getBegin().intValue() == current.getBegin().intValue() && range.getEnd() > current.getEnd()) {
            current.setEnd(range.getEnd());
        } else if (range.getBegin() > current.getBegin()) {
            // If we are starting farther ahead, then there are 2 cases:
            if (range.getBegin() <= current.getEnd() + 1) {
                // 1. Ranges are overlapping and we can merge them.
                current.setEnd(Math.max(current.getEnd(), range.getEnd()));
            } else {
                // 2. No overlap and we are adding a new range.
                current = range.clone();
                resultList.add(current);
            }
        }
    }
    return resultList;
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange)

Example 9 with ValueRange

use of com.microsoft.frameworklauncher.common.model.ValueRange in project pai by Microsoft.

the class ValueRangeUtils method intersectRangeList.

/*
    get the overlap part of tow range lists
   */
public static List<ValueRange> intersectRangeList(List<ValueRange> leftRange, List<ValueRange> rightRange) {
    if (leftRange == null || rightRange == null) {
        return null;
    }
    List<ValueRange> leftList = coalesceRangeList(leftRange);
    List<ValueRange> rightList = coalesceRangeList(rightRange);
    List<ValueRange> result = new ArrayList<ValueRange>();
    int i = 0;
    int j = 0;
    while (i < leftList.size() && j < rightList.size()) {
        ValueRange left = leftList.get(i);
        ValueRange right = rightList.get(j);
        // 1. no overlap, right is bigger than left
        if (left.getEnd() < right.getBegin()) {
            i++;
        // 2. no overlap, left is bigger than right
        } else if (right.getEnd() < left.getBegin()) {
            j++;
        // 3. has overlap, get the overlap
        } else {
            result.add(ValueRange.newInstance(Math.max(left.getBegin(), right.getBegin()), Math.min(left.getEnd(), right.getEnd())));
            if (left.getEnd() < right.getEnd()) {
                i++;
            } else {
                j++;
            }
        }
    }
    return result;
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange)

Example 10 with ValueRange

use of com.microsoft.frameworklauncher.common.model.ValueRange in project pai by Microsoft.

the class ValueRangeUtils method toEnviromentVariableString.

// This function is to convert port from List<ValueRange> format to string format
// The string format is "httpPort:80,81,82;sshPort:1021,1022,1023;"
// the Ports label defined in portsDefinitions
public static String toEnviromentVariableString(List<ValueRange> portRanges, Map<String, Ports> portsDefinitions) {
    StringBuilder portsString = new StringBuilder();
    if (portsDefinitions != null && !portsDefinitions.isEmpty()) {
        Iterator iter = portsDefinitions.entrySet().iterator();
        int basePort = 0;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            Ports ports = (Ports) entry.getValue();
            // if user specified ports, directly use the PortDefinitions in request.
            if (ports.getStart() > 0) {
                portsString.append(key + ":" + ports.getStart());
                for (int i = 2; i <= ports.getCount(); i++) {
                    portsString.append("," + (ports.getStart() + i - 1));
                }
                portsString.append(";");
            } else {
                // if user not specified ports, assign the allocated ContainerPorts to each port label.
                List<ValueRange> assignPorts = ValueRangeUtils.getSubRange(portRanges, ports.getCount(), basePort);
                basePort = assignPorts.get(assignPorts.size() - 1).getEnd() + 1;
                portsString.append(key + ":" + assignPorts.get(0).toDetailString(","));
                for (int i = 1; i < assignPorts.size(); i++) {
                    portsString.append("," + assignPorts.get(i).toDetailString(","));
                }
                portsString.append(";");
            }
        }
    }
    return portsString.toString();
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange) Ports(com.microsoft.frameworklauncher.common.model.Ports)

Aggregations

ValueRange (com.microsoft.frameworklauncher.common.model.ValueRange)11 Ports (com.microsoft.frameworklauncher.common.model.Ports)2 ResourceDescriptor (com.microsoft.frameworklauncher.common.model.ResourceDescriptor)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 NotAvailableException (com.microsoft.frameworklauncher.common.exceptions.NotAvailableException)1 NodeConfiguration (com.microsoft.frameworklauncher.common.model.NodeConfiguration)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1