diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/QueryCommand.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/QueryCommand.java new file mode 100644 index 00000000000..36448b8ebf5 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/diskbalancer/command/QueryCommand.java @@ -0,0 +1,82 @@ +/* + * 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.hadoop.hdfs.server.diskbalancer.command; + +import com.google.common.base.Preconditions; +import org.apache.commons.cli.CommandLine; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.protocol.ClientDatanodeProtocol; +import org.apache.hadoop.hdfs.server.datanode.DiskBalancerWorkStatus; +import org.apache.hadoop.hdfs.server.diskbalancer.DiskBalancerException; +import org.apache.hadoop.hdfs.tools.DiskBalancer; + +/** + * Gets the current status of disk balancer command. + */ +public class QueryCommand extends Command { + + /** + * Constructs QueryCommand. + * + * @param conf - Configuration. + */ + public QueryCommand(Configuration conf) { + super(conf); + addValidCommandParameters(DiskBalancer.QUERY, "Queries the status of disk" + + " plan running on a given datanode."); + addValidCommandParameters(DiskBalancer.VERBOSE, "Prints verbose results."); + } + /** + * Executes the Client Calls. + * + * @param cmd - CommandLine + */ + @Override + public void execute(CommandLine cmd) throws Exception { + LOG.info("Executing \"query plan\" command."); + Preconditions.checkState(cmd.hasOption(DiskBalancer.QUERY)); + verifyCommandOptions(DiskBalancer.QUERY, cmd); + String nodeName = cmd.getOptionValue(DiskBalancer.QUERY); + Preconditions.checkNotNull(nodeName); + ClientDatanodeProtocol dataNode = getDataNodeProxy(nodeName); + try { + DiskBalancerWorkStatus workStatus = dataNode.queryDiskBalancerPlan(); + System.out.printf("Plan ID: %s Result: %s%n", workStatus.getPlanID(), + workStatus.getResult().toString()); + + if(cmd.hasOption(DiskBalancer.VERBOSE)) { + System.out.printf("%s", workStatus.currentStateString()); + } + } catch (DiskBalancerException ex) { + LOG.error("Query plan failed. ex: {}", ex); + throw ex; + } + } + + /** + * Gets extended help for this command. + * + * @return Help Message + */ + @Override + protected String getHelp() { + return "Gets the status of disk balancing on a given node"; + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DiskBalancer.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DiskBalancer.java index 87fbf4afe03..f5dbe4ebf8d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DiskBalancer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DiskBalancer.java @@ -27,6 +27,7 @@ import org.apache.hadoop.hdfs.HdfsConfiguration; import org.apache.hadoop.hdfs.server.diskbalancer.command.Command; import org.apache.hadoop.hdfs.server.diskbalancer.command.ExecuteCommand; import org.apache.hadoop.hdfs.server.diskbalancer.command.PlanCommand; +import org.apache.hadoop.hdfs.server.diskbalancer.command.QueryCommand; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.slf4j.Logger; @@ -100,6 +101,11 @@ public class DiskBalancer extends Configured implements Tool { */ public static final String VERBOSE = "v"; public static final int PLAN_VERSION = 1; + /** + * Reports the status of disk balancer operation. + */ + public static final String QUERY = "query"; + /** * Template for the Before File. It is node.before.json. */ @@ -160,6 +166,8 @@ public class DiskBalancer extends Configured implements Tool { private Options getOpts() { Options opts = new Options(); addPlanCommands(opts); + addExecuteCommands(opts); + addQueryCommands(opts); return opts; } @@ -215,6 +223,16 @@ public class DiskBalancer extends Configured implements Tool { opt.addOption(execute); } + /** + * Adds query command options. + * @param opt Options + */ + private void addQueryCommands(Options opt) { + Option query = new Option(QUERY, true, "Queries the disk balancer " + + "status of a given datanode. e.g. -query "); + opt.addOption(query); + } + /** * This function parses all command line arguments and returns the appropriate * values. @@ -249,6 +267,10 @@ public class DiskBalancer extends Configured implements Tool { currentCommand = new ExecuteCommand(getConf()); } + if(cmd.hasOption(DiskBalancer.QUERY)) { + currentCommand = new QueryCommand(getConf()); + } + if(currentCommand == null) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp(80, "hdfs diskbalancer -uri [args]",