From 727cbcb41dd1c6e85f6a451b4fc5057b6c87628b Mon Sep 17 00:00:00 2001 From: Wangda Tan Date: Mon, 6 Nov 2017 15:26:49 -0800 Subject: [PATCH] YARN-7410. Cleanup FixedValueResource to avoid dependency to ResourceUtils. (wangda) (cherry picked from commit df27824b80b8eca03d02837efe2da31f089e67ec) --- .../hadoop/yarn/api/records/Resource.java | 2 +- .../yarn/util/resource/ResourceUtils.java | 1 + .../hadoop/yarn/util/resource/Resources.java | 59 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) 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 f7c699f1a01..ce3ea13c25a 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 @@ -374,7 +374,7 @@ public abstract class Resource implements Comparable { } } - private void throwExceptionWhenArrayOutOfBound(int index) { + protected void throwExceptionWhenArrayOutOfBound(int index) { String exceptionMsg = String.format( "Trying to access ResourceInformation for given index=%d. " + "Acceptable index range is [0,%d), please check double check " diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java index a359ad197b7..4c65cae10ac 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java @@ -290,6 +290,7 @@ public class ResourceUtils { updateKnownResources(); updateResourceTypeIndex(); initializedResources = true; + numKnownResourceTypes = resourceTypes.size(); } private static void updateKnownResources() { 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 325bce465b0..11e53ad9e20 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 @@ -125,6 +125,65 @@ public class Resources { throw new RuntimeException(name + " cannot be modified!"); } + /* + * FixedValueResource cannot be updated when any resource types refresh + * by using approach introduced by YARN-7307 and do operations like + * Resources.compare(resource_x, Resources.none()) will throw exceptions. + * + * That's why we do reinitialize resource maps for following methods. + */ + + @Override + public ResourceInformation getResourceInformation(int index) + throws ResourceNotFoundException { + ResourceInformation ri = null; + try { + ri = super.getResourceInformation(index); + } catch (ResourceNotFoundException e) { + // Retry once to reinitialize resource information. + initResourceMap(); + try { + return super.getResourceInformation(index); + } catch (ResourceNotFoundException ee) { + throwExceptionWhenArrayOutOfBound(index); + } + } + return ri; + } + + @Override + public ResourceInformation getResourceInformation(String resource) + throws ResourceNotFoundException { + ResourceInformation ri; + try { + ri = super.getResourceInformation(resource); + } catch (ResourceNotFoundException e) { + // Retry once to reinitialize resource information. + initResourceMap(); + try { + return super.getResourceInformation(resource); + } catch (ResourceNotFoundException ee) { + throw ee; + } + } + return ri; + } + + @Override + public ResourceInformation[] getResources() { + if (resources.length != ResourceUtils.getNumberOfKnownResourceTypes()) { + // Retry once to reinitialize resource information. + initResourceMap(); + if (resources.length != ResourceUtils.getNumberOfKnownResourceTypes()) { + throw new ResourceNotFoundException("Failed to reinitialize " + + "FixedValueResource to get number of resource types same " + + "as configured"); + } + } + + return resources; + } + private void initResourceMap() { ResourceInformation[] types = ResourceUtils.getResourceTypesArray(); if (types != null) {