YARN-6995. Improve use of ResourceNotFoundException in resource types code. (Daniel Templeton and Szilard Nemeth via Haibo Chen)

This commit is contained in:
Haibo Chen 2018-07-19 15:34:12 -07:00
parent b3b4d4ccb5
commit f354f47f99
4 changed files with 36 additions and 35 deletions

View File

@ -257,18 +257,15 @@ public abstract class Resource implements Comparable<Resource> {
*
* @param resource name of the resource
* @return the ResourceInformation object for the resource
* @throws ResourceNotFoundException if the resource can't be found
*/
@Public
@InterfaceStability.Unstable
public ResourceInformation getResourceInformation(String resource)
throws ResourceNotFoundException {
public ResourceInformation getResourceInformation(String resource) {
Integer index = ResourceUtils.getResourceTypeIndex().get(resource);
if (index != null) {
return resources[index];
}
throw new ResourceNotFoundException("Unknown resource '" + resource
+ "'. Known resources are " + Arrays.toString(resources));
throw new ResourceNotFoundException(this, resource);
}
/**
@ -299,12 +296,10 @@ public abstract class Resource implements Comparable<Resource> {
*
* @param resource name of the resource
* @return the value for the resource
* @throws ResourceNotFoundException if the resource can't be found
*/
@Public
@InterfaceStability.Unstable
public long getResourceValue(String resource)
throws ResourceNotFoundException {
public long getResourceValue(String resource) {
return getResourceInformation(resource).getValue();
}
@ -313,13 +308,11 @@ public abstract class Resource implements Comparable<Resource> {
*
* @param resource the resource for which the ResourceInformation is provided
* @param resourceInformation ResourceInformation object
* @throws ResourceNotFoundException if the resource is not found
*/
@Public
@InterfaceStability.Unstable
public void setResourceInformation(String resource,
ResourceInformation resourceInformation)
throws ResourceNotFoundException {
ResourceInformation resourceInformation) {
if (resource.equals(ResourceInformation.MEMORY_URI)) {
this.setMemorySize(resourceInformation.getValue());
return;
@ -348,8 +341,7 @@ public abstract class Resource implements Comparable<Resource> {
ResourceInformation resourceInformation)
throws ResourceNotFoundException {
if (index < 0 || index >= resources.length) {
throw new ResourceNotFoundException("Unknown resource at index '" + index
+ "'. Valid resources are " + Arrays.toString(resources));
throwExceptionWhenArrayOutOfBound(index);
}
ResourceInformation.copy(resourceInformation, resources[index]);
}
@ -360,12 +352,10 @@ public abstract class Resource implements Comparable<Resource> {
*
* @param resource the resource for which the value is provided.
* @param value the value to set
* @throws ResourceNotFoundException if the resource is not found
*/
@Public
@InterfaceStability.Unstable
public void setResourceValue(String resource, long value)
throws ResourceNotFoundException {
public void setResourceValue(String resource, long value) {
if (resource.equals(ResourceInformation.MEMORY_URI)) {
this.setMemorySize(value);
return;

View File

@ -18,8 +18,10 @@
package org.apache.hadoop.yarn.exceptions;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.yarn.api.records.Resource;
/**
* This exception is thrown when details of an unknown resource type
@ -28,18 +30,31 @@ import org.apache.hadoop.classification.InterfaceStability;
@InterfaceAudience.Public
@InterfaceStability.Unstable
public class ResourceNotFoundException extends YarnRuntimeException {
private static final long serialVersionUID = 10081982L;
private static final String MESSAGE = "The resource manager encountered a "
+ "problem that should not occur under normal circumstances. "
+ "Please report this error to the Hadoop community by opening a "
+ "JIRA ticket at http://issues.apache.org/jira and including the "
+ "following information:%n* Resource type requested: %s%n* Resource "
+ "object: %s%n* The stack trace for this exception: %s%n"
+ "After encountering this error, the resource manager is "
+ "in an inconsistent state. It is safe for the resource manager "
+ "to be restarted as the error encountered should be transitive. "
+ "If high availability is enabled, failing over to "
+ "a standby resource manager is also safe.";
public ResourceNotFoundException(Resource resource, String type) {
this(String.format(MESSAGE, type, resource,
ExceptionUtils.getStackTrace(new Exception())));
}
public ResourceNotFoundException(Resource resource, String type,
Throwable cause) {
super(String.format(MESSAGE, type, resource,
ExceptionUtils.getStackTrace(cause)), cause);
}
public ResourceNotFoundException(String message) {
super(message);
}
public ResourceNotFoundException(Throwable cause) {
super(cause);
}
public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -193,8 +193,7 @@ public class ResourcePBImpl extends Resource {
}
@Override
public void setResourceValue(String resource, long value)
throws ResourceNotFoundException {
public void setResourceValue(String resource, long value) {
maybeInitBuilder();
if (resource == null) {
throw new IllegalArgumentException("resource type object cannot be null");
@ -203,14 +202,13 @@ public class ResourcePBImpl extends Resource {
}
@Override
public ResourceInformation getResourceInformation(String resource)
throws ResourceNotFoundException {
public ResourceInformation getResourceInformation(String resource) {
initResources();
return super.getResourceInformation(resource);
}
@Override
public long getResourceValue(String resource)
throws ResourceNotFoundException {
public long getResourceValue(String resource) {
return super.getResourceValue(resource);
}

View File

@ -128,14 +128,12 @@ public class Resources {
@Override
public void setResourceInformation(String resource,
ResourceInformation resourceInformation)
throws ResourceNotFoundException {
ResourceInformation resourceInformation) {
throw new RuntimeException(name + " cannot be modified!");
}
@Override
public void setResourceValue(String resource, long value)
throws ResourceNotFoundException {
public void setResourceValue(String resource, long value) {
throw new RuntimeException(name + " cannot be modified!");
}