001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hdfs.tools; 019 020import org.apache.hadoop.conf.Configuration; 021import org.apache.hadoop.conf.Configured; 022import org.apache.hadoop.fs.FileSystem; 023import org.apache.hadoop.fs.Path; 024import org.apache.hadoop.hdfs.DistributedFileSystem; 025import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy; 026import org.apache.hadoop.hdfs.protocol.HdfsFileStatus; 027import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite; 028import org.apache.hadoop.tools.TableListing; 029import org.apache.hadoop.util.StringUtils; 030import org.apache.hadoop.util.Tool; 031 032import java.io.IOException; 033import java.util.Arrays; 034import java.util.LinkedList; 035import java.util.List; 036 037/** 038 * This class implements block storage policy operations. 039 */ 040public class StoragePolicyAdmin extends Configured implements Tool { 041 private static final int MAX_LINE_WIDTH = 80; 042 043 public static void main(String[] argsArray) throws Exception { 044 final StoragePolicyAdmin admin = new StoragePolicyAdmin(new 045 Configuration()); 046 System.exit(admin.run(argsArray)); 047 } 048 049 private static DistributedFileSystem getDFS(Configuration conf) 050 throws IOException { 051 final FileSystem fs = FileSystem.get(conf); 052 if (!(fs instanceof DistributedFileSystem)) { 053 throw new IllegalArgumentException("FileSystem " + fs.getUri() + 054 " is not an HDFS file system"); 055 } 056 return (DistributedFileSystem) fs; 057 } 058 059 /** 060 * NN exceptions contain the stack trace as part of the exception message. 061 * When it's a known error, pretty-print the error and squish the stack trace. 062 */ 063 private static String prettifyException(Exception e) { 064 return e.getClass().getSimpleName() + ": " + 065 e.getLocalizedMessage().split("\n")[0]; 066 } 067 068 private static TableListing getOptionDescriptionListing() { 069 return new TableListing.Builder() 070 .addField("").addField("", true) 071 .wrapWidth(MAX_LINE_WIDTH).hideHeaders().build(); 072 } 073 074 public StoragePolicyAdmin(Configuration conf) { 075 super(conf); 076 } 077 078 @Override 079 public int run(String[] args) throws Exception { 080 if (args.length == 0) { 081 printUsage(false); 082 return 1; 083 } 084 final Command command = determineCommand(args[0]); 085 if (command == null) { 086 System.err.println("Can't understand command '" + args[0] + "'"); 087 if (!args[0].startsWith("-")) { 088 System.err.println("Command names must start with dashes."); 089 } 090 printUsage(false); 091 return 1; 092 } 093 final List<String> argsList = new LinkedList<String>(); 094 argsList.addAll(Arrays.asList(args).subList(1, args.length)); 095 try { 096 return command.run(getConf(), argsList); 097 } catch (IllegalArgumentException e) { 098 System.err.println(prettifyException(e)); 099 return -1; 100 } 101 } 102 103 interface Command { 104 String getName(); 105 String getShortUsage(); 106 String getLongUsage(); 107 int run(Configuration conf, List<String> args) throws IOException; 108 } 109 110 /** Command to list all the existing storage policies */ 111 private static class ListStoragePoliciesCommand implements Command { 112 @Override 113 public String getName() { 114 return "-listPolicies"; 115 } 116 117 @Override 118 public String getShortUsage() { 119 return "[" + getName() + "]\n"; 120 } 121 122 @Override 123 public String getLongUsage() { 124 return getShortUsage() + "\n" + 125 "List all the existing block storage policies.\n"; 126 } 127 128 @Override 129 public int run(Configuration conf, List<String> args) throws IOException { 130 final DistributedFileSystem dfs = getDFS(conf); 131 try { 132 BlockStoragePolicy[] policies = dfs.getStoragePolicies(); 133 System.out.println("Block Storage Policies:"); 134 for (BlockStoragePolicy policy : policies) { 135 if (policy != null) { 136 System.out.println("\t" + policy); 137 } 138 } 139 } catch (IOException e) { 140 System.err.println(prettifyException(e)); 141 return 2; 142 } 143 return 0; 144 } 145 } 146 147 /** Command to get the storage policy of a file/directory */ 148 private static class GetStoragePolicyCommand implements Command { 149 @Override 150 public String getName() { 151 return "-getStoragePolicy"; 152 } 153 154 @Override 155 public String getShortUsage() { 156 return "[" + getName() + " -path <path>]\n"; 157 } 158 159 @Override 160 public String getLongUsage() { 161 final TableListing listing = getOptionDescriptionListing(); 162 listing.addRow("<path>", 163 "The path of the file/directory for getting the storage policy"); 164 return getShortUsage() + "\n" + 165 "Get the storage policy of a file/directory.\n\n" + 166 listing.toString(); 167 } 168 169 @Override 170 public int run(Configuration conf, List<String> args) throws IOException { 171 final String path = StringUtils.popOptionWithArgument("-path", args); 172 if (path == null) { 173 System.err.println("Please specify the path with -path.\nUsage:" + 174 getLongUsage()); 175 return 1; 176 } 177 178 final DistributedFileSystem dfs = getDFS(conf); 179 try { 180 HdfsFileStatus status = dfs.getClient().getFileInfo(path); 181 if (status == null) { 182 System.err.println("File/Directory does not exist: " + path); 183 return 2; 184 } 185 byte storagePolicyId = status.getStoragePolicy(); 186 if (storagePolicyId == BlockStoragePolicySuite.ID_UNSPECIFIED) { 187 System.out.println("The storage policy of " + path + " is unspecified"); 188 return 0; 189 } 190 BlockStoragePolicy[] policies = dfs.getStoragePolicies(); 191 for (BlockStoragePolicy p : policies) { 192 if (p.getId() == storagePolicyId) { 193 System.out.println("The storage policy of " + path + ":\n" + p); 194 return 0; 195 } 196 } 197 } catch (Exception e) { 198 System.err.println(prettifyException(e)); 199 return 2; 200 } 201 System.err.println("Cannot identify the storage policy for " + path); 202 return 2; 203 } 204 } 205 206 /** Command to set the storage policy to a file/directory */ 207 private static class SetStoragePolicyCommand implements Command { 208 @Override 209 public String getName() { 210 return "-setStoragePolicy"; 211 } 212 213 @Override 214 public String getShortUsage() { 215 return "[" + getName() + " -path <path> -policy <policy>]\n"; 216 } 217 218 @Override 219 public String getLongUsage() { 220 TableListing listing = getOptionDescriptionListing(); 221 listing.addRow("<path>", "The path of the file/directory to set storage" + 222 " policy"); 223 listing.addRow("<policy>", "The name of the block storage policy"); 224 return getShortUsage() + "\n" + 225 "Set the storage policy to a file/directory.\n\n" + 226 listing.toString(); 227 } 228 229 @Override 230 public int run(Configuration conf, List<String> args) throws IOException { 231 final String path = StringUtils.popOptionWithArgument("-path", args); 232 if (path == null) { 233 System.err.println("Please specify the path for setting the storage " + 234 "policy.\nUsage: " + getLongUsage()); 235 return 1; 236 } 237 238 final String policyName = StringUtils.popOptionWithArgument("-policy", 239 args); 240 if (policyName == null) { 241 System.err.println("Please specify the policy name.\nUsage: " + 242 getLongUsage()); 243 return 1; 244 } 245 246 final DistributedFileSystem dfs = getDFS(conf); 247 try { 248 dfs.setStoragePolicy(new Path(path), policyName); 249 System.out.println("Set storage policy " + policyName + " on " + path); 250 } catch (Exception e) { 251 System.err.println(prettifyException(e)); 252 return 2; 253 } 254 return 0; 255 } 256 } 257 258 private static class HelpCommand implements Command { 259 @Override 260 public String getName() { 261 return "-help"; 262 } 263 264 @Override 265 public String getShortUsage() { 266 return "[-help <command-name>]\n"; 267 } 268 269 @Override 270 public String getLongUsage() { 271 final TableListing listing = getOptionDescriptionListing(); 272 listing.addRow("<command-name>", "The command for which to get " + 273 "detailed help. If no command is specified, print detailed help for " + 274 "all commands"); 275 return getShortUsage() + "\n" + 276 "Get detailed help about a command.\n\n" + 277 listing.toString(); 278 } 279 280 @Override 281 public int run(Configuration conf, List<String> args) throws IOException { 282 if (args.size() == 0) { 283 for (Command command : COMMANDS) { 284 System.err.println(command.getLongUsage()); 285 } 286 return 0; 287 } 288 if (args.size() != 1) { 289 System.out.println("You must give exactly one argument to -help."); 290 return 0; 291 } 292 final String commandName = args.get(0); 293 // prepend a dash to match against the command names 294 final Command command = determineCommand("-" + commandName); 295 if (command == null) { 296 System.err.print("Unknown command '" + commandName + "'.\n"); 297 System.err.print("Valid help command names are:\n"); 298 String separator = ""; 299 for (Command c : COMMANDS) { 300 System.err.print(separator + c.getName().substring(1)); 301 separator = ", "; 302 } 303 System.err.print("\n"); 304 return 1; 305 } 306 System.err.print(command.getLongUsage()); 307 return 0; 308 } 309 } 310 311 private static final Command[] COMMANDS = { 312 new ListStoragePoliciesCommand(), 313 new SetStoragePolicyCommand(), 314 new GetStoragePolicyCommand(), 315 new HelpCommand() 316 }; 317 318 private static void printUsage(boolean longUsage) { 319 System.err.println( 320 "Usage: bin/hdfs storagepolicies [COMMAND]"); 321 for (Command command : COMMANDS) { 322 if (longUsage) { 323 System.err.print(command.getLongUsage()); 324 } else { 325 System.err.print(" " + command.getShortUsage()); 326 } 327 } 328 System.err.println(); 329 } 330 331 private static Command determineCommand(String commandName) { 332 for (Command COMMAND : COMMANDS) { 333 if (COMMAND.getName().equals(commandName)) { 334 return COMMAND; 335 } 336 } 337 return null; 338 } 339}