diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Tail.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Tail.java index 1d49bf14e6a..8a75a60f435 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Tail.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Tail.java @@ -28,6 +28,8 @@ import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.PathIsDirectoryException; import org.apache.hadoop.io.IOUtils; +import com.google.common.annotations.VisibleForTesting; + /** * Get a listing of all files in that match the file patterns. */ @@ -40,20 +42,37 @@ class Tail extends FsCommand { } public static final String NAME = "tail"; - public static final String USAGE = "[-f] "; + public static final String USAGE = "[-f] [-s ] "; public static final String DESCRIPTION = - "Show the last 1KB of the file.\n" + - "-f: Shows appended data as the file grows.\n"; + "Show the last 1KB of the file.\n" + + "-f: Shows appended data as the file grows.\n" + + "-s: With -f , " + + "defines the sleep interval between iterations in milliseconds.\n"; private long startingOffset = -1024; private boolean follow = false; private long followDelay = 5000; // milliseconds + @VisibleForTesting + public long getFollowDelay() { + return followDelay; + } + @Override protected void processOptions(LinkedList args) throws IOException { CommandFormat cf = new CommandFormat(1, 1, "f"); + cf.addOptionWithValue("s"); cf.parse(args); follow = cf.getOpt("f"); + if (follow) { + String sleep = cf.getOptValue("s"); + if (sleep != null && !sleep.isEmpty()) { + long sleepInterval = Long.parseLong(sleep); + if (sleepInterval > 0) { + followDelay = sleepInterval; + } + } + } } // TODO: HADOOP-7234 will add glob support; for now, be backwards compat diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTail.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTail.java new file mode 100644 index 00000000000..31a5a4ee178 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTail.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.hadoop.fs.shell; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.LinkedList; + +import org.junit.Test; + +/** + * Test class to verify Tail shell command. + */ +public class TestTail { + + // check follow delay with -s parameter. + @Test + public void testSleepParameter() throws IOException { + Tail tail = new Tail(); + LinkedList options = new LinkedList(); + options.add("-f"); + options.add("-s"); + options.add("10000"); + options.add("/path"); + tail.processOptions(options); + assertEquals(10000, tail.getFollowDelay()); + } + + // check follow delay without -s parameter. + @Test + public void testFollowParameter() throws IOException { + Tail tail = new Tail(); + LinkedList options = new LinkedList(); + options.add("-f"); + options.add("/path"); + tail.processOptions(options); + // Follow delay should be the default 5000 ms. + assertEquals(5000, tail.getFollowDelay()); + } +} \ No newline at end of file diff --git a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml index 1798563e224..29a88fc8fba 100644 --- a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml +++ b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml @@ -1001,7 +1001,7 @@ RegexpComparator - ^-tail \[-f\] <file> :\s* + ^-tail \[-f\] \[-s <sleep interval>\] <file> :\s* RegexpComparator @@ -1011,6 +1011,10 @@ RegexpComparator ^( |\t)*-f\s+Shows appended data as the file grows.( )* + + RegexpComparator + ^( |\t)*-s\s+With -f , defines the sleep interval between iterations in milliseconds.( )* +