HDFS-9157. [OEV and OIV] : Unnecessary parsing for mandatory arguements if '-h' option is specified as the only option (Contributed by nijel)

This commit is contained in:
Vinayakumar B 2015-10-15 17:37:06 +05:30
parent a49298d585
commit 08f5de1ef5
5 changed files with 83 additions and 6 deletions

View File

@ -2066,6 +2066,9 @@ Release 2.8.0 - UNRELEASED
HDFS-9187. Fix null pointer error in Globber when FS was not constructed HDFS-9187. Fix null pointer error in Globber when FS was not constructed
via FileSystem#createFileSystem (cmccabe) via FileSystem#createFileSystem (cmccabe)
HDFS-9157. [OEV and OIV] : Unnecessary parsing for mandatory arguements if
'-h' option is specified as the only option (nijel via vinayakumarb)
Release 2.7.2 - UNRELEASED Release 2.7.2 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -39,7 +39,8 @@ import org.apache.commons.cli.PosixParser;
@InterfaceAudience.Private @InterfaceAudience.Private
@InterfaceStability.Unstable @InterfaceStability.Unstable
public class OfflineEditsViewer extends Configured implements Tool { public class OfflineEditsViewer extends Configured implements Tool {
private static final String HELP_OPT = "-h";
private static final String HELP_LONGOPT = "--help";
private final static String defaultProcessor = "xml"; private final static String defaultProcessor = "xml";
/** /**
@ -192,7 +193,12 @@ public class OfflineEditsViewer extends Configured implements Tool {
Options options = buildOptions(); Options options = buildOptions();
if(argv.length == 0) { if(argv.length == 0) {
printHelp(); printHelp();
return -1; return 0;
}
// print help and exit with zero exit code
if (argv.length == 1 && isHelpOption(argv[0])) {
printHelp();
return 0;
} }
CommandLineParser parser = new PosixParser(); CommandLineParser parser = new PosixParser();
CommandLine cmd; CommandLine cmd;
@ -205,7 +211,9 @@ public class OfflineEditsViewer extends Configured implements Tool {
return -1; return -1;
} }
if(cmd.hasOption("h")) { // print help and exit if (cmd.hasOption("h")) {
// print help and exit with non zero exit code since
// it is not expected to give help and other options together.
printHelp(); printHelp();
return -1; return -1;
} }
@ -237,4 +245,9 @@ public class OfflineEditsViewer extends Configured implements Tool {
int res = ToolRunner.run(new OfflineEditsViewer(), argv); int res = ToolRunner.run(new OfflineEditsViewer(), argv);
System.exit(res); System.exit(res);
} }
private static boolean isHelpOption(String arg) {
return arg.equalsIgnoreCase(HELP_OPT) ||
arg.equalsIgnoreCase(HELP_LONGOPT);
}
} }

View File

@ -41,6 +41,8 @@ import org.apache.hadoop.net.NetUtils;
*/ */
@InterfaceAudience.Private @InterfaceAudience.Private
public class OfflineImageViewerPB { public class OfflineImageViewerPB {
private static final String HELP_OPT = "-h";
private static final String HELP_LONGOPT = "--help";
public static final Log LOG = LogFactory.getLog(OfflineImageViewerPB.class); public static final Log LOG = LogFactory.getLog(OfflineImageViewerPB.class);
private final static String usage = "Usage: bin/hdfs oiv [OPTIONS] -i INPUTFILE -o OUTPUTFILE\n" private final static String usage = "Usage: bin/hdfs oiv [OPTIONS] -i INPUTFILE -o OUTPUTFILE\n"
@ -131,7 +133,11 @@ public class OfflineImageViewerPB {
printUsage(); printUsage();
return 0; return 0;
} }
// print help and exit with zero exit code
if (args.length == 1 && isHelpOption(args[0])) {
printUsage();
return 0;
}
CommandLineParser parser = new PosixParser(); CommandLineParser parser = new PosixParser();
CommandLine cmd; CommandLine cmd;
@ -143,9 +149,11 @@ public class OfflineImageViewerPB {
return -1; return -1;
} }
if (cmd.hasOption("h")) { // print help and exit if (cmd.hasOption("h")) {
// print help and exit with non zero exit code since
// it is not expected to give help and other options together.
printUsage(); printUsage();
return 0; return -1;
} }
String inputFile = cmd.getOptionValue("i"); String inputFile = cmd.getOptionValue("i");
@ -202,4 +210,9 @@ public class OfflineImageViewerPB {
private static void printUsage() { private static void printUsage() {
System.out.println(usage); System.out.println(usage);
} }
private static boolean isHelpOption(String arg) {
return arg.equalsIgnoreCase(HELP_OPT) ||
arg.equalsIgnoreCase(HELP_LONGOPT);
}
} }

View File

@ -21,9 +21,11 @@ package org.apache.hadoop.hdfs.tools.offlineEditsViewer;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
@ -35,8 +37,10 @@ import org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes;
import org.apache.hadoop.hdfs.server.namenode.NameNodeLayoutVersion; import org.apache.hadoop.hdfs.server.namenode.NameNodeLayoutVersion;
import org.apache.hadoop.hdfs.server.namenode.OfflineEditsViewerHelper; import org.apache.hadoop.hdfs.server.namenode.OfflineEditsViewerHelper;
import org.apache.hadoop.hdfs.tools.offlineEditsViewer.OfflineEditsViewer.Flags; import org.apache.hadoop.hdfs.tools.offlineEditsViewer.OfflineEditsViewer.Flags;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.test.PathUtils; import org.apache.hadoop.test.PathUtils;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -285,4 +289,23 @@ public class TestOfflineEditsViewer {
return true; return true;
} }
@Test
public void testOfflineEditsViewerHelpMessage() throws Throwable {
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final PrintStream out = new PrintStream(bytes);
final PrintStream oldOut = System.out;
try {
System.setOut(out);
int status = new OfflineEditsViewer().run(new String[] { "-h" });
assertTrue("" + "Exit code returned for help option is incorrect",
status == 0);
Assert.assertFalse(
"Invalid Command error displayed when help option is passed.", bytes
.toString().contains("Error parsing command-line options"));
} finally {
System.setOut(oldOut);
IOUtils.closeStream(out);
}
}
} }

View File

@ -67,6 +67,7 @@ import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.token.Token; import org.apache.hadoop.security.token.Token;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -350,6 +351,30 @@ public class TestOfflineImageViewer {
status != 0); status != 0);
} }
@Test
public void testOfflineImageViewerHelpMessage() throws Throwable {
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final PrintStream out = new PrintStream(bytes);
final PrintStream oldOut = System.out;
try {
System.setOut(out);
int status = OfflineImageViewerPB.run(new String[] { "-h" });
assertTrue("Exit code returned for help option is incorrect", status == 0);
Assert.assertFalse(
"Invalid Command error displayed when help option is passed.", bytes
.toString().contains("Error parsing command-line options"));
status =
OfflineImageViewerPB.run(new String[] { "-h", "-i",
originalFsimage.getAbsolutePath(), "-o", "-", "-p",
"FileDistribution", "-maxSize", "512", "-step", "8" });
Assert.assertTrue(
"Exit code returned for help with other option is incorrect",
status == -1);
} finally {
System.setOut(oldOut);
IOUtils.closeStream(out);
}
}
private void testPBDelimitedWriter(String db) private void testPBDelimitedWriter(String db)
throws IOException, InterruptedException { throws IOException, InterruptedException {
final String DELIMITER = "\t"; final String DELIMITER = "\t";