From common-commits-return-92434-archive-asf-public=cust-asf.ponee.io@hadoop.apache.org Tue Jan 8 21:55:42 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 87495180652 for ; Tue, 8 Jan 2019 21:55:41 +0100 (CET) Received: (qmail 83380 invoked by uid 500); 8 Jan 2019 20:55:35 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 83358 invoked by uid 99); 8 Jan 2019 20:55:34 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Jan 2019 20:55:34 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 2F5CE85BDB; Tue, 8 Jan 2019 20:55:34 +0000 (UTC) Date: Tue, 08 Jan 2019 20:55:35 +0000 To: "common-commits@hadoop.apache.org" Subject: [hadoop] 03/03: YARN-6892. [YARN-3926] Improve API implementation in Resources and DominantResourceCalculator class. Contributed by Sunil G. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: jhung@apache.org In-Reply-To: <154698093287.4987.203533256302287472@gitbox.apache.org> References: <154698093287.4987.203533256302287472@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: hadoop X-Git-Refname: refs/heads/YARN-8200 X-Git-Reftype: branch X-Git-Rev: 7082f22728f5097aa3b6f7486a178642045791d2 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20190108205534.2F5CE85BDB@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. jhung pushed a commit to branch YARN-8200 in repository https://gitbox.apache.org/repos/asf/hadoop.git commit 7082f22728f5097aa3b6f7486a178642045791d2 Author: Sunil G AuthorDate: Wed Aug 16 15:25:36 2017 +0530 YARN-6892. [YARN-3926] Improve API implementation in Resources and DominantResourceCalculator class. Contributed by Sunil G. (cherry picked from commit 2b51b262aba0191b80dc93799574c0b959cb4f4e) --- .../apache/hadoop/yarn/api/records/Resource.java | 70 ++++- .../util/resource/DominantResourceCalculator.java | 317 +++++++++------------ .../hadoop/yarn/util/resource/Resources.java | 98 ++++--- 3 files changed, 254 insertions(+), 231 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java index 54f0b18..6c0bed5 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java @@ -164,7 +164,6 @@ public abstract class Resource implements Comparable { "This method is implemented by ResourcePBImpl"); } - /** * Get number of virtual cpu cores of the resource. * @@ -179,7 +178,7 @@ public abstract class Resource implements Comparable { @Public @Evolving public abstract int getVirtualCores(); - + /** * Set number of virtual cpu cores of the resource. * @@ -225,6 +224,27 @@ public abstract class Resource implements Comparable { } /** + * 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. * @@ -264,6 +284,29 @@ public abstract class Resource implements Comparable { } /** + * 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 { 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; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java index ea5c8a8..5992ba3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java @@ -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,21 +66,17 @@ 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 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); + 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; } } if (lhsGreater && rhsGreater) { @@ -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(), - clusterResourceResourceInformation.getUnits(), - resourceInformation.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); - } + 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(); + min = min < tmp ? min : tmp; + max = max > tmp ? max : tmp; } 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(), - 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); + 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; } - } 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(), - bResourceInformation.getValue()); - 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); - } + 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; + ratio = ratio > tmp ? ratio : tmp; } 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,41 +235,36 @@ 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); - - long rValue = rResourceInformation.getValue(); - long minimumValue = UnitsConversionUtil - .convert(minimumResourceInformation.getUnits(), - rResourceInformation.getUnits(), - minimumResourceInformation.getValue()); - long maximumValue = UnitsConversionUtil - .convert(maximumResourceInformation.getUnits(), - rResourceInformation.getUnits(), - maximumResourceInformation.getValue()); - long stepFactorValue = UnitsConversionUtil - .convert(stepFactorResourceInformation.getUnits(), - rResourceInformation.getUnits(), - stepFactorResourceInformation.getValue()); - long value = Math.max(rValue, minimumValue); - if (stepFactorValue != 0) { - 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); + 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(), + rResourceInformation.getUnits(), + minimumResourceInformation.getValue()); + long maximumValue = UnitsConversionUtil.convert( + maximumResourceInformation.getUnits(), + rResourceInformation.getUnits(), + maximumResourceInformation.getValue()); + long stepFactorValue = UnitsConversionUtil.convert( + stepFactorResourceInformation.getUnits(), + rResourceInformation.getUnits(), + stepFactorResourceInformation.getValue()); + long value = Math.max(rValue, minimumValue); + if (stepFactorValue != 0) { + value = roundUp(value, stepFactorValue); } + tmp.setValue(Math.min(value, maximumValue)); + 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); - - long rValue = rResourceInformation.getValue(); - 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); + 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(), + rResourceInformation.getUnits(), + stepFactorResourceInformation.getValue()); + long value = rValue; + if (stepFactorValue != 0) { + value = roundUp + ? roundUp(rValue, stepFactorValue) + : roundDown(rValue, stepFactorValue); } + ResourceInformation.copy(rResourceInformation, + ret.getResourceInformation(i)); + ret.getResourceInformation(i).setValue(value); } return ret; } @@ -364,54 +320,43 @@ 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); - ResourceInformation stepFactorResourceInformation = stepFactor - .getResourceInformation(resource); - ResourceInformation tmp = ret.getResourceInformation(resource); - - long rValue = rResourceInformation.getValue(); - long stepFactorValue = UnitsConversionUtil.convert( - stepFactorResourceInformation.getUnits(), - rResourceInformation.getUnits(), - stepFactorResourceInformation.getValue()); - long value; - if (stepFactorValue != 0) { - value = roundUp - ? roundUp((long) Math.ceil(rValue * by), stepFactorValue) - : roundDown((long) (rValue * by), stepFactorValue); - } else { - 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); + int maxLength = ResourceUtils.getResourceTypesArray().length; + for (int i = 0; i < maxLength; i++) { + ResourceInformation rResourceInformation = r.getResourceInformation(i); + ResourceInformation stepFactorResourceInformation = stepFactor + .getResourceInformation(i); + ResourceInformation tmp = ret.getResourceInformation(i); + + long rValue = rResourceInformation.getValue(); + long stepFactorValue = UnitsConversionUtil.convert( + stepFactorResourceInformation.getUnits(), + rResourceInformation.getUnits(), + stepFactorResourceInformation.getValue()); + long value; + if (stepFactorValue != 0) { + value = roundUp + ? roundUp((long) Math.ceil(rValue * by), stepFactorValue) + : roundDown((long) (rValue * by), stepFactorValue); + } else { + value = roundUp ? (long) Math.ceil(rValue * by) : (long) (rValue * by); } + tmp.setValue(value); } 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(), - sResourceInformation.getValue()); - if(sResourceValue > bResourceInformation.getValue()) { - return false; - } - } catch (ResourceNotFoundException ye) { + 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; } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java index 3cf78ed..702c1c8 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java @@ -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; --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org