Assign implements Command { @Override public void run() throws Exception { String actionStr = ((String) args.getOptionSet().nonOptionArguments().get(1)).toUpperCase(); AssignAction assignAction = AssignAction.valueOf(actionStr); assignAction.run(admin, args); } Assign(HBaseAdmin admin, Args args); @SuppressWarnings("unused") static String usage(); @Override void run();  }
@Test public void testExportImportWithBalancerOffOption() throws Exception { String expFileName = "export_test.exp"; boolean balancerRunning = false; try { balancerRunning = false; String[] argsParam = {"zookeeper", "export", expFileName}; Args args = new ManagerArgs(argsParam); Assign command = new Assign(admin, args); balancerRunning = admin.setBalancerRunning(true, true); command.run(); fail(); } catch (IllegalStateException e) { if (!e.getMessage().contains(AssignAction.MESSAGE_TURN_BALANCER_OFF)) throw e; } finally { if (balancerRunning) admin.setBalancerRunning(true, true); } try { balancerRunning = false; String[] argsParam = {"zookeeper", "export", expFileName, "--" + Args.OPTION_TURN_BALANCER_OFF, "--skip-export", "--force-proceed"}; Args args = new ManagerArgs(argsParam); Assign command = new Assign(admin, args); balancerRunning = admin.setBalancerRunning(true, true); command.run(); } finally { if (balancerRunning) admin.setBalancerRunning(true, true); } try { balancerRunning = false; String[] argsParam = {"zookeeper", "import", expFileName}; Args args = new ManagerArgs(argsParam); Assign command = new Assign(admin, args); balancerRunning = admin.setBalancerRunning(true, true); command.run(); fail(); } catch (IllegalStateException e) { if (!e.getMessage().contains(AssignAction.MESSAGE_TURN_BALANCER_OFF)) throw e; } finally { if (balancerRunning) admin.setBalancerRunning(true, true); } try { balancerRunning = false; String[] argsParam = {"zookeeper", "import", expFileName, "--" + Args.OPTION_TURN_BALANCER_OFF, "--skip-export", "--force-proceed"}; Args args = new ManagerArgs(argsParam); Assign command = new Assign(admin, args); balancerRunning = admin.setBalancerRunning(true, true); command.run(); } finally { if (balancerRunning) admin.setBalancerRunning(true, true); } }
@Test public void testExportImport() throws Exception { ArrayList<ServerName> serverNameList; List<HRegionInfo> regionInfoList; ServerName rs1, rs2; List<String> assignmentList; String expFileName = "export_test.exp"; splitTable("a".getBytes()); splitTable("b".getBytes()); serverNameList = getServerNameList(); rs1 = serverNameList.get(0); rs2 = serverNameList.get(1); regionInfoList = getRegionInfoList(tableName); for (HRegionInfo hRegionInfo : regionInfoList) move(hRegionInfo, rs1); assertEquals(regionInfoList.size(), getRegionInfoList(rs1, tableName).size()); boolean balancerRunning = false; try { String[] argsParam; Args args; Assign command; balancerRunning = admin.setBalancerRunning(false, true); argsParam = new String[]{"zookeeper", "export", expFileName}; args = new ManagerArgs(argsParam); command = new Assign(admin, args); command.run(); assignmentList = readExportFile(expFileName); assertEquals(AssignAction.getProcessedCount(), assignmentList.size()); try (PrintWriter writer = new PrintWriter(expFileName, Constant.CHARSET.name())) { int i = 0, j = 0; for (String assignment : assignmentList) { if (i == 0) { writer.println(assignment.replaceAll("/.*/", "/nonono/")); } if (j == 0 && assignment.endsWith(tableName)) { writer.println(assignment.replaceAll("/" + tableName, "/" + tableName + "_INVALID")); j++; } if (i == 2) { int index1 = assignment.indexOf(","); int index2 = assignment.indexOf(",", index1 + 1); int index3 = assignment.indexOf(AssignAction.DELIMITER, index2 + 1); String rsTimestamp = assignment.substring(index2 + 1, index3); writer.println(assignment.replaceAll(rsTimestamp, String.valueOf(System.currentTimeMillis()))); } else { writer.println(assignment); } i++; } } assignmentList = readExportFile(expFileName); assertEquals(AssignAction.getProcessedCount() + 2, assignmentList.size()); int regionCountRS1 = getRegionInfoList(rs1, tableName).size(); regionInfoList = getRegionInfoList(tableName); for (HRegionInfo hRegionInfo : regionInfoList) move(hRegionInfo, rs2); assertEquals(0, getRegionInfoList(rs1, tableName).size()); assertEquals(regionInfoList.size(), getRegionInfoList(rs2, tableName).size()); balancerRunning = admin.setBalancerRunning(false, true); argsParam = new String[]{"zookeeper", "import", expFileName, "--force-proceed"}; args = new ManagerArgs(argsParam); command = new Assign(admin, args); command.run(); assertEquals(regionCountRS1, getRegionInfoList(rs1, tableName).size()); assertEquals(regionInfoList.size(), getRegionInfoList(tableName).size()); } finally { if (balancerRunning) admin.setBalancerRunning(true, true); Files.delete(Paths.get(expFileName)); } }
@Test public void testExportWithRS() throws Exception { ArrayList<ServerName> serverNameList; List<HRegionInfo> regionInfoList; ServerName rs1, rs2; List<String> assignmentList; String expFileName = "export_test.exp"; splitTable("a".getBytes()); splitTable("b".getBytes()); serverNameList = getServerNameList(); rs1 = serverNameList.get(0); rs2 = serverNameList.get(1); regionInfoList = getRegionInfoList(tableName); for (HRegionInfo hRegionInfo : regionInfoList) move(hRegionInfo, rs1); HRegionInfo firstRegion = regionInfoList.get(0); move(firstRegion, rs2); assertEquals(regionInfoList.size() - 1, getRegionInfoList(rs1, tableName).size()); assertEquals(1, getRegionInfoList(rs2, tableName).size()); boolean balancerRunning = false; try { String[] argsParam; Args args; Assign command; balancerRunning = admin.setBalancerRunning(false, true); argsParam = new String[]{"zookeeper", "export", expFileName, "--rs=" + rs1.getServerName()}; args = new ManagerArgs(argsParam); command = new Assign(admin, args); command.run(); assignmentList = readExportFile(expFileName); assertEquals(AssignAction.getProcessedCount(), assignmentList.size()); for (String assignment : assignmentList) { assertFalse(firstRegion.getEncodedName() + " must not be exported.", assignment.contains(firstRegion.getEncodedName())); } String regex = rs2.getServerName().substring(0, rs2.getServerName().indexOf(',')) + ".*"; argsParam = new String[]{"zookeeper", "export", expFileName, "--rs=" + regex}; args = new ManagerArgs(argsParam); command = new Assign(admin, args); command.run(); assignmentList = readExportFile(expFileName); assertEquals(AssignAction.getProcessedCount(), assignmentList.size()); int actual = 0; for (String assignment : assignmentList) { if (assignment.contains(firstRegion.getEncodedName())) actual++; } assertEquals(firstRegion.getEncodedName() + " must be exported.", 1, actual); } finally { if (balancerRunning) admin.setBalancerRunning(true, true); Files.delete(Paths.get(expFileName)); } }
@Test public void testExportRS1ImportRS2() throws Exception { ArrayList<ServerName> serverNameList; List<HRegionInfo> regionInfoList; ServerName rs1, rs2; String expFileName = "export_test.exp"; splitTable("a".getBytes()); splitTable("b".getBytes()); serverNameList = getServerNameList(); rs1 = serverNameList.get(0); rs2 = serverNameList.get(1); regionInfoList = getRegionInfoList(tableName); for (HRegionInfo hRegionInfo : regionInfoList) move(hRegionInfo, rs1); HRegionInfo region1 = regionInfoList.get(0); HRegionInfo region2 = regionInfoList.get(1); HRegionInfo region3 = regionInfoList.get(2); move(region1, rs2); assertEquals(region1, getRegionInfoList(rs2, tableName).get(0)); assertEquals(region2, getRegionInfoList(rs1, tableName).get(0)); assertEquals(region3, getRegionInfoList(rs1, tableName).get(1)); boolean balancerRunning = false; try { String[] argsParam; Args args; Assign command; balancerRunning = admin.setBalancerRunning(false, true); argsParam = new String[]{"zookeeper", "export", expFileName, "--rs=" + rs1.getServerName()}; args = new ManagerArgs(argsParam); command = new Assign(admin, args); command.run(); move(region1, rs1); move(region2, rs2); move(region3, rs1); assertEquals(region1, getRegionInfoList(rs1, tableName).get(0)); assertEquals(region2, getRegionInfoList(rs2, tableName).get(0)); assertEquals(region3, getRegionInfoList(rs1, tableName).get(1)); argsParam = new String[]{"zookeeper", "import", expFileName, "--force-proceed", "--rs=" + rs2.getServerName()}; args = new ManagerArgs(argsParam); command = new Assign(admin, args); command.run(); assertEquals(region1, getRegionInfoList(rs1, tableName).get(0)); assertEquals(region2, getRegionInfoList(rs2, tableName).get(0)); assertEquals(region3, getRegionInfoList(rs1, tableName).get(1)); } finally { if (balancerRunning) admin.setBalancerRunning(true, true); Files.delete(Paths.get(expFileName)); } }