Prechádzať zdrojové kódy

YARN-1761. Modified RMAdmin CLI to check whether HA is enabled or not before it executes any of the HA admin related commands. Contributed by Xuan Gong.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1574661 13f79535-47bb-0310-9956-ffa450edef68
Vinod Kumar Vavilapalli 11 rokov pred
rodič
commit
d963b3cd52

+ 3 - 0
hadoop-yarn-project/CHANGES.txt

@@ -262,6 +262,9 @@ Release 2.4.0 - UNRELEASED
     YARN-1752. Fixed ApplicationMasterService to reject unregister request if
     AM did not register before. (Rohith Sharma via jianhe)
 
+    YARN-1761. Modified RMAdmin CLI to check whether HA is enabled or not before
+    it executes any of the HA admin related commands. (Xuan Gong via vinodkv)
+
   OPTIMIZATIONS
 
   BUG FIXES

+ 44 - 23
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java

@@ -27,13 +27,11 @@ import com.google.common.collect.ImmutableMap;
 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;
@@ -115,34 +113,42 @@ public class RMAdminCLI extends HAAdmin {
 
   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) {
+  private static void buildUsageMsg(StringBuilder builder,
+      boolean isHAEnabled) {
     builder.append("Usage: yarn rmadmin\n");
     for (String cmdKey : ADMIN_USAGE.keySet()) {
       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");
+        }
       }
     }
   }
 
-  private static void printHelp(String cmd) {
+  private static void printHelp(String cmd, boolean isHAEnabled) {
     StringBuilder summary = new StringBuilder();
     summary.append("rmadmin is the command to execute YARN administrative " +
         "commands.\n");
@@ -156,7 +162,9 @@ public class RMAdminCLI extends HAAdmin {
       " [-refreshServiceAcl]" +
       " [-getGroup [username]]" +
       " [-help [cmd]]");
-    appendHAUsage(summary);
+    if (isHAEnabled) {
+      appendHAUsage(summary);
+    }
     summary.append("\n");
 
     StringBuilder helpBuilder = new StringBuilder();
@@ -165,10 +173,12 @@ public class RMAdminCLI extends HAAdmin {
       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);
@@ -180,12 +190,12 @@ public class RMAdminCLI extends HAAdmin {
    * Displays format of commands.
    * @param cmd The command that is being executed.
    */
-  private static void printUsage(String cmd) {
+  private static void printUsage(String cmd, boolean isHAEnabled) {
     StringBuilder usageBuilder = new StringBuilder();
     if (ADMIN_USAGE.containsKey(cmd) || USAGE.containsKey(cmd)) {
       buildIndividualUsageMsg(cmd, usageBuilder);
     } else {
-      buildUsageMsg(usageBuilder);
+      buildUsageMsg(usageBuilder, isHAEnabled);
     }
     System.err.println(usageBuilder);
     ToolRunner.printGenericCommandUsage(System.err);
@@ -277,8 +287,15 @@ public class RMAdminCLI extends HAAdmin {
   
   @Override
   public int run(String[] args) throws Exception {
+    YarnConfiguration yarnConf =
+        getConf() == null ? new YarnConfiguration() : new YarnConfiguration(
+            getConf());
+    boolean isHAEnabled =
+        yarnConf.getBoolean(YarnConfiguration.RM_HA_ENABLED,
+            YarnConfiguration.DEFAULT_RM_HA_ENABLED);
+
     if (args.length < 1) {
-      printUsage("");
+      printUsage("", isHAEnabled);
       return -1;
     }
 
@@ -289,15 +306,20 @@ public class RMAdminCLI extends HAAdmin {
     exitCode = 0;
     if ("-help".equals(cmd)) {
       if (i < args.length) {
-        printUsage(args[i]);
+        printUsage(args[i], isHAEnabled);
       } else {
-        printHelp("");
+        printHelp("", isHAEnabled);
       }
       return exitCode;
     }
 
     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;
     }
 
     //
@@ -308,7 +330,7 @@ public class RMAdminCLI extends HAAdmin {
         "-refreshUserToGroupsMappings".equals(cmd) ||
         "-refreshSuperUserGroupsConfiguration".equals(cmd)) {
       if (args.length != 1) {
-        printUsage(cmd);
+        printUsage(cmd, isHAEnabled);
         return exitCode;
       }
     }
@@ -332,14 +354,13 @@ public class RMAdminCLI extends HAAdmin {
       } else {
         exitCode = -1;
         System.err.println(cmd.substring(1) + ": Unknown command");
-        printUsage("");
-        printUsage("");
+        printUsage("", isHAEnabled);
       }
 
     } catch (IllegalArgumentException arge) {
       exitCode = -1;
       System.err.println(cmd.substring(1) + ": " + arge.getLocalizedMessage());
-      printUsage(cmd);
+      printUsage(cmd, isHAEnabled);
     } catch (RemoteException e) {
       //
       // This is a error returned by hadoop server. Print

+ 73 - 11
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.Matchers.eq;
 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 @@ public class TestRMAdminCLI {
   private ResourceManagerAdministrationProtocol admin;
   private HAServiceProtocol haadmin;
   private RMAdminCLI rmAdminCLI;
+  private RMAdminCLI rmAdminCLIWithHAEnabled;
 
   @Before
   public void configure() throws IOException {
@@ -66,7 +68,23 @@ public class TestRMAdminCLI {
     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 class TestRMAdminCLI {
   @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 class TestRMAdminCLI {
   @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 class TestRMAdminCLI {
   @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 class TestRMAdminCLI {
               "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper" +
               "UserGroupsConfiguration] [-refreshUserToGroupsMappings] " +
               "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup" +
-              " [username]] [-help [cmd]] [-transitionToActive <serviceId>]" +
-              " [-transitionToStandby <serviceId>] [-failover [--forcefence] " +
-                  "[--forceactive] <serviceId> <serviceId>] " +
-                  "[-getServiceState <serviceId>] [-checkHealth <serviceId>]"
-          ));
+              " [username]] [-help [cmd]]"));
       assertTrue(dataOut
           .toString()
           .contains(
@@ -273,7 +321,21 @@ public class TestRMAdminCLI {
       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 <serviceId>]" +
+              " [-transitionToStandby <serviceId>] [-failover [--forcefence]" +
+              " [--forceactive] <serviceId> <serviceId>] " +
+              "[-getServiceState <serviceId>] [-checkHealth <serviceId>]"));
     } finally {
       System.setOut(oldOutPrintStream);
       System.setErr(oldErrPrintStream);