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("", 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) || "-refreshNodesResources".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 = handleRefreshNodes(args, cmd, isHAEnabled); } else if ("-refreshNodesResources".equals(cmd)) { exitCode = refreshNodesResources(); } 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 ("-refreshClusterMaxPriority".equals(cmd)) { exitCode = refreshClusterMaxPriority(); } else if ("-getGroups".equals(cmd)) { String[] usernames = Arrays.copyOfRange(args, i, args.length); exitCode = getGroups(usernames); } else if ("-updateNodeResource".equals(cmd)) { exitCode = handleUpdateNodeResource(args, cmd, isHAEnabled); } else if ("-addToClusterNodeLabels".equals(cmd)) { exitCode = handleAddToClusterNodeLabels(args, cmd, isHAEnabled); } else if ("-removeFromClusterNodeLabels".equals(cmd)) { exitCode = handleRemoveFromClusterNodeLabels(args, cmd, isHAEnabled); } else if ("-replaceLabelsOnNode".equals(cmd)) { exitCode = handleReplaceLabelsOnNodes(args, cmd, isHAEnabled); } 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 public void testRefreshNodesGracefulInvalidArgs() throws Exception { String[] invalidArgs = {"-refreshNodes", "-ginvalid", "invalid", "-client"}; assertEquals(-1, rmAdminCLI.run(invalidArgs)); String[] invalidTimeoutArgs = {"-refreshNodes", "-g", "invalid", "-client"}; assertEquals(-1, rmAdminCLI.run(invalidTimeoutArgs)); String[] negativeTimeoutArgs = {"-refreshNodes", "-g", "-1000", "-client"}; assertEquals(-1, rmAdminCLI.run(negativeTimeoutArgs)); String[] invalidTrackingArgs = {"-refreshNodes", "-g", "1", "-foo"}; assertEquals(-1, rmAdminCLI.run(invalidTrackingArgs)); }
@Test 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)); verify(haadmin, times(1)).getServiceStatus(); }
@Test 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 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 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 public void testAccessLocalNodeLabelManager() throws Exception { assertFalse(dummyNodeLabelsManager.getServiceState() == STATE.STOPPED); String[] args = { "-addToClusterNodeLabels", "x,y", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getClusterNodeLabelNames().containsAll( ImmutableSet.of("x", "y"))); dummyNodeLabelsManager.removeFromClusterNodeLabels(ImmutableSet.of("x", "y")); args = new String[] { "-addToClusterNodeLabels", "-directlyAccessNodeLabelStore", "x,y" }; assertEquals(-1, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getServiceState() == STATE.STOPPED); }
@Test public void testAccessRemoteNodeLabelManager() throws Exception { String[] args = { "-addToClusterNodeLabels", "x,y" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getClusterNodeLabelNames().isEmpty()); assertTrue(remoteAdminServiceAccessed); }
@Test public void testAddToClusterNodeLabels() throws Exception { String[] args = { "-addToClusterNodeLabels", "x", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getClusterNodeLabelNames().containsAll( ImmutableSet.of("x"))); args = new String[] { "-addToClusterNodeLabels" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", "-directlyAccessNodeLabelStore" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", " " }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", " , " }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", ",x,,", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getClusterNodeLabelNames().containsAll( ImmutableSet.of("x"))); }
@Test public void testAddToClusterNodeLabelsWithExclusivitySetting() throws Exception { String[] args = new String[] { "-addToClusterNodeLabels", "x(" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", "x)" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", "x(key=value)" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", "x(exclusive=)" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-addToClusterNodeLabels", "w,x(exclusive=true), y(exclusive=false),z()", "-directlyAccessNodeLabelStore" }; assertTrue(0 == rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("w")); assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("x")); assertFalse(dummyNodeLabelsManager.isExclusiveNodeLabel("y")); assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("z")); args = new String[] { "-addToClusterNodeLabels", "a (exclusive= true) , b( exclusive =false),c ", "-directlyAccessNodeLabelStore" }; assertTrue(0 == rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("a")); assertFalse(dummyNodeLabelsManager.isExclusiveNodeLabel("b")); assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("c")); }
@Test public void testRemoveFromClusterNodeLabels() throws Exception { dummyNodeLabelsManager.addToCluserNodeLabelsWithDefaultExclusivity(ImmutableSet.of("x", "y")); String[] args = { "-removeFromClusterNodeLabels", "x,,y", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getClusterNodeLabelNames().isEmpty()); args = new String[] { "-removeFromClusterNodeLabels" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-removeFromClusterNodeLabels", "-directlyAccessNodeLabelStore" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-removeFromClusterNodeLabels", " " }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-removeFromClusterNodeLabels", ", " }; assertTrue(0 != rmAdminCLI.run(args)); }
@Test public void testReplaceLabelsOnNode() throws Exception { dummyNodeLabelsManager .addToCluserNodeLabelsWithDefaultExclusivity(ImmutableSet.of("x", "y", "Y")); String[] args = { "-replaceLabelsOnNode", "node1:8000,x node2:8000=y node3,x node4=Y", "-directlyAccessNodeLabelStore" }; assertEquals(0, rmAdminCLI.run(args)); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node1", 8000))); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node2", 8000))); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node3", 0))); assertTrue(dummyNodeLabelsManager.getNodeLabels().containsKey( NodeId.newInstance("node4", 0))); args = new String[] { "-replaceLabelsOnNode" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-replaceLabelsOnNode", "-failOnUnknownNodes" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-replaceLabelsOnNode", "-directlyAccessNodeLabelStore" }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-replaceLabelsOnNode", " " }; assertTrue(0 != rmAdminCLI.run(args)); args = new String[] { "-replaceLabelsOnNode", ", " }; assertTrue(0 != rmAdminCLI.run(args)); }
@Test public void testReplaceMultipleLabelsOnSingleNode() throws Exception { dummyNodeLabelsManager.addToCluserNodeLabelsWithDefaultExclusivity(ImmutableSet.of("x", "y")); String[] args = { "-replaceLabelsOnNode", "node1,x,y", "-directlyAccessNodeLabelStore" }; assertTrue(0 != rmAdminCLI.run(args)); }
@Test public void testRemoveLabelsOnNodes() throws Exception { dummyNodeLabelsManager .addToCluserNodeLabelsWithDefaultExclusivity(ImmutableSet.of("x", "y")); String[] args = { "-replaceLabelsOnNode", "node1=x node2=y", "-directlyAccessNodeLabelStore" }; assertTrue(0 == rmAdminCLI.run(args)); args = new String[] { "-replaceLabelsOnNode", "node1= node2=", "-directlyAccessNodeLabelStore" }; assertTrue("Labels should get replaced even '=' is used ", 0 == rmAdminCLI.run(args)); }
@Test public void testNoUnsupportedHACommandsInHelp() throws Exception { ByteArrayOutputStream dataErr = new ByteArrayOutputStream(); System.setErr(new PrintStream(dataErr)); String[] args = {}; assertEquals(-1, rmAdminCLIWithHAEnabled.run(args)); String errOut = dataErr.toString(); assertFalse(errOut.contains("-transitionToObserver")); dataErr.reset(); String[] args1 = {"-transitionToObserver"}; assertEquals(-1, rmAdminCLIWithHAEnabled.run(args1)); errOut = dataErr.toString(); assertTrue(errOut.contains("transitionToObserver: Unknown command")); dataErr.reset(); args1[0] = "-failover"; assertEquals(-1, rmAdminCLIWithHAEnabled.run(args1)); errOut = dataErr.toString(); assertTrue(errOut.contains("failover: Unknown command")); dataErr.reset(); String[] args2 = {"-help", "-transitionToObserver"}; assertEquals(0, rmAdminCLIWithHAEnabled.run(args2)); errOut = dataErr.toString(); assertFalse(errOut.contains("-transitionToObserver")); dataErr.reset(); args2[1] = "-failover"; assertEquals(0, rmAdminCLIWithHAEnabled.run(args2)); errOut = dataErr.toString(); assertFalse(errOut.contains("-failover")); dataErr.reset(); }