Repository: hbase Updated Branches: refs/heads/master b3d8d0670 -> 37b30d93d
HBASE-17111 Use Apache CLI in SnapshotInfo tool. Change-Id: Ifd4b0200ba58737c5583dfbcf23f2549c4f233d0 Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/37b30d93 Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/37b30d93 Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/37b30d93 Branch: refs/heads/master Commit: 37b30d93d8b73a0c7392890e1e2498a298c2be86 Parents: b3d8d06 Author: Apekshit Sharma <a...@apache.org> Authored: Wed Nov 16 12:46:55 2016 +0100 Committer: Apekshit Sharma <a...@apache.org> Committed: Thu Dec 1 17:53:01 2016 -0800 ---------------------------------------------------------------------- .../hadoop/hbase/snapshot/ExportSnapshot.java | 1 - .../hadoop/hbase/snapshot/SnapshotInfo.java | 144 ++++++++----------- 2 files changed, 63 insertions(+), 82 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/37b30d93/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java index 6c7283a..e8ca73e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java @@ -1087,7 +1087,6 @@ public class ExportSnapshot extends AbstractHBaseTool implements Tool { + " hbase snapshot export \\\n" + " --snapshot MySnapshot --copy-from hdfs://srv2:8082/hbase \\\n" + " --copy-to hdfs://srv1:50070/hbase"); - System.exit(1); } @Override protected void addOptions() { http://git-wip-us.apache.org/repos/asf/hbase/blob/37b30d93/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java index cd20274..3922348 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java @@ -32,6 +32,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -41,15 +43,12 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.hbase.client.SnapshotDescription; -import org.apache.hadoop.conf.Configured; import org.apache.hadoop.hbase.HRegionInfo; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.util.AbstractHBaseTool; import org.apache.hadoop.util.StringUtils; -import org.apache.hadoop.util.Tool; -import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.io.HFileLink; import org.apache.hadoop.hbase.io.WALLink; import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; @@ -68,9 +67,23 @@ import org.apache.hadoop.hbase.util.FSUtils; */ @InterfaceAudience.Public @InterfaceStability.Evolving -public final class SnapshotInfo extends Configured implements Tool { +public final class SnapshotInfo extends AbstractHBaseTool { private static final Log LOG = LogFactory.getLog(SnapshotInfo.class); + static final class Options { + static final Option SNAPSHOT = new Option(null, "snapshot", true, "Snapshot to examine."); + static final Option REMOTE_DIR = new Option(null, "remote-dir", true, + "Root directory that contains the snapshots."); + static final Option LIST_SNAPSHOTS = new Option(null, "list-snapshots", false, + "List all the available snapshots and exit."); + static final Option FILES = new Option(null, "files", false, "Files and logs list."); + static final Option STATS = new Option(null, "stats", false, "Files and logs stats."); + static final Option SCHEMA = new Option(null, "schema", false, + "Describe the snapshotted table."); + static final Option SIZE_IN_BYTES = new Option(null, "size-in-bytes", false, + "Print the size of the files in bytes."); + } + /** * Statistics about the snapshot * <ol> @@ -339,54 +352,25 @@ public final class SnapshotInfo extends Configured implements Tool { } } - private boolean printSizeInBytes = false; private FileSystem fs; private Path rootDir; private SnapshotManifest snapshotManifest; + private boolean listSnapshots = false; + private String snapshotName; + private Path remoteDir; + private boolean showSchema = false; + private boolean showFiles = false; + private boolean showStats = false; + private boolean printSizeInBytes = false; + @Override - @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION", - justification="Intentional") - public int run(String[] args) throws IOException, InterruptedException { - final Configuration conf = getConf(); - boolean listSnapshots = false; - String snapshotName = null; - boolean showSchema = false; - boolean showFiles = false; - boolean showStats = false; - - // Process command line args - for (int i = 0; i < args.length; i++) { - String cmd = args[i]; - try { - if (cmd.equals("-snapshot")) { - snapshotName = args[++i]; - } else if (cmd.equals("-files")) { - showFiles = true; - showStats = true; - } else if (cmd.equals("-stats")) { - showStats = true; - } else if (cmd.equals("-schema")) { - showSchema = true; - } else if (cmd.equals("-remote-dir")) { - Path sourceDir = new Path(args[++i]); - URI defaultFs = sourceDir.getFileSystem(conf).getUri(); - FSUtils.setFsDefault(conf, new Path(defaultFs)); - FSUtils.setRootDir(conf, sourceDir); - } else if (cmd.equals("-list-snapshots")) { - listSnapshots = true; - } else if (cmd.equals("-size-in-bytes")) { - printSizeInBytes = true; - } else if (cmd.equals("-h") || cmd.equals("--help")) { - printUsageAndExit(); - } else { - System.err.println("UNEXPECTED: " + cmd); - printUsageAndExit(); - } - } catch (Exception e) { - printUsageAndExit(); // FindBugs: REC_CATCH_EXCEPTION - } + public int doWork() throws IOException, InterruptedException { + if (remoteDir != null) { + URI defaultFs = remoteDir.getFileSystem(conf).getUri(); + FSUtils.setFsDefault(conf, new Path(defaultFs)); + FSUtils.setRootDir(conf, remoteDir); } // List Available Snapshots @@ -402,12 +386,6 @@ public final class SnapshotInfo extends Configured implements Tool { return 0; } - if (snapshotName == null) { - System.err.println("Missing snapshot name!"); - printUsageAndExit(); - return 1; - } - rootDir = FSUtils.getRootDir(conf); fs = FileSystem.get(rootDir.toUri(), conf); LOG.debug("fs=" + fs.getUri().toString() + " root=" + rootDir); @@ -461,7 +439,7 @@ public final class SnapshotInfo extends Configured implements Tool { } /** - * Dump the {@link HTableDescriptor} + * Dump the {@link org.apache.hadoop.hbase.HTableDescriptor} */ private void printSchema() { System.out.println("Table Descriptor"); @@ -536,22 +514,36 @@ public final class SnapshotInfo extends Configured implements Tool { return printSizeInBytes ? Long.toString(size) : StringUtils.humanReadableInt(size); } - private void printUsageAndExit() { - System.err.printf("Usage: bin/hbase snapshot info [options]%n"); - System.err.println(" where [options] are:"); - System.err.println(" -h|-help Show this help and exit."); - System.err.println(" -remote-dir Root directory that contains the snapshots."); - System.err.println(" -list-snapshots List all the available snapshots and exit."); - System.err.println(" -size-in-bytes Print the size of the files in bytes."); - System.err.println(" -snapshot NAME Snapshot to examine."); - System.err.println(" -files Files and logs list."); - System.err.println(" -stats Files and logs stats."); - System.err.println(" -schema Describe the snapshotted table."); - System.err.println(); + @Override + protected void addOptions() { + addRequiredOption(Options.SNAPSHOT); + addOption(Options.REMOTE_DIR); + addOption(Options.LIST_SNAPSHOTS); + addOption(Options.FILES); + addOption(Options.STATS); + addOption(Options.SCHEMA); + addOption(Options.SIZE_IN_BYTES); + } + + @Override + protected void processOptions(CommandLine cmd) { + snapshotName = cmd.getOptionValue(Options.SNAPSHOT.getLongOpt()); + showFiles = cmd.hasOption(Options.FILES.getLongOpt()); + showStats = cmd.hasOption(Options.FILES.getLongOpt()) + || cmd.hasOption(Options.STATS.getLongOpt()); + showSchema = cmd.hasOption(Options.SCHEMA.getLongOpt()); + listSnapshots = cmd.hasOption(Options.LIST_SNAPSHOTS.getLongOpt()); + printSizeInBytes = cmd.hasOption(Options.SIZE_IN_BYTES.getLongOpt()); + if (cmd.hasOption(Options.REMOTE_DIR.getLongOpt())) { + remoteDir = new Path(cmd.getOptionValue(Options.REMOTE_DIR.getLongOpt())); + } + } + + @Override + protected void printUsage() { + printUsage("bin/hbase snapshot info [options]", "Options:", ""); System.err.println("Examples:"); - System.err.println(" hbase snapshot info \\"); - System.err.println(" -snapshot MySnapshot -files"); - System.exit(1); + System.err.println(" hbase snapshot info --snapshot MySnapshot --files"); } /** @@ -714,18 +706,8 @@ public final class SnapshotInfo extends Configured implements Tool { return fileMap; } - /** - * The guts of the {@link #main} method. - * Call this method to avoid the {@link #main(String[])} System.exit. - * @param args - * @return errCode - * @throws Exception - */ - static int innerMain(final String [] args) throws Exception { - return ToolRunner.run(HBaseConfiguration.create(), new SnapshotInfo(), args); - } - public static void main(String[] args) throws Exception { - System.exit(innerMain(args)); + public static void main(String[] args) { + new SnapshotInfo().doStaticMain(args); } }