From b9b906c8cf6c32807f471e599135fac1878f9001 Mon Sep 17 00:00:00 2001 From: Sunil G Date: Tue, 25 Jul 2017 14:43:48 +0530 Subject: [PATCH] YARN-6788-YARN-3926 --- .../hadoop-yarn/dev-support/findbugs-exclude.xml | 18 ++ .../hadoop/yarn/api/records/ProfileCapability.java | 8 +- .../apache/hadoop/yarn/api/records/Resource.java | 242 ++++++++++----------- .../yarn/api/records/ResourceInformation.java | 13 +- .../hadoop/yarn/api/records/impl/BaseResource.java | 133 +++++++++++ .../hadoop/yarn/api/records/impl/package-info.java | 20 ++ .../hadoop/yarn/util/UnitsConversionUtil.java | 8 +- .../hadoop/yarn/util/resource/ResourceUtils.java | 161 +++++++++----- .../hadoop/yarn/util/resource/package-info.java | 18 ++ .../yarn/api/records/impl/pb/ProtoUtils.java | 5 +- .../yarn/api/records/impl/pb/ResourcePBImpl.java | 116 +++++----- .../util/resource/DominantResourceCalculator.java | 48 ++-- .../hadoop/yarn/util/resource/Resources.java | 155 ++++++------- .../yarn/util/resource/TestResourceUtils.java | 14 +- .../hadoop/yarn/util/resource/TestResources.java | 11 +- .../resource/ResourceProfilesManagerImpl.java | 8 +- .../rmapp/attempt/RMAppAttemptMetrics.java | 11 +- .../scheduler/SchedulerApplicationAttempt.java | 9 +- .../resourcemanager/webapp/dao/SchedulerInfo.java | 3 +- 19 files changed, 609 insertions(+), 392 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 (78%) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/package-info.java diff --git a/hadoop-yarn-project/hadoop-yarn/dev-support/findbugs-exclude.xml b/hadoop-yarn-project/hadoop-yarn/dev-support/findbugs-exclude.xml index 576db93..a2de624 100644 --- a/hadoop-yarn-project/hadoop-yarn/dev-support/findbugs-exclude.xml +++ b/hadoop-yarn-project/hadoop-yarn/dev-support/findbugs-exclude.xml @@ -583,4 +583,22 @@ + + + + + + + + + + + + + + + + + + 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..f5152e1 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,97 +60,49 @@ @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); - } - } + protected static final String MEMORY = ResourceInformation.MEMORY_MB.getName(); + protected static final String VCORES = ResourceInformation.VCORES.getName(); @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 @InterfaceStability.Unstable public static Resource newInstance(Resource resource) { - Resource ret = Resource.newInstance(0, 0); - Resource.copy(resource, ret); + Resource ret = Resource.newInstance(resource.getMemorySize(), + resource.getVirtualCores()); + if (ResourceUtils.getResourceTypesArray().length > 2) { + Resource.copy(resource, ret); + } return ret; } @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 +203,7 @@ public void setMemorySize(long memory) { */ @Public @Evolving - public abstract Map getResources(); + public abstract ResourceInformation[] getResources(); /** * Get ResourceInformation for a specified resource. @@ -264,12 +216,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)); } /** @@ -282,13 +235,14 @@ public ResourceInformation getResourceInformation(String resource) */ @Public @Evolving - public Long getResourceValue(String resource) throws YarnException { - if (getResources().containsKey(resource)) { - return getResources().get(resource).getValue(); + public long getResourceValue(String resource) throws YarnException { + 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,23 +255,24 @@ public Long getResourceValue(String resource) throws YarnException { @Public @Evolving public void setResourceInformation(String resource, - ResourceInformation resourceInformation) throws ResourceNotFoundException { - if (resource.equals(ResourceInformation.MEMORY_MB.getName())) { + ResourceInformation resourceInformation) + throws ResourceNotFoundException { + if (resource.equals(MEMORY)) { this.setMemorySize(resourceInformation.getValue()); return; } - if (resource.equals(ResourceInformation.VCORES.getName())) { + if (resource.equals(VCORES)) { 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)); } /** @@ -332,21 +287,23 @@ public void setResourceInformation(String resource, @Evolving public void setResourceValue(String resource, Long value) throws ResourceNotFoundException { - if (resource.equals(ResourceInformation.MEMORY_MB.getName())) { + if (resource.equals(MEMORY)) { this.setMemorySize(value); return; } - if (resource.equals(ResourceInformation.VCORES.getName())) { + if (resource.equals(VCORES)) { 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 +313,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(MEMORY) + || entry.getName().equals(VCORES)) { continue; } - result = prime * result + entry.getValue().hashCode(); + result = prime * result + entry.hashCode(); } return result; } @@ -379,11 +335,32 @@ 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; + } + + ResourceInformation[] myVectors = getResources(); + ResourceInformation[] otherVectors = other.getResources(); + + if (myVectors.length != otherVectors.length) { return false; } - return this.getResources().equals(other.getResources()); + + for (int i = 0; i < myVectors.length; i++) { + ResourceInformation a = myVectors[i]; + ResourceInformation b = otherVectors[i]; + if (a == b || (a == null && b == null)) { + continue; + } else if ((a == null && b != null) || (a != null && b == null)) { + return false; + } else { + if (a != null && !a.equals(b)) { + return false; + } + } + } + return true; } @Override @@ -391,21 +368,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(MEMORY) + && entry.getUnits() .equals(ResourceInformation.MEMORY_MB.getUnits())) { continue; } - if (entry.getKey().equals(ResourceInformation.VCORES.getName()) - && entry.getValue().getUnits() + if (entry.getName().equals(VCORES) + && 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 +389,30 @@ public String toString() { @Override public int compareTo(Resource other) { - Map thisResources, otherResources; - thisResources = this.getResources(); - otherResources = other.getResources(); - long diff = thisResources.size() - otherResources.size(); + ResourceInformation[] thisResources = this.getResources(); + ResourceInformation[] otherResources = other.getResources(); + + // compare memory and vcores first(in that order) to preserve + // existing behaviour + long diff = this.getMemorySize() - other.getMemorySize(); 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.getVirtualCores() - other.getVirtualCores(); + } + if (diff == 0) { + diff = thisResources.length - otherResources.length; + 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/ResourceInformation.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java index d75b441..3ab7ccd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java @@ -242,10 +242,15 @@ public boolean equals(Object obj) { return false; } ResourceInformation r = (ResourceInformation) obj; - int cmp = - UnitsConversionUtil.compare(this.units, this.value, r.units, r.value); - return this.name.equals(r.getName()) && this.resourceType - .equals(r.getResourceType()) && (cmp == 0); + if (!this.name.equals(r.getName()) + || !this.resourceType.equals(r.getResourceType())) { + return false; + } + if (this.units.equals(r.units)) { + return this.value == r.value; + } + return (UnitsConversionUtil.compare(this.units, this.value, r.units, + r.value) == 0); } @Override 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..83db542 --- /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,133 @@ +/** + * 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 java.util.Arrays; + +/** + *

+ * 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; + protected ResourceInformation[] readOnlyResources = null; + + protected enum MandatoryResources { + MEMORY(0), VCORES(1); + + private final int id; + + MandatoryResources(int id) { + this.id = id; + } + + public int getId() { + return this.id; + } + } + + public BaseResource() { + // Base constructor. + } + + public BaseResource(long memory, long vcores) { + this.memoryResInfo = ResourceInformation.newInstance(MEMORY, + ResourceInformation.MEMORY_MB.getUnits(), memory); + this.vcoresResInfo = ResourceInformation.newInstance(VCORES, "", vcores); + + resources = new ResourceInformation[MandatoryResources.values().length]; + readOnlyResources = new ResourceInformation[MandatoryResources + .values().length]; + resources[MandatoryResources.MEMORY.id] = memoryResInfo; + resources[MandatoryResources.VCORES.id] = vcoresResInfo; + readOnlyResources = Arrays.copyOf(resources, resources.length); + } + + @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 readOnlyResources; + } +} 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..7b737bc 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 @@ -186,11 +186,11 @@ public static int compare(String unitA, long valueA, String unitB, if (!KNOWN_UNITS.contains(unitB)) { throw new IllegalArgumentException("Unknown unit '" + unitB + "'"); } - Converter unitAC = getConverter(unitA); - Converter unitBC = getConverter(unitB); if (unitA.equals(unitB)) { - return Long.valueOf(valueA).compareTo(valueB); + return Long.compare(valueA, valueB); } + Converter unitAC = getConverter(unitA); + Converter unitBC = getConverter(unitB); int unitAPos = SORTED_UNITS.indexOf(unitA); int unitBPos = SORTED_UNITS.indexOf(unitB); try { @@ -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 78% 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..bf6c968 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,11 @@ } private static volatile Object lock; - private static Map readOnlyResources; + private static final Map resourceNameToIndex = + new ConcurrentHashMap(); + private static Map knownResourceTypes; + private static String[] resourceNamesArray; + private static ResourceInformation[] knownResourceTypesArray; private static volatile Object nodeLock; private static Map readOnlyNodeResources; @@ -127,21 +132,17 @@ private static void addManadtoryResources( private static void setMinimumAllocationForMandatoryResources( Map res, Configuration conf) { - String[][] resourceTypesKeys = - { - { ResourceInformation.MEMORY_MB.getName(), + String[][] resourceTypesKeys = { + {ResourceInformation.MEMORY_MB.getName(), YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, String.valueOf( - YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB), - ResourceInformation.MEMORY_MB.getName() - }, - { ResourceInformation.VCORES.getName(), + YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB), + ResourceInformation.MEMORY_MB.getName()}, + {ResourceInformation.VCORES.getName(), YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES, String.valueOf( - YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES), - ResourceInformation.VCORES.getName() - } - }; + YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES), + ResourceInformation.VCORES.getName()}}; for (String[] arr : resourceTypesKeys) { String resourceTypesKey = YarnConfiguration.RESOURCE_TYPES + "." + arr[0] + MINIMUM_ALLOCATION; @@ -166,23 +167,17 @@ private static void setMinimumAllocationForMandatoryResources( private static void setMaximumAllocationForMandatoryResources( Map res, Configuration conf) { - String[][] resourceTypesKeys = - { - { - ResourceInformation.MEMORY_MB.getName(), + String[][] resourceTypesKeys = { + {ResourceInformation.MEMORY_MB.getName(), YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, String.valueOf( - YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB), - ResourceInformation.MEMORY_MB.getName() - }, - { - ResourceInformation.VCORES.getName(), + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB), + ResourceInformation.MEMORY_MB.getName()}, + {ResourceInformation.VCORES.getName(), YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, String.valueOf( - YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES), - ResourceInformation.VCORES.getName() - } - }; + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES), + ResourceInformation.VCORES.getName()}}; for (String[] arr : resourceTypesKeys) { String resourceTypesKey = YarnConfiguration.RESOURCE_TYPES + "." + arr[0] + MAXIMUM_ALLOCATION; @@ -251,7 +246,49 @@ static void initializeResourcesMap(Configuration conf, addManadtoryResources(resourceInformationMap); setMinimumAllocationForMandatoryResources(resourceInformationMap, conf); setMaximumAllocationForMandatoryResources(resourceInformationMap, conf); - readOnlyResources = Collections.unmodifiableMap(resourceInformationMap); + knownResourceTypes = Collections.unmodifiableMap(resourceInformationMap); + updateReadOnlyResources(); + updateResourceTypeIndex(); + } + + private static void updateReadOnlyResources() { + knownResourceTypesArray = new ResourceInformation[knownResourceTypes.size()]; + knownResourceTypesArray[0] = ResourceInformation + .newInstance(knownResourceTypes.get(MEMORY)); + knownResourceTypesArray[1] = ResourceInformation + .newInstance(knownResourceTypes.get(VCORES)); + + if (knownResourceTypes.size() > 2) { + int index = 2; + for (ResourceInformation resInfo : knownResourceTypes.values()) { + if (resInfo.getName().equals(MEMORY) + || resInfo.getName().equals(VCORES)) { + continue; + } + knownResourceTypesArray[index] = ResourceInformation + .newInstance(resInfo); + index++; + } + } + } + + private static void updateResourceTypeIndex() { + resourceNameToIndex.clear(); + + int index = 0; + for (ResourceInformation resInfo : knownResourceTypesArray) { + resourceNameToIndex.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 resourceNameToIndex; } /** @@ -264,6 +301,21 @@ static void initializeResourcesMap(Configuration conf, YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE); } + /** + * Get resource names array, this is mostly for performance perspective. Never + * modify returned array. + * + * @return resourceNamesArray + */ + public static String[] getResourceNamesArray() { + return resourceNamesArray; + } + + public static ResourceInformation[] getResourceTypesArray() { + getResourceTypes(null, YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE); + return knownResourceTypesArray; + } + private static Map getResourceTypes( Configuration conf) { return getResourceTypes(conf, @@ -286,16 +338,26 @@ static void initializeResourcesMap(Configuration conf, initializeResourcesMap(conf, resources); lock = new Object(); } catch (FileNotFoundException fe) { - LOG.info("Unable to find '" + resourceFile - + "'. Falling back to memory and vcores as resources", fe); + LOG.info( + "Unable to find '" + resourceFile + + "'. Falling back to memory and vcores as resources", + fe); initializeResourcesMap(conf, resources); lock = new Object(); } + + // Update resource names. + resourceNamesArray = new String[resources.size()]; + int i = 0; + for (String s : knownResourceTypes.keySet()) { + resourceNamesArray[i] = s; + i++; + } } } } } - return readOnlyResources; + return knownResourceTypes; } private static InputStream getConfInputStream(String resourceFile, @@ -379,10 +441,12 @@ public static String getUnits(String resourceValue) { synchronized (ResourceUtils.class) { if (nodeLock == null) { synchronized (ResourceUtils.class) { - Map nodeResources = - initializeNodeResourceInformation(conf); + Map nodeResources = initializeNodeResourceInformation( + conf); addManadtoryResources(nodeResources); checkMandatatoryResources(nodeResources); + setMinimumAllocationForMandatoryResources(nodeResources, conf); + setMaximumAllocationForMandatoryResources(nodeResources, conf); readOnlyNodeResources = Collections.unmodifiableMap(nodeResources); nodeLock = new Object(); } @@ -437,24 +501,20 @@ synchronized public static void resetNodeResources() { } public static Resource getResourceTypesMinimumAllocation() { - Map resourceTypes = getResourceTypes(); Resource ret = Resource.newInstance(0, 0); - for (Map.Entry entry : resourceTypes - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : knownResourceTypesArray) { + String name = entry.getName(); if (name.equals(ResourceInformation.MEMORY_MB.getName())) { - ret.setMemorySize(entry.getValue().getMinimumAllocation()); - continue; - } - if (name.equals(ResourceInformation.VCORES.getName())) { - Long tmp = entry.getValue().getMinimumAllocation(); + ret.setMemorySize(entry.getMinimumAllocation()); + } else if (name.equals(ResourceInformation.VCORES.getName())) { + Long tmp = entry.getMinimumAllocation(); if (tmp > Integer.MAX_VALUE) { tmp = (long) Integer.MAX_VALUE; } ret.setVirtualCores(tmp.intValue()); - continue; + } else { + ret.setResourceValue(name, entry.getMinimumAllocation()); } - ret.setResourceValue(name, entry.getValue().getMinimumAllocation()); } return ret; } @@ -464,24 +524,21 @@ public static Resource getResourceTypesMinimumAllocation() { * @return a Resource object with the maximum allocation for the scheduler */ public static Resource getResourceTypesMaximumAllocation() { - Map resourceTypes = getResourceTypes(); Resource ret = Resource.newInstance(0, 0); - for (Map.Entry entry : resourceTypes - .entrySet()) { - String name = entry.getKey(); + for (ResourceInformation entry : knownResourceTypesArray) { + String name = entry.getName(); if (name.equals(ResourceInformation.MEMORY_MB.getName())) { - ret.setMemorySize(entry.getValue().getMaximumAllocation()); - continue; - } - if (name.equals(ResourceInformation.VCORES.getName())) { - Long tmp = entry.getValue().getMaximumAllocation(); + ret.setMemorySize(entry.getMaximumAllocation()); + } else if (name.equals(ResourceInformation.VCORES.getName())) { + Long tmp = entry.getMaximumAllocation(); if (tmp > Integer.MAX_VALUE) { tmp = (long) Integer.MAX_VALUE; } ret.setVirtualCores(tmp.intValue()); continue; + } else { + ret.setResourceValue(name, entry.getMaximumAllocation()); } - ret.setResourceValue(name, entry.getValue().getMaximumAllocation()); } return ret; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/package-info.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/package-info.java new file mode 100644 index 0000000..37a63de --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/package-info.java @@ -0,0 +1,18 @@ +/* + * 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.util.resource; 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..360403e 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,7 +25,9 @@ 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.YarnException; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProtoOrBuilder; @@ -33,14 +35,13 @@ import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.util.UnitsConversionUtil; -import java.util.HashMap; +import java.util.Arrays; import java.util.Map; -import java.util.Collections; @Private @Unstable -public class ResourcePBImpl extends Resource { +public class ResourcePBImpl extends BaseResource { private static final Log LOG = LogFactory.getLog(ResourcePBImpl.class); @@ -48,10 +49,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 +69,6 @@ public ResourcePBImpl() { public ResourcePBImpl(ResourceProto proto) { this.proto = proto; viaProto = true; - this.readOnlyResources = null; - this.resources = null; initResources(); } @@ -101,11 +96,13 @@ public int getMemory() { public long getMemorySize() { // memory should always be present initResources(); - ResourceInformation ri = - this.getResourceInformation(ResourceInformation.MEMORY_MB.getName()); - return UnitsConversionUtil - .convert(ri.getUnits(), ResourceInformation.MEMORY_MB.getUnits(), - ri.getValue()); + ResourceInformation ri = resources[MandatoryResources.MEMORY.getId()]; + + if (ri.getUnits().isEmpty()) { + return ri.getValue(); + } + return UnitsConversionUtil.convert(ri.getUnits(), + ResourceInformation.MEMORY_MB.getUnits(), ri.getValue()); } @Override @@ -117,23 +114,28 @@ public void setMemory(int memory) { @Override public void setMemorySize(long memory) { maybeInitBuilder(); - getResourceInformation(ResourceInformation.MEMORY_MB.getName()) - .setValue(memory); + try { + getResourceInformation(MEMORY).setValue(memory); + } catch (YarnException e) { + throw new ResourceNotFoundException(e); + } } @Override public int getVirtualCores() { // vcores should always be present initResources(); - return this.getResourceValue(ResourceInformation.VCORES.getName()) - .intValue(); + return (int) resources[MandatoryResources.VCORES.getId()].getValue(); } @Override public void setVirtualCores(int vCores) { maybeInitBuilder(); - getResourceInformation(ResourceInformation.VCORES.getName()) - .setValue(vCores); + try { + getResourceInformation(VCORES).setValue(vCores); + } catch (YarnException e) { + throw new ResourceNotFoundException(e); + } } private void initResources() { @@ -142,6 +144,7 @@ private void initResources() { } ResourceProtoOrBuilder p = viaProto ? proto : builder; initResourcesMap(); + Map indexMap = ResourceUtils.getResourceTypeIndex(); for (ResourceInformationProto entry : p.getResourceValueMapList()) { ResourceTypes type = entry.hasType() ? ProtoUtils.convertFromProtoFormat(entry.getType()) : @@ -150,14 +153,16 @@ 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); } } + readOnlyResources = Arrays.copyOf(resources, resources.length); this.setMemorySize(p.getMemory()); this.setVirtualCores(p.getVirtualCores()); } @@ -174,8 +179,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 +193,61 @@ 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 super.getResources(); } @Override - public ResourceInformation getResourceInformation(String resource) { + public ResourceInformation getResourceInformation(String resource) + throws YarnException { initResources(); - if (this.resources.containsKey(resource)) { - return this.resources.get(resource); - } - throw new ResourceNotFoundException("Could not find entry for " + resource); + return super.getResourceInformation(resource); } @Override - public Long getResourceValue(String resource) { + public long getResourceValue(String resource) throws YarnException { initResources(); - if (this.resources.containsKey(resource)) { - return this.resources.get(resource).getValue(); - } - throw new ResourceNotFoundException("Could not find entry for " + resource); + return super.getResourceValue(resource); } private void initResourcesMap() { 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]; + readOnlyResources = new ResourceInformation[types.length]; + for (ResourceInformation entry : types) { + int index = ResourceUtils.getResourceTypeIndex().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 (ResourceInformation resInfo : resources) { + 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/DominantResourceCalculator.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java index 79bb03d..6aece65 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/DominantResourceCalculator.java @@ -26,7 +26,6 @@ import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.util.UnitsConversionUtil; -import java.util.Set; /** * A {@link ResourceCalculator} which uses the concept of @@ -56,10 +55,11 @@ LogFactory.getLog(DominantResourceCalculator.class); - private Set resourceNames; + private String[] resourceNames; public DominantResourceCalculator() { - resourceNames = ResourceUtils.getResourceTypes().keySet(); + ResourceUtils.getResourceTypes(); + resourceNames = ResourceUtils.getResourceNamesArray(); } /** @@ -206,14 +206,9 @@ public float divide(Resource clusterResource, @Override public boolean isInvalidDivisor(Resource r) { - for (String resource : resourceNames) { - try { - if (r.getResourceValue(resource).equals(0L)) { - return true; - } - } catch (YarnException ye) { - throw new IllegalArgumentException( - "Error getting resource value for " + resource, ye); + for (ResourceInformation res : r.getResources()) { + if (res.getValue() == 0L) { + return true; } } return false; @@ -372,25 +367,26 @@ private Resource multiplyAndNormalize(Resource r, double by, Resource ret = Resource.newInstance(r); for (String resource : resourceNames) { try { - ResourceInformation rResourceInformation = - r.getResourceInformation(resource); - ResourceInformation stepFactorResourceInformation = - stepFactor.getResourceInformation(resource); + ResourceInformation rResourceInformation = r + .getResourceInformation(resource); + ResourceInformation stepFactorResourceInformation = stepFactor + .getResourceInformation(resource); ResourceInformation tmp = ret.getResourceInformation(resource); - Long rValue = rResourceInformation.getValue(); - Long stepFactorValue = UnitsConversionUtil - .convert(stepFactorResourceInformation.getUnits(), - rResourceInformation.getUnits(), - stepFactorResourceInformation.getValue()); - Long value; + long rValue = rResourceInformation.getValue(); + long stepFactorValue = UnitsConversionUtil.convert( + stepFactorResourceInformation.getUnits(), + rResourceInformation.getUnits(), + stepFactorResourceInformation.getValue()); + long value; if (stepFactorValue != 0) { - value = roundUp ? - roundUp((long) Math.ceil(rValue * by), stepFactorValue) : - roundDown((long) (rValue * by), stepFactorValue); + value = roundUp + ? roundUp((long) Math.ceil(rValue * by), stepFactorValue) + : roundDown((long) (rValue * by), stepFactorValue); } else { - value = - roundUp ? (long) Math.ceil(rValue * by) : (long) (rValue * by); + value = roundUp + ? (long) Math.ceil(rValue * by) + : (long) (rValue * by); } tmp.setValue(value); } catch (YarnException ye) { 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..fe268a4 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 @@ -22,12 +22,12 @@ 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.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 org.apache.hadoop.yarn.util.UnitsConversionUtil; -import java.util.Collections; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -39,9 +39,9 @@ * Helper class to create a resource with a fixed value for all resource * types. For example, a NONE resource which returns 0 for any resource type. */ - static class FixedValueResource extends Resource { + static class FixedValueResource extends BaseResource { - private Map resources; + private Map resourcesMap; private long resourceValue; private String name; @@ -53,7 +53,10 @@ FixedValueResource(String rName, long value) { this.resourceValue = value; this.name = rName; - resources = initResourceMap(); + resourcesMap = initResourceMap(); + resources = (ResourceInformation[]) this.resourcesMap.values() + .toArray(new ResourceInformation[this.resourcesMap.size()]); + readOnlyResources = Arrays.copyOf(resources, resources.length); } private int resourceValueToInt() { @@ -96,31 +99,6 @@ public void setVirtualCores(int virtualCores) { } @Override - public Map getResources() { - return Collections.unmodifiableMap(this.resources); - } - - @Override - public ResourceInformation getResourceInformation(String resource) - throws YarnException { - if (resources.containsKey(resource)) { - ResourceInformation value = this.resources.get(resource); - ResourceInformation ret = ResourceInformation.newInstance(value); - ret.setValue(resourceValue); - return ret; - } - throw new YarnException("" + resource + " not found"); - } - - @Override - public Long getResourceValue(String resource) throws YarnException { - if (resources.containsKey(resource)) { - return resourceValue; - } - throw new YarnException("" + resource + " not found"); - } - - @Override public void setResourceInformation(String resource, ResourceInformation resourceInformation) throws ResourceNotFoundException { @@ -135,12 +113,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 +128,6 @@ public void setResourceValue(String resource, Long value) } return tmp; } - } public static Resource createResource(int memory) { @@ -197,15 +174,16 @@ 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(); - long convertedRhs = UnitsConversionUtil - .convert(rhsValue.getUnits(), lhsValue.getUnits(), - rhsValue.getValue()); + ResourceInformation lhsValue = entry; + + long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) + ? rhsValue.getValue() + : UnitsConversionUtil.convert(rhsValue.getUnits(), + lhsValue.getUnits(), rhsValue.getValue()); lhs.setResourceValue(name, lhsValue.getValue() + convertedRhs); } catch (YarnException ye) { continue; @@ -219,15 +197,16 @@ 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(); - long convertedRhs = UnitsConversionUtil - .convert(rhsValue.getUnits(), lhsValue.getUnits(), - rhsValue.getValue()); + ResourceInformation lhsValue = entry; + + long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) + ? rhsValue.getValue() + : UnitsConversionUtil.convert(rhsValue.getUnits(), + lhsValue.getUnits(), rhsValue.getValue()); lhs.setResourceValue(name, lhsValue.getValue() - convertedRhs); } catch (YarnException ye) { continue; @@ -263,10 +242,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,15 +260,18 @@ 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(); - long convertedRhs = (long) (UnitsConversionUtil - .convert(rhsValue.getUnits(), lhsValue.getUnits(), - rhsValue.getValue()) * by); + ResourceInformation lhsValue = entry; + + long convertedRhs = (long) (((rhsValue.getUnits() + .equals(lhsValue.getUnits())) + ? rhsValue.getValue() + : UnitsConversionUtil.convert(rhsValue.getUnits(), + lhsValue.getUnits(), rhsValue.getValue())) + * by); lhs.setResourceValue(name, lhsValue.getValue() + convertedRhs); } catch (YarnException ye) { continue; @@ -311,10 +292,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,15 +396,16 @@ 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(); - long convertedRhs = UnitsConversionUtil - .convert(rhsValue.getUnits(), lhsValue.getUnits(), - rhsValue.getValue()); + ResourceInformation lhsValue = entry; + + long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) + ? rhsValue.getValue() + : UnitsConversionUtil.convert(rhsValue.getUnits(), + lhsValue.getUnits(), rhsValue.getValue()); if(lhsValue.getValue() > convertedRhs) { return false; } @@ -442,15 +423,16 @@ 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(); - long convertedRhs = UnitsConversionUtil - .convert(rhsValue.getUnits(), lhsValue.getUnits(), - rhsValue.getValue()); + ResourceInformation lhsValue = entry; + + long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) + ? rhsValue.getValue() + : UnitsConversionUtil.convert(rhsValue.getUnits(), + lhsValue.getUnits(), rhsValue.getValue()); ResourceInformation outInfo = lhsValue.getValue() < convertedRhs ? lhsValue : rhsValue; ret.setResourceInformation(name, outInfo); @@ -463,15 +445,16 @@ 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(); - long convertedRhs = UnitsConversionUtil - .convert(rhsValue.getUnits(), lhsValue.getUnits(), - rhsValue.getValue()); + ResourceInformation lhsValue = entry; + + long convertedRhs = (rhsValue.getUnits().equals(lhsValue.getUnits())) + ? rhsValue.getValue() + : UnitsConversionUtil.convert(rhsValue.getUnits(), + lhsValue.getUnits(), rhsValue.getValue()); ResourceInformation outInfo = lhsValue.getValue() > convertedRhs ? lhsValue : rhsValue; ret.setResourceInformation(name, outInfo); 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..b530150 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 @@ -276,13 +276,17 @@ 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()) { + 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..8ea3192 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,6 +22,8 @@ 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.Before; import org.junit.Test; @@ -57,8 +59,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() { @@ -246,7 +251,9 @@ public void testMultiplyAndRoundDown() { } @Test - public void testMultiplyAndAddTo() { + public void testMultiplyAndAddTo() throws Exception { + unsetExtraResourceType(); + setupExtraResourceType(); assertEquals(createResource(6, 4), multiplyAndAddTo(createResource(3, 1), createResource(2, 2), 1.5)); assertEquals(createResource(6, 4, 0), 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)