From 680af873777f7fce0224781b9ecc547f8f8ded77 Mon Sep 17 00:00:00 2001
From: slfan1989 <55643692+slfan1989@users.noreply.github.com>
Date: Wed, 12 Jul 2023 09:47:59 +0800
Subject: [PATCH] YARN-11515. [Federation] Improve
 DefaultRequestInterceptor#init Code. (#5752)

---
 .../DefaultClientRequestInterceptor.java      | 28 +++++++-------
 .../DefaultRMAdminRequestInterceptor.java     | 37 ++++---------------
 .../webapp/DefaultRequestInterceptorREST.java |  1 +
 .../MockDefaultRequestInterceptorREST.java    | 20 +++++-----
 4 files changed, 34 insertions(+), 52 deletions(-)

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/DefaultClientRequestInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/DefaultClientRequestInterceptor.java
index 8defc73b3ea27..e7cc024b64001 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/DefaultClientRequestInterceptor.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/DefaultClientRequestInterceptor.java
@@ -98,6 +98,8 @@
 import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
 
 import org.apache.hadoop.classification.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Extends the {@code AbstractRequestInterceptorClient} class and provides an
@@ -107,25 +109,26 @@
  */
 public class DefaultClientRequestInterceptor
     extends AbstractClientRequestInterceptor {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(DefaultClientRequestInterceptor.class);
   private ApplicationClientProtocol clientRMProxy;
 
   @Override
   public void init(String userName) {
     super.init(userName);
-
-    final Configuration conf = this.getConf();
     try {
-      clientRMProxy =
-          user.doAs(new PrivilegedExceptionAction<ApplicationClientProtocol>() {
-            @Override
-            public ApplicationClientProtocol run() throws Exception {
-              return ClientRMProxy.createRMProxy(conf,
-                  ApplicationClientProtocol.class);
-            }
-          });
+      final Configuration conf = this.getConf();
+      clientRMProxy = user.doAs(
+          (PrivilegedExceptionAction<ApplicationClientProtocol>) () ->
+               ClientRMProxy.createRMProxy(conf, ApplicationClientProtocol.class));
     } catch (Exception e) {
-      throw new YarnRuntimeException(
-          "Unable to create the interface to reach the YarnRM", e);
+      StringBuilder message = new StringBuilder();
+      message.append("Error while creating Router RMClient Service");
+      if (user != null) {
+        message.append(", user: " + user);
+      }
+      LOG.error(message.toString(), e);
+      throw new YarnRuntimeException(message.toString(), e);
     }
   }
 
@@ -355,6 +358,5 @@ public GetNodesToAttributesResponse getNodesToAttributes(
   @VisibleForTesting
   public void setRMClient(ApplicationClientProtocol clientRM) {
     this.clientRMProxy = clientRM;
-
   }
 }
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/rmadmin/DefaultRMAdminRequestInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/rmadmin/DefaultRMAdminRequestInterceptor.java
index 8388af66d288e..df5b4d5835df9 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/rmadmin/DefaultRMAdminRequestInterceptor.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/rmadmin/DefaultRMAdminRequestInterceptor.java
@@ -80,38 +80,18 @@ public class DefaultRMAdminRequestInterceptor
   public void init(String userName) {
     super.init(userName);
     try {
-      // Do not create a proxy user if user name matches the user name on
-      // current UGI
-      if (UserGroupInformation.isSecurityEnabled()) {
-        user = UserGroupInformation.createProxyUser(userName, UserGroupInformation.getLoginUser());
-      } else if (userName.equalsIgnoreCase(UserGroupInformation.getCurrentUser().getUserName())) {
-        user = UserGroupInformation.getCurrentUser();
-      } else {
-        user = UserGroupInformation.createProxyUser(userName,
-            UserGroupInformation.getCurrentUser());
-      }
-
       final Configuration conf = this.getConf();
-
       rmAdminProxy = user.doAs(
-          new PrivilegedExceptionAction<ResourceManagerAdministrationProtocol>() {
-            @Override
-            public ResourceManagerAdministrationProtocol run()
-                throws Exception {
-              return ClientRMProxy.createRMProxy(conf,
-                  ResourceManagerAdministrationProtocol.class);
-            }
-          });
-    } catch (IOException e) {
-      String message = "Error while creating Router RMAdmin Service for user:";
+          (PrivilegedExceptionAction<ResourceManagerAdministrationProtocol>) () ->
+               ClientRMProxy.createRMProxy(conf, ResourceManagerAdministrationProtocol.class));
+    } catch (Exception e) {
+      StringBuilder message = new StringBuilder();
+      message.append("Error while creating Router RMAdmin Service");
       if (user != null) {
-        message += ", user: " + user;
+        message.append(", user: " + user);
       }
-
-      LOG.info(message);
-      throw new YarnRuntimeException(message, e);
-    } catch (Exception e) {
-      throw new YarnRuntimeException(e);
+      LOG.error(message.toString(), e);
+      throw new YarnRuntimeException(message.toString(), e);
     }
   }
 
@@ -126,7 +106,6 @@ public void setNextInterceptor(RMAdminRequestInterceptor next) {
   @VisibleForTesting
   public void setRMAdmin(ResourceManagerAdministrationProtocol rmAdmin) {
     this.rmAdminProxy = rmAdmin;
-
   }
 
   @Override
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/DefaultRequestInterceptorREST.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/DefaultRequestInterceptorREST.java
index 918865da4675e..9d3e3be6f6e53 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/DefaultRequestInterceptorREST.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/DefaultRequestInterceptorREST.java
@@ -103,6 +103,7 @@ protected SubClusterId getSubClusterId() {
 
   @Override
   public void init(String user) {
+    super.init(user);
     webAppAddress = WebAppUtils.getRMWebAppURLWithScheme(getConf());
     client = RouterWebServiceUtil.createJerseyClient(getConf());
   }
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/webapp/MockDefaultRequestInterceptorREST.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/webapp/MockDefaultRequestInterceptorREST.java
index d4e1b5145cf0d..f162ab6be6593 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/webapp/MockDefaultRequestInterceptorREST.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/webapp/MockDefaultRequestInterceptorREST.java
@@ -211,7 +211,7 @@ public Response createNewApplication(HttpServletRequest hsr)
     validateRunning();
 
     ApplicationId applicationId =
-        ApplicationId.newInstance(Integer.valueOf(getSubClusterId().getId()),
+        ApplicationId.newInstance(Integer.parseInt(getSubClusterId().getId()),
             applicationCounter.incrementAndGet());
     NewApplication appId =
         new NewApplication(applicationId.toString(), new ResourceInfo());
@@ -275,7 +275,7 @@ public AppsInfo getApps(HttpServletRequest hsr, String stateQuery,
     AppInfo appInfo = new AppInfo();
 
     appInfo.setAppId(
-        ApplicationId.newInstance(Integer.valueOf(getSubClusterId().getId()),
+        ApplicationId.newInstance(Integer.parseInt(getSubClusterId().getId()),
             applicationCounter.incrementAndGet()).toString());
     appInfo.setAMHostHttpAddress("http://i_am_the_AM:1234");
 
@@ -316,7 +316,7 @@ public NodeInfo getNode(String nodeId) {
     if (nodeId.contains(subClusterId) || nodeId.contains("test")) {
       node = new NodeInfo();
       node.setId(nodeId);
-      node.setLastHealthUpdate(Integer.valueOf(getSubClusterId().getId()));
+      node.setLastHealthUpdate(Integer.parseInt(getSubClusterId().getId()));
     }
     return node;
   }
@@ -328,7 +328,7 @@ public NodesInfo getNodes(String states) {
     }
     NodeInfo node = new NodeInfo();
     node.setId("Node " + Integer.valueOf(getSubClusterId().getId()));
-    node.setLastHealthUpdate(Integer.valueOf(getSubClusterId().getId()));
+    node.setLastHealthUpdate(Integer.parseInt(getSubClusterId().getId()));
     NodesInfo nodes = new NodesInfo();
     nodes.add(node);
     return nodes;
@@ -350,12 +350,12 @@ public ClusterMetricsInfo getClusterMetricsInfo() {
       throw new RuntimeException("RM is stopped");
     }
     ClusterMetricsInfo metrics = new ClusterMetricsInfo();
-    metrics.setAppsSubmitted(Integer.valueOf(getSubClusterId().getId()));
-    metrics.setAppsCompleted(Integer.valueOf(getSubClusterId().getId()));
-    metrics.setAppsPending(Integer.valueOf(getSubClusterId().getId()));
-    metrics.setAppsRunning(Integer.valueOf(getSubClusterId().getId()));
-    metrics.setAppsFailed(Integer.valueOf(getSubClusterId().getId()));
-    metrics.setAppsKilled(Integer.valueOf(getSubClusterId().getId()));
+    metrics.setAppsSubmitted(Integer.parseInt(getSubClusterId().getId()));
+    metrics.setAppsCompleted(Integer.parseInt(getSubClusterId().getId()));
+    metrics.setAppsPending(Integer.parseInt(getSubClusterId().getId()));
+    metrics.setAppsRunning(Integer.parseInt(getSubClusterId().getId()));
+    metrics.setAppsFailed(Integer.parseInt(getSubClusterId().getId()));
+    metrics.setAppsKilled(Integer.parseInt(getSubClusterId().getId()));
 
     return metrics;
   }