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
This commit is contained in:
Vinod Kumar Vavilapalli 2014-03-05 21:07:50 +00:00
parent 8cd23c5b48
commit d963b3cd52
3 changed files with 120 additions and 34 deletions

View File

@ -262,6 +262,9 @@ Release 2.4.0 - UNRELEASED
YARN-1752. Fixed ApplicationMasterService to reject unregister request if YARN-1752. Fixed ApplicationMasterService to reject unregister request if
AM did not register before. (Rohith Sharma via jianhe) 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 OPTIMIZATIONS
BUG FIXES BUG FIXES

View File

@ -27,13 +27,11 @@ import com.google.common.collect.ImmutableMap;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.classification.InterfaceStability.Unstable;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.ha.HAAdmin; import org.apache.hadoop.ha.HAAdmin;
import org.apache.hadoop.ha.HAServiceTarget; import org.apache.hadoop.ha.HAServiceTarget;
import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.yarn.client.ClientRMProxy; import org.apache.hadoop.yarn.client.ClientRMProxy;
import org.apache.hadoop.yarn.client.RMHAServiceTarget; import org.apache.hadoop.yarn.client.RMHAServiceTarget;
@ -115,34 +113,42 @@ public class RMAdminCLI extends HAAdmin {
private static void buildIndividualUsageMsg(String cmd, private static void buildIndividualUsageMsg(String cmd,
StringBuilder builder ) { StringBuilder builder ) {
boolean isHACommand = false;
UsageInfo usageInfo = ADMIN_USAGE.get(cmd); UsageInfo usageInfo = ADMIN_USAGE.get(cmd);
if (usageInfo == null) { if (usageInfo == null) {
usageInfo = USAGE.get(cmd); usageInfo = USAGE.get(cmd);
if (usageInfo == null) { if (usageInfo == null) {
return; return;
} }
isHACommand = true;
} }
String space = (usageInfo.args == "") ? "" : " "; String space = (usageInfo.args == "") ? "" : " ";
builder.append("Usage: yarn rmadmin [" builder.append("Usage: yarn rmadmin ["
+ cmd + space + usageInfo.args + cmd + space + usageInfo.args
+ "]\n"); + "]\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"); builder.append("Usage: yarn rmadmin\n");
for (String cmdKey : ADMIN_USAGE.keySet()) { for (String cmdKey : ADMIN_USAGE.keySet()) {
UsageInfo usageInfo = ADMIN_USAGE.get(cmdKey); UsageInfo usageInfo = ADMIN_USAGE.get(cmdKey);
builder.append(" " + cmdKey + " " + usageInfo.args + "\n"); builder.append(" " + cmdKey + " " + usageInfo.args + "\n");
} }
for (String cmdKey : USAGE.keySet()) { if (isHAEnabled) {
if (!cmdKey.equals("-help")) { for (String cmdKey : USAGE.keySet()) {
UsageInfo usageInfo = USAGE.get(cmdKey); if (!cmdKey.equals("-help")) {
builder.append(" " + cmdKey + " " + usageInfo.args + "\n"); 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(); StringBuilder summary = new StringBuilder();
summary.append("rmadmin is the command to execute YARN administrative " + summary.append("rmadmin is the command to execute YARN administrative " +
"commands.\n"); "commands.\n");
@ -156,7 +162,9 @@ public class RMAdminCLI extends HAAdmin {
" [-refreshServiceAcl]" + " [-refreshServiceAcl]" +
" [-getGroup [username]]" + " [-getGroup [username]]" +
" [-help [cmd]]"); " [-help [cmd]]");
appendHAUsage(summary); if (isHAEnabled) {
appendHAUsage(summary);
}
summary.append("\n"); summary.append("\n");
StringBuilder helpBuilder = new StringBuilder(); StringBuilder helpBuilder = new StringBuilder();
@ -165,10 +173,12 @@ public class RMAdminCLI extends HAAdmin {
buildHelpMsg(cmdKey, helpBuilder); buildHelpMsg(cmdKey, helpBuilder);
helpBuilder.append("\n"); helpBuilder.append("\n");
} }
for (String cmdKey : USAGE.keySet()) { if (isHAEnabled) {
if (!cmdKey.equals("-help")) { for (String cmdKey : USAGE.keySet()) {
buildHelpMsg(cmdKey, helpBuilder); if (!cmdKey.equals("-help")) {
helpBuilder.append("\n"); buildHelpMsg(cmdKey, helpBuilder);
helpBuilder.append("\n");
}
} }
} }
System.out.println(helpBuilder); System.out.println(helpBuilder);
@ -180,12 +190,12 @@ public class RMAdminCLI extends HAAdmin {
* Displays format of commands. * Displays format of commands.
* @param cmd The command that is being executed. * @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(); StringBuilder usageBuilder = new StringBuilder();
if (ADMIN_USAGE.containsKey(cmd) || USAGE.containsKey(cmd)) { if (ADMIN_USAGE.containsKey(cmd) || USAGE.containsKey(cmd)) {
buildIndividualUsageMsg(cmd, usageBuilder); buildIndividualUsageMsg(cmd, usageBuilder);
} else { } else {
buildUsageMsg(usageBuilder); buildUsageMsg(usageBuilder, isHAEnabled);
} }
System.err.println(usageBuilder); System.err.println(usageBuilder);
ToolRunner.printGenericCommandUsage(System.err); ToolRunner.printGenericCommandUsage(System.err);
@ -277,8 +287,15 @@ public class RMAdminCLI extends HAAdmin {
@Override @Override
public int run(String[] args) throws Exception { 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) { if (args.length < 1) {
printUsage(""); printUsage("", isHAEnabled);
return -1; return -1;
} }
@ -289,15 +306,20 @@ public class RMAdminCLI extends HAAdmin {
exitCode = 0; exitCode = 0;
if ("-help".equals(cmd)) { if ("-help".equals(cmd)) {
if (i < args.length) { if (i < args.length) {
printUsage(args[i]); printUsage(args[i], isHAEnabled);
} else { } else {
printHelp(""); printHelp("", isHAEnabled);
} }
return exitCode; return exitCode;
} }
if (USAGE.containsKey(cmd)) { 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) || "-refreshUserToGroupsMappings".equals(cmd) ||
"-refreshSuperUserGroupsConfiguration".equals(cmd)) { "-refreshSuperUserGroupsConfiguration".equals(cmd)) {
if (args.length != 1) { if (args.length != 1) {
printUsage(cmd); printUsage(cmd, isHAEnabled);
return exitCode; return exitCode;
} }
} }
@ -332,14 +354,13 @@ public class RMAdminCLI extends HAAdmin {
} else { } else {
exitCode = -1; exitCode = -1;
System.err.println(cmd.substring(1) + ": Unknown command"); System.err.println(cmd.substring(1) + ": Unknown command");
printUsage(""); printUsage("", isHAEnabled);
printUsage("");
} }
} catch (IllegalArgumentException arge) { } catch (IllegalArgumentException arge) {
exitCode = -1; exitCode = -1;
System.err.println(cmd.substring(1) + ": " + arge.getLocalizedMessage()); System.err.println(cmd.substring(1) + ": " + arge.getLocalizedMessage());
printUsage(cmd); printUsage(cmd, isHAEnabled);
} catch (RemoteException e) { } catch (RemoteException e) {
// //
// This is a error returned by hadoop server. Print // This is a error returned by hadoop server. Print

View File

@ -27,6 +27,7 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.Mockito.never;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -54,6 +55,7 @@ public class TestRMAdminCLI {
private ResourceManagerAdministrationProtocol admin; private ResourceManagerAdministrationProtocol admin;
private HAServiceProtocol haadmin; private HAServiceProtocol haadmin;
private RMAdminCLI rmAdminCLI; private RMAdminCLI rmAdminCLI;
private RMAdminCLI rmAdminCLIWithHAEnabled;
@Before @Before
public void configure() throws IOException { public void configure() throws IOException {
@ -66,7 +68,23 @@ public class TestRMAdminCLI {
final HAServiceTarget haServiceTarget = mock(HAServiceTarget.class); final HAServiceTarget haServiceTarget = mock(HAServiceTarget.class);
when(haServiceTarget.getProxy(any(Configuration.class), anyInt())) when(haServiceTarget.getProxy(any(Configuration.class), anyInt()))
.thenReturn(haadmin); .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 @Override
protected ResourceManagerAdministrationProtocol createAdminProtocol() protected ResourceManagerAdministrationProtocol createAdminProtocol()
@ -150,7 +168,16 @@ public class TestRMAdminCLI {
@Test(timeout = 500) @Test(timeout = 500)
public void testTransitionToActive() throws Exception { public void testTransitionToActive() throws Exception {
String[] args = {"-transitionToActive", "rm1"}; 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( verify(haadmin).transitionToActive(
any(HAServiceProtocol.StateChangeRequestInfo.class)); any(HAServiceProtocol.StateChangeRequestInfo.class));
} }
@ -158,7 +185,16 @@ public class TestRMAdminCLI {
@Test(timeout = 500) @Test(timeout = 500)
public void testTransitionToStandby() throws Exception { public void testTransitionToStandby() throws Exception {
String[] args = {"-transitionToStandby", "rm1"}; 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( verify(haadmin).transitionToStandby(
any(HAServiceProtocol.StateChangeRequestInfo.class)); any(HAServiceProtocol.StateChangeRequestInfo.class));
} }
@ -166,14 +202,30 @@ public class TestRMAdminCLI {
@Test(timeout = 500) @Test(timeout = 500)
public void testGetServiceState() throws Exception { public void testGetServiceState() throws Exception {
String[] args = {"-getServiceState", "rm1"}; 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(); verify(haadmin).getServiceStatus();
} }
@Test(timeout = 500) @Test(timeout = 500)
public void testCheckHealth() throws Exception { public void testCheckHealth() throws Exception {
String[] args = {"-checkHealth", "rm1"}; 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(); verify(haadmin).monitorHealth();
} }
@ -202,11 +254,7 @@ public class TestRMAdminCLI {
"yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper" + "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper" +
"UserGroupsConfiguration] [-refreshUserToGroupsMappings] " + "UserGroupsConfiguration] [-refreshUserToGroupsMappings] " +
"[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup" + "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup" +
" [username]] [-help [cmd]] [-transitionToActive <serviceId>]" + " [username]] [-help [cmd]]"));
" [-transitionToStandby <serviceId>] [-failover [--forcefence] " +
"[--forceactive] <serviceId> <serviceId>] " +
"[-getServiceState <serviceId>] [-checkHealth <serviceId>]"
));
assertTrue(dataOut assertTrue(dataOut
.toString() .toString()
.contains( .contains(
@ -273,7 +321,21 @@ public class TestRMAdminCLI {
testError(new String[] { "-help", "-badParameter" }, testError(new String[] { "-help", "-badParameter" },
"Usage: yarn rmadmin", dataErr, 0); "Usage: yarn rmadmin", dataErr, 0);
testError(new String[] { "-badParameter" }, 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 { } finally {
System.setOut(oldOutPrintStream); System.setOut(oldOutPrintStream);
System.setErr(oldErrPrintStream); System.setErr(oldErrPrintStream);