diff --git llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusOptionsProcessor.java llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusOptionsProcessor.java index cb848c0..306391b 100644 --- llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusOptionsProcessor.java +++ llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusOptionsProcessor.java @@ -40,12 +40,13 @@ enum OptionConstants { - NAME("name", 'n', "LLAP cluster name"), + NAME("name", 'n', "LLAP cluster name", true), FIND_APP_TIMEOUT("findAppTimeout", 'f', "Amount of time(s) that the tool will sleep to wait for the YARN application to start. negative values=wait forever, 0=Do not wait. default=" + - TimeUnit.SECONDS.convert(FIND_YARN_APP_TIMEOUT_MS, TimeUnit.MILLISECONDS) + "s"), + TimeUnit.SECONDS.convert(FIND_YARN_APP_TIMEOUT_MS, TimeUnit.MILLISECONDS) + "s", true), + OUTPUT_FILE("outputFile", 'o', "File to which output should be written (Default stdout)", true), HIVECONF("hiveconf", null, "Use value for given property. Overridden by explicit parameters", "property=value", 2), - HELP("help", 'H', "Print help information"); + HELP("help", 'H', "Print help information", false); private final String longOpt; @@ -54,9 +55,8 @@ private final String argName; private final int numArgs; - OptionConstants(String longOpt, char shortOpt, String description) { - this(longOpt, shortOpt, description, longOpt, 1); - + OptionConstants(String longOpt, char shortOpt, String description, boolean hasArgs) { + this(longOpt, shortOpt, description, longOpt, hasArgs ? 1 : 0); } OptionConstants(String longOpt, Character shortOpt, String description, String argName, int numArgs) { @@ -93,11 +93,18 @@ public int getNumArgs() { private final String name; private final Properties conf; private final long findAppTimeoutMs; + private final String outputFile; - LlapStatusOptions(String name, Properties hiveProperties, long findAppTimeoutMs) { + public LlapStatusOptions(String name, Properties hiveProperties, long findAppTimeoutMs, + String outputFile) { this.name = name; this.conf = hiveProperties; this.findAppTimeoutMs = findAppTimeoutMs; + this.outputFile = outputFile; + } + + public LlapStatusOptions(String name) { + this(name, new Properties(), FIND_YARN_APP_TIMEOUT_MS, null); } public String getName() { @@ -111,6 +118,10 @@ public Properties getConf() { public long getFindAppTimeoutMs() { return findAppTimeoutMs; } + + public String getOutputFile() { + return outputFile; + } } private final Options options = new Options(); @@ -154,11 +165,16 @@ public LlapStatusOptions processOptions(String[] args) throws ParseException { hiveConf = new Properties(); } - return new LlapStatusOptions(name, hiveConf, findAppTimeoutMs); + String outputFile = null; + if (commandLine.hasOption(OptionConstants.OUTPUT_FILE.getLongOpt())) { + outputFile = commandLine.getOptionValue(OptionConstants.OUTPUT_FILE.getLongOpt()); + } + + return new LlapStatusOptions(name, hiveConf, findAppTimeoutMs, outputFile); } - private void printUsage() { + public static void printUsage() { HelpFormatter hf = new HelpFormatter(); try { int width = hf.getWidth(); @@ -167,7 +183,9 @@ private void printUsage() { hf.setWidth(width); } catch (Throwable t) { // Ignore } - hf.printHelp(LLAPSTATUS_CONSTANT, options); + + LlapStatusOptionsProcessor optionsProcessor = new LlapStatusOptionsProcessor(); + hf.printHelp(LLAPSTATUS_CONSTANT, optionsProcessor.options); } } diff --git llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusServiceDriver.java llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusServiceDriver.java index 646c286..5209226 100644 --- llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusServiceDriver.java +++ llap-server/src/java/org/apache/hadoop/hive/llap/cli/LlapStatusServiceDriver.java @@ -19,7 +19,10 @@ package org.apache.hadoop.hive.llap.cli; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintWriter; import java.util.EnumSet; import java.util.HashMap; @@ -70,19 +73,29 @@ public LlapStatusServiceDriver() { conf = (ss != null) ? ss.getConf() : new HiveConf(SessionState.class); } + /** + * Parse command line options. + * + * @param args + * @return command line options. + */ + public LlapStatusOptions parseOptions(String[] args) throws LlapStatusCliException { + + LlapStatusOptionsProcessor optionsProcessor = new LlapStatusOptionsProcessor(); + LlapStatusOptions options; + try { + options = optionsProcessor.processOptions(args); + return options; + } catch (Exception e) { + LOG.info("Failed to parse arguments", e); + throw new LlapStatusCliException(ExitCode.INCORRECT_USAGE, "Incorrect usage"); + } + } - public int run(String[] args) { + public int run(LlapStatusOptions options) { SliderClient sliderClient = null; try { - LlapStatusOptionsProcessor optionsProcessor = new LlapStatusOptionsProcessor(); - LlapStatusOptions options; - try { - options = optionsProcessor.processOptions(args); - } catch (Exception e) { - LOG.info("Failed to parse arguments", e); - return ExitCode.INCORRECT_USAGE.getInt(); - } for (String f : LlapDaemonConfiguration.DAEMON_CONFIGS) { conf.addResource(f); @@ -111,6 +124,9 @@ public int run(String[] args) { LOG.info(message); return ExitCode.INCORRECT_USAGE.getInt(); } + if (LOG.isDebugEnabled()) { + LOG.debug("Using appName: {}", appName); + } try { sliderClient = createSliderClient(); @@ -157,7 +173,7 @@ public int run(String[] args) { return ret.getInt(); } else { try { - ret = populateAppStatusFromLlapRegistry(options, appStatusBuilder); + ret = populateAppStatusFromLlapRegistry(appName, appStatusBuilder); } catch (LlapStatusCliException e) { logError(e); return e.getExitCode().getInt(); @@ -382,16 +398,16 @@ private ExitCode populateAppStatusFromSlider(String appName, SliderClient slider /** * - * @param options + * @param appName * @param appStatusBuilder * @return an ExitCode. An ExitCode other than ExitCode.SUCCESS implies future progress not possible * @throws LlapStatusCliException */ - private ExitCode populateAppStatusFromLlapRegistry(LlapStatusOptions options, AppStatusBuilder appStatusBuilder) throws + private ExitCode populateAppStatusFromLlapRegistry(String appName, AppStatusBuilder appStatusBuilder) throws LlapStatusCliException { Configuration llapRegistryConf= new Configuration(conf); llapRegistryConf - .set(HiveConf.ConfVars.LLAP_DAEMON_SERVICE_HOSTS.varname, "@" + options.getName()); + .set(HiveConf.ConfVars.LLAP_DAEMON_SERVICE_HOSTS.varname, "@" + appName); LlapRegistryService llapRegistry; try { llapRegistry = LlapRegistryService.getClient(llapRegistryConf); @@ -836,12 +852,35 @@ private static void logError(Throwable t) { public static void main(String[] args) { LOG.info("LLAP status invoked with arguments = {}", args); - int ret; + int ret = ExitCode.SUCCESS.getInt(); + + LlapStatusServiceDriver statusServiceDriver = null; + LlapStatusOptions options = null; + try { + statusServiceDriver = new LlapStatusServiceDriver(); + options = statusServiceDriver.parseOptions(args); + } catch (Throwable t) { + logError(t); + if (t instanceof LlapStatusCliException) { + LlapStatusCliException ce = (LlapStatusCliException) t; + ret = ce.getExitCode().getInt(); + } else { + ret = ExitCode.INTERNAL_ERROR.getInt(); + } + } finally { + LOG.info("LLAP status finished"); + } + if (ret != 0 || options == null) { // Failure / help + System.exit(ret); + } + try { - LlapStatusServiceDriver statusServiceDriver = new LlapStatusServiceDriver(); - ret = statusServiceDriver.run(args); + ret = statusServiceDriver.run(options); if (ret == ExitCode.SUCCESS.getInt()) { - try (PrintWriter pw = new PrintWriter(System.out)) { + try (OutputStream os = options.getOutputFile() == null ? System.out : + new BufferedOutputStream( + new FileOutputStream(options.getOutputFile())); PrintWriter pw = new PrintWriter( + os)) { statusServiceDriver.outputJson(pw); } } diff --git service/src/java/org/apache/hive/http/LlapServlet.java service/src/java/org/apache/hive/http/LlapServlet.java index 65e23fc..993766b 100644 --- service/src/java/org/apache/hive/http/LlapServlet.java +++ service/src/java/org/apache/hive/http/LlapServlet.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.llap.cli.LlapStatusOptionsProcessor; import org.apache.hadoop.hive.llap.cli.LlapStatusServiceDriver; @SuppressWarnings("serial") @@ -97,7 +98,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) { LOG.info("Retrieving info for cluster: " + clusterName); LlapStatusServiceDriver driver = new LlapStatusServiceDriver(); - int ret = driver.run(new String[] { "-n", clusterName }); + int ret = driver.run(new LlapStatusOptionsProcessor.LlapStatusOptions(clusterName)); if (ret == LlapStatusServiceDriver.ExitCode.SUCCESS.getInt()) { driver.outputJson(writer); }