private long getValueInDefaultUnits(long value, String unit, String resourceName) { return unit.isEmpty() ? value : UnitsConversionUtil.convert(unit, ResourceUtils.getDefaultUnit(resourceName), value); }
Long resourceValue = Long.valueOf(valueWithoutUnit); if (!units.isEmpty()) { resourceValue = UnitsConversionUtil.convert(units, "Mi", resourceValue);
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); } } }
@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()); }
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; } }
if (!resourceTypesFromRM.get(resName).getUnits() .equals(resourceValue[0])) { value = UnitsConversionUtil.convert(resourceValue[0], resourceTypesFromRM.get(resName).getUnits(), value);
@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; }
Resource max = Resource.newInstance(Long.MAX_VALUE, Integer.MAX_VALUE, Collections.singletonMap(A_CUSTOM_RESOURCE, Long.MAX_VALUE / UnitsConversionUtil.convert("k", "", 1L))); assertEquals(customResourceInformation(10000L, ""), calculator.normalize(customResource(9999L, ""), min, max, increment)