DFSAdmin extends FsShell { public int setBalancerBandwidth(String[] argv, int idx) throws IOException { long bandwidth; int exitCode = -1; try { bandwidth = StringUtils.TraditionalBinaryPrefix.string2long(argv[idx]); } catch (NumberFormatException nfe) { System.err.println("NumberFormatException: " + nfe.getMessage()); System.err.println("Usage: hdfs dfsadmin" + " [-setBalancerBandwidth <bandwidth in bytes per second>]"); return exitCode; } if (bandwidth < 0) { System.err.println("Bandwidth should be a non-negative integer"); return exitCode; } DistributedFileSystem dfs = getDFS(); try{ dfs.setBalancerBandwidth(bandwidth); System.out.println("Balancer bandwidth is set to " + bandwidth); } catch (IOException ioe){ System.err.println("Balancer bandwidth is set failed."); throw ioe; } exitCode = 0; return exitCode; } DFSAdmin();  DFSAdmin(Configuration conf); void report(String[] argv, int i); void setSafeMode(String[] argv, int idx); int triggerBlockReport(String[] argv); void allowSnapshot(String[] argv); void disallowSnapshot(String[] argv); void provisionSnapshotTrash(String[] argv); int saveNamespace(String[] argv); int rollEdits(); int restoreFailedStorage(String arg); int refreshNodes(); int listOpenFiles(String[] argv); int setBalancerBandwidth(String[] argv, int idx); int getBalancerBandwidth(String[] argv, int idx); int fetchImage(final String[] argv, final int idx); int finalizeUpgrade(); int getUpgradeStatus(); int upgrade(String arg); int metaSave(String[] argv, int idx); int printTopology(); int refreshServiceAcl(); int refreshUserToGroupsMappings(); int refreshSuperUserGroupsConfiguration(); int refreshCallQueue(); int reconfig(String[] argv, int i); int genericRefresh(String[] argv, int i); @Override int run(String[] argv); static void main(String[] argv);  }
@Test public void testSetBalancerBandwidth() throws Exception { redirectStream(); final DFSAdmin dfsAdmin = new DFSAdmin(conf); String outStr; assertEquals(0, ToolRunner.run(dfsAdmin, new String[]{"-setBalancerBandwidth", "10000"})); outStr = scanIntoString(out); assertTrue("Did not set bandwidth!", outStr.contains("Balancer " + "bandwidth is set to 10000")); resetStream(); assertEquals(0, ToolRunner.run(dfsAdmin, new String[]{"-setBalancerBandwidth", "10m"})); outStr = scanIntoString(out); assertTrue("Did not set bandwidth!", outStr.contains("Balancer " + "bandwidth is set to 10485760")); resetStream(); assertEquals(0, ToolRunner.run(dfsAdmin, new String[]{"-setBalancerBandwidth", "10k"})); outStr = scanIntoString(out); assertTrue("Did not set bandwidth!", outStr.contains("Balancer " + "bandwidth is set to 10240")); assertEquals(-1, ToolRunner.run(dfsAdmin, new String[]{"-setBalancerBandwidth", "-10000"})); assertEquals(-1, ToolRunner.run(dfsAdmin, new String[]{"-setBalancerBandwidth", "-10m"})); }