phoenix-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] dbwong commented on a change in pull request #430: PHOENIX-5091 Add new features to UpdateStatisticsTool
Date Fri, 25 Jan 2019 23:15:49 GMT
dbwong commented on a change in pull request #430: PHOENIX-5091 Add new features to UpdateStatisticsTool
URL: https://github.com/apache/phoenix/pull/430#discussion_r251165764
 
 

 ##########
 File path: phoenix-core/src/main/java/org/apache/phoenix/schema/stats/UpdateStatisticsTool.java
 ##########
 @@ -77,49 +82,103 @@
             "Restore Directory for HBase snapshot");
     private static final Option RUN_FOREGROUND_OPTION =
             new Option("runfg", "run-foreground", false,
-                    "If specified, runs UpdateStatisticsTool in Foreground. Default - Runs
the build in background.");
+                    "If specified, runs UpdateStatisticsTool in Foreground. Default - Runs
the build in background");
+    private static final Option MANAGE_SNAPSHOT_OPTION =
+            new Option("ms", "manage-snapshot", false,
+                    "Creates a new snapshot, runs the tool and deletes it");
+
     private static final Option HELP_OPTION = new Option("h", "help", false, "Help");
 
-    private Configuration conf;
     private String tableName;
     private String snapshotName;
     private Path restoreDir;
+    private boolean manageSnapshot;
     private boolean isForeground;
 
+    private Job job;
+
     @Override
     public int run(String[] args) throws Exception {
         parseArgs(args);
-        Job job = configureJob(conf, tableName, snapshotName, restoreDir);
+        preJobTask();
+        configureJob();
         TableMapReduceUtil.initCredentials(job);
-        return runJob(job, isForeground);
+        int ret = runJob();
+        postJobTask();
+        return ret;
+    }
+
+    /**
+     * Run any tasks before the MR job is launched
+     * Currently being used for snapshot creation
+     */
+    private void preJobTask() throws Exception {
+        if (!manageSnapshot) {
+            return;
+        }
+
+        try (final Connection conn = ConnectionUtil.getInputConnection(getConf())) {
+            HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
+            boolean namespaceMapping = getConf().getBoolean(IS_NAMESPACE_MAPPING_ENABLED,
+                    DEFAULT_IS_NAMESPACE_MAPPING_ENABLED);
+            String physicalTableName =  SchemaUtil.getPhysicalTableName(tableName.getBytes(),
+                    namespaceMapping).getNameAsString();
+            admin.snapshot(snapshotName, physicalTableName);
+            LOG.info("Successfully created snapshot " + snapshotName + " for " + physicalTableName);
+        }
+    }
+
+    /**
+     * Run any tasks before the MR job is completed successfully
+     * Currently being used for snapshot deletion
+     */
+    private void postJobTask() throws Exception {
+        if (!manageSnapshot) {
+            return;
+        }
+
+        try (final Connection conn = ConnectionUtil.getInputConnection(getConf())) {
+            HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
+            admin.deleteSnapshot(snapshotName);
+            LOG.info("Successfully deleted snapshot " + snapshotName);
+        }
     }
 
-    private void parseArgs(String[] args) {
+    void parseArgs(String[] args) {
         CommandLine cmdLine = null;
         try {
             cmdLine = parseOptions(args);
         } catch (IllegalStateException e) {
             printHelpAndExit(e.getMessage(), getOptions());
         }
 
-        conf = HBaseConfiguration.create();
+        if (getConf() == null) {
+            setConf(HBaseConfiguration.create());
+        }
+
         tableName = cmdLine.getOptionValue(TABLE_NAME_OPTION.getOpt());
         snapshotName = cmdLine.getOptionValue(SNAPSHOT_NAME_OPTION.getOpt());
+        if (snapshotName == null) {
+            snapshotName = "UpdateStatisticsTool_" + tableName + "_" + System.currentTimeMillis();
+        }
+
         String restoreDirOptionValue = cmdLine.getOptionValue(RESTORE_DIR_OPTION.getOpt());
         if (restoreDirOptionValue == null) {
-            restoreDirOptionValue = conf.get(FS_DEFAULT_NAME_KEY) + "/tmp";
+            restoreDirOptionValue = getConf().get(FS_DEFAULT_NAME_KEY) + "/tmp";
         }
+        
         restoreDir = new Path(restoreDirOptionValue);
+        manageSnapshot = cmdLine.hasOption(MANAGE_SNAPSHOT_OPTION.getOpt());
         isForeground = cmdLine.hasOption(RUN_FOREGROUND_OPTION.getOpt());
     }
 
-    Job configureJob(Configuration conf, String tableName,
-                     String snapshotName, Path restoreDir) throws Exception {
-        Job job = Job.getInstance(conf, "Update statistics for " + tableName);
+    private void configureJob() throws Exception {
+        job = Job.getInstance(getConf(), "Update statistics for " + tableName);
 
 Review comment:
   I think you misunderstood slightly, I'm wondering if there is any benefit to add some of
the new options to the job Name like background/managed etc?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message