MC implements Command { @Override public void run() throws Exception { targets = Collections.newSetFromMap(new TreeMap<byte[], Boolean>(Bytes.BYTES_COMPARATOR)); tableLevel = false; Set<String> tables = Args.tables(args, admin); assert tables != null; for (String table : tables) { if (args.has(Args.OPTION_REGION_SERVER) || args.has(Args.OPTION_LOCALITY_THRESHOLD)) { tableLevel = false; if (args.has(Args.OPTION_REGION_SERVER)) { filterWithRsAndLocality(targets, table); } else { if (args.has(Args.OPTION_LOCALITY_THRESHOLD)) { filterWithLocalityOnly(targets, table); } } } else { tableLevel = true; targets.add(table.getBytes()); } } if (tableLevel) { System.out.println(targets.size() + " tables will be compacted."); } else { System.out.println(targets.size() + " regions will be compacted."); } if (targets.size() == 0) return; if (!args.isForceProceed() && !Util.askProceed()) return; mc(tableLevel, targets); if (mcCounter.get() > 0) waitUntilFinish(tables); } MC(HBaseAdmin admin, Args args); @SuppressWarnings("unused") static String usage(); @Override void run();  }
@Test public void testMC_CF() throws Exception { HColumnDescriptor cd = new HColumnDescriptor(TEST_TABLE_CF2.getBytes()); admin.addColumn(tableName, cd); ArrayList<ServerName> serverNameList = getServerNameList(); assertTrue(serverNameList.size() >= 2); ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName); assertEquals(1, regionInfoList.size()); HRegionInfo regionInfo = regionInfoList.get(0); ServerName serverName = serverNameList.get(0); move(regionInfo, serverName); putData2(table, "a".getBytes()); admin.flush(tableName); putData2(table, "b".getBytes()); admin.flush(tableName); Thread.sleep(3000); assertEquals(2 + 2, getRegionLoad(regionInfo, serverName).getStorefiles()); String[] argsParam = {"zookeeper", tableName, "--cf=d", "--force-proceed", "--wait", "--test"}; Args args = new ManagerArgs(argsParam); MC command = new MC(admin, args); command.run(); assertRegionName(command); assertEquals(2 + 1, getRegionLoad(regionInfo, serverName).getStorefiles()); }
@Test public void testMC_InvalidCF() throws Exception { ArrayList<ServerName> serverNameList = getServerNameList(); assertTrue(serverNameList.size() >= 2); ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName); assertEquals(1, regionInfoList.size()); HRegionInfo regionInfo = regionInfoList.get(0); ServerName serverName = serverNameList.get(0); move(regionInfo, serverName); putData(table, "a".getBytes()); admin.flush(tableName); putData(table, "b".getBytes()); admin.flush(tableName); Thread.sleep(3000); assertEquals(2, getRegionLoad(regionInfo, serverName).getStorefiles()); String[] argsParam = {"zookeeper", tableName, "--cf=e", "--force-proceed", "--wait", "--test"}; Args args = new ManagerArgs(argsParam); MC command = new MC(admin, args); command.run(); assertRegionName(command); assertEquals(2, getRegionLoad(regionInfo, serverName).getStorefiles()); }
@Test public void testMC_MultipleRSs() throws Exception { splitTable("c".getBytes()); ArrayList<ServerName> serverNameList = getServerNameList(); assertTrue(serverNameList.size() >= 2); ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName); assertEquals(2, regionInfoList.size()); HRegionInfo regionInfo1 = regionInfoList.get(0); HRegionInfo regionInfo2 = regionInfoList.get(1); ServerName serverName1 = serverNameList.get(0); ServerName serverName2 = serverNameList.get(1); move(regionInfo1, serverName1); move(regionInfo2, serverName2); putData(table, "a".getBytes()); admin.flush(tableName); putData(table, "b".getBytes()); admin.flush(tableName); putData(table, "c".getBytes()); admin.flush(tableName); putData(table, "d".getBytes()); admin.flush(tableName); Thread.sleep(3000); assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles()); assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles()); String[] argsParam = {"zookeeper", tableName, "--force-proceed", "--wait", "--test"}; Args args = new ManagerArgs(argsParam); MC command = new MC(admin, args); command.run(); assertRegionName(command); assertEquals(1, getRegionLoad(regionInfo1, serverName1).getStorefiles()); assertEquals(1, getRegionLoad(regionInfo2, serverName2).getStorefiles()); }
@Test public void testMC_RS() throws Exception { splitTable("c".getBytes()); ArrayList<ServerName> serverNameList = getServerNameList(); assertTrue(serverNameList.size() >= 2); ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName); assertEquals(2, regionInfoList.size()); HRegionInfo regionInfo1 = regionInfoList.get(0); HRegionInfo regionInfo2 = regionInfoList.get(1); ServerName serverName1 = serverNameList.get(0); ServerName serverName2 = serverNameList.get(1); move(regionInfo1, serverName1); move(regionInfo2, serverName2); putData(table, "a".getBytes()); admin.flush(tableName); putData(table, "b".getBytes()); admin.flush(tableName); putData(table, "c".getBytes()); admin.flush(tableName); putData(table, "d".getBytes()); admin.flush(tableName); Thread.sleep(3000); assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles()); assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles()); String regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*"; String[] argsParam = {"zookeeper", tableName, "--rs=" + regex, "--force-proceed", "--wait", "--test"}; Args args = new ManagerArgs(argsParam); MC command = new MC(admin, args); command.run(); assertRegionName(command); assertEquals(1, getRegionLoad(regionInfo1, serverName1).getStorefiles()); assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles()); }
@Test public void testMC_Locality() throws Exception { putData(table, "a".getBytes()); admin.flush(tableName); putData(table, "b".getBytes()); admin.flush(tableName); Thread.sleep(3000); String[] argsParam = {"zookeeper", tableName, "--locality=100", "--force-proceed"}; Args args = new ManagerArgs(argsParam); MC command = new MC(admin, args); try { command.run(); fail(); } catch (IllegalStateException e) { if (!e.getMessage().contains("Option locality is not supported in this HBase version")) throw e; } }
@Test public void testMC_WaitUntilFinishing() throws Exception { ArrayList<ServerName> serverNameList = getServerNameList(); assertTrue(serverNameList.size() >= 2); ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName); assertEquals(1, regionInfoList.size()); HRegionInfo regionInfo = regionInfoList.get(0); ServerName serverName = serverNameList.get(0); move(regionInfo, serverName); putData(table, "a".getBytes()); admin.flush(tableName); putData(table, "b".getBytes()); admin.flush(tableName); Thread.sleep(3000); assertEquals(2, getRegionLoad(regionInfo, serverName).getStorefiles()); String[] argsParam = {"zookeeper", tableName, "--force-proceed", "--wait", "--test"}; Args args = new ManagerArgs(argsParam); MC command = new MC(admin, args); command.run(); assertRegionName(command); assertEquals(1, getRegionLoad(regionInfo, serverName).getStorefiles()); }