From 492bc4ed54100d5f1a7eb5c3682d25f4c2902158 Mon Sep 17 00:00:00 2001 From: blrunner Date: Mon, 4 Aug 2014 15:09:49 +0900 Subject: [PATCH 1/2] TAJO-990: Implement a tool to find tajo configurations. --- .../java/org/apache/tajo/cli/TajoCli.java | 3 +- .../apache/tajo/cli/TajoGetConfCommand.java | 57 ++++++ .../org/apache/tajo/client/TajoGetConf.java | 173 ++++++++++++++++++ tajo-dist/src/main/bin/tajo | 4 + 4 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java create mode 100644 tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java b/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java index 98a5bf456a..c20e44b1d9 100644 --- a/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java +++ b/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java @@ -79,7 +79,8 @@ public class TajoCli { UnsetCommand.class, ExecExternalShellCommand.class, HdfsCommand.class, - TajoAdminCommand.class + TajoAdminCommand.class, + TajoGetConfCommand.class }; private final Map commands = new TreeMap(); diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java b/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java new file mode 100644 index 0000000000..e65fa5f2ea --- /dev/null +++ b/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.cli; + +import org.apache.tajo.client.TajoGetConf; + +public class TajoGetConfCommand extends TajoShellCommand { + private TajoGetConf getconf; + + public TajoGetConfCommand(TajoCli.TajoCliContext context) { + super(context); + getconf = new TajoGetConf(context.getConf(), context.getOutput(), context.getTajoClient()); + } + + @Override + public String getCommand() { + return "\\getconf"; + } + + @Override + public void invoke(String[] command) throws Exception { + try { + String[] dfsCommands = new String[command.length - 1]; + System.arraycopy(command, 1, dfsCommands, 0, dfsCommands.length); + + getconf.runCommand(dfsCommands); + } catch (Exception e) { + context.getOutput().println("ERROR: " + e.getMessage()); + } + } + + @Override + public String getUsage() { + return " [options]"; + } + + @Override + public String getDescription() { + return "execute a tajo getconf command."; + } +} diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java new file mode 100644 index 0000000000..6da4c82068 --- /dev/null +++ b/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java @@ -0,0 +1,173 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tajo.client; + +import com.google.protobuf.ServiceException; +import org.apache.commons.cli.*; +import org.apache.commons.lang.StringUtils; +import org.apache.tajo.QueryId; +import org.apache.tajo.TajoProtos; +import org.apache.tajo.conf.TajoConf; +import org.apache.tajo.ipc.ClientProtos.BriefQueryInfo; +import org.apache.tajo.ipc.ClientProtos.WorkerResourceInfo; +import org.apache.tajo.util.NetUtils; +import org.apache.tajo.util.TajoIdUtils; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.net.InetSocketAddress; +import java.sql.SQLException; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +public class TajoGetConf { + private static final org.apache.commons.cli.Options options; + + static { + options = new Options(); + options.addOption("h", "host", true, "Tajo server host"); + options.addOption("p", "port", true, "Tajo server port"); + options.addOption("masters", null, false, "gets list of tajomasters in the cluster"); + options.addOption("confKey", null, true, "gets a specific key from the configuration"); + } + + private TajoConf tajoConf; + private TajoClient tajoClient; + private Writer writer; + + public TajoGetConf(TajoConf tajoConf, Writer writer) { + this(tajoConf, writer, null); + } + + public TajoGetConf(TajoConf tajoConf, Writer writer, TajoClient tajoClient) { + this.tajoConf = tajoConf; + this.writer = writer; + this.tajoClient = tajoClient; + } + + private void printUsage() { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp( "getconf [options]", options ); + } + + public void runCommand(String[] args) throws Exception { + CommandLineParser parser = new PosixParser(); + CommandLine cmd = parser.parse(options, args); + + String param = ""; + int cmdType = 0; + + String hostName = null; + Integer port = null; + if (cmd.hasOption("h")) { + hostName = cmd.getOptionValue("h"); + } + if (cmd.hasOption("p")) { + port = Integer.parseInt(cmd.getOptionValue("p")); + } + + if (cmd.hasOption("masters")) { + cmdType = 1; + } else if (cmd.hasOption("confKey")) { + cmdType = 2; + param = cmd.getOptionValue("confKey"); + } + + // if there is no "-h" option, + if(hostName == null) { + if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { + // it checks if the client service address is given in configuration and distributed mode. + // if so, it sets entryAddr. + hostName = tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[0]; + } + } + if (port == null) { + if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { + // it checks if the client service address is given in configuration and distributed mode. + // if so, it sets entryAddr. + port = Integer.parseInt(tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[1]); + } + } + + if (cmdType == 0) { + printUsage(); + return; + } + + if ((hostName == null) ^ (port == null)) { + return; + } else if (hostName != null && port != null) { + tajoConf.setVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS, hostName + ":" + port); + tajoClient = new TajoClient(tajoConf); + } else if (hostName == null && port == null) { + tajoClient = new TajoClient(tajoConf); + } + + switch (cmdType) { + case 1: + processMasters(writer); + break; + case 2: + processConfKey(writer, param); + break; + default: + printUsage(); + break; + } + + writer.flush(); + } + + private void processMasters(Writer writer) throws ParseException, IOException, + ServiceException, SQLException { + String confMasterServiceAddr = tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_UMBILICAL_RPC_ADDRESS); + InetSocketAddress masterAddress = NetUtils.createSocketAddr(confMasterServiceAddr); + writer.write(masterAddress.getHostName()); + writer.write("\n"); + } + + private void processConfKey(Writer writer, String param) throws ParseException, IOException, + ServiceException, SQLException { + String value = tajoConf.get(param); + + if (value != null) { + writer.write(value); + } else { + writer.write("Configuration a is missing."); + } + + writer.write("\n"); + } + + public static void main(String [] args) throws Exception { + TajoConf conf = new TajoConf(); + + Writer writer = new PrintWriter(System.out); + try { + TajoGetConf admin = new TajoGetConf(conf, writer); + admin.runCommand(args); + } finally { + writer.close(); + System.exit(0); + } + } +} diff --git a/tajo-dist/src/main/bin/tajo b/tajo-dist/src/main/bin/tajo index a05c65c22f..a14c95edbe 100755 --- a/tajo-dist/src/main/bin/tajo +++ b/tajo-dist/src/main/bin/tajo @@ -348,6 +348,10 @@ elif [ "$COMMAND" = "admin" ] ; then CLASS='org.apache.tajo.client.TajoAdmin' TAJO_ROOT_LOGGER_APPENDER="${TAJO_ROOT_LOGGER_APPENDER:-NullAppender}" TAJO_OPTS="$TAJO_OPTS $TAJO_CLI_OPTS" +elif [ "$COMMAND" = "getconf" ] ; then + CLASS='org.apache.tajo.client.TajoGetConf' + TAJO_ROOT_LOGGER_APPENDER="${TAJO_ROOT_LOGGER_APPENDER:-NullAppender}" + TAJO_OPTS="$TAJO_OPTS $TAJO_CLI_OPTS" elif [ "$COMMAND" = "dump" ] ; then CLASS='org.apache.tajo.client.TajoDump' TAJO_ROOT_LOGGER_APPENDER="${TAJO_ROOT_LOGGER_APPENDER:-NullAppender}" From b2b6ef75355d48181461645882377e7ff24d4be2 Mon Sep 17 00:00:00 2001 From: blrunner Date: Mon, 4 Aug 2014 15:12:33 +0900 Subject: [PATCH 2/2] Modified local variable name. --- .../main/java/org/apache/tajo/cli/TajoGetConfCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java b/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java index e65fa5f2ea..83ba4dde53 100644 --- a/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java +++ b/tajo-client/src/main/java/org/apache/tajo/cli/TajoGetConfCommand.java @@ -36,10 +36,10 @@ public String getCommand() { @Override public void invoke(String[] command) throws Exception { try { - String[] dfsCommands = new String[command.length - 1]; - System.arraycopy(command, 1, dfsCommands, 0, dfsCommands.length); + String[] getConfCommands = new String[command.length - 1]; + System.arraycopy(command, 1, getConfCommands, 0, getConfCommands.length); - getconf.runCommand(dfsCommands); + getconf.runCommand(getConfCommands); } catch (Exception e) { context.getOutput().println("ERROR: " + e.getMessage()); }