Skip to content

Commit 6d970d8

Browse files
committed
YARN-10012. Guaranteed and max capacity queue metrics for custom resources. Contributed by Manikandan R
1 parent cd4779f commit 6d970d8

File tree

6 files changed

+307
-39
lines changed

6 files changed

+307
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.hadoop.yarn.server.resourcemanager.scheduler;
18+
19+
import org.apache.hadoop.yarn.api.records.Resource;
20+
21+
import java.util.Map;
22+
23+
/**
24+
* This class is a main entry-point for any kind of CSQueueMetrics for
25+
* custom resources.
26+
* It provides increase and decrease methods for all types of metrics.
27+
*/
28+
public class CSQueueMetricsForCustomResources
29+
extends QueueMetricsForCustomResources {
30+
private final QueueMetricsCustomResource guaranteedCapacity =
31+
new QueueMetricsCustomResource();
32+
private final QueueMetricsCustomResource maxCapacity =
33+
new QueueMetricsCustomResource();
34+
35+
public void setGuaranteedCapacity(Resource res) {
36+
guaranteedCapacity.set(res);
37+
}
38+
39+
public void setMaxCapacity(Resource res) {
40+
maxCapacity.set(res);
41+
}
42+
43+
public Map<String, Long> getGuaranteedCapacity() {
44+
return guaranteedCapacity.getValues();
45+
}
46+
47+
public Map<String, Long> getMaxCapacity() {
48+
return maxCapacity.getValues();
49+
}
50+
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/QueueMetrics.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,7 @@ protected QueueMetrics(MetricsSystem ms, String queueName, Queue parent,
159159
this.conf = conf;
160160
runningTime = buildBuckets(conf);
161161

162-
if (ResourceUtils.getNumberOfKnownResourceTypes() > 2) {
163-
this.queueMetricsForCustomResources =
164-
new QueueMetricsForCustomResources();
165-
registerCustomResources();
166-
}
162+
createQueueMetricsForCustomResources();
167163
}
168164

169165
protected QueueMetrics tag(MetricsInfo info, String value) {
@@ -449,25 +445,33 @@ public void incrPendingResources(String partition, String user,
449445
}
450446
}
451447

448+
protected Map<String, Long> initAndGetCustomResources() {
449+
Map<String, Long> customResources = new HashMap<String, Long>();
450+
ResourceInformation[] resources = ResourceUtils.getResourceTypesArray();
451+
452+
for (int i = 2; i < resources.length; i++) {
453+
ResourceInformation resource = resources[i];
454+
customResources.put(resource.getName(), Long.valueOf(0));
455+
}
456+
return customResources;
457+
}
458+
459+
protected void createQueueMetricsForCustomResources() {
460+
if (ResourceUtils.getNumberOfKnownResourceTypes() > 2) {
461+
this.queueMetricsForCustomResources =
462+
new QueueMetricsForCustomResources();
463+
registerCustomResources();
464+
}
465+
}
466+
452467
/**
453468
* Register all custom resources metrics as part of initialization. As and
454469
* when this metric object construction happens for any queue, all custom
455470
* resource metrics value would be initialized with '0' like any other
456471
* mandatory resources metrics
457472
*/
458-
private void registerCustomResources() {
459-
Map<String, Long> customResources =
460-
new HashMap<String, Long>();
461-
ResourceInformation[] resources =
462-
ResourceUtils.getResourceTypesArray();
463-
464-
for (int i =
465-
2; i < resources.length; i++) {
466-
ResourceInformation resource =
467-
resources[i];
468-
customResources.put(resource.getName(), Long.valueOf(0));
469-
}
470-
473+
protected void registerCustomResources() {
474+
Map<String, Long> customResources = initAndGetCustomResources();
471475
registerCustomResources(customResources, ALLOCATED_RESOURCE_METRIC_PREFIX,
472476
ALLOCATED_RESOURCE_METRIC_DESC);
473477
registerCustomResources(customResources, AVAILABLE_RESOURCE_METRIC_PREFIX,
@@ -481,7 +485,7 @@ private void registerCustomResources() {
481485
AGGREGATE_PREEMPTED_SECONDS_METRIC_DESC);
482486
}
483487

484-
private void registerCustomResources(Map<String, Long> customResources,
488+
protected void registerCustomResources(Map<String, Long> customResources,
485489
String metricPrefix, String metricDesc) {
486490
for (Entry<String, Long> entry : customResources.entrySet()) {
487491
String resourceName = entry.getKey();
@@ -1007,4 +1011,14 @@ public void fillInValuesFromAvailableResources(Resource fromResource,
10071011
}
10081012
}
10091013
}
1014+
1015+
@VisibleForTesting
1016+
public QueueMetricsForCustomResources getQueueMetricsForCustomResources() {
1017+
return this.queueMetricsForCustomResources;
1018+
}
1019+
1020+
public void setQueueMetricsForCustomResources(
1021+
QueueMetricsForCustomResources metrics) {
1022+
this.queueMetricsForCustomResources = metrics;
1023+
}
10101024
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CSQueueMetrics.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity;
2020

21+
import java.util.Map;
22+
2123
import org.apache.hadoop.conf.Configuration;
2224
import org.apache.hadoop.metrics2.MetricsSystem;
2325
import org.apache.hadoop.metrics2.annotation.Metric;
@@ -28,8 +30,10 @@
2830
import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
2931
import org.apache.hadoop.yarn.api.records.Resource;
3032
import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager;
33+
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.CSQueueMetricsForCustomResources;
3134
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.Queue;
3235
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.QueueMetrics;
36+
import org.apache.hadoop.yarn.util.resource.ResourceUtils;
3337

3438
@Metrics(context = "yarn")
3539
public class CSQueueMetrics extends QueueMetrics {
@@ -64,11 +68,36 @@ public class CSQueueMetrics extends QueueMetrics {
6468
@Metric("Maximum capacity in percentage relative to total partition")
6569
private MutableGaugeFloat maxAbsoluteCapacity;
6670

71+
private static final String GUARANTEED_CAPACITY_METRIC_PREFIX =
72+
"GuaranteedCapacity.";
73+
private static final String GUARANTEED_CAPACITY_METRIC_DESC =
74+
"GuaranteedCapacity of NAME";
75+
76+
private static final String MAX_CAPACITY_METRIC_PREFIX =
77+
"MaxCapacity.";
78+
private static final String MAX_CAPACITY_METRIC_DESC =
79+
"MaxCapacity of NAME";
80+
6781
CSQueueMetrics(MetricsSystem ms, String queueName, Queue parent,
6882
boolean enableUserMetrics, Configuration conf) {
6983
super(ms, queueName, parent, enableUserMetrics, conf);
7084
}
7185

86+
/**
87+
* Register all custom resources metrics as part of initialization. As and
88+
* when this metric object construction happens for any queue, all custom
89+
* resource metrics value would be initialized with '0' like any other
90+
* mandatory resources metrics
91+
*/
92+
protected void registerCustomResources() {
93+
Map<String, Long> customResources = initAndGetCustomResources();
94+
registerCustomResources(customResources, GUARANTEED_CAPACITY_METRIC_PREFIX,
95+
GUARANTEED_CAPACITY_METRIC_DESC);
96+
registerCustomResources(customResources, MAX_CAPACITY_METRIC_PREFIX,
97+
MAX_CAPACITY_METRIC_DESC);
98+
super.registerCustomResources();
99+
}
100+
72101
public long getAMResourceLimitMB() {
73102
return AMResourceLimitMB.value();
74103
}
@@ -155,6 +184,14 @@ public void setGuaranteedResources(String partition, Resource res) {
155184
if (partition == null || partition.equals(RMNodeLabelsManager.NO_LABEL)) {
156185
guaranteedMB.set(res.getMemorySize());
157186
guaranteedVCores.set(res.getVirtualCores());
187+
if (getQueueMetricsForCustomResources() != null) {
188+
((CSQueueMetricsForCustomResources) getQueueMetricsForCustomResources())
189+
.setGuaranteedCapacity(res);
190+
registerCustomResources(
191+
((CSQueueMetricsForCustomResources)
192+
getQueueMetricsForCustomResources()).getGuaranteedCapacity(),
193+
GUARANTEED_CAPACITY_METRIC_PREFIX, GUARANTEED_CAPACITY_METRIC_DESC);
194+
}
158195
}
159196
}
160197

@@ -170,6 +207,22 @@ public void setMaxCapacityResources(String partition, Resource res) {
170207
if (partition == null || partition.equals(RMNodeLabelsManager.NO_LABEL)) {
171208
maxCapacityMB.set(res.getMemorySize());
172209
maxCapacityVCores.set(res.getVirtualCores());
210+
if (getQueueMetricsForCustomResources() != null) {
211+
((CSQueueMetricsForCustomResources) getQueueMetricsForCustomResources())
212+
.setMaxCapacity(res);
213+
registerCustomResources(
214+
((CSQueueMetricsForCustomResources)
215+
getQueueMetricsForCustomResources()).getMaxCapacity(),
216+
MAX_CAPACITY_METRIC_PREFIX, MAX_CAPACITY_METRIC_DESC);
217+
}
218+
}
219+
}
220+
221+
@Override
222+
protected void createQueueMetricsForCustomResources() {
223+
if (ResourceUtils.getNumberOfKnownResourceTypes() > 2) {
224+
setQueueMetricsForCustomResources(new CSQueueMetricsForCustomResources());
225+
registerCustomResources();
173226
}
174227
}
175228

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockNodes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.hadoop.yarn.factories.RecordFactory;
3737
import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider;
3838
import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager;
39+
import org.apache.hadoop.yarn.resourcetypes.ResourceTypesTestHelper;
3940
import org.apache.hadoop.yarn.server.api.protocolrecords.NodeHeartbeatResponse;
4041
import org.apache.hadoop.yarn.server.api.records.OpportunisticContainersStatus;
4142
import org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNode;
@@ -92,6 +93,12 @@ public static Resource newResource(int mem) {
9293
return rs;
9394
}
9495

96+
public static Resource newResource(long memory, int vCores,
97+
Map<String, String> customResources) {
98+
return ResourceTypesTestHelper.newResource(memory, vCores, customResources);
99+
}
100+
101+
95102
public static Resource newUsedResource(Resource total) {
96103
Resource rs = recordFactory.newRecordInstance(Resource.class);
97104
rs.setMemorySize((int)(Math.random() * total.getMemorySize()));

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestQueueMetricsForCustomResources.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public enum MetricsForCustomResource {
9393

9494
public static final long GB = 1024; // MB
9595
private static final Configuration CONF = new Configuration();
96-
private static final String CUSTOM_RES_1 = "custom_res_1";
97-
private static final String CUSTOM_RES_2 = "custom_res_2";
96+
public static final String CUSTOM_RES_1 = "custom_res_1";
97+
public static final String CUSTOM_RES_2 = "custom_res_2";
9898
public static final String USER = "alice";
9999
private Resource defaultResource;
100100
private MetricsSystem ms;

0 commit comments

Comments
 (0)