HADOOP-16108. Tail Follow Interval Should Allow To Specify The Sleep Interval To Save Unnecessary RPC's. Contributed by Ayush Saxena.

This commit is contained in:
Vinayakumar B 2019-02-13 09:29:37 +05:30
parent 1f3e737407
commit c450f9f2c4
3 changed files with 84 additions and 4 deletions

View 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

View File

@ -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());
}
}

View File

@ -1001,7 +1001,7 @@
<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>
@ -1011,6 +1011,10 @@
<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>