From 00c5ffaee2fb16eaef512a47054c7b9ee7ea2e50 Mon Sep 17 00:00:00 2001 From: Vinayakumar B Date: Wed, 13 Feb 2019 09:29:37 +0530 Subject: [PATCH] HADOOP-16108. Tail Follow Interval Should Allow To Specify The Sleep Interval To Save Unnecessary RPC's. Contributed by Ayush Saxena. --- .../java/org/apache/hadoop/fs/shell/Tail.java | 25 +++++++- .../org/apache/hadoop/fs/shell/TestTail.java | 57 +++++++++++++++++++ .../src/test/resources/testConf.xml | 6 +- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTail.java 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 1d49bf14e6..8a75a60f43 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.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 @@ public static void registerCommands(CommandFactory factory) { } 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 0000000000..31a5a4ee17 --- /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 1798563e22..29a88fc8fb 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.( )* +