HADOOP-16108. Tail Follow Interval Should Allow To Specify The Sleep Interval To...
authorVinayakumar B <vinayakumarb@apache.org>
Wed, 13 Feb 2019 03:59:37 +0000 (09:29 +0530)
committerVinayakumar B <vinayakumarb@apache.org>
Wed, 13 Feb 2019 11:14:32 +0000 (16:44 +0530)
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Tail.java
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTail.java [new file with mode: 0644]
hadoop-common-project/hadoop-common/src/test/resources/testConf.xml

index 1d49bf1..8a75a60 100644 (file)
@@ -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] <file>";
+  public static final String USAGE = "[-f] [-s <sleep interval>] <file>";
   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<String> 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 (file)
index 0000000..31a5a4e
--- /dev/null
@@ -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<String> options = new LinkedList<String>();
+    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<String> options = new LinkedList<String>();
+    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
index 1798563..29a88fc 100644 (file)
       <comparators>
         <comparator>
           <type>RegexpComparator</type>
-          <expected-output>^-tail \[-f\] &lt;file&gt; :\s*</expected-output>
+          <expected-output>^-tail \[-f\] \[-s &lt;sleep interval&gt;\] &lt;file&gt; :\s*</expected-output>
         </comparator>
         <comparator>
           <type>RegexpComparator</type>
           <type>RegexpComparator</type>
           <expected-output>^( |\t)*-f\s+Shows appended data as the file grows.( )*</expected-output>
         </comparator>
+        <comparator>
+          <type>RegexpComparator</type>
+          <expected-output>^( |\t)*-s\s+With -f , defines the sleep interval between iterations in milliseconds.( )*</expected-output>
+        </comparator>
       </comparators>
     </test>