YARN-6892. [YARN-3926] Improve API implementation in Resources and DominantResourceCalculator class. Contributed by Sunil G.
This commit is contained in:
parent
d5e9939ebb
commit
2b51b262ab
|
@ -164,7 +164,6 @@ public abstract class Resource implements Comparable<Resource> {
|
|||
"This method is implemented by ResourcePBImpl");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get <em>number of virtual cpu cores</em> of the resource.
|
||||
*
|
||||
|
@ -224,6 +223,27 @@ public abstract class Resource implements Comparable<Resource> {
|
|||
+ "'. 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
|
||||
* returned.
|
||||
|
@ -263,6 +283,29 @@ public abstract class Resource implements Comparable<Resource> {
|
|||
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
|
||||
* the value is assumed to be the one in the ResourceInformation object.
|
||||
|
@ -288,6 +331,29 @@ public abstract class Resource implements Comparable<Resource> {
|
|||
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
|
||||
public int hashCode() {
|
||||
final int prime = 263167;
|
||||
|
|
|
@ -17,13 +17,10 @@
|
|||
*/
|
||||
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.InterfaceStability.Unstable;
|
||||
import org.apache.hadoop.yarn.api.records.Resource;
|
||||
import org.apache.hadoop.yarn.api.records.ResourceInformation;
|
||||
import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException;
|
||||
import org.apache.hadoop.yarn.util.UnitsConversionUtil;
|
||||
|
||||
|
||||
|
@ -51,14 +48,8 @@ import org.apache.hadoop.yarn.util.UnitsConversionUtil;
|
|||
@Private
|
||||
@Unstable
|
||||
public class DominantResourceCalculator extends ResourceCalculator {
|
||||
private static final Log LOG =
|
||||
LogFactory.getLog(DominantResourceCalculator.class);
|
||||
|
||||
|
||||
private String[] resourceNames;
|
||||
|
||||
public DominantResourceCalculator() {
|
||||
resourceNames = ResourceUtils.getResourceNamesArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,22 +66,18 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
boolean rhsGreater = false;
|
||||
int ret = 0;
|
||||
|
||||
for (String rName : resourceNames) {
|
||||
try {
|
||||
ResourceInformation lhsResourceInformation =
|
||||
lhs.getResourceInformation(rName);
|
||||
ResourceInformation rhsResourceInformation =
|
||||
rhs.getResourceInformation(rName);
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation lhsResourceInformation = lhs
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation rhsResourceInformation = rhs
|
||||
.getResourceInformation(i);
|
||||
int diff = lhsResourceInformation.compareTo(rhsResourceInformation);
|
||||
if (diff >= 1) {
|
||||
lhsGreater = true;
|
||||
} else if (diff <= -1) {
|
||||
rhsGreater = true;
|
||||
}
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
throw new IllegalArgumentException(
|
||||
"Error getting resource information for " + rName, ye);
|
||||
}
|
||||
}
|
||||
if (lhsGreater && rhsGreater) {
|
||||
ret = 0;
|
||||
|
@ -147,50 +134,40 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
|
||||
float min = Float.MAX_VALUE;
|
||||
float max = 0.0f;
|
||||
for (String rName : resourceNames) {
|
||||
try {
|
||||
ResourceInformation clusterResourceResourceInformation =
|
||||
clusterResource.getResourceInformation(rName);
|
||||
ResourceInformation resourceInformation =
|
||||
resource.getResourceInformation(rName);
|
||||
long resourceValue = UnitsConversionUtil
|
||||
.convert(resourceInformation.getUnits(),
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation clusterResourceResourceInformation = clusterResource
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation resourceInformation = resource
|
||||
.getResourceInformation(i);
|
||||
long resourceValue = UnitsConversionUtil.convert(
|
||||
resourceInformation.getUnits(),
|
||||
clusterResourceResourceInformation.getUnits(),
|
||||
resourceInformation.getValue());
|
||||
float tmp =
|
||||
(float) resourceValue / (float) clusterResourceResourceInformation
|
||||
.getValue();
|
||||
float tmp = (float) resourceValue
|
||||
/ (float) clusterResourceResourceInformation.getValue();
|
||||
min = min < tmp ? min : tmp;
|
||||
max = max > tmp ? max : tmp;
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
throw new IllegalArgumentException(
|
||||
"Error getting resource information for " + resource, ye);
|
||||
}
|
||||
}
|
||||
return (dominant) ? max : min;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long computeAvailableContainers(Resource available, Resource required) {
|
||||
public long computeAvailableContainers(Resource available,
|
||||
Resource required) {
|
||||
long min = Long.MAX_VALUE;
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation availableResource =
|
||||
available.getResourceInformation(resource);
|
||||
ResourceInformation requiredResource =
|
||||
required.getResourceInformation(resource);
|
||||
long requiredResourceValue = UnitsConversionUtil
|
||||
.convert(requiredResource.getUnits(), availableResource.getUnits(),
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation availableResource = available
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation requiredResource = required.getResourceInformation(i);
|
||||
long requiredResourceValue = UnitsConversionUtil.convert(
|
||||
requiredResource.getUnits(), availableResource.getUnits(),
|
||||
requiredResource.getValue());
|
||||
if (requiredResourceValue != 0) {
|
||||
long tmp = availableResource.getValue() / requiredResourceValue;
|
||||
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;
|
||||
}
|
||||
|
@ -216,23 +193,16 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
@Override
|
||||
public float ratio(Resource a, Resource b) {
|
||||
float ratio = 0.0f;
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation aResourceInformation =
|
||||
a.getResourceInformation(resource);
|
||||
ResourceInformation bResourceInformation =
|
||||
b.getResourceInformation(resource);
|
||||
long bResourceValue = UnitsConversionUtil
|
||||
.convert(bResourceInformation.getUnits(),
|
||||
aResourceInformation.getUnits(),
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation aResourceInformation = a.getResourceInformation(i);
|
||||
ResourceInformation bResourceInformation = b.getResourceInformation(i);
|
||||
long bResourceValue = UnitsConversionUtil.convert(
|
||||
bResourceInformation.getUnits(), aResourceInformation.getUnits(),
|
||||
bResourceInformation.getValue());
|
||||
float tmp =
|
||||
(float) aResourceInformation.getValue() / (float) bResourceValue;
|
||||
float tmp = (float) aResourceInformation.getValue()
|
||||
/ (float) bResourceValue;
|
||||
ratio = ratio > tmp ? ratio : tmp;
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
throw new IllegalArgumentException(
|
||||
"Error getting resource information for " + resource, ye);
|
||||
}
|
||||
}
|
||||
return ratio;
|
||||
}
|
||||
|
@ -244,16 +214,11 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
|
||||
public Resource divideAndCeil(Resource numerator, long denominator) {
|
||||
Resource ret = Resource.newInstance(numerator);
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation resourceInformation =
|
||||
ret.getResourceInformation(resource);
|
||||
resourceInformation.setValue(
|
||||
divideAndCeil(resourceInformation.getValue(), denominator));
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
throw new IllegalArgumentException(
|
||||
"Error getting resource information for " + resource, ye);
|
||||
}
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation resourceInformation = ret.getResourceInformation(i);
|
||||
resourceInformation
|
||||
.setValue(divideAndCeil(resourceInformation.getValue(), denominator));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -270,29 +235,28 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
public Resource normalize(Resource r, Resource minimumResource,
|
||||
Resource maximumResource, Resource stepFactor) {
|
||||
Resource ret = Resource.newInstance(r);
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation rResourceInformation =
|
||||
r.getResourceInformation(resource);
|
||||
ResourceInformation minimumResourceInformation =
|
||||
minimumResource.getResourceInformation(resource);
|
||||
ResourceInformation maximumResourceInformation =
|
||||
maximumResource.getResourceInformation(resource);
|
||||
ResourceInformation stepFactorResourceInformation =
|
||||
stepFactor.getResourceInformation(resource);
|
||||
ResourceInformation tmp = ret.getResourceInformation(resource);
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation rResourceInformation = r.getResourceInformation(i);
|
||||
ResourceInformation minimumResourceInformation = minimumResource
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation maximumResourceInformation = maximumResource
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation stepFactorResourceInformation = stepFactor
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation tmp = ret.getResourceInformation(i);
|
||||
|
||||
long rValue = rResourceInformation.getValue();
|
||||
long minimumValue = UnitsConversionUtil
|
||||
.convert(minimumResourceInformation.getUnits(),
|
||||
long minimumValue = UnitsConversionUtil.convert(
|
||||
minimumResourceInformation.getUnits(),
|
||||
rResourceInformation.getUnits(),
|
||||
minimumResourceInformation.getValue());
|
||||
long maximumValue = UnitsConversionUtil
|
||||
.convert(maximumResourceInformation.getUnits(),
|
||||
long maximumValue = UnitsConversionUtil.convert(
|
||||
maximumResourceInformation.getUnits(),
|
||||
rResourceInformation.getUnits(),
|
||||
maximumResourceInformation.getValue());
|
||||
long stepFactorValue = UnitsConversionUtil
|
||||
.convert(stepFactorResourceInformation.getUnits(),
|
||||
long stepFactorValue = UnitsConversionUtil.convert(
|
||||
stepFactorResourceInformation.getUnits(),
|
||||
rResourceInformation.getUnits(),
|
||||
stepFactorResourceInformation.getValue());
|
||||
long value = Math.max(rValue, minimumValue);
|
||||
|
@ -300,11 +264,7 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
value = roundUp(value, stepFactorValue);
|
||||
}
|
||||
tmp.setValue(Math.min(value, maximumValue));
|
||||
ret.setResourceInformation(resource, tmp);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
throw new IllegalArgumentException(
|
||||
"Error getting resource information for " + resource, ye);
|
||||
}
|
||||
ret.setResourceInformation(i, tmp);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -321,30 +281,26 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
|
||||
private Resource rounding(Resource r, Resource stepFactor, boolean roundUp) {
|
||||
Resource ret = Resource.newInstance(r);
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation rResourceInformation =
|
||||
r.getResourceInformation(resource);
|
||||
ResourceInformation stepFactorResourceInformation =
|
||||
stepFactor.getResourceInformation(resource);
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation rResourceInformation = r.getResourceInformation(i);
|
||||
ResourceInformation stepFactorResourceInformation = stepFactor
|
||||
.getResourceInformation(i);
|
||||
|
||||
long rValue = rResourceInformation.getValue();
|
||||
long stepFactorValue = UnitsConversionUtil
|
||||
.convert(stepFactorResourceInformation.getUnits(),
|
||||
long stepFactorValue = UnitsConversionUtil.convert(
|
||||
stepFactorResourceInformation.getUnits(),
|
||||
rResourceInformation.getUnits(),
|
||||
stepFactorResourceInformation.getValue());
|
||||
long value = rValue;
|
||||
if (stepFactorValue != 0) {
|
||||
value = roundUp ? 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);
|
||||
value = roundUp
|
||||
? roundUp(rValue, stepFactorValue)
|
||||
: roundDown(rValue, stepFactorValue);
|
||||
}
|
||||
ResourceInformation.copy(rResourceInformation,
|
||||
ret.getResourceInformation(i));
|
||||
ret.getResourceInformation(i).setValue(value);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -364,13 +320,12 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
private Resource multiplyAndNormalize(Resource r, double by,
|
||||
Resource stepFactor, boolean roundUp) {
|
||||
Resource ret = Resource.newInstance(r);
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation rResourceInformation = r
|
||||
.getResourceInformation(resource);
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation rResourceInformation = r.getResourceInformation(i);
|
||||
ResourceInformation stepFactorResourceInformation = stepFactor
|
||||
.getResourceInformation(resource);
|
||||
ResourceInformation tmp = ret.getResourceInformation(resource);
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation tmp = ret.getResourceInformation(i);
|
||||
|
||||
long rValue = rResourceInformation.getValue();
|
||||
long stepFactorValue = UnitsConversionUtil.convert(
|
||||
|
@ -383,35 +338,25 @@ public class DominantResourceCalculator extends ResourceCalculator {
|
|||
? roundUp((long) Math.ceil(rValue * by), stepFactorValue)
|
||||
: roundDown((long) (rValue * by), stepFactorValue);
|
||||
} else {
|
||||
value = roundUp
|
||||
? (long) Math.ceil(rValue * by)
|
||||
: (long) (rValue * by);
|
||||
value = roundUp ? (long) Math.ceil(rValue * by) : (long) (rValue * by);
|
||||
}
|
||||
tmp.setValue(value);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
throw new IllegalArgumentException(
|
||||
"Error getting resource information for " + resource, ye);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fitsIn(Resource cluster, Resource smaller, Resource bigger) {
|
||||
for (String resource : resourceNames) {
|
||||
try {
|
||||
ResourceInformation sResourceInformation =
|
||||
smaller.getResourceInformation(resource);
|
||||
ResourceInformation bResourceInformation =
|
||||
bigger.getResourceInformation(resource);
|
||||
long sResourceValue = UnitsConversionUtil
|
||||
.convert(sResourceInformation.getUnits(),
|
||||
bResourceInformation.getUnits(),
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
ResourceInformation sResourceInformation = smaller
|
||||
.getResourceInformation(i);
|
||||
ResourceInformation bResourceInformation = bigger
|
||||
.getResourceInformation(i);
|
||||
long sResourceValue = UnitsConversionUtil.convert(
|
||||
sResourceInformation.getUnits(), bResourceInformation.getUnits(),
|
||||
sResourceInformation.getValue());
|
||||
if(sResourceValue > bResourceInformation.getValue()) {
|
||||
return false;
|
||||
}
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
if (sResourceValue > bResourceInformation.getValue()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -173,17 +173,17 @@ public class Resources {
|
|||
}
|
||||
|
||||
public static Resource addTo(Resource lhs, Resource rhs) {
|
||||
for (ResourceInformation entry : lhs.getResources()) {
|
||||
String name = entry.getName();
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(name);
|
||||
ResourceInformation lhsValue = entry;
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(i);
|
||||
ResourceInformation lhsValue = lhs.getResourceInformation(i);
|
||||
|
||||
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
|
||||
? rhsValue.getValue()
|
||||
: UnitsConversionUtil.convert(rhsValue.getUnits(),
|
||||
lhsValue.getUnits(), rhsValue.getValue());
|
||||
lhs.setResourceValue(name, lhsValue.getValue() + convertedRhs);
|
||||
lhs.setResourceValue(i, lhsValue.getValue() + convertedRhs);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
LOG.warn("Resource is missing:" + ye.getMessage());
|
||||
continue;
|
||||
|
@ -197,17 +197,17 @@ public class Resources {
|
|||
}
|
||||
|
||||
public static Resource subtractFrom(Resource lhs, Resource rhs) {
|
||||
for (ResourceInformation entry : lhs.getResources()) {
|
||||
String name = entry.getName();
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(name);
|
||||
ResourceInformation lhsValue = entry;
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(i);
|
||||
ResourceInformation lhsValue = lhs.getResourceInformation(i);
|
||||
|
||||
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
|
||||
? rhsValue.getValue()
|
||||
: UnitsConversionUtil.convert(rhsValue.getUnits(),
|
||||
lhsValue.getUnits(), rhsValue.getValue());
|
||||
lhs.setResourceValue(name, lhsValue.getValue() - convertedRhs);
|
||||
lhs.setResourceValue(i, lhsValue.getValue() - convertedRhs);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
LOG.warn("Resource is missing:" + ye.getMessage());
|
||||
continue;
|
||||
|
@ -243,10 +243,15 @@ public class Resources {
|
|||
}
|
||||
|
||||
public static Resource multiplyTo(Resource lhs, double by) {
|
||||
for (ResourceInformation entry : lhs.getResources()) {
|
||||
String name = entry.getName();
|
||||
ResourceInformation lhsValue = entry;
|
||||
lhs.setResourceValue(name, (long) (lhsValue.getValue() * by));
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
@ -261,11 +266,11 @@ public class Resources {
|
|||
*/
|
||||
public static Resource multiplyAndAddTo(
|
||||
Resource lhs, Resource rhs, double by) {
|
||||
for (ResourceInformation entry : lhs.getResources()) {
|
||||
String name = entry.getName();
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(name);
|
||||
ResourceInformation lhsValue = entry;
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(i);
|
||||
ResourceInformation lhsValue = lhs.getResourceInformation(i);
|
||||
|
||||
long convertedRhs = (long) (((rhsValue.getUnits()
|
||||
.equals(lhsValue.getUnits()))
|
||||
|
@ -273,7 +278,7 @@ public class Resources {
|
|||
: UnitsConversionUtil.convert(rhsValue.getUnits(),
|
||||
lhsValue.getUnits(), rhsValue.getValue()))
|
||||
* by);
|
||||
lhs.setResourceValue(name, lhsValue.getValue() + convertedRhs);
|
||||
lhs.setResourceValue(i, lhsValue.getValue() + convertedRhs);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
LOG.warn("Resource is missing:" + ye.getMessage());
|
||||
continue;
|
||||
|
@ -294,10 +299,15 @@ public class Resources {
|
|||
|
||||
public static Resource multiplyAndRoundDown(Resource lhs, double by) {
|
||||
Resource out = clone(lhs);
|
||||
for (ResourceInformation entry : out.getResources()) {
|
||||
String name = entry.getName();
|
||||
ResourceInformation lhsValue = entry;
|
||||
out.setResourceValue(name, (long) (lhsValue.getValue() * by));
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
@ -398,22 +408,22 @@ public class Resources {
|
|||
}
|
||||
|
||||
public static boolean fitsIn(Resource smaller, Resource bigger) {
|
||||
for (ResourceInformation entry : smaller.getResources()) {
|
||||
String name = entry.getName();
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
ResourceInformation rhsValue = bigger.getResourceInformation(name);
|
||||
ResourceInformation lhsValue = entry;
|
||||
ResourceInformation rhsValue = bigger.getResourceInformation(i);
|
||||
ResourceInformation lhsValue = smaller.getResourceInformation(i);
|
||||
|
||||
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
|
||||
? rhsValue.getValue()
|
||||
: UnitsConversionUtil.convert(rhsValue.getUnits(),
|
||||
lhsValue.getUnits(), rhsValue.getValue());
|
||||
if(lhsValue.getValue() > convertedRhs) {
|
||||
if (lhsValue.getValue() > convertedRhs) {
|
||||
return false;
|
||||
}
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
LOG.warn("Resource is missing:" + ye.getMessage());
|
||||
return false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
@ -426,19 +436,20 @@ public class Resources {
|
|||
|
||||
public static Resource componentwiseMin(Resource lhs, Resource rhs) {
|
||||
Resource ret = createResource(0);
|
||||
for (ResourceInformation entry : lhs.getResources()) {
|
||||
String name = entry.getName();
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(name);
|
||||
ResourceInformation lhsValue = entry;
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(i);
|
||||
ResourceInformation lhsValue = lhs.getResourceInformation(i);
|
||||
|
||||
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
|
||||
? rhsValue.getValue()
|
||||
: UnitsConversionUtil.convert(rhsValue.getUnits(),
|
||||
lhsValue.getUnits(), rhsValue.getValue());
|
||||
ResourceInformation outInfo =
|
||||
lhsValue.getValue() < convertedRhs ? lhsValue : rhsValue;
|
||||
ret.setResourceInformation(name, outInfo);
|
||||
ResourceInformation outInfo = lhsValue.getValue() < convertedRhs
|
||||
? lhsValue
|
||||
: rhsValue;
|
||||
ret.setResourceInformation(i, outInfo);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
LOG.warn("Resource is missing:" + ye.getMessage());
|
||||
continue;
|
||||
|
@ -449,19 +460,20 @@ public class Resources {
|
|||
|
||||
public static Resource componentwiseMax(Resource lhs, Resource rhs) {
|
||||
Resource ret = createResource(0);
|
||||
for (ResourceInformation entry : lhs.getResources()) {
|
||||
String name = entry.getName();
|
||||
int maxLength = ResourceUtils.getResourceTypesArray().length;
|
||||
for (int i = 0; i < maxLength; i++) {
|
||||
try {
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(name);
|
||||
ResourceInformation lhsValue = entry;
|
||||
ResourceInformation rhsValue = rhs.getResourceInformation(i);
|
||||
ResourceInformation lhsValue = lhs.getResourceInformation(i);
|
||||
|
||||
long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits()))
|
||||
? rhsValue.getValue()
|
||||
: UnitsConversionUtil.convert(rhsValue.getUnits(),
|
||||
lhsValue.getUnits(), rhsValue.getValue());
|
||||
ResourceInformation outInfo =
|
||||
lhsValue.getValue() > convertedRhs ? lhsValue : rhsValue;
|
||||
ret.setResourceInformation(name, outInfo);
|
||||
ResourceInformation outInfo = lhsValue.getValue() > convertedRhs
|
||||
? lhsValue
|
||||
: rhsValue;
|
||||
ret.setResourceInformation(i, outInfo);
|
||||
} catch (ResourceNotFoundException ye) {
|
||||
LOG.warn("Resource is missing:" + ye.getMessage());
|
||||
continue;
|
||||
|
|
Loading…
Reference in New Issue