YARN-6892. [YARN-3926] Improve API implementation in Resources and DominantResourceCalculator class. Contributed by Sunil G.

This commit is contained in:
Sunil G 2017-08-16 15:25:36 +05:30 committed by Wangda Tan
parent d5e9939ebb
commit 2b51b262ab
3 changed files with 251 additions and 228 deletions

View File

@ -164,7 +164,6 @@ public void setMemorySize(long memory) {
"This method is implemented by ResourcePBImpl"); "This method is implemented by ResourcePBImpl");
} }
/** /**
* Get <em>number of virtual cpu cores</em> of the resource. * Get <em>number of virtual cpu cores</em> of the resource.
* *
@ -224,6 +223,27 @@ public ResourceInformation getResourceInformation(String resource)
+ "'. Known resources are " + Arrays.toString(resources)); + "'. Known resources are " + Arrays.toString(resources));
} }
/**
* Get ResourceInformation for a specified resource from a given index.
*
* @param index
* of the resource
* @return the ResourceInformation object for the resource
* @throws ResourceNotFoundException
* if the resource can't be found
*/
@Public
@Evolving
public ResourceInformation getResourceInformation(int index)
throws ResourceNotFoundException {
ResourceInformation[] resources = getResources();
if (index < 0 || index >= resources.length) {
throw new ResourceNotFoundException("Unknown resource at index '" + index
+ "'. Vaid resources are: " + Arrays.toString(resources));
}
return resources[index];
}
/** /**
* Get the value for a specified resource. No information about the units is * Get the value for a specified resource. No information about the units is
* returned. * returned.
@ -263,6 +283,29 @@ public void setResourceInformation(String resource,
ResourceInformation.copy(resourceInformation, storedResourceInfo); ResourceInformation.copy(resourceInformation, storedResourceInfo);
} }
/**
* Set the ResourceInformation object for a particular resource.
*
* @param index
* the resource index for which the ResourceInformation is provided
* @param resourceInformation
* ResourceInformation object
* @throws ResourceNotFoundException
* if the resource is not found
*/
@Public
@Evolving
public void setResourceInformation(int index,
ResourceInformation resourceInformation)
throws ResourceNotFoundException {
ResourceInformation[] resources = getResources();
if (index < 0 || index >= resources.length) {
throw new ResourceNotFoundException("Unknown resource at index '" + index
+ "'. Valid resources are " + Arrays.toString(resources));
}
ResourceInformation.copy(resourceInformation, resources[index]);
}
/** /**
* Set the value of a resource in the ResourceInformation object. The unit of * Set the value of a resource in the ResourceInformation object. The unit of
* the value is assumed to be the one in the ResourceInformation object. * the value is assumed to be the one in the ResourceInformation object.
@ -288,6 +331,29 @@ public void setResourceValue(String resource, long value)
storedResourceInfo.setValue(value); storedResourceInfo.setValue(value);
} }
/**
* Set the value of a resource in the ResourceInformation object. The unit of
* the value is assumed to be the one in the ResourceInformation object.
*
* @param index
* the resource index for which the value is provided.
* @param value
* the value to set
* @throws ResourceNotFoundException
* if the resource is not found
*/
@Public
@Evolving
public void setResourceValue(int index, long value)
throws ResourceNotFoundException {
ResourceInformation[] resources = getResources();
if (index < 0 || index >= resources.length) {
throw new ResourceNotFoundException("Unknown resource at index '" + index
+ "'. Valid resources are " + Arrays.toString(resources));
}
resources[index].setValue(value);
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 263167; final int prime = 263167;

View File

@ -17,13 +17,10 @@
*/ */
package org.apache.hadoop.yarn.util.resource; package org.apache.hadoop.yarn.util.resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.classification.InterfaceStability.Unstable;
import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.api.records.ResourceInformation; import org.apache.hadoop.yarn.api.records.ResourceInformation;
import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException;
import org.apache.hadoop.yarn.util.UnitsConversionUtil; import org.apache.hadoop.yarn.util.UnitsConversionUtil;
@ -51,14 +48,8 @@
@Private @Private
@Unstable @Unstable
public class DominantResourceCalculator extends ResourceCalculator { public class DominantResourceCalculator extends ResourceCalculator {
private static final Log LOG =
LogFactory.getLog(DominantResourceCalculator.class);
private String[] resourceNames;
public DominantResourceCalculator() { public DominantResourceCalculator() {
resourceNames = ResourceUtils.getResourceNamesArray();
} }
/** /**
@ -75,22 +66,18 @@ private int compare(Resource lhs, Resource rhs) {
boolean rhsGreater = false; boolean rhsGreater = false;
int ret = 0; int ret = 0;
for (String rName : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation lhsResourceInformation = ResourceInformation lhsResourceInformation = lhs
lhs.getResourceInformation(rName); .getResourceInformation(i);
ResourceInformation rhsResourceInformation = ResourceInformation rhsResourceInformation = rhs
rhs.getResourceInformation(rName); .getResourceInformation(i);
int diff = lhsResourceInformation.compareTo(rhsResourceInformation); int diff = lhsResourceInformation.compareTo(rhsResourceInformation);
if (diff >= 1) { if (diff >= 1) {
lhsGreater = true; lhsGreater = true;
} else if (diff <= -1) { } else if (diff <= -1) {
rhsGreater = true; rhsGreater = true;
} }
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + rName, ye);
}
} }
if (lhsGreater && rhsGreater) { if (lhsGreater && rhsGreater) {
ret = 0; ret = 0;
@ -147,50 +134,40 @@ protected float getResourceAsValue(Resource clusterResource,
float min = Float.MAX_VALUE; float min = Float.MAX_VALUE;
float max = 0.0f; float max = 0.0f;
for (String rName : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation clusterResourceResourceInformation = ResourceInformation clusterResourceResourceInformation = clusterResource
clusterResource.getResourceInformation(rName); .getResourceInformation(i);
ResourceInformation resourceInformation = ResourceInformation resourceInformation = resource
resource.getResourceInformation(rName); .getResourceInformation(i);
long resourceValue = UnitsConversionUtil long resourceValue = UnitsConversionUtil.convert(
.convert(resourceInformation.getUnits(), resourceInformation.getUnits(),
clusterResourceResourceInformation.getUnits(), clusterResourceResourceInformation.getUnits(),
resourceInformation.getValue()); resourceInformation.getValue());
float tmp = float tmp = (float) resourceValue
(float) resourceValue / (float) clusterResourceResourceInformation / (float) clusterResourceResourceInformation.getValue();
.getValue();
min = min < tmp ? min : tmp; min = min < tmp ? min : tmp;
max = max > tmp ? max : tmp; max = max > tmp ? max : tmp;
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
}
} }
return (dominant) ? max : min; return (dominant) ? max : min;
} }
@Override @Override
public long computeAvailableContainers(Resource available, Resource required) { public long computeAvailableContainers(Resource available,
Resource required) {
long min = Long.MAX_VALUE; long min = Long.MAX_VALUE;
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation availableResource = ResourceInformation availableResource = available
available.getResourceInformation(resource); .getResourceInformation(i);
ResourceInformation requiredResource = ResourceInformation requiredResource = required.getResourceInformation(i);
required.getResourceInformation(resource); long requiredResourceValue = UnitsConversionUtil.convert(
long requiredResourceValue = UnitsConversionUtil requiredResource.getUnits(), availableResource.getUnits(),
.convert(requiredResource.getUnits(), availableResource.getUnits(),
requiredResource.getValue()); requiredResource.getValue());
if (requiredResourceValue != 0) { if (requiredResourceValue != 0) {
long tmp = availableResource.getValue() / requiredResourceValue; long tmp = availableResource.getValue() / requiredResourceValue;
min = min < tmp ? min : tmp; min = min < tmp ? min : tmp;
} }
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
}
} }
return min > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) min; return min > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) min;
} }
@ -216,23 +193,16 @@ public boolean isInvalidDivisor(Resource r) {
@Override @Override
public float ratio(Resource a, Resource b) { public float ratio(Resource a, Resource b) {
float ratio = 0.0f; float ratio = 0.0f;
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation aResourceInformation = ResourceInformation aResourceInformation = a.getResourceInformation(i);
a.getResourceInformation(resource); ResourceInformation bResourceInformation = b.getResourceInformation(i);
ResourceInformation bResourceInformation = long bResourceValue = UnitsConversionUtil.convert(
b.getResourceInformation(resource); bResourceInformation.getUnits(), aResourceInformation.getUnits(),
long bResourceValue = UnitsConversionUtil
.convert(bResourceInformation.getUnits(),
aResourceInformation.getUnits(),
bResourceInformation.getValue()); bResourceInformation.getValue());
float tmp = float tmp = (float) aResourceInformation.getValue()
(float) aResourceInformation.getValue() / (float) bResourceValue; / (float) bResourceValue;
ratio = ratio > tmp ? ratio : tmp; ratio = ratio > tmp ? ratio : tmp;
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
}
} }
return ratio; return ratio;
} }
@ -244,16 +214,11 @@ public Resource divideAndCeil(Resource numerator, int denominator) {
public Resource divideAndCeil(Resource numerator, long denominator) { public Resource divideAndCeil(Resource numerator, long denominator) {
Resource ret = Resource.newInstance(numerator); Resource ret = Resource.newInstance(numerator);
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation resourceInformation = ResourceInformation resourceInformation = ret.getResourceInformation(i);
ret.getResourceInformation(resource); resourceInformation
resourceInformation.setValue( .setValue(divideAndCeil(resourceInformation.getValue(), denominator));
divideAndCeil(resourceInformation.getValue(), denominator));
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
}
} }
return ret; return ret;
} }
@ -270,29 +235,28 @@ public Resource divideAndCeil(Resource numerator, float denominator) {
public Resource normalize(Resource r, Resource minimumResource, public Resource normalize(Resource r, Resource minimumResource,
Resource maximumResource, Resource stepFactor) { Resource maximumResource, Resource stepFactor) {
Resource ret = Resource.newInstance(r); Resource ret = Resource.newInstance(r);
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation rResourceInformation = ResourceInformation rResourceInformation = r.getResourceInformation(i);
r.getResourceInformation(resource); ResourceInformation minimumResourceInformation = minimumResource
ResourceInformation minimumResourceInformation = .getResourceInformation(i);
minimumResource.getResourceInformation(resource); ResourceInformation maximumResourceInformation = maximumResource
ResourceInformation maximumResourceInformation = .getResourceInformation(i);
maximumResource.getResourceInformation(resource); ResourceInformation stepFactorResourceInformation = stepFactor
ResourceInformation stepFactorResourceInformation = .getResourceInformation(i);
stepFactor.getResourceInformation(resource); ResourceInformation tmp = ret.getResourceInformation(i);
ResourceInformation tmp = ret.getResourceInformation(resource);
long rValue = rResourceInformation.getValue(); long rValue = rResourceInformation.getValue();
long minimumValue = UnitsConversionUtil long minimumValue = UnitsConversionUtil.convert(
.convert(minimumResourceInformation.getUnits(), minimumResourceInformation.getUnits(),
rResourceInformation.getUnits(), rResourceInformation.getUnits(),
minimumResourceInformation.getValue()); minimumResourceInformation.getValue());
long maximumValue = UnitsConversionUtil long maximumValue = UnitsConversionUtil.convert(
.convert(maximumResourceInformation.getUnits(), maximumResourceInformation.getUnits(),
rResourceInformation.getUnits(), rResourceInformation.getUnits(),
maximumResourceInformation.getValue()); maximumResourceInformation.getValue());
long stepFactorValue = UnitsConversionUtil long stepFactorValue = UnitsConversionUtil.convert(
.convert(stepFactorResourceInformation.getUnits(), stepFactorResourceInformation.getUnits(),
rResourceInformation.getUnits(), rResourceInformation.getUnits(),
stepFactorResourceInformation.getValue()); stepFactorResourceInformation.getValue());
long value = Math.max(rValue, minimumValue); long value = Math.max(rValue, minimumValue);
@ -300,11 +264,7 @@ public Resource normalize(Resource r, Resource minimumResource,
value = roundUp(value, stepFactorValue); value = roundUp(value, stepFactorValue);
} }
tmp.setValue(Math.min(value, maximumValue)); tmp.setValue(Math.min(value, maximumValue));
ret.setResourceInformation(resource, tmp); ret.setResourceInformation(i, tmp);
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
}
} }
return ret; return ret;
} }
@ -321,30 +281,26 @@ public Resource roundDown(Resource r, Resource stepFactor) {
private Resource rounding(Resource r, Resource stepFactor, boolean roundUp) { private Resource rounding(Resource r, Resource stepFactor, boolean roundUp) {
Resource ret = Resource.newInstance(r); Resource ret = Resource.newInstance(r);
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation rResourceInformation = ResourceInformation rResourceInformation = r.getResourceInformation(i);
r.getResourceInformation(resource); ResourceInformation stepFactorResourceInformation = stepFactor
ResourceInformation stepFactorResourceInformation = .getResourceInformation(i);
stepFactor.getResourceInformation(resource);
long rValue = rResourceInformation.getValue(); long rValue = rResourceInformation.getValue();
long stepFactorValue = UnitsConversionUtil long stepFactorValue = UnitsConversionUtil.convert(
.convert(stepFactorResourceInformation.getUnits(), stepFactorResourceInformation.getUnits(),
rResourceInformation.getUnits(), rResourceInformation.getUnits(),
stepFactorResourceInformation.getValue()); stepFactorResourceInformation.getValue());
long value = rValue; long value = rValue;
if (stepFactorValue != 0) { if (stepFactorValue != 0) {
value = roundUp ? roundUp(rValue, stepFactorValue) : value = roundUp
roundDown(rValue, stepFactorValue); ? roundUp(rValue, stepFactorValue)
} : roundDown(rValue, stepFactorValue);
ResourceInformation
.copy(rResourceInformation, ret.getResourceInformation(resource));
ret.getResourceInformation(resource).setValue(value);
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
} }
ResourceInformation.copy(rResourceInformation,
ret.getResourceInformation(i));
ret.getResourceInformation(i).setValue(value);
} }
return ret; return ret;
} }
@ -364,13 +320,12 @@ public Resource multiplyAndNormalizeDown(Resource r, double by,
private Resource multiplyAndNormalize(Resource r, double by, private Resource multiplyAndNormalize(Resource r, double by,
Resource stepFactor, boolean roundUp) { Resource stepFactor, boolean roundUp) {
Resource ret = Resource.newInstance(r); Resource ret = Resource.newInstance(r);
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation rResourceInformation = r ResourceInformation rResourceInformation = r.getResourceInformation(i);
.getResourceInformation(resource);
ResourceInformation stepFactorResourceInformation = stepFactor ResourceInformation stepFactorResourceInformation = stepFactor
.getResourceInformation(resource); .getResourceInformation(i);
ResourceInformation tmp = ret.getResourceInformation(resource); ResourceInformation tmp = ret.getResourceInformation(i);
long rValue = rResourceInformation.getValue(); long rValue = rResourceInformation.getValue();
long stepFactorValue = UnitsConversionUtil.convert( long stepFactorValue = UnitsConversionUtil.convert(
@ -383,37 +338,27 @@ private Resource multiplyAndNormalize(Resource r, double by,
? roundUp((long) Math.ceil(rValue * by), stepFactorValue) ? roundUp((long) Math.ceil(rValue * by), stepFactorValue)
: roundDown((long) (rValue * by), stepFactorValue); : roundDown((long) (rValue * by), stepFactorValue);
} else { } else {
value = roundUp value = roundUp ? (long) Math.ceil(rValue * by) : (long) (rValue * by);
? (long) Math.ceil(rValue * by)
: (long) (rValue * by);
} }
tmp.setValue(value); tmp.setValue(value);
} catch (ResourceNotFoundException ye) {
throw new IllegalArgumentException(
"Error getting resource information for " + resource, ye);
}
} }
return ret; return ret;
} }
@Override @Override
public boolean fitsIn(Resource cluster, Resource smaller, Resource bigger) { public boolean fitsIn(Resource cluster, Resource smaller, Resource bigger) {
for (String resource : resourceNames) { int maxLength = ResourceUtils.getResourceTypesArray().length;
try { for (int i = 0; i < maxLength; i++) {
ResourceInformation sResourceInformation = ResourceInformation sResourceInformation = smaller
smaller.getResourceInformation(resource); .getResourceInformation(i);
ResourceInformation bResourceInformation = ResourceInformation bResourceInformation = bigger
bigger.getResourceInformation(resource); .getResourceInformation(i);
long sResourceValue = UnitsConversionUtil long sResourceValue = UnitsConversionUtil.convert(
.convert(sResourceInformation.getUnits(), sResourceInformation.getUnits(), bResourceInformation.getUnits(),
bResourceInformation.getUnits(),
sResourceInformation.getValue()); sResourceInformation.getValue());
if (sResourceValue > bResourceInformation.getValue()) { if (sResourceValue > bResourceInformation.getValue()) {
return false; return false;
} }
} catch (ResourceNotFoundException ye) {
return false;
}
} }
return true; return true;
} }

View File

@ -173,17 +173,17 @@ public static Resource clone(Resource res) {
} }
public static Resource addTo(Resource lhs, Resource rhs) { public static Resource addTo(Resource lhs, Resource rhs) {
for (ResourceInformation entry : lhs.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
try { try {
ResourceInformation rhsValue = rhs.getResourceInformation(name); ResourceInformation rhsValue = rhs.getResourceInformation(i);
ResourceInformation lhsValue = entry; ResourceInformation lhsValue = lhs.getResourceInformation(i);
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
? rhsValue.getValue() ? rhsValue.getValue()
: UnitsConversionUtil.convert(rhsValue.getUnits(), : UnitsConversionUtil.convert(rhsValue.getUnits(),
lhsValue.getUnits(), rhsValue.getValue()); lhsValue.getUnits(), rhsValue.getValue());
lhs.setResourceValue(name, lhsValue.getValue() + convertedRhs); lhs.setResourceValue(i, lhsValue.getValue() + convertedRhs);
} catch (ResourceNotFoundException ye) { } catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage()); LOG.warn("Resource is missing:" + ye.getMessage());
continue; continue;
@ -197,17 +197,17 @@ public static Resource add(Resource lhs, Resource rhs) {
} }
public static Resource subtractFrom(Resource lhs, Resource rhs) { public static Resource subtractFrom(Resource lhs, Resource rhs) {
for (ResourceInformation entry : lhs.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
try { try {
ResourceInformation rhsValue = rhs.getResourceInformation(name); ResourceInformation rhsValue = rhs.getResourceInformation(i);
ResourceInformation lhsValue = entry; ResourceInformation lhsValue = lhs.getResourceInformation(i);
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
? rhsValue.getValue() ? rhsValue.getValue()
: UnitsConversionUtil.convert(rhsValue.getUnits(), : UnitsConversionUtil.convert(rhsValue.getUnits(),
lhsValue.getUnits(), rhsValue.getValue()); lhsValue.getUnits(), rhsValue.getValue());
lhs.setResourceValue(name, lhsValue.getValue() - convertedRhs); lhs.setResourceValue(i, lhsValue.getValue() - convertedRhs);
} catch (ResourceNotFoundException ye) { } catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage()); LOG.warn("Resource is missing:" + ye.getMessage());
continue; continue;
@ -243,10 +243,15 @@ public static Resource negate(Resource resource) {
} }
public static Resource multiplyTo(Resource lhs, double by) { public static Resource multiplyTo(Resource lhs, double by) {
for (ResourceInformation entry : lhs.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
ResourceInformation lhsValue = entry; try {
lhs.setResourceValue(name, (long) (lhsValue.getValue() * by)); ResourceInformation lhsValue = lhs.getResourceInformation(i);
lhs.setResourceValue(i, (long) (lhsValue.getValue() * by));
} catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage());
continue;
}
} }
return lhs; return lhs;
} }
@ -261,11 +266,11 @@ public static Resource multiply(Resource lhs, double by) {
*/ */
public static Resource multiplyAndAddTo( public static Resource multiplyAndAddTo(
Resource lhs, Resource rhs, double by) { Resource lhs, Resource rhs, double by) {
for (ResourceInformation entry : lhs.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
try { try {
ResourceInformation rhsValue = rhs.getResourceInformation(name); ResourceInformation rhsValue = rhs.getResourceInformation(i);
ResourceInformation lhsValue = entry; ResourceInformation lhsValue = lhs.getResourceInformation(i);
long convertedRhs = (long) (((rhsValue.getUnits() long convertedRhs = (long) (((rhsValue.getUnits()
.equals(lhsValue.getUnits())) .equals(lhsValue.getUnits()))
@ -273,7 +278,7 @@ public static Resource multiplyAndAddTo(
: UnitsConversionUtil.convert(rhsValue.getUnits(), : UnitsConversionUtil.convert(rhsValue.getUnits(),
lhsValue.getUnits(), rhsValue.getValue())) lhsValue.getUnits(), rhsValue.getValue()))
* by); * by);
lhs.setResourceValue(name, lhsValue.getValue() + convertedRhs); lhs.setResourceValue(i, lhsValue.getValue() + convertedRhs);
} catch (ResourceNotFoundException ye) { } catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage()); LOG.warn("Resource is missing:" + ye.getMessage());
continue; continue;
@ -294,10 +299,15 @@ public static Resource multiplyAndNormalizeDown(
public static Resource multiplyAndRoundDown(Resource lhs, double by) { public static Resource multiplyAndRoundDown(Resource lhs, double by) {
Resource out = clone(lhs); Resource out = clone(lhs);
for (ResourceInformation entry : out.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
ResourceInformation lhsValue = entry; try {
out.setResourceValue(name, (long) (lhsValue.getValue() * by)); ResourceInformation lhsValue = lhs.getResourceInformation(i);
out.setResourceValue(i, (long) (lhsValue.getValue() * by));
} catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage());
continue;
}
} }
return out; return out;
} }
@ -398,11 +408,11 @@ public static Resource max(
} }
public static boolean fitsIn(Resource smaller, Resource bigger) { public static boolean fitsIn(Resource smaller, Resource bigger) {
for (ResourceInformation entry : smaller.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
try { try {
ResourceInformation rhsValue = bigger.getResourceInformation(name); ResourceInformation rhsValue = bigger.getResourceInformation(i);
ResourceInformation lhsValue = entry; ResourceInformation lhsValue = smaller.getResourceInformation(i);
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
? rhsValue.getValue() ? rhsValue.getValue()
@ -413,7 +423,7 @@ public static boolean fitsIn(Resource smaller, Resource bigger) {
} }
} catch (ResourceNotFoundException ye) { } catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage()); LOG.warn("Resource is missing:" + ye.getMessage());
return false; continue;
} }
} }
return true; return true;
@ -426,19 +436,20 @@ public static boolean fitsIn(ResourceCalculator rc, Resource cluster,
public static Resource componentwiseMin(Resource lhs, Resource rhs) { public static Resource componentwiseMin(Resource lhs, Resource rhs) {
Resource ret = createResource(0); Resource ret = createResource(0);
for (ResourceInformation entry : lhs.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
try { try {
ResourceInformation rhsValue = rhs.getResourceInformation(name); ResourceInformation rhsValue = rhs.getResourceInformation(i);
ResourceInformation lhsValue = entry; ResourceInformation lhsValue = lhs.getResourceInformation(i);
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
? rhsValue.getValue() ? rhsValue.getValue()
: UnitsConversionUtil.convert(rhsValue.getUnits(), : UnitsConversionUtil.convert(rhsValue.getUnits(),
lhsValue.getUnits(), rhsValue.getValue()); lhsValue.getUnits(), rhsValue.getValue());
ResourceInformation outInfo = ResourceInformation outInfo = lhsValue.getValue() < convertedRhs
lhsValue.getValue() < convertedRhs ? lhsValue : rhsValue; ? lhsValue
ret.setResourceInformation(name, outInfo); : rhsValue;
ret.setResourceInformation(i, outInfo);
} catch (ResourceNotFoundException ye) { } catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage()); LOG.warn("Resource is missing:" + ye.getMessage());
continue; continue;
@ -449,19 +460,20 @@ public static Resource componentwiseMin(Resource lhs, Resource rhs) {
public static Resource componentwiseMax(Resource lhs, Resource rhs) { public static Resource componentwiseMax(Resource lhs, Resource rhs) {
Resource ret = createResource(0); Resource ret = createResource(0);
for (ResourceInformation entry : lhs.getResources()) { int maxLength = ResourceUtils.getResourceTypesArray().length;
String name = entry.getName(); for (int i = 0; i < maxLength; i++) {
try { try {
ResourceInformation rhsValue = rhs.getResourceInformation(name); ResourceInformation rhsValue = rhs.getResourceInformation(i);
ResourceInformation lhsValue = entry; ResourceInformation lhsValue = lhs.getResourceInformation(i);
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
? rhsValue.getValue() ? rhsValue.getValue()
: UnitsConversionUtil.convert(rhsValue.getUnits(), : UnitsConversionUtil.convert(rhsValue.getUnits(),
lhsValue.getUnits(), rhsValue.getValue()); lhsValue.getUnits(), rhsValue.getValue());
ResourceInformation outInfo = ResourceInformation outInfo = lhsValue.getValue() > convertedRhs
lhsValue.getValue() > convertedRhs ? lhsValue : rhsValue; ? lhsValue
ret.setResourceInformation(name, outInfo); : rhsValue;
ret.setResourceInformation(i, outInfo);
} catch (ResourceNotFoundException ye) { } catch (ResourceNotFoundException ye) {
LOG.warn("Resource is missing:" + ye.getMessage()); LOG.warn("Resource is missing:" + ye.getMessage());
continue; continue;