|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.fs.store.commands; |
| 20 | + |
| 21 | +import java.io.FileNotFoundException; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import org.apache.hadoop.conf.Configuration; |
| 28 | +import org.apache.hadoop.fs.EtagSource; |
| 29 | +import org.apache.hadoop.fs.FileStatus; |
| 30 | +import org.apache.hadoop.fs.FileSystem; |
| 31 | +import org.apache.hadoop.fs.Path; |
| 32 | +import org.apache.hadoop.fs.store.StoreDurationInfo; |
| 33 | +import org.apache.hadoop.fs.store.StoreEntryPoint; |
| 34 | +import org.apache.hadoop.util.ExitUtil; |
| 35 | +import org.apache.hadoop.util.ToolRunner; |
| 36 | + |
| 37 | +import static org.apache.hadoop.fs.store.CommonParameters.STANDARD_OPTS; |
| 38 | +import static org.apache.hadoop.fs.store.StoreExitCodes.E_NOT_FOUND; |
| 39 | +import static org.apache.hadoop.fs.store.StoreExitCodes.E_SERVICE_UNAVAILABLE; |
| 40 | +import static org.apache.hadoop.fs.store.StoreExitCodes.E_UNIMPLEMENTED; |
| 41 | + |
| 42 | +/** |
| 43 | + * Print the status. |
| 44 | + * <p> |
| 45 | + * Prints some performance numbers at the end. |
| 46 | + */ |
| 47 | +public class EtagCommand extends StoreEntryPoint { |
| 48 | + |
| 49 | + private static final Logger LOG = LoggerFactory.getLogger(EtagCommand.class); |
| 50 | + |
| 51 | + public static final String USAGE |
| 52 | + = "Usage: etag\n" |
| 53 | + + STANDARD_OPTS |
| 54 | + + " <path>"; |
| 55 | + |
| 56 | + |
| 57 | + public EtagCommand() { |
| 58 | + createCommandFormat(1, 999); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int run(String[] args) throws Exception { |
| 63 | + List<String> paths = processArgs(args, 1, 1, USAGE); |
| 64 | + final Configuration conf = createPreconfiguredConfig(); |
| 65 | + |
| 66 | + final Path source = new Path(paths.get(0)); |
| 67 | + FileSystem fs = source.getFileSystem(conf); |
| 68 | + FileStatus st = null; |
| 69 | + |
| 70 | + try (StoreDurationInfo duration = new StoreDurationInfo(LOG, |
| 71 | + "get path status for %s", source)) { |
| 72 | + st = fs.getFileStatus(source); |
| 73 | + } catch (FileNotFoundException e) { |
| 74 | + throw new ExitUtil.ExitException(E_NOT_FOUND, "Not found: " + source, e); |
| 75 | + } |
| 76 | + |
| 77 | + if (st instanceof EtagSource) { |
| 78 | + final String etag = ((EtagSource) st).getEtag(); |
| 79 | + println("Etag of %s = %s", source, etag); |
| 80 | + if (etag == null) { |
| 81 | + errorln("File status of path %s is an EtagSource but the value is null:\n%s", source, st); |
| 82 | + throw new ExitUtil.ExitException(E_SERVICE_UNAVAILABLE, "Etag is null"); |
| 83 | + } |
| 84 | + if (etag.isEmpty()) { |
| 85 | + errorln("File status of path %s is an EtagSource but the value is the empty string:\n%s", |
| 86 | + source, st); |
| 87 | + throw new ExitUtil.ExitException(E_SERVICE_UNAVAILABLE, "Etag is empty string"); |
| 88 | + } |
| 89 | + |
| 90 | + } else { |
| 91 | + errorln("File status of path %s is not an EtagSource:\n%s", source, st); |
| 92 | + throw new ExitUtil.ExitException(E_UNIMPLEMENTED, |
| 93 | + "Filesystem does not provide Etag information"); |
| 94 | + } |
| 95 | + return 0; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Execute the command, return the result or throw an exception, |
| 100 | + * as appropriate. |
| 101 | + * @param args argument varags. |
| 102 | + * @return return code |
| 103 | + * @throws Exception failure |
| 104 | + */ |
| 105 | + public static int exec(String... args) throws Exception { |
| 106 | + return ToolRunner.run(new EtagCommand(), args); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Main entry point. Calls {@code System.exit()} on all execution paths. |
| 111 | + * @param args argument list |
| 112 | + */ |
| 113 | + public static void main(String[] args) { |
| 114 | + try { |
| 115 | + exit(exec(args), ""); |
| 116 | + } catch (Throwable e) { |
| 117 | + exitOnThrowable(e); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments