diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/FederationClientInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/FederationClientInterceptor.java index de80a35f49b78..4ea97cdfc9362 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/FederationClientInterceptor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/FederationClientInterceptor.java @@ -703,6 +703,12 @@ public GetApplicationsResponse getApplications(GetApplicationsRequest request) @Override public GetClusterMetricsResponse getClusterMetrics( GetClusterMetricsRequest request) throws YarnException, IOException { + if(request == null) { + routerMetrics.incrGetClusterMetricsFailedRetrieved(); + RouterServerUtil.logAndThrowException( + "Missing getClusterMetrics request.", + null); + } long startTime = clock.getTime(); Map subclusters = federationFacade.getSubClusters(true); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestFederationClientInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestFederationClientInterceptor.java index 74d10a44c52b5..7a8ce96162e68 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestFederationClientInterceptor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestFederationClientInterceptor.java @@ -516,14 +516,14 @@ public void testGetApplicationAttemptEmptyRequest() @Test - public void testGetClusterMetricsRequest() throws YarnException, IOException { + public void testGetClusterMetricsRequest() throws Exception { LOG.info("Test FederationClientInterceptor : Get Cluster Metrics request"); // null request - GetClusterMetricsResponse response = interceptor.getClusterMetrics(null); - Assert.assertEquals(subClusters.size(), - response.getClusterMetrics().getNumNodeManagers()); + LambdaTestUtils.intercept(YarnException.class, + "Missing getClusterMetrics request.", () -> interceptor.getClusterMetrics(null)); + // normal request. - response = + GetClusterMetricsResponse response = interceptor.getClusterMetrics(GetClusterMetricsRequest.newInstance()); Assert.assertEquals(subClusters.size(), response.getClusterMetrics().getNumNodeManagers()); @@ -641,4 +641,25 @@ public void testGetApplicationsApplicationStateNotExists() throws Exception{ Assert.assertNotNull(responseGet); Assert.assertTrue(responseGet.getApplicationList().isEmpty()); } + + /** + * This test validates that if the request is null, + * calling the interface for obtaining cluster metrics should throw an exception, + * verifying that the exception is as expected. + * + * @throws YarnException + * @throws IOException + */ + @Test + public void testGetClusterMetricsRequestNull() throws YarnException, IOException { + LOG.info( + "Test FederationClientInterceptor: GetClusterMetrics - Empty"); + try { + interceptor.getClusterMetrics(null); + Assert.fail(); + } catch (YarnException e) { + Assert.assertTrue( + e.getMessage().startsWith("Missing getClusterMetrics request")); + } + } }