2020
2121import static org .apache .hadoop .metrics2 .lib .Interns .info ;
2222
23+ import java .util .Map ;
2324import java .util .concurrent .atomic .AtomicBoolean ;
2425
2526import org .apache .hadoop .classification .InterfaceAudience ;
3536import org .apache .hadoop .yarn .api .records .Resource ;
3637import org .apache .hadoop .thirdparty .com .google .common .annotations .VisibleForTesting ;
3738import org .apache .hadoop .yarn .api .records .ResourceInformation ;
39+ import org .apache .hadoop .yarn .metrics .CustomResourceMetricValue ;
40+ import org .apache .hadoop .yarn .metrics .CustomResourceMetrics ;
41+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .QueueMetricsForCustomResources ;
3842import org .apache .hadoop .yarn .util .resource .ResourceUtils ;
3943
4044@ InterfaceAudience .Private
@@ -58,10 +62,19 @@ public class ClusterMetrics {
5862 @ Metric ("Vcore Utilization" ) MutableGaugeLong utilizedVirtualCores ;
5963 @ Metric ("Memory Capability" ) MutableGaugeLong capabilityMB ;
6064 @ Metric ("Vcore Capability" ) MutableGaugeLong capabilityVirtualCores ;
61- @ Metric ("GPU Capability" ) MutableGaugeLong capabilityGPUs ;
6265
6366 private static final MetricsInfo RECORD_INFO = info ("ClusterMetrics" ,
6467 "Metrics for the Yarn Cluster" );
68+
69+ private static final String CUSTOM_RESOURCE_CAPABILITY_METRIC_PREFIX =
70+ "Capability." ;
71+ private static final String CUSTOM_RESOURCE_CAPABILITY_METRIC_DESC =
72+ "NAME Capability" ;
73+
74+ private static CustomResourceMetrics customResourceMetrics ;
75+
76+ private final CustomResourceMetricValue customResourceCapability =
77+ new CustomResourceMetricValue ();
6578
6679 private static volatile ClusterMetrics INSTANCE = null ;
6780 private static MetricsRegistry registry ;
@@ -86,6 +99,17 @@ private static void registerMetrics() {
8699 if (ms != null ) {
87100 ms .register ("ClusterMetrics" , "Metrics for the Yarn Cluster" , INSTANCE );
88101 }
102+
103+ if (ResourceUtils .getNumberOfKnownResourceTypes () > 2 ) {
104+ customResourceMetrics =
105+ new CustomResourceMetrics ();
106+ Map <String , Long > customResources =
107+ customResourceMetrics .initAndGetCustomResources ();
108+ customResourceMetrics .
109+ registerCustomResources (customResources ,
110+ registry , CUSTOM_RESOURCE_CAPABILITY_METRIC_PREFIX ,
111+ CUSTOM_RESOURCE_CAPABILITY_METRIC_DESC );
112+ }
89113 }
90114
91115 @ VisibleForTesting
@@ -209,23 +233,20 @@ public long getCapabilityVirtualCores() {
209233 return capabilityVirtualCores .value ();
210234 }
211235
212- public long getCapabilityGPUs () {
213- if (capabilityGPUs == null ) {
214- return 0 ;
215- }
236+ public Map <String , Long > getCustomResourceCapability () {
237+ return customResourceCapability .getValues ();
238+ }
216239
217- return capabilityGPUs .value ();
240+ public void setCustomResourceCapability (Resource res ) {
241+ this .customResourceCapability .set (res );
218242 }
219243
220244 public void incrCapability (Resource res ) {
221245 if (res != null ) {
222246 capabilityMB .incr (res .getMemorySize ());
223247 capabilityVirtualCores .incr (res .getVirtualCores ());
224- Integer gpuIndex = ResourceUtils .getResourceTypeIndex ()
225- .get (ResourceInformation .GPU_URI );
226- if (gpuIndex != null ) {
227- capabilityGPUs .incr (res .
228- getResourceValue (ResourceInformation .GPU_URI ));
248+ if (customResourceCapability != null ) {
249+ customResourceCapability .increase (res );
229250 }
230251 }
231252 }
@@ -234,11 +255,8 @@ public void decrCapability(Resource res) {
234255 if (res != null ) {
235256 capabilityMB .decr (res .getMemorySize ());
236257 capabilityVirtualCores .decr (res .getVirtualCores ());
237- Integer gpuIndex = ResourceUtils .getResourceTypeIndex ()
238- .get (ResourceInformation .GPU_URI );
239- if (gpuIndex != null ) {
240- capabilityGPUs .decr (res .
241- getResourceValue (ResourceInformation .GPU_URI ));
258+ if (customResourceCapability != null ) {
259+ customResourceCapability .decrease (res );
242260 }
243261 }
244262 }
0 commit comments