From 29c4d8d8f70b98ee7896ca2affbc6f5a2530672f Mon Sep 17 00:00:00 2001 From: slfan1989 <55643692+slfan1989@users.noreply.github.com> Date: Fri, 9 Sep 2022 02:42:36 +0800 Subject: [PATCH] YARN-11298. Improve Yarn Router Junit Test Close MockRM. (#4870) --- .../MockClientRequestInterceptor.java | 11 ++++++- .../TestableFederationClientInterceptor.java | 32 +++++++++++++++++++ .../MockRMAdminRequestInterceptor.java | 11 ++++++- .../secure/AbstractSecureRouterTest.java | 10 ++++++ 4 files changed, 62 insertions(+), 2 deletions(-) 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/MockClientRequestInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/MockClientRequestInterceptor.java index e0538b8a42..c85bbd6f2e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/MockClientRequestInterceptor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/MockClientRequestInterceptor.java @@ -31,8 +31,10 @@ public class MockClientRequestInterceptor extends DefaultClientRequestInterceptor { + MockRM mockRM = null; + public void init(String user) { - MockRM mockRM = new MockRM(super.getConf()) { + mockRM = new MockRM(super.getConf()) { @Override protected ClientRMService createClientRMService() { return new ClientRMService(getRMContext(), getResourceScheduler(), @@ -68,4 +70,11 @@ public MoveApplicationAcrossQueuesResponse moveApplicationAcrossQueues( super.setRMClient(mockRM.getClientRMService()); } + @Override + public void shutdown() { + if (mockRM != null) { + mockRM.stop(); + } + super.shutdown(); + } } 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/TestableFederationClientInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestableFederationClientInterceptor.java index 7c82476ec4..4e4df3f123 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestableFederationClientInterceptor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestableFederationClientInterceptor.java @@ -48,6 +48,8 @@ import org.apache.hadoop.yarn.server.resourcemanager.security.RMDelegationTokenSecretManager; import org.apache.hadoop.yarn.server.security.ApplicationACLsManager; import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Extends the FederationClientInterceptor and overrides methods to provide a @@ -56,6 +58,9 @@ public class TestableFederationClientInterceptor extends FederationClientInterceptor { + private static final Logger LOG = + LoggerFactory.getLogger(TestableFederationClientInterceptor.class); + private ConcurrentHashMap mockRMs = new ConcurrentHashMap<>(); @@ -161,4 +166,31 @@ private void initNodeAttributes(SubClusterId subClusterId, MockRM mockRM) { throw new RuntimeException(e); } } + + @Override + public void shutdown() { + if (mockRMs != null && !mockRMs.isEmpty()) { + for (Map.Entry item : mockRMs.entrySet()) { + SubClusterId subClusterId = item.getKey(); + + // close mockNM + MockNM mockNM = mockNMs.getOrDefault(subClusterId, null); + try { + mockNM.unRegisterNode(); + mockNM = null; + } catch (Exception e) { + LOG.error("mockNM unRegisterNode error.", e); + } + + // close mockRM + MockRM mockRM = item.getValue(); + if (mockRM != null) { + mockRM.stop(); + } + } + } + mockNMs.clear(); + mockRMs.clear(); + super.shutdown(); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/rmadmin/MockRMAdminRequestInterceptor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/rmadmin/MockRMAdminRequestInterceptor.java index f9adbfdec8..a1a366f766 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/rmadmin/MockRMAdminRequestInterceptor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/rmadmin/MockRMAdminRequestInterceptor.java @@ -32,8 +32,9 @@ public class MockRMAdminRequestInterceptor extends DefaultRMAdminRequestInterceptor { + MockRM mockRM = null; public void init(String user) { - MockRM mockRM = new MockRM(super.getConf()) { + mockRM = new MockRM(super.getConf()) { @Override protected AdminService createAdminService() { return new AdminService(this) { @@ -60,4 +61,12 @@ protected void stopServer() { mockRM.start(); super.setRMAdmin(mockRM.getAdminService()); } + + @Override + public void shutdown() { + if (mockRM != null) { + mockRM.stop(); + } + super.shutdown(); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/secure/AbstractSecureRouterTest.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/secure/AbstractSecureRouterTest.java index 61a5e8923b..f9d1d04764 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/secure/AbstractSecureRouterTest.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/secure/AbstractSecureRouterTest.java @@ -20,6 +20,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; import org.apache.hadoop.minikdc.MiniKdc; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.yarn.conf.YarnConfiguration; @@ -34,6 +35,7 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; @@ -107,6 +109,8 @@ public static void beforeSecureRouterTestClass() throws Exception { // Router Kerberos KeyTab configuration conf.set(YarnConfiguration.ROUTER_PRINCIPAL, ROUTER_LOCALHOST_REALM); conf.set(YarnConfiguration.ROUTER_KEYTAB, routerKeytab.getAbsolutePath()); + + DefaultMetricsSystem.setMiniClusterMode(true); } /** @@ -214,6 +218,12 @@ protected synchronized void stopSecureRouter() throws Exception { public static void afterSecureRouterTest() throws Exception { LOG.info("teardown of kdc instance."); teardownKDC(); + if (mockRMs != null && mockRMs.isEmpty()) { + for (MockRM mockRM : mockRMs.values()) { + mockRM.close(); + } + } + mockRMs.clear(); } public static MiniKdc getKdc() {