Skip to content
Permalink
Browse files
HBASE-22865 [HBCK2] shows the whole help/usage message after the erro…
…r message (#15)

Signed-off-by: Sean Busbey <busbey@apache.org>
  • Loading branch information
jatsakthi committed Aug 20, 2019
1 parent 379b8cc commit ca849afa13fa84668193ade1413af05e079d6d54
Showing 5 changed files with 51 additions and 30 deletions.
@@ -56,7 +56,7 @@ public void close() {
// Nothing to do.
}

int fsck(Options hbck2Options, String[] args) throws IOException {
int fsck(String[] args) throws IOException {
Options options = new Options();
Option fixOption = Option.builder("f").longOpt("fix").build();
options.addOption(fixOption);
@@ -66,7 +66,7 @@ int fsck(Options hbck2Options, String[] args) throws IOException {
try {
commandLine = parser.parse(options, args, false);
} catch(ParseException e) {
HBCK2.usage(hbck2Options, e.getMessage());
HBCK2.showErrorMessage(e.getMessage());
return -1;
}
boolean fix = commandLine.hasOption(fixOption.getOpt());
@@ -168,7 +168,7 @@ List<Long> assigns(Hbck hbck, String [] args) throws IOException {
try {
commandLine = parser.parse(options, args, false);
} catch (ParseException e) {
usage(options, e.getMessage());
showErrorMessage(e.getMessage());
return null;
}
boolean overrideFlag = commandLine.hasOption(override.getOpt());
@@ -185,7 +185,7 @@ List<Long> unassigns(Hbck hbck, String [] args) throws IOException {
try {
commandLine = parser.parse(options, args, false);
} catch (ParseException e) {
usage(options, e.getMessage());
showErrorMessage(e.getMessage());
return null;
}
boolean overrideFlag = commandLine.hasOption(override.getOpt());
@@ -211,7 +211,7 @@ private List<Boolean> bypass(String[] args) throws IOException {
try {
commandLine = parser.parse(options, args, false);
} catch (ParseException e) {
usage(options, e.getMessage());
showErrorMessage(e.getMessage());
return null;
}
long lockWait = DEFAULT_LOCK_WAIT;
@@ -220,7 +220,7 @@ private List<Boolean> bypass(String[] args) throws IOException {
}
String[] pidStrs = commandLine.getArgs();
if (pidStrs == null || pidStrs.length <= 0) {
usage(options, "No pids supplied.");
showErrorMessage("No pids supplied.");
return null;
}
boolean overrideFlag = commandLine.hasOption(override.getOpt());
@@ -258,7 +258,7 @@ private String readHBCK2BuildProperties(final String propertyKey) throws IOExcep
}

private static String getCommandUsage() {
// NOTE: List commands belonw alphabetically!
// NOTE: List commands below alphabetically!
StringWriter sw = new StringWriter();
PrintWriter writer = new PrintWriter(sw);
writer.println("Command:");
@@ -365,14 +365,14 @@ private static String getCommandUsage() {
return sw.toString();
}

private static void usage(Options options) {
usage(options, null);
}

static void usage(Options options, String error) {
static void showErrorMessage(String error) {
if (error != null) {
System.out.println("ERROR: " + error);
System.out.println("FOR USAGE, use the -h or --help option");
}
}

static void showUsage(Options options){
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("HBCK2 [OPTIONS] COMMAND <ARGS>",
"Options:", options, getCommandUsage());
@@ -421,7 +421,7 @@ public int run(String[] args) throws IOException {
try {
commandLine = parser.parse(options, args, true);
} catch (ParseException e) {
usage(options, e.getMessage());
showErrorMessage(e.getMessage());
return EXIT_FAILURE;
}
// Process general options.
@@ -430,7 +430,7 @@ public int run(String[] args) throws IOException {
return EXIT_SUCCESS;
}
if (commandLine.hasOption(help.getOpt()) || commandLine.getArgList().isEmpty()) {
usage(options);
showUsage(options);
return EXIT_SUCCESS;
}
if (commandLine.hasOption(debug.getOpt())) {
@@ -446,7 +446,7 @@ public int run(String[] args) throws IOException {
if (optionValue.matches("[0-9]+")) {
getConf().setInt(HConstants.ZOOKEEPER_CLIENT_PORT, Integer.valueOf(optionValue));
} else {
usage(options,
showErrorMessage(
"Invalid client port. Please provide proper port for target hbase ensemble.");
return EXIT_FAILURE;
}
@@ -456,7 +456,7 @@ public int run(String[] args) throws IOException {
if (optionValue.startsWith("/")) {
getConf().set(HConstants.ZOOKEEPER_ZNODE_PARENT, optionValue);
} else {
usage(options, "Invalid parent znode. Please provide proper parent znode of target hbase."
showErrorMessage("Invalid parent znode. Please provide proper parent znode of target hbase."
+ " Note that valid znodes must start with \"/\".");
return EXIT_FAILURE;
}
@@ -488,7 +488,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc
// the feature FIRST, then move to process the command.
case SET_TABLE_STATE:
if (commands.length < 3) {
usage(options, command + " takes tablename and state arguments: e.g. user ENABLED");
showErrorMessage(command + " takes tablename and state arguments: e.g. user ENABLED");
return EXIT_FAILURE;
}
try (ClusterConnection connection = connect(); Hbck hbck = connection.getHbck()) {
@@ -500,7 +500,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc

case ASSIGNS:
if (commands.length < 2) {
usage(options, command + " takes one or more encoded region names");
showErrorMessage(command + " takes one or more encoded region names");
return EXIT_FAILURE;
}
try (ClusterConnection connection = connect(); Hbck hbck = connection.getHbck()) {
@@ -511,7 +511,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc

case BYPASS:
if (commands.length < 2) {
usage(options, command + " takes one or more pids");
showErrorMessage(command + " takes one or more pids");
return EXIT_FAILURE;
}
// bypass does the connection setup and the checkHBCKSupport down
@@ -529,7 +529,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc

case UNASSIGNS:
if (commands.length < 2) {
usage(options, command + " takes one or more encoded region names");
showErrorMessage(command + " takes one or more encoded region names");
return EXIT_FAILURE;
}
try (ClusterConnection connection = connect(); Hbck hbck = connection.getHbck()) {
@@ -540,7 +540,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc

case SET_REGION_STATE:
if (commands.length < 3) {
usage(options, command + " takes region encoded name and state arguments: e.g. "
showErrorMessage(command + " takes region encoded name and state arguments: e.g. "
+ "35f30b0ce922c34bf5c284eff33ba8b3 CLOSING");
return EXIT_FAILURE;
}
@@ -554,7 +554,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc
try (ClusterConnection connection = connect()) {
checkHBCKSupport(connection, command);
try (FileSystemFsck fsfsck = new FileSystemFsck(getConf())) {
if (fsfsck.fsck(options, purgeFirst(commands)) != 0) {
if (fsfsck.fsck(purgeFirst(commands)) != 0) {
return EXIT_FAILURE;
}
}
@@ -565,7 +565,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc
try (ClusterConnection connection = connect()) {
checkHBCKSupport(connection, command);
try (ReplicationFsck replicationFsck = new ReplicationFsck(getConf())) {
if (replicationFsck.fsck(options, purgeFirst(commands)) != 0) {
if (replicationFsck.fsck(purgeFirst(commands)) != 0) {
return EXIT_FAILURE;
}
}
@@ -574,7 +574,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc

case SCHEDULE_RECOVERIES:
if (commands.length < 2) {
usage(options, command + " takes one or more serverNames");
showErrorMessage(command + " takes one or more serverNames");
return EXIT_FAILURE;
}
try (ClusterConnection connection = connect(); Hbck hbck = connection.getHbck()) {
@@ -584,7 +584,7 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc
break;

default:
usage(options, "Unsupported command: " + command);
showErrorMessage("Unsupported command: " + command);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
@@ -46,7 +46,7 @@ public void close() {
// Nothing to do.
}

int fsck(Options hbck2Options, String[] args) throws IOException {
int fsck(String[] args) throws IOException {
Options options = new Options();
Option fixOption = Option.builder("f").longOpt("fix").build();
options.addOption(fixOption);
@@ -56,7 +56,7 @@ int fsck(Options hbck2Options, String[] args) throws IOException {
try {
commandLine = parser.parse(options, args, false);
} catch(ParseException e) {
HBCK2.usage(hbck2Options, e.getMessage());
HBCK2.showErrorMessage(e.getMessage());
return -1;
}
boolean fix = commandLine.hasOption(fixOption.getOpt());
@@ -33,7 +33,7 @@ private Version() {}

/**
* @param thresholdVersions List of versions from oldest to newest.
* @return true if <code>version</code> is less-than or equal to thresholdVersions.
* @return true if <code>version</code> is greater-than or equal to thresholdVersions.
* For example, if passed threshold list is <code>{"2.0.2", "2.1.3", "2.2.1"}</code>
* and the version is 2.1.2 then the result should be false since 2.1.2
* is less than the matching passed-in 2.1.3 but if version is 2.1.5 then we return true.
@@ -38,8 +38,25 @@ public class TestHBCKCommandLineParsing {

@Test
public void testHelp() throws IOException {
String output = retrieveOptionOutput("-h");
// Passing no argument echoes out the usage info
String output = retrieveOptionOutput(null);
assertTrue(output, output.startsWith("usage: HBCK2"));

// Passing -h/--help does the same
output = retrieveOptionOutput("-h");
assertTrue(output, output.startsWith("usage: HBCK2"));
}

@Test
public void testErrorMessage() throws IOException{
// just chose some of the commands to test for
String[] cmds = new String[]{"setTableState", "bypass", "scheduleRecoveries"};
String output;
for(String cmd: cmds){
output = retrieveOptionOutput(cmd);
assertTrue(output, output.startsWith("ERROR: "));
assertTrue(output, output.contains("FOR USAGE, use the -h or --help option"));
}
}

@Test (expected=NumberFormatException.class)
@@ -74,7 +91,11 @@ private String retrieveOptionOutput(String option) throws IOException {
PrintStream oldOut = System.out;
System.setOut(stream);
HBCK2 hbck = new HBCK2(TEST_UTIL.getConfiguration());
hbck.run(new String[] { option });
if (option != null) {
hbck.run(new String[] { option });
} else {
hbck.run(null);
}
stream.close();
os.close();
System.setOut(oldOut);

0 comments on commit ca849af

Please sign in to comment.