From 52d1e871097117983749d1d6e5bfa81af0be403a Mon Sep 17 00:00:00 2001 From: Sunil G Date: Thu, 13 Jul 2017 16:28:16 +0530 Subject: [PATCH] YARN-6788-YARN-3926 --- .../hadoop/yarn/api/records/ProfileCapability.java | 8 +- .../apache/hadoop/yarn/api/records/Resource.java | 195 ++++++++------------- .../yarn/api/records/impl/AbstractResource.java | 122 +++++++++++++ .../hadoop/yarn/api/records/impl/package-info.java | 20 +++ .../hadoop/yarn/util/UnitsConversionUtil.java | 4 +- .../hadoop/yarn/util/resource/ResourceUtils.java | 33 ++++ .../yarn/api/records/impl/pb/ProtoUtils.java | 5 +- .../yarn/api/records/impl/pb/ResourcePBImpl.java | 87 ++++----- .../hadoop/yarn/util/resource/Resources.java | 74 ++++---- .../rmapp/attempt/RMAppAttemptMetrics.java | 11 +- .../scheduler/SchedulerApplicationAttempt.java | 9 +- .../resourcemanager/webapp/dao/SchedulerInfo.java | 3 +- 12 files changed, 344 insertions(+), 227 deletions(-) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/AbstractResource.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 (93%) 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..a866c33 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.AbstractResource; 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 AbstractResource(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 AbstractResource(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,11 @@ 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()); + return Arrays.equals(this.getResources(), other.getResources()); } @Override @@ -391,21 +341,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 +362,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/AbstractResource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/AbstractResource.java new file mode 100644 index 0000000..46fe3c0 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/impl/AbstractResource.java @@ -0,0 +1,122 @@ +/** + * 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.Map; + +/** + *

AbstractResource extends Resource to handle base resources + * such as memory and CPU.

+ * + *

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 AbstractResource extends Resource { + + private ResourceInformation memoryResInfo; + private ResourceInformation vcoresResInfo; + protected ResourceInformation[] resources = null; + + public AbstractResource() { + // Base constructor. + } + + public AbstractResource(long memory, long vcores) { + this.memoryResInfo = ResourceInformation + .newInstance(ResourceInformation.MEMORY_MB); + this.vcoresResInfo = ResourceInformation + .newInstance(ResourceInformation.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 this.resources; + } +} 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 93% 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..2e936aa 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,9 @@ } private static volatile Object lock; + private static Map indexForResourceInformation = null; private static Map readOnlyResources; + private static ResourceInformation[] readOnlyResourcesArray; private static volatile Object nodeLock; private static Map readOnlyNodeResources; @@ -252,6 +255,31 @@ static void initializeResourcesMap(Configuration conf, setMinimumAllocationForMandatoryResources(resourceInformationMap, conf); setMaximumAllocationForMandatoryResources(resourceInformationMap, conf); readOnlyResources = Collections.unmodifiableMap(resourceInformationMap); + readOnlyResourcesArray = resourceInformationMap.values() + .toArray(new ResourceInformation[readOnlyResources.size()]); + updateResourceTypeIndex(); + } + + private static void updateResourceTypeIndex() { + if (indexForResourceInformation == null) { + indexForResourceInformation = new ConcurrentHashMap(); + } + 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 +292,11 @@ static void initializeResourcesMap(Configuration conf, YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE); } + public static ResourceInformation[] getResourceTypesArray() { + getResourceTypes(null, YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE); + return readOnlyResourcesArray; + } + private static Map getResourceTypes( Configuration conf) { return getResourceTypes(conf, 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 a9abed9..a0e93bd 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 @@ -18,7 +18,6 @@ package org.apache.hadoop.yarn.api.records.impl.pb; -import org.apache.commons.collections.map.UnmodifiableMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.classification.InterfaceAudience.Private; @@ -26,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.AbstractResource; import org.apache.hadoop.yarn.exceptions.ResourceNotFoundException; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; @@ -38,7 +38,7 @@ @Private @Unstable -public class ResourcePBImpl extends Resource { +public class ResourcePBImpl extends AbstractResource { private static final Log LOG = LogFactory.getLog(ResourcePBImpl.class); @@ -46,10 +46,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; @@ -70,8 +66,6 @@ public ResourcePBImpl() { public ResourcePBImpl(ResourceProto proto) { this.proto = proto; viaProto = true; - this.readOnlyResources = null; - this.resources = null; initResources(); } @@ -139,7 +133,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()) : @@ -148,12 +143,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()); @@ -172,8 +168,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]); } } @@ -185,65 +182,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-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)