diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java index ee389b3..d73bb57 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java @@ -27,13 +27,11 @@ import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.ha.HAAdmin; import org.apache.hadoop.ha.HAServiceTarget; import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.security.UserGroupInformation; -import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.yarn.client.ClientRMProxy; import org.apache.hadoop.yarn.client.RMHAServiceTarget; @@ -58,6 +56,8 @@ private final RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null); + private static boolean isHAEnabled = false; + protected final static Map ADMIN_USAGE = ImmutableMap.builder() .put("-refreshQueues", new UsageInfo("", @@ -115,17 +115,22 @@ private static void buildHelpMsg(String cmd, StringBuilder builder) { private static void buildIndividualUsageMsg(String cmd, StringBuilder builder ) { + boolean isHACommand = false; UsageInfo usageInfo = ADMIN_USAGE.get(cmd); if (usageInfo == null) { usageInfo = USAGE.get(cmd); if (usageInfo == null) { return; } + isHACommand = true; } String space = (usageInfo.args == "") ? "" : " "; builder.append("Usage: yarn rmadmin [" + cmd + space + usageInfo.args + "]\n"); + if (isHACommand) { + builder.append(cmd + " can only be used when RM HA is enabled"); + } } private static void buildUsageMsg(StringBuilder builder) { @@ -134,10 +139,12 @@ private static void buildUsageMsg(StringBuilder builder) { UsageInfo usageInfo = ADMIN_USAGE.get(cmdKey); builder.append(" " + cmdKey + " " + usageInfo.args + "\n"); } - for (String cmdKey : USAGE.keySet()) { - if (!cmdKey.equals("-help")) { - UsageInfo usageInfo = USAGE.get(cmdKey); - builder.append(" " + cmdKey + " " + usageInfo.args + "\n"); + if (isHAEnabled) { + for (String cmdKey : USAGE.keySet()) { + if (!cmdKey.equals("-help")) { + UsageInfo usageInfo = USAGE.get(cmdKey); + builder.append(" " + cmdKey + " " + usageInfo.args + "\n"); + } } } } @@ -156,7 +163,9 @@ private static void printHelp(String cmd) { " [-refreshServiceAcl]" + " [-getGroup [username]]" + " [-help [cmd]]"); - appendHAUsage(summary); + if (isHAEnabled) { + appendHAUsage(summary); + } summary.append("\n"); StringBuilder helpBuilder = new StringBuilder(); @@ -165,10 +174,12 @@ private static void printHelp(String cmd) { buildHelpMsg(cmdKey, helpBuilder); helpBuilder.append("\n"); } - for (String cmdKey : USAGE.keySet()) { - if (!cmdKey.equals("-help")) { - buildHelpMsg(cmdKey, helpBuilder); - helpBuilder.append("\n"); + if (isHAEnabled) { + for (String cmdKey : USAGE.keySet()) { + if (!cmdKey.equals("-help")) { + buildHelpMsg(cmdKey, helpBuilder); + helpBuilder.append("\n"); + } } } System.out.println(helpBuilder); @@ -277,6 +288,13 @@ private int getGroups(String[] usernames) throws IOException { @Override public int run(String[] args) throws Exception { + YarnConfiguration yarnConf = + getConf() == null ? new YarnConfiguration() : new YarnConfiguration( + getConf()); + isHAEnabled = + yarnConf.getBoolean(YarnConfiguration.RM_HA_ENABLED, + YarnConfiguration.DEFAULT_RM_HA_ENABLED); + if (args.length < 1) { printUsage(""); return -1; @@ -297,7 +315,12 @@ public int run(String[] args) throws Exception { } if (USAGE.containsKey(cmd)) { - return super.run(args); + if (isHAEnabled) { + return super.run(args); + } + System.out.println("Cannot run " + cmd + + " when ResourceManager HA is not enabled"); + return -1; } // @@ -333,7 +356,6 @@ public int run(String[] args) throws Exception { exitCode = -1; System.err.println(cmd.substring(1) + ": Unknown command"); printUsage(""); - printUsage(""); } } catch (IllegalArgumentException arge) { diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMAdminCLI.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMAdminCLI.java index cec8fcc..f4ef0aa 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMAdminCLI.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMAdminCLI.java @@ -27,6 +27,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.never; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -54,6 +55,7 @@ private ResourceManagerAdministrationProtocol admin; private HAServiceProtocol haadmin; private RMAdminCLI rmAdminCLI; + private RMAdminCLI rmAdminCLIWithHAEnabled; @Before public void configure() throws IOException { @@ -66,7 +68,23 @@ public void configure() throws IOException { final HAServiceTarget haServiceTarget = mock(HAServiceTarget.class); when(haServiceTarget.getProxy(any(Configuration.class), anyInt())) .thenReturn(haadmin); - rmAdminCLI = new RMAdminCLI() { + rmAdminCLI = new RMAdminCLI(new Configuration()) { + + @Override + protected ResourceManagerAdministrationProtocol createAdminProtocol() + throws IOException { + return admin; + } + + @Override + protected HAServiceTarget resolveTarget(String rmId) { + return haServiceTarget; + } + }; + + YarnConfiguration conf = new YarnConfiguration(); + conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); + rmAdminCLIWithHAEnabled = new RMAdminCLI(conf) { @Override protected ResourceManagerAdministrationProtocol createAdminProtocol() @@ -150,7 +168,16 @@ public boolean matches(Object argument) { @Test(timeout = 500) public void testTransitionToActive() throws Exception { String[] args = {"-transitionToActive", "rm1"}; - assertEquals(0, rmAdminCLI.run(args)); + + // RM HA is disabled. + // transitionToActive should not be executed + assertEquals(-1, rmAdminCLI.run(args)); + verify(haadmin, never()).transitionToActive( + any(HAServiceProtocol.StateChangeRequestInfo.class)); + + // Now RM HA is enabled. + // transitionToActive should be executed + assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).transitionToActive( any(HAServiceProtocol.StateChangeRequestInfo.class)); } @@ -158,7 +185,16 @@ public void testTransitionToActive() throws Exception { @Test(timeout = 500) public void testTransitionToStandby() throws Exception { String[] args = {"-transitionToStandby", "rm1"}; - assertEquals(0, rmAdminCLI.run(args)); + + // RM HA is disabled. + // transitionToStandby should not be executed + assertEquals(-1, rmAdminCLI.run(args)); + verify(haadmin, never()).transitionToStandby( + any(HAServiceProtocol.StateChangeRequestInfo.class)); + + // Now RM HA is enabled. + // transitionToActive should be executed + assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).transitionToStandby( any(HAServiceProtocol.StateChangeRequestInfo.class)); } @@ -166,14 +202,30 @@ public void testTransitionToStandby() throws Exception { @Test(timeout = 500) public void testGetServiceState() throws Exception { String[] args = {"-getServiceState", "rm1"}; - assertEquals(0, rmAdminCLI.run(args)); + + // RM HA is disabled. + // getServiceState should not be executed + assertEquals(-1, rmAdminCLI.run(args)); + verify(haadmin, never()).getServiceStatus(); + + // Now RM HA is enabled. + // getServiceState should be executed + assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).getServiceStatus(); } @Test(timeout = 500) public void testCheckHealth() throws Exception { String[] args = {"-checkHealth", "rm1"}; - assertEquals(0, rmAdminCLI.run(args)); + + // RM HA is disabled. + // getServiceState should not be executed + assertEquals(-1, rmAdminCLI.run(args)); + verify(haadmin, never()).monitorHealth(); + + // Now RM HA is enabled. + // getServiceState should be executed + assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).monitorHealth(); } @@ -202,11 +254,7 @@ public void testHelp() throws Exception { "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper" + "UserGroupsConfiguration] [-refreshUserToGroupsMappings] " + "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup" + - " [username]] [-help [cmd]] [-transitionToActive ]" + - " [-transitionToStandby ] [-failover [--forcefence] " + - "[--forceactive] ] " + - "[-getServiceState ] [-checkHealth ]" - )); + " [username]] [-help [cmd]]")); assertTrue(dataOut .toString() .contains( @@ -273,7 +321,21 @@ public void testHelp() throws Exception { testError(new String[] { "-help", "-badParameter" }, "Usage: yarn rmadmin", dataErr, 0); testError(new String[] { "-badParameter" }, - "badParameter: Unknown command", dataErr, -1); + "badParameter: Unknown command", dataErr, -1); + + // Test -help when RM HA is enabled + assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); + oldOutPrintStream.println(dataOut); + assertTrue(dataOut + .toString() + .contains( + "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper" + + "UserGroupsConfiguration] [-refreshUserToGroupsMappings] " + + "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup" + + " [username]] [-help [cmd]] [-transitionToActive ]" + + " [-transitionToStandby ] [-failover [--forcefence]" + + " [--forceactive] ] " + + "[-getServiceState ] [-checkHealth ]")); } finally { System.setOut(oldOutPrintStream); System.setErr(oldErrPrintStream);