Balance implements Command { @Override public void run() throws Exception { boolean balancerRunning = false; try { balancerRunning = turnBalancerOff(); BalanceRule rule = BalanceRule.valueOf(ruleParam.toUpperCase()); if (rule.equals(BalanceRule.DEFAULT)) { if (!args.isForceProceed()) { if (!Util.askProceed()) { return; } } admin.balancer(); System.out.println("Run hbase default balancer. This is an asynchronous operation."); } else { List<RegionPlan> regionPlanList = rule.makePlan(admin, tableNameSet, args); BalanceFactor.printFactor(BalanceFactor.parseArg(args)); boolean asynchronous = args.has(Args.OPTION_MOVE_ASYNC); if (preview(regionPlanList, asynchronous)) balance(args, regionPlanList, Phase.BALANCE, asynchronous); } } finally { if (balancerRunning) { admin.setBalancerRunning(true, true); System.out.println("Turn balancer on."); } } } Balance(HBaseAdmin admin, Args args); static String usage(); @SuppressWarnings("deprecation") static Map<HRegionInfo, ServerName> getRegionAssignmentMap(HBaseAdmin admin, Set<String> tableNameSet); @Override void run();  }
@Test public void testBalanceAllTables() throws Exception { List<ServerName> serverNameList; List<HRegionInfo> regionInfoList; String tableName2 = createAdditionalTable(TestBase.tableName + "2"); String tableName3 = createAdditionalTable(TestBase.tableName + "3"); serverNameList = getServerNameList(); ServerName rs1 = serverNameList.get(0); regionInfoList = getRegionInfoList(tableName); regionInfoList.addAll(getRegionInfoList(tableName2)); regionInfoList.addAll(getRegionInfoList(tableName3)); for (HRegionInfo hRegionInfo : regionInfoList) { move(hRegionInfo, rs1); } Assert.assertEquals(3, getRegionInfoList(rs1, tableName).size() + getRegionInfoList(rs1, tableName2).size() + getRegionInfoList(rs1, tableName3).size()); String[] argsParam = {"zookeeper", ".*", "st", "--force-proceed", "--test"}; Args args = new ManagerArgs(argsParam); Assert.assertEquals("zookeeper", args.getZookeeperQuorum()); Balance command = new Balance(admin, args); command.run(); Assert.assertNotEquals(3, getRegionInfoList(rs1, tableName).size() + getRegionInfoList(rs1, tableName2).size() + getRegionInfoList(rs1, tableName3).size()); }