From 19a6762639498ed9d8daeb86e49491d516858e68 Mon Sep 17 00:00:00 2001 From: huhaiyang Date: Sun, 21 May 2023 11:33:34 +0800 Subject: [PATCH] HDFS-17017. Fix the issue of arguments number limit in report command in DFSAdmin (#5667). Contributed by Haiyang Hu. Reviewed-by: Viraj Jasani Signed-off-by: Ayush Saxena --- .../apache/hadoop/hdfs/tools/DFSAdmin.java | 21 +++++++++++-------- .../hadoop/hdfs/tools/TestDFSAdmin.java | 8 +++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java index 656431d91a..640af66e4b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/DFSAdmin.java @@ -484,7 +484,11 @@ public DFSAdmin(Configuration conf) { protected DistributedFileSystem getDFS() throws IOException { return AdminHelper.checkAndGetDFS(getFS(), getConf()); } - + + public static final String[] DFS_REPORT_ARGS = + new String[] {"-live", "-dead", "-decommissioning", "-enteringmaintenance", + "-inmaintenance", "-slownodes"}; + /** * Gives a report on how the FileSystem is doing. * @exception IOException if the filesystem does not exist. @@ -576,17 +580,16 @@ public void report(String[] argv, int i) throws IOException { List args = Arrays.asList(argv); // Truncate already handled arguments before parsing report()-specific ones args = new ArrayList(args.subList(i, args.size())); - final boolean listLive = StringUtils.popOption("-live", args); - final boolean listDead = StringUtils.popOption("-dead", args); + final boolean listLive = StringUtils.popOption(DFS_REPORT_ARGS[0], args); + final boolean listDead = StringUtils.popOption(DFS_REPORT_ARGS[1], args); final boolean listDecommissioning = - StringUtils.popOption("-decommissioning", args); + StringUtils.popOption(DFS_REPORT_ARGS[2], args); final boolean listEnteringMaintenance = - StringUtils.popOption("-enteringmaintenance", args); + StringUtils.popOption(DFS_REPORT_ARGS[3], args); final boolean listInMaintenance = - StringUtils.popOption("-inmaintenance", args); + StringUtils.popOption(DFS_REPORT_ARGS[4], args); final boolean listSlowNodes = - StringUtils.popOption("-slownodes", args); - + StringUtils.popOption(DFS_REPORT_ARGS[5], args); // If no filter flags are found, then list all DN types boolean listAll = (!listLive && !listDead && !listDecommissioning @@ -2338,7 +2341,7 @@ public int run(String[] argv) { return exitCode; } } else if ("-report".equals(cmd)) { - if (argv.length > 6) { + if (argv.length > DFS_REPORT_ARGS.length + 1) { printUsage(cmd); return exitCode; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSAdmin.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSAdmin.java index ceb3d4ef15..ed85347cb3 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSAdmin.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestDFSAdmin.java @@ -786,6 +786,14 @@ public void testReportCommand() throws Exception { resetStream(); assertEquals(0, ToolRunner.run(dfsAdmin, new String[] {"-report"})); verifyNodesAndCorruptBlocks(numDn, numDn - 1, 1, 1, client, 0L, 0L); + + // verify report command for list all DN types + resetStream(); + String[] reportWithArg = new String[DFSAdmin.DFS_REPORT_ARGS.length + 1]; + reportWithArg[0] = "-report"; + System.arraycopy(DFSAdmin.DFS_REPORT_ARGS, 0, reportWithArg, 1, + DFSAdmin.DFS_REPORT_ARGS.length); + assertEquals(0, ToolRunner.run(dfsAdmin, reportWithArg)); } }