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 5ed57123f95..997c2c01332 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 @@ -201,9 +201,9 @@ public class ResourceUtils { } @VisibleForTesting - static void initializeResourcesMap(Configuration conf, - Map resourceInformationMap) { + static void initializeResourcesMap(Configuration conf) { + Map resourceInformationMap = new HashMap<>(); String[] resourceNames = conf.getStrings(YarnConfiguration.RESOURCE_TYPES); if (resourceNames != null && resourceNames.length != 0) { @@ -339,19 +339,18 @@ public class ResourceUtils { if (!initializedResources) { synchronized (ResourceUtils.class) { if (!initializedResources) { - Map resources = new HashMap<>(); if (conf == null) { conf = new YarnConfiguration(); } try { addResourcesFileToConf(resourceFile, conf); LOG.debug("Found " + resourceFile + ", adding to configuration"); - initializeResourcesMap(conf, resources); + initializeResourcesMap(conf); initializedResources = true; } catch (FileNotFoundException fe) { LOG.info("Unable to find '" + resourceFile + "'. Falling back to memory and vcores as resources", fe); - initializeResourcesMap(conf, resources); + initializeResourcesMap(conf); initializedResources = true; } } @@ -414,11 +413,12 @@ public class ResourceUtils { } @VisibleForTesting - public static void resetResourceTypes(Configuration conf) { + public static Map + resetResourceTypes(Configuration conf) { synchronized (ResourceUtils.class) { initializedResources = false; } - getResourceTypes(conf); + return getResourceTypes(conf); } public static String getUnits(String resourceValue) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java index b530150e56b..4e4671a6175 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java @@ -183,8 +183,9 @@ public class TestResourceUtils { YarnConfiguration.RESOURCE_TYPES + "." + resources[0] + ".units"; conf.set(name, resources[1]); } - Map ret = new HashMap<>(); - ResourceUtils.initializeResourcesMap(conf, ret); + Map ret = + ResourceUtils.resetResourceTypes(conf); + // for test1, 4 - length will be 1, 4 // for the others, len will be 3 int len = 3; @@ -248,9 +249,8 @@ public class TestResourceUtils { YarnConfiguration.RESOURCE_TYPES + "." + resources[0] + ".units"; conf.set(name, resources[1]); } - Map ret = new HashMap<>(); try { - ResourceUtils.initializeResourcesMap(conf, ret); + ResourceUtils.initializeResourcesMap(conf); Assert.fail("resource map initialization should fail"); } catch (Exception e) { // do nothing