You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2019/01/25 23:13:27 UTC

[GitHub] karanmehta93 commented on a change in pull request #430: PHOENIX-5091 Add new features to UpdateStatisticsTool

karanmehta93 commented on a change in pull request #430: PHOENIX-5091 Add new features to UpdateStatisticsTool
URL: https://github.com/apache/phoenix/pull/430#discussion_r251165374
 
 

 ##########
 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:
   Yes, we can search in the Job history server UI with this specific prefix.

----------------------------------------------------------------
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