From 8a8bf31bbdcd821727576653b8192e13926fe2ef Mon Sep 17 00:00:00 2001 From: Sunil G Date: Tue, 18 Jul 2017 22:45:58 +0530 Subject: [PATCH] YARN-6788-YARN-3926 --- .../hadoop/yarn/api/records/ProfileCapability.java | 8 +- .../apache/hadoop/yarn/api/records/Resource.java | 209 +++++++++------------ .../hadoop/yarn/api/records/impl/BaseResource.java | 132 +++++++++++++ .../hadoop/yarn/api/records/impl/package-info.java | 20 ++ .../hadoop/yarn/util/UnitsConversionUtil.java | 4 +- .../hadoop/yarn/util/resource/ResourceUtils.java | 60 ++++-- .../yarn/api/records/impl/pb/ProtoUtils.java | 5 +- .../yarn/api/records/impl/pb/ResourcePBImpl.java | 89 ++++----- .../hadoop/yarn/util/resource/Resources.java | 74 ++++---- .../yarn/util/resource/TestResourceUtils.java | 17 +- .../hadoop/yarn/util/resource/TestResources.java | 8 +- .../resource/ResourceProfilesManagerImpl.java | 8 +- .../rmapp/attempt/RMAppAttemptMetrics.java | 11 +- .../scheduler/SchedulerApplicationAttempt.java | 9 +- .../resourcemanager/webapp/dao/SchedulerInfo.java | 3 +- 15 files changed, 405 insertions(+), 252 deletions(-) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/BaseResource.java create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/package-info.java rename hadoop-yarn-project/hadoop-yarn/{hadoop-yarn-common => hadoop-yarn-api}/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java (89%) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ProfileCapability.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ProfileCapability.java index 1a8d1c3..2cb4670 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ProfileCapability.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ProfileCapability.java @@ -162,10 +162,10 @@ public static Resource toResource(ProfileCapability capability, if (capability.getProfileCapabilityOverride() != null && !capability.getProfileCapabilityOverride().equals(none)) { - for (Map.Entry entry : capability - .getProfileCapabilityOverride().getResources().entrySet()) { - if (entry.getValue() != null && entry.getValue().getValue() >= 0) { - resource.setResourceInformation(entry.getKey(), entry.getValue()); + for (ResourceInformation entry : capability + .getProfileCapabilityOverride().getResources()) { + if (entry != null && entry.getValue() >= 0) { + resource.setResourceInformation(entry.getName(), entry); } } } 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 9a8e2ec..079e695 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 @@ -18,6 +18,8 @@ package org.apache.hadoop.yarn.api.records; +import java.util.Arrays; + import org.apache.commons.lang.NotImplementedException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience.Public; @@ -25,13 +27,11 @@ import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.classification.InterfaceStability.Stable; import org.apache.hadoop.yarn.api.ApplicationMasterProtocol; +import org.apache.hadoop.yarn.api.records.impl.BaseResource; import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException; import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.util.Records; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; /** *

Resource models a set of computer resources in the @@ -60,81 +60,28 @@ @Stable public abstract class Resource implements Comparable { - private static Resource tmpResource = Records.newRecord(Resource.class); - - private static class SimpleResource extends Resource { - private long memory; - private long vcores; - private Map resourceInformationMap; - - SimpleResource(long memory, long vcores) { - this.memory = memory; - this.vcores = vcores; - - } - @Override - public int getMemory() { - return (int)memory; - } - @Override - public void setMemory(int memory) { - this.memory = memory; - } - @Override - public long getMemorySize() { - return memory; - } - @Override - public void setMemorySize(long memory) { - this.memory = memory; - } - @Override - public int getVirtualCores() { - return (int)vcores; - } - @Override - public void setVirtualCores(int vcores) { - this.vcores = vcores; - } - @Override - public Map getResources() { - if (resourceInformationMap == null) { - resourceInformationMap = new HashMap<>(); - resourceInformationMap.put(ResourceInformation.MEMORY_MB.getName(), - ResourceInformation.newInstance(ResourceInformation.MEMORY_MB)); - resourceInformationMap.put(ResourceInformation.VCORES.getName(), - ResourceInformation.newInstance(ResourceInformation.VCORES)); - } - resourceInformationMap.get(ResourceInformation.MEMORY_MB.getName()) - .setValue(this.memory); - resourceInformationMap.get(ResourceInformation.VCORES.getName()) - .setValue(this.vcores); - return Collections.unmodifiableMap(resourceInformationMap); - } - } - @Public @Stable public static Resource newInstance(int memory, int vCores) { - if (tmpResource.getResources().size() > 2) { + if (ResourceUtils.getResourceTypesArray().length > 2) { Resource ret = Records.newRecord(Resource.class); ret.setMemorySize(memory); ret.setVirtualCores(vCores); return ret; } - return new SimpleResource(memory, vCores); + return new BaseResource(memory, vCores); } @Public @Stable public static Resource newInstance(long memory, int vCores) { - if (tmpResource.getResources().size() > 2) { + if (ResourceUtils.getResourceTypesArray().length > 2) { Resource ret = Records.newRecord(Resource.class); ret.setMemorySize(memory); ret.setVirtualCores(vCores); return ret; } - return new SimpleResource(memory, vCores); + return new BaseResource(memory, vCores); } @InterfaceAudience.Private @@ -148,9 +95,8 @@ public static Resource newInstance(Resource resource) { @InterfaceAudience.Private @InterfaceStability.Unstable public static void copy(Resource source, Resource dest) { - for (Map.Entry entry : source.getResources() - .entrySet()) { - dest.setResourceInformation(entry.getKey(), entry.getValue()); + for (ResourceInformation entry : source.getResources()) { + dest.setResourceInformation(entry.getName(), entry); } } @@ -251,7 +197,7 @@ public void setMemorySize(long memory) { */ @Public @Evolving - public abstract Map getResources(); + public abstract ResourceInformation[] getResources(); /** * Get ResourceInformation for a specified resource. @@ -264,12 +210,13 @@ public void setMemorySize(long memory) { @Evolving public ResourceInformation getResourceInformation(String resource) throws YarnException { - if (getResources().containsKey(resource)) { - return getResources().get(resource); + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + ResourceInformation[] resources = getResources(); + if (index != null) { + return resources[index]; } - throw new YarnException( - "Unknown resource '" + resource + "'. Known resources are " - + getResources().keySet()); + throw new YarnException("Unknown resource '" + resource + + "'. Known resources are " + Arrays.toString(resources)); } /** @@ -283,12 +230,13 @@ public ResourceInformation getResourceInformation(String resource) @Public @Evolving public Long getResourceValue(String resource) throws YarnException { - if (getResources().containsKey(resource)) { - return getResources().get(resource).getValue(); + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + ResourceInformation[] resources = getResources(); + if (index != null) { + return resources[index].getValue(); } - throw new YarnException( - "Unknown resource '" + resource + "'. Known resources are " - + getResources().keySet()); + throw new YarnException("Unknown resource '" + resource + + "'. Known resources are " + Arrays.toString(resources)); } /** @@ -301,7 +249,8 @@ public Long getResourceValue(String resource) throws YarnException { @Public @Evolving public void setResourceInformation(String resource, - ResourceInformation resourceInformation) throws ResourceNotFoundException { + ResourceInformation resourceInformation) + throws ResourceNotFoundException { if (resource.equals(ResourceInformation.MEMORY_MB.getName())) { this.setMemorySize(resourceInformation.getValue()); return; @@ -310,14 +259,14 @@ public void setResourceInformation(String resource, this.setVirtualCores((int) resourceInformation.getValue()); return; } - if (getResources().containsKey(resource)) { - ResourceInformation - .copy(resourceInformation, getResources().get(resource)); + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + ResourceInformation[] resources = getResources(); + if (index != null) { + ResourceInformation.copy(resourceInformation, resources[index]); return; } - throw new ResourceNotFoundException( - "Unknown resource '" + resource + "'. Known resources are " - + getResources().keySet()); + throw new ResourceNotFoundException("Unknown resource '" + resource + + "'. Known resources are " + Arrays.toString(resources)); } /** @@ -340,13 +289,15 @@ public void setResourceValue(String resource, Long value) this.setVirtualCores(value.intValue()); return; } - if (getResources().containsKey(resource)) { - getResources().get(resource).setValue(value); + + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + ResourceInformation[] resources = getResources(); + if (index != null) { + resources[index].setValue(value); return; } - throw new ResourceNotFoundException( - "Unknown resource '" + resource + "'. Known resources are " - + getResources().keySet()); + throw new ResourceNotFoundException("Unknown resource '" + resource + + "'. Known resources are " + Arrays.toString(resources)); } @Override @@ -356,13 +307,12 @@ public int hashCode() { int result = (int) (939769357 + getMemorySize()); // prime * result = 939769357 initially result = prime * result + getVirtualCores(); - for (Map.Entry entry : getResources() - .entrySet()) { - if (entry.getKey().equals(ResourceInformation.MEMORY_MB.getName()) - || entry.getKey().equals(ResourceInformation.VCORES.getName())) { + for (ResourceInformation entry : getResources()) { + if (entry.getName().equals(ResourceInformation.MEMORY_MB.getName()) + || entry.getName().equals(ResourceInformation.VCORES.getName())) { continue; } - result = prime * result + entry.getValue().hashCode(); + result = prime * result + entry.hashCode(); } return result; } @@ -379,11 +329,25 @@ public boolean equals(Object obj) { return false; } Resource other = (Resource) obj; - if (getMemorySize() != other.getMemorySize() || getVirtualCores() != other - .getVirtualCores()) { + if (getMemorySize() != other.getMemorySize() + || getVirtualCores() != other.getVirtualCores()) { return false; } - return this.getResources().equals(other.getResources()); + for (ResourceInformation entry : getResources()) { + for (ResourceInformation otherEntry : other.getResources()) { + if (entry.getName().equals(ResourceInformation.MEMORY_MB.getName()) + || entry.getName().equals(ResourceInformation.VCORES.getName())) { + continue; + } + + if (entry.getName().equals(otherEntry.getName())) { + if (!entry.equals(otherEntry)) { + return false; + } + } + } + } + return true; } @Override @@ -391,21 +355,20 @@ public String toString() { StringBuilder sb = new StringBuilder(); sb.append(" entry : getResources() - .entrySet()) { - if (entry.getKey().equals(ResourceInformation.MEMORY_MB.getName()) - && entry.getValue().getUnits() + for (ResourceInformation entry : getResources()) { + if (entry.getName().equals(ResourceInformation.MEMORY_MB.getName()) + && entry.getUnits() .equals(ResourceInformation.MEMORY_MB.getUnits())) { continue; } - if (entry.getKey().equals(ResourceInformation.VCORES.getName()) - && entry.getValue().getUnits() + if (entry.getName().equals(ResourceInformation.VCORES.getName()) + && entry.getUnits() .equals(ResourceInformation.VCORES.getUnits())) { continue; } - sb.append(", ").append(entry.getKey()).append(": ") - .append(entry.getValue().getValue()) - .append(entry.getValue().getUnits()); + sb.append(", ").append(entry.getName()).append(": ") + .append(entry.getValue()) + .append(entry.getUnits()); } sb.append(">"); return sb.toString(); @@ -413,30 +376,30 @@ public String toString() { @Override public int compareTo(Resource other) { - Map thisResources, otherResources; + ResourceInformation[] thisResources, otherResources; thisResources = this.getResources(); otherResources = other.getResources(); - long diff = thisResources.size() - otherResources.size(); + long diff = thisResources.length - otherResources.length; if (diff == 0) { // compare memory and vcores first(in that order) to preserve // existing behaviour - if (thisResources.keySet().equals(otherResources.keySet())) { - diff = this.getMemorySize() - other.getMemorySize(); - if (diff == 0) { - diff = this.getVirtualCores() - other.getVirtualCores(); - } - if (diff == 0) { - for (Map.Entry entry : thisResources - .entrySet()) { - if (entry.getKey().equals(ResourceInformation.MEMORY_MB.getName()) - || entry.getKey() - .equals(ResourceInformation.VCORES.getName())) { - continue; - } - diff = - entry.getValue().compareTo(otherResources.get(entry.getKey())); - if (diff != 0) { - break; + diff = this.getMemorySize() - other.getMemorySize(); + if (diff == 0) { + diff = this.getVirtualCores() - other.getVirtualCores(); + } + if (diff == 0) { + for (ResourceInformation entry : thisResources) { + if (entry.getName().equals(ResourceInformation.MEMORY_MB.getName()) + || entry.getName().equals(ResourceInformation.VCORES.getName())) { + continue; + } + + for (ResourceInformation otherEntry : otherResources) { + if (entry.getName().equals(otherEntry.getName())) { + diff = entry.compareTo(otherEntry); + if (diff != 0) { + break; + } } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/BaseResource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/BaseResource.java new file mode 100644 index 0000000..a373912 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/BaseResource.java @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.api.records.impl; + +import org.apache.hadoop.classification.InterfaceAudience.Public; +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.util.resource.ResourceUtils; + +import java.util.Arrays; +import java.util.Map; + +/** + *

+ * BaseResource extends Resource to handle base resources such + * as memory and CPU. + * TODO: We have a long term plan to use AbstractResource when additional + * resource types are to be handled as well. + *

+ * + *

+ * Currently it models both memory and CPU. + *

+ * + *

+ * The unit for memory is megabytes. CPU is modeled with virtual cores (vcores), + * a unit for expressing parallelism. A node's capacity should be configured + * with virtual cores equal to its number of physical cores. A container should + * be requested with the number of cores it can saturate, i.e. the average + * number of threads it expects to have runnable at a time. + *

+ * + *

+ * Virtual cores take integer values and thus currently CPU-scheduling is very + * coarse. A complementary axis for CPU requests that represents processing + * power will likely be added in the future to enable finer-grained resource + * configuration. + *

+ * + * @see Resource + */ +@Public +@Unstable +public class BaseResource extends Resource { + + private ResourceInformation memoryResInfo; + private ResourceInformation vcoresResInfo; + protected ResourceInformation[] resources = null; + + public BaseResource() { + // Base constructor. + } + + public BaseResource(long memory, long vcores) { + this.memoryResInfo = ResourceInformation + .newInstance(ResourceInformation.MEMORY_MB.getName(), "", memory); + this.vcoresResInfo = ResourceInformation + .newInstance(ResourceInformation.VCORES.getName(), "", vcores); + + ResourceInformation[] types = ResourceUtils.getResourceTypesArray(); + Map indexMap = ResourceUtils.getResourceTypeIndex(); + resources = new ResourceInformation[types.length]; + resources[indexMap.get(memoryResInfo.getName())] = memoryResInfo; + resources[indexMap.get(vcoresResInfo.getName())] = vcoresResInfo; + + if (types.length > 2) { + for (ResourceInformation resourceInfo : types) { + if (resourceInfo.equals(ResourceInformation.MEMORY_MB) + || resourceInfo.equals(ResourceInformation.VCORES)) { + continue; + } + + int index = indexMap.get(resourceInfo.getName()); + resources[index] = ResourceInformation.newInstance(resourceInfo); + } + } + } + + @Override + @SuppressWarnings("deprecation") + public int getMemory() { + return (int) memoryResInfo.getValue(); + } + + @Override + @SuppressWarnings("deprecation") + public void setMemory(int memory) { + this.memoryResInfo.setValue(memory); + } + + @Override + public long getMemorySize() { + return memoryResInfo.getValue(); + } + + @Override + public void setMemorySize(long memory) { + this.memoryResInfo.setValue(memory); + } + + @Override + public int getVirtualCores() { + return (int) vcoresResInfo.getValue(); + } + + @Override + public void setVirtualCores(int vcores) { + this.vcoresResInfo.setValue(vcores); + } + + @Override + public ResourceInformation[] getResources() { + return Arrays.copyOf(resources, resources.length); + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/package-info.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/package-info.java new file mode 100644 index 0000000..a43b3cb --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@InterfaceAudience.Public +package org.apache.hadoop.yarn.api.records.impl; +import org.apache.hadoop.classification.InterfaceAudience; \ No newline at end of file diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/UnitsConversionUtil.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/UnitsConversionUtil.java index c7663de..510ace0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/UnitsConversionUtil.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/UnitsConversionUtil.java @@ -189,7 +189,7 @@ public static int compare(String unitA, long valueA, String unitB, Converter unitAC = getConverter(unitA); Converter unitBC = getConverter(unitB); if (unitA.equals(unitB)) { - return Long.valueOf(valueA).compareTo(valueB); + return Long.compare(valueA, valueB); } int unitAPos = SORTED_UNITS.indexOf(unitA); int unitBPos = SORTED_UNITS.indexOf(unitB); @@ -201,7 +201,7 @@ public static int compare(String unitA, long valueA, String unitB, } else { tmpA = convert(unitA, unitB, valueA); } - return Long.valueOf(tmpA).compareTo(tmpB); + return Long.compare(tmpA, tmpB); } catch (IllegalArgumentException ie) { BigInteger tmpA = BigInteger.valueOf(valueA); BigInteger tmpB = BigInteger.valueOf(valueB); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/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 similarity index 89% rename from hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java rename to hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java index 86cf872..2f907b0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/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 @@ -42,6 +42,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * Helper class to read the resource-types to be supported by the system. @@ -66,7 +67,10 @@ } private static volatile Object lock; + private static Map indexForResourceInformation = + new ConcurrentHashMap(); private static Map readOnlyResources; + private static ResourceInformation[] readOnlyResourcesArray; private static volatile Object nodeLock; private static Map readOnlyNodeResources; @@ -252,6 +256,28 @@ static void initializeResourcesMap(Configuration conf, setMinimumAllocationForMandatoryResources(resourceInformationMap, conf); setMaximumAllocationForMandatoryResources(resourceInformationMap, conf); readOnlyResources = Collections.unmodifiableMap(resourceInformationMap); + readOnlyResourcesArray = readOnlyResources.values() + .toArray(new ResourceInformation[readOnlyResources.size()]); + updateResourceTypeIndex(); + } + + private static void updateResourceTypeIndex() { + indexForResourceInformation.clear(); + + int index = 0; + for (ResourceInformation resInfo : readOnlyResourcesArray) { + indexForResourceInformation.put(resInfo.getName(), index); + index++; + } + } + + /** + * Get associate index of resource types such memory, cpu etc. + * This could help to access each resource types in a resource faster. + * @return Index map for all Resource Types. + */ + public static Map getResourceTypeIndex() { + return Collections.unmodifiableMap(indexForResourceInformation); } /** @@ -264,6 +290,12 @@ static void initializeResourcesMap(Configuration conf, YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE); } + public static ResourceInformation[] getResourceTypesArray() { + getResourceTypes(null, YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE); + return readOnlyResources.values() + .toArray(new ResourceInformation[readOnlyResources.size()]); + } + private static Map getResourceTypes( Configuration conf) { return getResourceTypes(conf, @@ -383,6 +415,8 @@ public static String getUnits(String resourceValue) { initializeNodeResourceInformation(conf); addManadtoryResources(nodeResources); checkMandatatoryResources(nodeResources); + setMinimumAllocationForMandatoryResources(nodeResources, conf); + setMaximumAllocationForMandatoryResources(nodeResources, conf); readOnlyNodeResources = Collections.unmodifiableMap(nodeResources); nodeLock = new Object(); } @@ -437,24 +471,23 @@ synchronized public static void resetNodeResources() { } public static Resource getResourceTypesMinimumAllocation() { - Map resourceTypes = getResourceTypes(); + ResourceInformation[] resourceTypes = getResourceTypesArray(); Resource ret = Resource.newInstance(0, 0); - for (Map.Entry entry : resourceTypes - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : resourceTypes) { + String name = entry.getName(); if (name.equals(ResourceInformation.MEMORY_MB.getName())) { - ret.setMemorySize(entry.getValue().getMinimumAllocation()); + ret.setMemorySize(entry.getMinimumAllocation()); continue; } if (name.equals(ResourceInformation.VCORES.getName())) { - Long tmp = entry.getValue().getMinimumAllocation(); + Long tmp = entry.getMinimumAllocation(); if (tmp > Integer.MAX_VALUE) { tmp = (long) Integer.MAX_VALUE; } ret.setVirtualCores(tmp.intValue()); continue; } - ret.setResourceValue(name, entry.getValue().getMinimumAllocation()); + ret.setResourceValue(name, entry.getMinimumAllocation()); } return ret; } @@ -464,24 +497,23 @@ public static Resource getResourceTypesMinimumAllocation() { * @return a Resource object with the maximum allocation for the scheduler */ public static Resource getResourceTypesMaximumAllocation() { - Map resourceTypes = getResourceTypes(); + ResourceInformation[] resourceTypes = getResourceTypesArray(); Resource ret = Resource.newInstance(0, 0); - for (Map.Entry entry : resourceTypes - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : resourceTypes) { + String name = entry.getName(); if (name.equals(ResourceInformation.MEMORY_MB.getName())) { - ret.setMemorySize(entry.getValue().getMaximumAllocation()); + ret.setMemorySize(entry.getMaximumAllocation()); continue; } if (name.equals(ResourceInformation.VCORES.getName())) { - Long tmp = entry.getValue().getMaximumAllocation(); + Long tmp = entry.getMaximumAllocation(); if (tmp > Integer.MAX_VALUE) { tmp = (long) Integer.MAX_VALUE; } ret.setVirtualCores(tmp.intValue()); continue; } - ret.setResourceValue(name, entry.getValue().getMaximumAllocation()); + ret.setResourceValue(name, entry.getMaximumAllocation()); } return ret; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java index 626ff9b..158c2ae 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java @@ -456,9 +456,8 @@ public static ResourceTypes convertFromProtoFormat(ResourceTypesProto e) { List pList) { Resource tmp = Resource.newInstance(0, 0); Map ret = new HashMap<>(); - for (Map.Entry entry : tmp.getResources() - .entrySet()) { - ret.put(entry.getKey(), 0L); + for (ResourceInformation entry : tmp.getResources()) { + ret.put(entry.getName(), 0L); } if (pList != null) { for (YarnProtos.StringLongMapProto p : pList) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java index 7bc7f5f..027f424 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java @@ -25,6 +25,7 @@ import org.apache.hadoop.yarn.api.protocolrecords.ResourceTypes; import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.ResourceInformation; +import org.apache.hadoop.yarn.api.records.impl.BaseResource; import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; @@ -33,14 +34,13 @@ import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.util.UnitsConversionUtil; -import java.util.HashMap; import java.util.Map; -import java.util.Collections; +import java.util.Arrays; @Private @Unstable -public class ResourcePBImpl extends Resource { +public class ResourcePBImpl extends BaseResource { private static final Log LOG = LogFactory.getLog(ResourcePBImpl.class); @@ -48,10 +48,6 @@ ResourceProto.Builder builder = null; boolean viaProto = false; - private Map resources; - private Map readOnlyResources; - - // call via ProtoUtils.convertToProtoFormat(Resource) static ResourceProto getProto(Resource r) { final ResourcePBImpl pb; @@ -72,8 +68,6 @@ public ResourcePBImpl() { public ResourcePBImpl(ResourceProto proto) { this.proto = proto; viaProto = true; - this.readOnlyResources = null; - this.resources = null; initResources(); } @@ -141,7 +135,8 @@ private void initResources() { return; } ResourceProtoOrBuilder p = viaProto ? proto : builder; - initResourcesMap(); + Map indexMap = ResourceUtils.getResourceTypeIndex(); + initResourcesMap(indexMap); for (ResourceInformationProto entry : p.getResourceValueMapList()) { ResourceTypes type = entry.hasType() ? ProtoUtils.convertFromProtoFormat(entry.getType()) : @@ -150,12 +145,13 @@ private void initResources() { long value = entry.hasValue() ? entry.getValue() : 0L; ResourceInformation ri = ResourceInformation .newInstance(entry.getKey(), units, value, type, 0L, Long.MAX_VALUE); - if (resources.containsKey(ri.getName())) { - resources.get(ri.getName()).setResourceType(ri.getResourceType()); - resources.get(ri.getName()).setUnits(ri.getUnits()); - resources.get(ri.getName()).setValue(value); - } else { + Integer index = indexMap.get(entry.getKey()); + if(index == null) { LOG.warn("Got unknown resource type: " + ri.getName() + "; skipping"); + } else { + resources[index].setResourceType(ri.getResourceType()); + resources[index].setUnits(ri.getUnits()); + resources[index].setValue(value); } } this.setMemorySize(p.getMemory()); @@ -174,8 +170,9 @@ public void setResourceInformation(String resource, resourceInformation.setName(resource); } initResources(); - if (resources.containsKey(resource)) { - ResourceInformation.copy(resourceInformation, resources.get(resource)); + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + if(index != null) { + ResourceInformation.copy(resourceInformation, resources[index]); } } @@ -187,65 +184,69 @@ public void setResourceValue(String resource, Long value) if (resource == null) { throw new IllegalArgumentException("resource type object cannot be null"); } - if (resources == null || (!resources.containsKey(resource))) { + + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + if (index == null) { throw new ResourceNotFoundException( "Resource " + resource + " not found"); } - resources.get(resource).setValue(value); + resources[index].setValue(value); } @Override - public Map getResources() { + public ResourceInformation[] getResources() { initResources(); - return readOnlyResources; + return Arrays.copyOf(resources, resources.length); } @Override public ResourceInformation getResourceInformation(String resource) { initResources(); - if (this.resources.containsKey(resource)) { - return this.resources.get(resource); + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + if (index == null) { + throw new ResourceNotFoundException( + "Could not find entry for " + resource); } - throw new ResourceNotFoundException("Could not find entry for " + resource); + return resources[index]; } @Override public Long getResourceValue(String resource) { initResources(); - if (this.resources.containsKey(resource)) { - return this.resources.get(resource).getValue(); + Integer index = ResourceUtils.getResourceTypeIndex().get(resource); + if(index == null) { + throw new ResourceNotFoundException("Could not find entry for " + resource); } - throw new ResourceNotFoundException("Could not find entry for " + resource); + return resources[index].getValue(); } - private void initResourcesMap() { + private void initResourcesMap(Map indexMap) { if (resources == null) { - resources = new HashMap<>(); - Map types = ResourceUtils.getResourceTypes(); + ResourceInformation[] types = ResourceUtils.getResourceTypesArray(); if (types == null) { throw new YarnRuntimeException( "Got null return value from ResourceUtils.getResourceTypes()"); } - for (Map.Entry entry : types.entrySet()) { - resources.put(entry.getKey(), - ResourceInformation.newInstance(entry.getValue())); + + resources = new ResourceInformation[types.length]; + for (ResourceInformation entry : types) { + int index = indexMap.get(entry.getName()); + resources[index] = ResourceInformation.newInstance(entry); } - readOnlyResources = Collections.unmodifiableMap(resources); } } synchronized private void mergeLocalToBuilder() { builder.clearResourceValueMap(); - if (resources != null && !resources.isEmpty()) { - for (Map.Entry entry : - resources.entrySet()) { - ResourceInformationProto.Builder e = - ResourceInformationProto.newBuilder(); - e.setKey(entry.getKey()); - e.setUnits(entry.getValue().getUnits()); - e.setType( - ProtoUtils.converToProtoFormat(entry.getValue().getResourceType())); - e.setValue(entry.getValue().getValue()); + if(resources != null && resources.length != 0) { + for (int i = 0; i < resources.length - 1; i++) { + ResourceInformation resInfo = resources[i]; + ResourceInformationProto.Builder e = ResourceInformationProto + .newBuilder(); + e.setKey(resInfo.getName()); + e.setUnits(resInfo.getUnits()); + e.setType(ProtoUtils.converToProtoFormat(resInfo.getResourceType())); + e.setValue(resInfo.getValue()); builder.addResourceValueMap(e); } } 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 d143e93..93c9aaf 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 @@ -27,8 +27,10 @@ import org.apache.hadoop.yarn.util.Records; import org.apache.hadoop.yarn.util.UnitsConversionUtil; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; @InterfaceAudience.LimitedPrivate({ "YARN", "MapReduce" }) @@ -96,8 +98,9 @@ public void setVirtualCores(int virtualCores) { } @Override - public Map getResources() { - return Collections.unmodifiableMap(this.resources); + public ResourceInformation[] getResources() { + return (ResourceInformation[]) this.resources.values() + .toArray(new ResourceInformation[this.resources.size()]); } @Override @@ -135,12 +138,12 @@ public void setResourceValue(String resource, Long value) private Map initResourceMap() { Map tmp = new HashMap<>(); - Map types = ResourceUtils.getResourceTypes(); + ResourceInformation[] types = ResourceUtils.getResourceTypesArray(); if (types != null) { - for (Map.Entry entry : types.entrySet()) { - tmp.put(entry.getKey(), - ResourceInformation.newInstance(entry.getValue())); - tmp.get(entry.getKey()).setValue(resourceValue); + for (ResourceInformation entry : types) { + tmp.put(entry.getName(), + ResourceInformation.newInstance(entry)); + tmp.get(entry.getName()).setValue(resourceValue); } } // this is a fix for getVirtualCores returning an int @@ -150,7 +153,6 @@ public void setResourceValue(String resource, Long value) } return tmp; } - } public static Resource createResource(int memory) { @@ -197,12 +199,11 @@ public static Resource clone(Resource res) { } public static Resource addTo(Resource lhs, Resource rhs) { - for (Map.Entry entry : lhs.getResources() - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : lhs.getResources()) { + String name = entry.getName(); try { ResourceInformation rhsValue = rhs.getResourceInformation(name); - ResourceInformation lhsValue = entry.getValue(); + ResourceInformation lhsValue = entry; long convertedRhs = UnitsConversionUtil .convert(rhsValue.getUnits(), lhsValue.getUnits(), rhsValue.getValue()); @@ -219,12 +220,11 @@ public static Resource add(Resource lhs, Resource rhs) { } public static Resource subtractFrom(Resource lhs, Resource rhs) { - for (Map.Entry entry : lhs.getResources() - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : lhs.getResources()) { + String name = entry.getName(); try { ResourceInformation rhsValue = rhs.getResourceInformation(name); - ResourceInformation lhsValue = entry.getValue(); + ResourceInformation lhsValue = entry; long convertedRhs = UnitsConversionUtil .convert(rhsValue.getUnits(), lhsValue.getUnits(), rhsValue.getValue()); @@ -263,10 +263,9 @@ public static Resource negate(Resource resource) { } public static Resource multiplyTo(Resource lhs, double by) { - for (Map.Entry entry : lhs.getResources() - .entrySet()) { - String name = entry.getKey(); - ResourceInformation lhsValue = entry.getValue(); + for (ResourceInformation entry : lhs.getResources()) { + String name = entry.getName(); + ResourceInformation lhsValue = entry; lhs.setResourceValue(name, (long) (lhsValue.getValue() * by)); } return lhs; @@ -282,12 +281,11 @@ public static Resource multiply(Resource lhs, double by) { */ public static Resource multiplyAndAddTo( Resource lhs, Resource rhs, double by) { - for (Map.Entry entry : lhs.getResources() - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : lhs.getResources()) { + String name = entry.getName(); try { ResourceInformation rhsValue = rhs.getResourceInformation(name); - ResourceInformation lhsValue = entry.getValue(); + ResourceInformation lhsValue = entry; long convertedRhs = (long) (UnitsConversionUtil .convert(rhsValue.getUnits(), lhsValue.getUnits(), rhsValue.getValue()) * by); @@ -311,10 +309,9 @@ public static Resource multiplyAndNormalizeDown( public static Resource multiplyAndRoundDown(Resource lhs, double by) { Resource out = clone(lhs); - for (Map.Entry entry : out.getResources() - .entrySet()) { - String name = entry.getKey(); - ResourceInformation lhsValue = entry.getValue(); + for (ResourceInformation entry : out.getResources()) { + String name = entry.getName(); + ResourceInformation lhsValue = entry; out.setResourceValue(name, (long) (lhsValue.getValue() * by)); } return out; @@ -416,12 +413,11 @@ public static Resource max( } public static boolean fitsIn(Resource smaller, Resource bigger) { - for (Map.Entry entry : smaller.getResources() - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : smaller.getResources()) { + String name = entry.getName(); try { ResourceInformation rhsValue = bigger.getResourceInformation(name); - ResourceInformation lhsValue = entry.getValue(); + ResourceInformation lhsValue = entry; long convertedRhs = UnitsConversionUtil .convert(rhsValue.getUnits(), lhsValue.getUnits(), rhsValue.getValue()); @@ -442,12 +438,11 @@ public static boolean fitsIn(ResourceCalculator rc, Resource cluster, public static Resource componentwiseMin(Resource lhs, Resource rhs) { Resource ret = createResource(0); - for (Map.Entry entry : lhs.getResources() - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : lhs.getResources()) { + String name = entry.getName(); try { ResourceInformation rhsValue = rhs.getResourceInformation(name); - ResourceInformation lhsValue = entry.getValue(); + ResourceInformation lhsValue = entry; long convertedRhs = UnitsConversionUtil .convert(rhsValue.getUnits(), lhsValue.getUnits(), rhsValue.getValue()); @@ -463,12 +458,11 @@ public static Resource componentwiseMin(Resource lhs, Resource rhs) { public static Resource componentwiseMax(Resource lhs, Resource rhs) { Resource ret = createResource(0); - for (Map.Entry entry : lhs.getResources() - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : lhs.getResources()) { + String name = entry.getName(); try { ResourceInformation rhsValue = rhs.getResourceInformation(name); - ResourceInformation lhsValue = entry.getValue(); + ResourceInformation lhsValue = entry; long convertedRhs = UnitsConversionUtil .convert(rhsValue.getUnits(), lhsValue.getUnits(), rhsValue.getValue()); 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 38554b6..9305d3a 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 @@ -30,6 +30,8 @@ import org.junit.Test; import java.io.File; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -276,13 +278,18 @@ public void testGetResourceInformation() throws Exception { String resourceFile = entry.getKey(); ResourceUtils.resetNodeResources(); File dest; - File source = - new File(conf.getClassLoader().getResource(resourceFile).getFile()); + File source = new File( + conf.getClassLoader().getResource(resourceFile).getFile()); dest = new File(source.getParent(), "node-resources.xml"); FileUtils.copyFile(source, dest); - Map actual = - ResourceUtils.getNodeResourceInformation(conf); - Assert.assertEquals(entry.getValue().getResources(), actual); + Map actual = ResourceUtils + .getNodeResourceInformation(conf); + Assert.assertEquals(actual.size(), entry.getValue().getResources().length); + for (ResourceInformation resInfo : entry.getValue().getResources()) { + if (actual.containsKey(resInfo.getName())) { + Assert.assertEquals(resInfo, actual.get(resInfo.getName())); + } + } dest.delete(); } } 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 1555e55..9f7b88f 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 @@ -22,7 +22,10 @@ import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.ResourceInformation; import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.util.resource.TestResourceUtils; +import org.apache.hadoop.yarn.util.resource.TestResourceUtils.ResourceFileInformation; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -57,8 +60,11 @@ public static Resource none() { private void setupExtraResourceType() throws Exception { Configuration conf = new YarnConfiguration(); + ResourceFileInformation testFile = + new ResourceFileInformation("resource-types-3.xml", 3); + testFile.resourceNameUnitsMap.put(EXTRA_RESOURCE_TYPE, ""); resourceTypesFile = - TestResourceUtils.setupResourceTypes(conf, "resource-types-3.xml"); + TestResourceUtils.setupResourceTypes(conf, testFile.filename); } private void unsetExtraResourceType() { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/resource/ResourceProfilesManagerImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/resource/ResourceProfilesManagerImpl.java index 7987ded..ab33336 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/resource/ResourceProfilesManagerImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/resource/ResourceProfilesManagerImpl.java @@ -119,13 +119,13 @@ private void loadProfiles() throws IOException { private Resource parseResource(String key, Map value) throws IOException { Resource resource = Resource.newInstance(0, 0); Iterator iterator = value.entrySet().iterator(); - Map resourceTypes = - ResourceUtils.getResourceTypes(); + Map resourceTypes = ResourceUtils + .getResourceTypes(); while (iterator.hasNext()) { Map.Entry resourceEntry = (Map.Entry) iterator.next(); String resourceName = resourceEntry.getKey().toString(); - ResourceInformation resourceValue = - fromString(resourceName, resourceEntry.getValue().toString()); + ResourceInformation resourceValue = fromString(resourceName, + resourceEntry.getValue().toString()); if (resourceName.equals(MEMORY)) { resource.setMemorySize(resourceValue.getValue()); continue; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmapp/attempt/RMAppAttemptMetrics.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmapp/attempt/RMAppAttemptMetrics.java index ff18223..c514cb3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmapp/attempt/RMAppAttemptMetrics.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmapp/attempt/RMAppAttemptMetrics.java @@ -176,16 +176,15 @@ public void updateAggregatePreemptedAppResourceUsage( private void updateUsageMap(Resource allocated, long deltaUsedMillis, Map targetMap) { - for (Map.Entry entry : allocated.getResources() - .entrySet()) { + for (ResourceInformation entry : allocated.getResources()) { AtomicLong resourceUsed; - if (!targetMap.containsKey(entry.getKey())) { + if (!targetMap.containsKey(entry.getName())) { resourceUsed = new AtomicLong(0); - targetMap.put(entry.getKey(), resourceUsed); + targetMap.put(entry.getName(), resourceUsed); } - resourceUsed = targetMap.get(entry.getKey()); - resourceUsed.addAndGet((entry.getValue().getValue() * deltaUsedMillis) + resourceUsed = targetMap.get(entry.getName()); + resourceUsed.addAndGet((entry.getValue() * deltaUsedMillis) / DateUtils.MILLIS_PER_SECOND); } } 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/SchedulerApplicationAttempt.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java index 811d9dc..b571322 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java @@ -984,13 +984,12 @@ private AggregateAppResourceUsage getRunningAggregateAppResourceUsage() { for (RMContainer rmContainer : this.liveContainers.values()) { long usedMillis = currentTimeMillis - rmContainer.getCreationTime(); Resource resource = rmContainer.getContainer().getResource(); - for (Map.Entry entry : resource - .getResources().entrySet()) { + for (ResourceInformation entry : resource.getResources()) { long value = RMServerUtils - .getOrDefault(resourceSecondsMap, entry.getKey(), 0L); - value += entry.getValue().getValue() * usedMillis + .getOrDefault(resourceSecondsMap, entry.getName(), 0L); + value += entry.getValue() * usedMillis / DateUtils.MILLIS_PER_SECOND; - resourceSecondsMap.put(entry.getKey(), value); + resourceSecondsMap.put(entry.getName(), value); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/SchedulerInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/SchedulerInfo.java index 887b854..81491b1 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/SchedulerInfo.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/SchedulerInfo.java @@ -18,6 +18,7 @@ package org.apache.hadoop.yarn.server.resourcemanager.webapp.dao; +import java.util.Arrays; import java.util.EnumSet; import javax.xml.bind.annotation.XmlRootElement; @@ -73,7 +74,7 @@ public ResourceInfo getMaxAllocation() { } public String getSchedulerResourceTypes() { - return minAllocResource.getResource().getResources().keySet().toString(); + return Arrays.toString(minAllocResource.getResource().getResources()); } public int getMaxClusterLevelAppPriority() { -- 2.10.1 (Apple Git-78)