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 173d4c9e777..3cac1d1a3c4 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 @@ -76,17 +76,6 @@ public abstract class Resource implements Comparable { @Private public static final int VCORES_INDEX = 1; - /** - * Return a new {@link Resource} instance with all resource values - * initialized to {@code value}. - * @param value the value to use for all resources - * @return a new {@link Resource} instance - */ - @Private - @Unstable - public static Resource newInstance(long value) { - return new LightWeightResource(value); - } @Public @Stable diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/LightWeightResource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/LightWeightResource.java index 77f77f312ce..02afe50cc23 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/LightWeightResource.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/LightWeightResource.java @@ -64,22 +64,6 @@ public class LightWeightResource extends Resource { private ResourceInformation memoryResInfo; private ResourceInformation vcoresResInfo; - /** - * Create a new {@link LightWeightResource} instance with all resource values - * initialized to {@code value}. - * @param value the value to use for all resources - */ - public LightWeightResource(long value) { - ResourceInformation[] types = ResourceUtils.getResourceTypesArray(); - initResourceInformations(value, value, types.length); - - for (int i = 2; i < types.length; i++) { - resources[i] = new ResourceInformation(); - ResourceInformation.copy(types[i], resources[i]); - resources[i].setValue(value); - } - } - public LightWeightResource(long memory, int vcores) { int numberOfKnownResourceTypes = ResourceUtils .getNumberOfKnownResourceTypes(); 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 7826f51cd4d..ace8b5db0f7 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 @@ -21,9 +21,11 @@ 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; +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.api.records.impl.LightWeightResource; import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException; import org.apache.hadoop.yarn.util.UnitsConversionUtil; @@ -38,11 +40,30 @@ public class Resources { private static final Log LOG = LogFactory.getLog(Resources.class); + /** + * Return a new {@link Resource} instance with all resource values + * initialized to {@code value}. + * @param value the value to use for all resources + * @return a new {@link Resource} instance + */ + @Private + @Unstable + public static Resource createResourceWithSameValue(long value) { + LightWeightResource res = new LightWeightResource(value, + Long.valueOf(value).intValue()); + int numberOfResources = ResourceUtils.getNumberOfKnownResourceTypes(); + for (int i = 2; i < numberOfResources; i++) { + res.setResourceValue(i, value); + } + + return res; + } + /** * Helper class to create a resource with a fixed value for all resource * types. For example, a NONE resource which returns 0 for any resource type. */ - @InterfaceAudience.Private + @Private @Unstable static class FixedValueResource extends Resource { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResources.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResources.java index a8404fbaee7..07b24eb2618 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResources.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResources.java @@ -263,4 +263,40 @@ public class TestResources { multiplyAndAddTo(createResource(3, 1, 2), createResource(2, 2, 3), 1.5)); } + + @Test + public void testCreateResourceWithSameLongValue() throws Exception { + unsetExtraResourceType(); + setupExtraResourceType(); + + Resource res = Resources.createResourceWithSameValue(11L); + assertEquals(11L, res.getMemorySize()); + assertEquals(11, res.getVirtualCores()); + assertEquals(11L, res.getResourceInformation(EXTRA_RESOURCE_TYPE).getValue()); + } + + @Test + public void testCreateResourceWithSameIntValue() throws Exception { + unsetExtraResourceType(); + setupExtraResourceType(); + + Resource res = Resources.createResourceWithSameValue(11); + assertEquals(11, res.getMemorySize()); + assertEquals(11, res.getVirtualCores()); + assertEquals(11, res.getResourceInformation(EXTRA_RESOURCE_TYPE).getValue()); + } + + @Test + public void testCreateSimpleResourceWithSameLongValue() { + Resource res = Resources.createResourceWithSameValue(11L); + assertEquals(11L, res.getMemorySize()); + assertEquals(11, res.getVirtualCores()); + } + + @Test + public void testCreateSimpleResourceWithSameIntValue() { + Resource res = Resources.createResourceWithSameValue(11); + assertEquals(11, res.getMemorySize()); + assertEquals(11, res.getVirtualCores()); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/ConfigurableResource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/ConfigurableResource.java index 0c3b0ddc6b6..f772c4d1c0b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/ConfigurableResource.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/ConfigurableResource.java @@ -25,6 +25,7 @@ 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.resource.ResourceUtils; +import org.apache.hadoop.yarn.util.resource.Resources; /** * A {@code ConfigurableResource} object represents an entity that is used to @@ -46,8 +47,13 @@ public class ConfigurableResource { this.resource = null; } + /** + * Creates a {@link ConfigurableResource} instance with all resource values + * initialized to {@code value}. + * @param value the value to use for all resources + */ ConfigurableResource(long value) { - this(Resource.newInstance(value)); + this(Resources.createResourceWithSameValue(value)); } public ConfigurableResource(Resource resource) {