You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by sa...@apache.org on 2019/08/20 21:21:52 UTC

[hbase-operator-tools] branch master updated: HBASE-22865 [HBCK2] shows the whole help/usage message after the error message (#15)

This is an automated email from the ASF dual-hosted git repository.

sakthi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hbase-operator-tools.git


The following commit(s) were added to refs/heads/master by this push:
     new ca849af  HBASE-22865 [HBCK2] shows the whole help/usage message after the error message (#15)
ca849af is described below

commit ca849afa13fa84668193ade1413af05e079d6d54
Author: Sakthi <sa...@apache.org>
AuthorDate: Tue Aug 20 14:21:47 2019 -0700

    HBASE-22865 [HBCK2] shows the whole help/usage message after the error message (#15)
    
    Signed-off-by: Sean Busbey <bu...@apache.org>
---
 .../main/java/org/apache/hbase/FileSystemFsck.java |  4 +-
 .../src/main/java/org/apache/hbase/HBCK2.java      | 46 +++++++++++-----------
 .../java/org/apache/hbase/ReplicationFsck.java     |  4 +-
 .../src/main/java/org/apache/hbase/Version.java    |  2 +-
 .../apache/hbase/TestHBCKCommandLineParsing.java   | 25 +++++++++++-
 5 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/hbase-hbck2/src/main/java/org/apache/hbase/FileSystemFsck.java b/hbase-hbck2/src/main/java/org/apache/hbase/FileSystemFsck.java
index 28fe2ec..47892b9 100644
--- a/hbase-hbck2/src/main/java/org/apache/hbase/FileSystemFsck.java
+++ b/hbase-hbck2/src/main/java/org/apache/hbase/FileSystemFsck.java
@@ -56,7 +56,7 @@ public class FileSystemFsck implements Closeable {
     // 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 @@ public class FileSystemFsck implements Closeable {
     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());
diff --git a/hbase-hbck2/src/main/java/org/apache/hbase/HBCK2.java b/hbase-hbck2/src/main/java/org/apache/hbase/HBCK2.java
index a1c3d47..cab15a8 100644
--- a/hbase-hbck2/src/main/java/org/apache/hbase/HBCK2.java
+++ b/hbase-hbck2/src/main/java/org/apache/hbase/HBCK2.java
@@ -168,7 +168,7 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
     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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
     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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
     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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
     }
     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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
   }
 
   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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
     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 class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
     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 class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
       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 class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
       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 class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
       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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
       // 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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
 
       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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
 
       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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
 
       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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
 
       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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
         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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
         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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
 
       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 @@ public class HBCK2 extends Configured implements org.apache.hadoop.util.Tool {
         break;
 
       default:
-        usage(options, "Unsupported command: " + command);
+        showErrorMessage("Unsupported command: " + command);
         return EXIT_FAILURE;
     }
     return EXIT_SUCCESS;
diff --git a/hbase-hbck2/src/main/java/org/apache/hbase/ReplicationFsck.java b/hbase-hbck2/src/main/java/org/apache/hbase/ReplicationFsck.java
index 22be5a7..12965a6 100644
--- a/hbase-hbck2/src/main/java/org/apache/hbase/ReplicationFsck.java
+++ b/hbase-hbck2/src/main/java/org/apache/hbase/ReplicationFsck.java
@@ -46,7 +46,7 @@ public class ReplicationFsck implements Closeable {
     // 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 @@ public class ReplicationFsck implements Closeable {
     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());
diff --git a/hbase-hbck2/src/main/java/org/apache/hbase/Version.java b/hbase-hbck2/src/main/java/org/apache/hbase/Version.java
index 284c61a..505456a 100644
--- a/hbase-hbck2/src/main/java/org/apache/hbase/Version.java
+++ b/hbase-hbck2/src/main/java/org/apache/hbase/Version.java
@@ -33,7 +33,7 @@ public final class 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.
diff --git a/hbase-hbck2/src/test/java/org/apache/hbase/TestHBCKCommandLineParsing.java b/hbase-hbck2/src/test/java/org/apache/hbase/TestHBCKCommandLineParsing.java
index ca489c6..31b0247 100644
--- a/hbase-hbck2/src/test/java/org/apache/hbase/TestHBCKCommandLineParsing.java
+++ b/hbase-hbck2/src/test/java/org/apache/hbase/TestHBCKCommandLineParsing.java
@@ -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 @@ public class TestHBCKCommandLineParsing {
     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);