private long getValueInDefaultUnits(long value, String unit, String resourceName) { return unit.isEmpty() ? value : UnitsConversionUtil.convert(unit, ResourceUtils.getDefaultUnit(resourceName), value); }
@Override public int compareTo(ResourceInformation other) { int diff = this.name.compareTo(other.name); if (diff == 0) { diff = UnitsConversionUtil .compare(this.units, this.value, other.units, other.value); if (diff == 0) { diff = this.resourceType.compareTo(other.resourceType); } } return diff; } }
checkUnitArgument(unitA); checkUnitArgument(unitB); if (unitA.equals(unitB)) { return Long.compare(valueA, valueB); Converter unitAC = getConverter(unitA); Converter unitBC = getConverter(unitB); int unitAPos = SORTED_UNITS.indexOf(unitA); int unitBPos = SORTED_UNITS.indexOf(unitB); long tmpB = valueB; if (unitAPos < unitBPos) { tmpB = convert(unitB, unitA, valueB); } else { tmpA = convert(unitA, unitB, valueA);
int unitsRelation = UnitsConversionUtil.compareUnits(requestedRI.getUnits(), availableRI.getUnits()); UnitsConversionUtil.convert(availableRI.getUnits(), requestedRI.getUnits(), availableRI.getValue()); UnitsConversionUtil.convert(requestedRI.getUnits(), availableRI.getUnits(), requestedRI.getValue());
/** * Compare a unit to another unit. * <br> * Examples:<br> * 1. 'm' (milli) is smaller than 'k' (kilo), so compareUnits("m", "k") * will return -1.<br> * 2. 'M' (MEGA) is greater than 'k' (kilo), so compareUnits("M", "k") will * return 1. * * @param unitA first unit * @param unitB second unit * @return +1, 0 or -1 depending on whether the relationship between units * is smaller than, * equal to or lesser than. */ public static int compareUnits(String unitA, String unitB) { checkUnitArgument(unitA); checkUnitArgument(unitB); int unitAPos = SORTED_UNITS.indexOf(unitA); int unitBPos = SORTED_UNITS.indexOf(unitB); return Integer.compare(unitAPos, unitBPos); } }
return fromValue; Converter fc = getConverter(fromUnit); Converter tc = getConverter(toUnit); long numerator = fc.numerator * tc.denominator; long denominator = fc.denominator * tc.numerator;
Long resourceValue = Long.valueOf(valueWithoutUnit); if (!units.isEmpty()) { resourceValue = UnitsConversionUtil.convert(units, "Mi", resourceValue);
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof ResourceInformation)) { return false; } ResourceInformation r = (ResourceInformation) obj; if (!this.name.equals(r.getName()) || !this.resourceType.equals(r.getResourceType())) { return false; } if (this.units.equals(r.units)) { return this.value == r.value; } return (UnitsConversionUtil.compare(this.units, this.value, r.units, r.value) == 0); }
private static void addResourceTypeInformation(String prop, String value, Map<String, ResourceInformation> nodeResources) { if (prop.startsWith(YarnConfiguration.NM_RESOURCES_PREFIX)) { LOG.info("Found resource entry " + prop); String resourceType = prop.substring( YarnConfiguration.NM_RESOURCES_PREFIX.length()); if (!nodeResources.containsKey(resourceType)) { nodeResources .put(resourceType, ResourceInformation.newInstance(resourceType)); } String units = getUnits(value); Long resourceValue = Long.valueOf(value.substring(0, value.length() - units.length())); String destUnit = getDefaultUnit(resourceType); if(!units.equals(destUnit)) { resourceValue = UnitsConversionUtil.convert( units, destUnit, resourceValue); units = destUnit; } nodeResources.get(resourceType).setValue(resourceValue); nodeResources.get(resourceType).setUnits(units); if (LOG.isDebugEnabled()) { LOG.debug("Setting value for resource type " + resourceType + " to " + resourceValue + " with units " + units); } } }
long valueB = 2; Assert.assertEquals(1, UnitsConversionUtil.compare(unitA, valueA, unitB, valueB)); Assert.assertEquals(-1, UnitsConversionUtil.compare(unitB, valueB, unitA, valueA)); Assert.assertEquals(0, UnitsConversionUtil.compare(unitA, valueA, unitA, valueA)); Assert.assertEquals(-1, UnitsConversionUtil.compare(unitA, valueA, unitA, valueB)); Assert.assertEquals(1, UnitsConversionUtil.compare(unitA, valueB, unitA, valueA)); UnitsConversionUtil.compare(unitA, valueA, unitB, valueB)); Assert.assertEquals(-1, UnitsConversionUtil.compare(unitB, valueB, unitA, valueA)); Assert.assertEquals(0, UnitsConversionUtil.compare(unitA, valueA, unitB, 1000L)); UnitsConversionUtil.compare(unitA, valueA, unitB, valueB)); Assert.assertEquals(1, UnitsConversionUtil.compare(unitB, valueB, unitA, valueA)); Assert.assertEquals(0, UnitsConversionUtil.compare(unitA, 1000L, unitB, valueA));
if (!resourceTypesFromRM.get(resName).getUnits() .equals(resourceValue[0])) { value = UnitsConversionUtil.convert(resourceValue[0], resourceTypesFromRM.get(resName).getUnits(), value);
private void updateResourceValuesFromConfig(Set<String> resourceTypes, Resource resource, String[] splits) { // If key is not a valid type, skip it. if (!resourceTypes.contains(splits[0])) { return; } String units = getUnits(splits[1]); Long resourceValue = Long .valueOf(splits[1].substring(0, splits[1].length() - units.length())); // Convert all incoming units to MB if units is configured. if (!units.isEmpty()) { resourceValue = UnitsConversionUtil.convert(units, "Mi", resourceValue); } // map it based on key. AbsoluteResourceType resType = AbsoluteResourceType .valueOf(StringUtils.toUpperCase(splits[0].trim())); switch (resType) { case MEMORY : resource.setMemorySize(resourceValue); break; case VCORES : resource.setVirtualCores(resourceValue.intValue()); break; default : resource.setResourceInformation(splits[0].trim(), ResourceInformation .newInstance(splits[0].trim(), units, resourceValue)); break; } }
@Override public long getMemorySize() { // memory should always be present ResourceInformation ri = resources[MEMORY_INDEX]; if (ri.getUnits().isEmpty()) { return ri.getValue(); } return UnitsConversionUtil.convert(ri.getUnits(), ResourceInformation.MEMORY_MB.getUnits(), ri.getValue()); }
@Test public void testOverflow() { long test = 5 * 1000L * 1000L * 1000L * 1000L * 1000L; try { UnitsConversionUtil.convert("P", "p", test); Assert.fail("this operation should result in an overflow"); } catch (IllegalArgumentException ie) { // do nothing } try { UnitsConversionUtil.convert("m", "p", Long.MAX_VALUE - 1); Assert.fail("this operation should result in an overflow"); } catch (IllegalArgumentException ie) { // do nothing } }
Assert.assertEquals("pico test failed", value * 1000L * 1000L * 1000L * 1000L, UnitsConversionUtil.convert(fromUnit, "p", test)); Assert.assertEquals("nano test failed", value * 1000L * 1000L * 1000L, UnitsConversionUtil.convert(fromUnit, "n", test)); Assert .assertEquals("micro test failed", value * 1000L * 1000L, UnitsConversionUtil.convert(fromUnit, "u", test)); Assert.assertEquals("milli test failed", value * 1000L, UnitsConversionUtil.convert(fromUnit, "m", test)); UnitsConversionUtil.convert(fromUnit, "k", test)); UnitsConversionUtil.convert(fromUnit, "M", test)); Assert.assertEquals("giga test failed", test / (1000L * 1000L * 1000L), UnitsConversionUtil.convert(fromUnit, "G", test)); Assert.assertEquals("tera test failed", test / (1000L * 1000L * 1000L * 1000L), UnitsConversionUtil.convert(fromUnit, "T", test)); Assert.assertEquals("peta test failed", test / (1000L * 1000L * 1000L * 1000L * 1000L), UnitsConversionUtil.convert(fromUnit, "P", test)); UnitsConversionUtil.convert("n", "p", value)); UnitsConversionUtil.convert("M", "G", value * 1000L));
private Map<String, Float> getEffectiveMinRatioPerResource( Resource configuredMinResources, Resource numeratorForMinRatio) { Map<String, Float> effectiveMinRatioPerResource = new HashMap<>(); if (numeratorForMinRatio != null) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation nResourceInformation = numeratorForMinRatio .getResourceInformation(i); ResourceInformation dResourceInformation = configuredMinResources .getResourceInformation(i); long nValue = nResourceInformation.getValue(); long dValue = UnitsConversionUtil.convert( dResourceInformation.getUnits(), nResourceInformation.getUnits(), dResourceInformation.getValue()); if (dValue != 0) { effectiveMinRatioPerResource.put(nResourceInformation.getName(), (float) nValue / dValue); } } } return effectiveMinRatioPerResource; }
resourceRequest.getUnits(); this.resourceCapability.setMemorySize( UnitsConversionUtil.convert(units, "Mi", resourceRequest.getValue())); memorySet = true; resourceName)) { this.resourceCapability.setVirtualCores( (int) UnitsConversionUtil.convert(resourceRequest.getUnits(), "", resourceRequest.getValue())); cpuVcoresSet = true;
long dValue = UnitsConversionUtil.convert( dResourceInformation.getUnits(), nResourceInformation.getUnits(), dResourceInformation.getValue());
private static ResourceInformation newDefaultInformation( ResourceInformation resourceInformation, ResourceInformationProto entry) { ResourceInformation ri = new ResourceInformation(); ri.setName(resourceInformation.getName()); ri.setMinimumAllocation(resourceInformation.getMinimumAllocation()); ri.setMaximumAllocation(resourceInformation.getMaximumAllocation()); ri.setResourceType(entry.hasType() ? ProtoUtils.convertFromProtoFormat(entry.getType()) : ResourceTypes.COUNTABLE); String units = entry.hasUnits() ? entry.getUnits() : ResourceUtils.getDefaultUnit(entry.getKey()); long value = entry.hasValue() ? entry.getValue() : 0L; String destUnit = ResourceUtils.getDefaultUnit(entry.getKey()); if(!units.equals(destUnit)) { ri.setValue(UnitsConversionUtil.convert(units, destUnit, value)); ri.setUnits(destUnit); } else { ri.setUnits(units); ri.setValue(value); } return ri; }