YARN-8524. Single parameter Resource / LightWeightResource constructor looks confusing. (Szilard Nemeth via wangda)

Change-Id: I4ae97548b5b8d76a6bcebb2d3d70bf8e0be3c125
This commit is contained in:
Wangda Tan 2018-07-16 10:58:00 -07:00
parent a2e49f41a8
commit 238ffff999
5 changed files with 65 additions and 29 deletions

View File

@ -76,17 +76,6 @@ public abstract class Resource implements Comparable<Resource> {
@Private @Private
public static final int VCORES_INDEX = 1; 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 @Public
@Stable @Stable

View File

@ -64,22 +64,6 @@ public class LightWeightResource extends Resource {
private ResourceInformation memoryResInfo; private ResourceInformation memoryResInfo;
private ResourceInformation vcoresResInfo; 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) { public LightWeightResource(long memory, int vcores) {
int numberOfKnownResourceTypes = ResourceUtils int numberOfKnownResourceTypes = ResourceUtils
.getNumberOfKnownResourceTypes(); .getNumberOfKnownResourceTypes();

View File

@ -21,9 +21,11 @@
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
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.api.records.impl.LightWeightResource;
import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException; import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException;
import org.apache.hadoop.yarn.util.UnitsConversionUtil; import org.apache.hadoop.yarn.util.UnitsConversionUtil;
@ -38,11 +40,30 @@ public class Resources {
private static final Log LOG = private static final Log LOG =
LogFactory.getLog(Resources.class); 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 * 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. * types. For example, a NONE resource which returns 0 for any resource type.
*/ */
@InterfaceAudience.Private @Private
@Unstable @Unstable
static class FixedValueResource extends Resource { static class FixedValueResource extends Resource {

View File

@ -263,4 +263,40 @@ public void testMultiplyAndAddTo() throws Exception {
multiplyAndAddTo(createResource(3, 1, 2), createResource(2, 2, 3), multiplyAndAddTo(createResource(3, 1, 2), createResource(2, 2, 3),
1.5)); 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());
}
} }

View File

@ -25,6 +25,7 @@
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.exceptions.ResourceNotFoundException;
import org.apache.hadoop.yarn.util.resource.ResourceUtils; 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 * A {@code ConfigurableResource} object represents an entity that is used to
@ -46,8 +47,13 @@ public class ConfigurableResource {
this.resource = null; 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) { ConfigurableResource(long value) {
this(Resource.newInstance(value)); this(Resources.createResourceWithSameValue(value));
} }
public ConfigurableResource(Resource resource) { public ConfigurableResource(Resource resource) {