From 7063b6b9c559aeff4b5f516d044237ac09eaf513 Mon Sep 17 00:00:00 2001 From: Sunil G Date: Wed, 22 Nov 2017 23:42:27 +0530 Subject: [PATCH] YARN-7538-YARN-5881 --- .../scheduler/AbstractResourceUsage.java | 66 ++++++++++++++-------- .../scheduler/capacity/AbstractCSQueue.java | 4 +- .../scheduler/capacity/CSQueueUtils.java | 8 +-- .../scheduler/capacity/ParentQueue.java | 4 +- 4 files changed, 51 insertions(+), 31 deletions(-) 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/AbstractResourceUsage.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractResourceUsage.java index c2953236f47..3b8a016e349 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractResourceUsage.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractResourceUsage.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; @@ -38,9 +39,11 @@ public class AbstractResourceUsage { protected ReadLock readLock; protected WriteLock writeLock; - protected Map usages; + protected final Map usages; + private final UsageByLabel noLabelUsages; // short for no-label :) - private static final String NL = CommonNodeLabelsManager.NO_LABEL; + private static String NL = CommonNodeLabelsManager.NO_LABEL; + private final UsageByLabel usageNoLabel; public AbstractResourceUsage() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); @@ -48,7 +51,11 @@ public AbstractResourceUsage() { writeLock = lock.writeLock(); usages = new HashMap(); - usages.put(NL, new UsageByLabel(NL)); + usageNoLabel = new UsageByLabel(NL); + usages.put(NL, usageNoLabel); + + // For default label, avoid map for faster access. + noLabelUsages = new UsageByLabel(NL); } // Usage enum here to make implement cleaner @@ -57,8 +64,7 @@ public AbstractResourceUsage() { // be written by ordering policies USED(0), PENDING(1), AMUSED(2), RESERVED(3), CACHED_USED(4), CACHED_PENDING( 5), AMLIMIT(6), MIN_RESOURCE(7), MAX_RESOURCE(8), EFF_MIN_RESOURCE( - 9), EFF_MAX_RESOURCE( - 10), EFF_MIN_RESOURCE_UP(11), EFF_MAX_RESOURCE_UP(12); + 9), EFF_MAX_RESOURCE(10); private int idx; @@ -69,29 +75,29 @@ private ResourceType(int value) { public static class UsageByLabel { // usage by label, contains all UsageType - private Resource[] resArr; + private final AtomicReferenceArray resArr; public UsageByLabel(String label) { - resArr = new Resource[ResourceType.values().length]; - for (int i = 0; i < resArr.length; i++) { - resArr[i] = Resource.newInstance(0, 0); - }; + resArr = new AtomicReferenceArray(ResourceType.values().length); + for (int i = 0; i < resArr.length(); i++) { + resArr.set(i, Resource.newInstance(0, 0)); + } ; } public Resource getUsed() { - return resArr[ResourceType.USED.idx]; + return resArr.get(ResourceType.USED.idx); } @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append("{used=" + resArr[0] + "%, "); - sb.append("pending=" + resArr[1] + "%, "); - sb.append("am_used=" + resArr[2] + "%, "); - sb.append("reserved=" + resArr[3] + "%}"); - sb.append("min_eff=" + resArr[9] + "%, "); - sb.append("max_eff=" + resArr[10] + "%}"); - sb.append("min_effup=" + resArr[11] + "%, "); + sb.append("{used=" + resArr.get(0) + "%, "); + sb.append("pending=" + resArr.get(1) + "%, "); + sb.append("am_used=" + resArr.get(2) + "%, "); + sb.append("reserved=" + resArr.get(3) + "%}"); + sb.append("min_eff=" + resArr.get(9) + "%, "); + sb.append("max_eff=" + resArr.get(10) + "%}"); + sb.append("min_effup=" + resArr.get(11) + "%, "); return sb.toString(); } } @@ -108,13 +114,17 @@ protected Resource _get(String label, ResourceType type) { label = RMNodeLabelsManager.NO_LABEL; } + if (label.equals(RMNodeLabelsManager.NO_LABEL)) { + return normalize(noLabelUsages.resArr.get(type.idx)); + } + try { readLock.lock(); UsageByLabel usage = usages.get(label); if (null == usage) { return Resources.none(); } - return normalize(usage.resArr[type.idx]); + return normalize(usage.resArr.get(type.idx)); } finally { readLock.unlock(); } @@ -126,7 +136,7 @@ protected Resource _getAll(ResourceType type) { Resource allOfType = Resources.createResource(0); for (Map.Entry usageEntry : usages.entrySet()) { //all usages types are initialized - Resources.addTo(allOfType, usageEntry.getValue().resArr[type.idx]); + Resources.addTo(allOfType, usageEntry.getValue().resArr.get(type.idx)); } return allOfType; } finally { @@ -138,6 +148,11 @@ private UsageByLabel getAndAddIfMissing(String label) { if (label == null) { label = RMNodeLabelsManager.NO_LABEL; } + + if (label.equals(RMNodeLabelsManager.NO_LABEL)) { + return noLabelUsages; + } + if (!usages.containsKey(label)) { UsageByLabel u = new UsageByLabel(label); usages.put(label, u); @@ -148,10 +163,15 @@ private UsageByLabel getAndAddIfMissing(String label) { } protected void _set(String label, ResourceType type, Resource res) { + if (label.equals(RMNodeLabelsManager.NO_LABEL)) { + noLabelUsages.resArr.set(type.idx, res); + return; + } + try { writeLock.lock(); UsageByLabel usage = getAndAddIfMissing(label); - usage.resArr[type.idx] = res; + usage.resArr.set(type.idx, res); } finally { writeLock.unlock(); } @@ -161,7 +181,7 @@ protected void _inc(String label, ResourceType type, Resource res) { try { writeLock.lock(); UsageByLabel usage = getAndAddIfMissing(label); - Resources.addTo(usage.resArr[type.idx], res); + Resources.addTo(usage.resArr.get(type.idx), res); } finally { writeLock.unlock(); } @@ -171,7 +191,7 @@ protected void _dec(String label, ResourceType type, Resource res) { try { writeLock.lock(); UsageByLabel usage = getAndAddIfMissing(label); - Resources.subtractFrom(usage.resArr[type.idx], res); + Resources.subtractFrom(usage.resArr.get(type.idx), res); } finally { writeLock.unlock(); } 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/capacity/AbstractCSQueue.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/AbstractCSQueue.java index 9caf5891000..140ea5dc903 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/AbstractCSQueue.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/AbstractCSQueue.java @@ -932,7 +932,7 @@ public void incUsedResource(String nodeLabel, Resource resourceToInc, queueUsage.incUsed(nodeLabel, resourceToInc); CSQueueUtils.updateUsedCapacity(resourceCalculator, labelManager.getResourceByLabel(nodeLabel, Resources.none()), - Resources.none(), nodeLabel, this); + nodeLabel, this); if (null != parent) { parent.incUsedResource(nodeLabel, resourceToInc, null); } @@ -948,7 +948,7 @@ public void decUsedResource(String nodeLabel, Resource resourceToDec, queueUsage.decUsed(nodeLabel, resourceToDec); CSQueueUtils.updateUsedCapacity(resourceCalculator, labelManager.getResourceByLabel(nodeLabel, Resources.none()), - Resources.none(), nodeLabel, this); + nodeLabel, this); if (null != parent) { parent.decUsedResource(nodeLabel, resourceToDec, null); } 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/capacity/CSQueueUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CSQueueUtils.java index 81dec80d0eb..6daca5158a3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CSQueueUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CSQueueUtils.java @@ -180,8 +180,8 @@ private static void updateAbsoluteCapacitiesByNodeLabels( * used resource for all partitions of this queue. */ public static void updateUsedCapacity(final ResourceCalculator rc, - final Resource totalPartitionResource, Resource clusterResource, - String nodePartition, AbstractCSQueue childQueue) { + final Resource totalPartitionResource, String nodePartition, + AbstractCSQueue childQueue) { QueueCapacities queueCapacities = childQueue.getQueueCapacities(); CSQueueMetrics queueMetrics = childQueue.getMetrics(); ResourceUsage queueResourceUsage = childQueue.getQueueResourceUsage(); @@ -287,11 +287,11 @@ public static void updateQueueStatistics( for (String partition : Sets.union(queueCapacities.getNodePartitionsSet(), queueResourceUsage.getNodePartitionsSet())) { updateUsedCapacity(rc, nlm.getResourceByLabel(partition, cluster), - cluster, partition, childQueue); + partition, childQueue); } } else { updateUsedCapacity(rc, nlm.getResourceByLabel(nodePartition, cluster), - cluster, nodePartition, childQueue); + nodePartition, childQueue); } // Update queue metrics w.r.t node labels. In a generic way, we can 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/capacity/ParentQueue.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/ParentQueue.java index a427fb135ab..c198d135ed2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/ParentQueue.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/ParentQueue.java @@ -739,8 +739,8 @@ private ResourceLimits getResourceLimitsOfChild(CSQueue child, child.getQueueResourceUsage().getUsed(nodePartition)); // Get child's max resource - Resource childConfiguredMaxResource = getEffectiveMaxCapacityDown( - nodePartition, minimumAllocation); + Resource childConfiguredMaxResource = child + .getEffectiveMaxCapacityDown(nodePartition, minimumAllocation); // Child's limit should be capped by child configured max resource childLimit = -- 2.13.6 (Apple Git-96)