RMAdminCLI extends HAAdmin { @Override public int run(String[] args) throws Exception { List<String> argsList = new ArrayList<String>(); for (int i = 0; i < args.length; i++) { if (args[i].equals("-directlyAccessNodeLabelStore")) { directlyAccessNodeLabelStore = true; } else { argsList.add(args[i]); } } args = argsList.toArray(new String[0]); 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("", isHAEnabled); return -1; } int exitCode = -1; int i = 0; String cmd = args[i++]; exitCode = 0; if ("-help".equals(cmd)) { if (i < args.length) { printUsage(args[i], isHAEnabled); } else { printHelp("", isHAEnabled); } return exitCode; } if (USAGE.containsKey(cmd)) { if (isHAEnabled) { return super.run(args); } System.out.println("Cannot run " + cmd + " when ResourceManager HA is not enabled"); return -1; } if ("-refreshAdminAcls".equals(cmd) || "-refreshQueues".equals(cmd) || "-refreshNodes".equals(cmd) || "-refreshServiceAcl".equals(cmd) || "-refreshUserToGroupsMappings".equals(cmd) || "-refreshSuperUserGroupsConfiguration".equals(cmd)) { if (args.length != 1) { printUsage(cmd, isHAEnabled); return exitCode; } } try { if ("-refreshQueues".equals(cmd)) { exitCode = refreshQueues(); } else if ("-refreshNodes".equals(cmd)) { exitCode = refreshNodes(); } else if ("-refreshUserToGroupsMappings".equals(cmd)) { exitCode = refreshUserToGroupsMappings(); } else if ("-refreshSuperUserGroupsConfiguration".equals(cmd)) { exitCode = refreshSuperUserGroupsConfiguration(); } else if ("-refreshAdminAcls".equals(cmd)) { exitCode = refreshAdminAcls(); } else if ("-refreshServiceAcl".equals(cmd)) { exitCode = refreshServiceAcls(); } else if ("-getGroups".equals(cmd)) { String[] usernames = Arrays.copyOfRange(args, i, args.length); exitCode = getGroups(usernames); } else if ("-addToClusterNodeLabels".equals(cmd)) { if (i >= args.length) { System.err.println("No cluster node-labels are specified"); exitCode = -1; } else { exitCode = addToClusterNodeLabels(args[i]); } } else if ("-removeFromClusterNodeLabels".equals(cmd)) { if (i >= args.length) { System.err.println("No cluster node-labels are specified"); exitCode = -1; } else { exitCode = removeFromClusterNodeLabels(args[i]); } } else if ("-replaceLabelsOnNode".equals(cmd)) { if (i >= args.length) { System.err.println("No cluster node-labels are specified"); exitCode = -1; } else { exitCode = replaceLabelsOnNodes(args[i]); } } else { exitCode = -1; System.err.println(cmd.substring(1) + ": Unknown command"); printUsage("", isHAEnabled); } } catch (IllegalArgumentException arge) { exitCode = -1; System.err.println(cmd.substring(1) + ": " + arge.getLocalizedMessage()); printUsage(cmd, isHAEnabled); } catch (RemoteException e) { exitCode = -1; try { String[] content; content = e.getLocalizedMessage().split("\n"); System.err.println(cmd.substring(1) + ": " + content[0]); } catch (Exception ex) { System.err.println(cmd.substring(1) + ": " + ex.getLocalizedMessage()); } } catch (Exception e) { exitCode = -1; System.err.println(cmd.substring(1) + ": " + e.getLocalizedMessage()); } if (null != localNodeLabelsManager) { localNodeLabelsManager.stop(); } return exitCode; } RMAdminCLI();  RMAdminCLI(Configuration conf); @Override int run(String[] args); @Override void setConf(Configuration conf); static void main(String[] args);  }
@Test(timeout = 500) public void testTransitionToActive() throws Exception { String[] args = {"-transitionToActive", "rm1"}; assertEquals(-1, rmAdminCLI.run(args)); verify(haadmin, never()).transitionToActive( any(HAServiceProtocol.StateChangeRequestInfo.class)); assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).transitionToActive( any(HAServiceProtocol.StateChangeRequestInfo.class)); }
@Test(timeout = 500) public void testTransitionToStandby() throws Exception { String[] args = {"-transitionToStandby", "rm1"}; assertEquals(-1, rmAdminCLI.run(args)); verify(haadmin, never()).transitionToStandby( any(HAServiceProtocol.StateChangeRequestInfo.class)); assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).transitionToStandby( any(HAServiceProtocol.StateChangeRequestInfo.class)); }
@Test(timeout = 500) public void testGetServiceState() throws Exception { String[] args = {"-getServiceState", "rm1"}; assertEquals(-1, rmAdminCLI.run(args)); verify(haadmin, never()).getServiceStatus(); assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).getServiceStatus(); }
@Test(timeout = 500) public void testCheckHealth() throws Exception { String[] args = {"-checkHealth", "rm1"}; assertEquals(-1, rmAdminCLI.run(args)); verify(haadmin, never()).monitorHealth(); assertEquals(0, rmAdminCLIWithHAEnabled.run(args)); verify(haadmin).monitorHealth(); }
@Test(timeout=500) public void testHelp() throws Exception { PrintStream oldOutPrintStream = System.out; PrintStream oldErrPrintStream = System.err; ByteArrayOutputStream dataOut = new ByteArrayOutputStream(); ByteArrayOutputStream dataErr = new ByteArrayOutputStream(); System.setOut(new PrintStream(dataOut)); System.setErr(new PrintStream(dataErr)); try { String[] args = { "-help" }; assertEquals(0, rmAdminCLI.run(args)); oldOutPrintStream.println(dataOut); assertTrue(dataOut .toString() .contains( "rmadmin is the command to execute YARN administrative commands.")); assertTrue(dataOut .toString() .contains( "yarn rmadmin [-refreshQueues] [-refreshNodes] [-refreshSuper" + "UserGroupsConfiguration] [-refreshUserToGroupsMappings] " + "[-refreshAdminAcls] [-refreshServiceAcl] [-getGroup" + " [username]] [-help [cmd]]")); assertTrue(dataOut .toString() .contains( "-refreshQueues: Reload the queues' acls, states and scheduler " + "specific properties.")); assertTrue(dataOut .toString() .contains( "-refreshNodes: Refresh the hosts information at the " + "ResourceManager.")); assertTrue(dataOut.toString().contains( "-refreshUserToGroupsMappings: Refresh user-to-groups mappings")); assertTrue(dataOut .toString() .contains( "-refreshSuperUserGroupsConfiguration: Refresh superuser proxy" + " groups mappings")); assertTrue(dataOut .toString() .contains( "-refreshAdminAcls: Refresh acls for administration of " + "ResourceManager")); assertTrue(dataOut .toString() .contains( "-refreshServiceAcl: Reload the service-level authorization" + " policy file")); assertTrue(dataOut .toString() .contains( "-help [cmd]: Displays help for the given command or all " + "commands if none")); testError(new String[] { "-help", "-refreshQueues" }, "Usage: yarn rmadmin [-refreshQueues]", dataErr, 0); testError(new String[] { "-help", "-refreshNodes" }, "Usage: yarn rmadmin [-refreshNodes]", dataErr, 0); testError(new String[] { "-help", "-refreshUserToGroupsMappings" }, "Usage: yarn rmadmin [-refreshUserToGroupsMappings]", dataErr, 0); testError( new String[] { "-help", "-refreshSuperUserGroupsConfiguration" }, "Usage: yarn rmadmin [-refreshSuperUserGroupsConfiguration]", dataErr, 0); testError(new String[] { "-help", "-refreshAdminAcls" }, "Usage: yarn rmadmin [-refreshAdminAcls]", dataErr, 0); testError(new String[] { "-help", "-refreshServiceAcl" }, "Usage: yarn rmadmin [-refreshServiceAcl]", dataErr, 0); testError(new String[] { "-help", "-getGroups" }, "Usage: yarn rmadmin [-getGroups [username]]", dataErr, 0); testError(new String[] { "-help", "-transitionToActive" }, "Usage: yarn rmadmin [-transitionToActive <serviceId>" + " [--forceactive]]", dataErr, 0); testError(new String[] { "-help", "-transitionToStandby" }, "Usage: yarn rmadmin [-transitionToStandby <serviceId>]", dataErr, 0); testError(new String[] { "-help", "-getServiceState" }, "Usage: yarn rmadmin [-getServiceState <serviceId>]", dataErr, 0); testError(new String[] { "-help", "-checkHealth" }, "Usage: yarn rmadmin [-checkHealth <serviceId>]", dataErr, 0); testError(new String[] { "-help", "-failover" }, "Usage: yarn rmadmin " + "[-failover [--forcefence] [--forceactive] " + "<serviceId> <serviceId>]", dataErr, 0); testError(new String[] { "-help", "-badParameter" }, "Usage: yarn rmadmin", dataErr, 0); testError(new String[] { "-badParameter" }, "badParameter: Unknown command", dataErr, -1); 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>" + " [--forceactive]] [-transitionToStandby <serviceId>] [-failover" + " [--forcefence] [--forceactive] <serviceId> <serviceId>] " + "[-getServiceState <serviceId>] [-checkHealth <serviceId>]")); } finally { System.setOut(oldOutPrintStream); System.setErr(oldErrPrintStream); } }
@Test public void testAccessRemoteNodeLabelManager() throws Exception { String[] args = { "-addToClusterNodeLabels", "x,y" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getClusterNodeLabels().isEmpty()); assertTrue(remoteAdminServiceAccessed); }
@Test public void testReplaceLabelsOnNode() throws Exception { dummyNodeLabelsManager.addToCluserNodeLabels(ImmutableSet.of("x", "y")); String[] args = { "-replaceLabelsOnNode", "node1,x node2,y", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node1", 0))); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node2", 0))); args = new String[] { "-replaceLabelsOnNode" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-replaceLabelsOnNode", "-directlyAccessNodeLabelStore" }; assertTrue(0 != rmAdminCLI.run(args)); }
@Test public void testReplaceLabelsOnNodeWithPort() throws Exception { dummyNodeLabelsManager.addToCluserNodeLabels(ImmutableSet.of("x", "y")); String[] args = { "-replaceLabelsOnNode", "node1:8000,x node2:8000,y", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node1", 8000))); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node2", 8000))); }
@Test public void testReplaceMultipleLabelsOnSingleNode() throws Exception { dummyNodeLabelsManager.addToCluserNodeLabels(ImmutableSet.of("x", "y")); String[] args = { "-replaceLabelsOnNode", "node1,x,y", "-directlyAccessNodeLabelStore" }; assertTrue(0 != rmAdminCLI.run(args)); }