From 41c94a636b1a6ede9fa9cabff5487d305d06b48f Mon Sep 17 00:00:00 2001 From: Vinayakumar B Date: Fri, 12 Oct 2018 17:19:55 +0530 Subject: [PATCH] HDFS-13906. RBF: Add multiple paths for dfsrouteradmin 'rm' and 'clrquota' commands. Contributed by Ayush Saxena. --- .../hdfs/tools/federation/RouterAdmin.java | 120 +++++++++--------- .../federation/router/TestRouterAdminCLI.java | 82 ++++++++++-- 2 files changed, 131 insertions(+), 71 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java index 1aefe4fe78..4a9cc7ac51 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java @@ -151,17 +151,7 @@ private String getUsage(String cmd) { * @param arg List of of command line parameters. */ private void validateMax(String[] arg) { - if (arg[0].equals("-rm")) { - if (arg.length > 2) { - throw new IllegalArgumentException( - "Too many arguments, Max=1 argument allowed"); - } - } else if (arg[0].equals("-ls")) { - if (arg.length > 2) { - throw new IllegalArgumentException( - "Too many arguments, Max=1 argument allowed"); - } - } else if (arg[0].equals("-clrQuota")) { + if (arg[0].equals("-ls")) { if (arg.length > 2) { throw new IllegalArgumentException( "Too many arguments, Max=1 argument allowed"); @@ -183,6 +173,45 @@ private void validateMax(String[] arg) { } } + /** + * Usage: validates the minimum number of arguments for a command. + * @param argv List of of command line parameters. + * @return true if number of arguments are valid for the command else false. + */ + private boolean validateMin(String[] argv) { + String cmd = argv[0]; + if ("-add".equals(cmd)) { + if (argv.length < 4) { + return false; + } + } else if ("-update".equals(cmd)) { + if (argv.length < 4) { + return false; + } + } else if ("-rm".equals(cmd)) { + if (argv.length < 2) { + return false; + } + } else if ("-setQuota".equals(cmd)) { + if (argv.length < 4) { + return false; + } + } else if ("-clrQuota".equals(cmd)) { + if (argv.length < 2) { + return false; + } + } else if ("-safemode".equals(cmd)) { + if (argv.length < 2) { + return false; + } + } else if ("-nameservice".equals(cmd)) { + if (argv.length < 3) { + return false; + } + } + return true; + } + @Override public int run(String[] argv) throws Exception { if (argv.length < 1) { @@ -196,50 +225,11 @@ public int run(String[] argv) throws Exception { String cmd = argv[i++]; // Verify that we have enough command line parameters - if ("-add".equals(cmd)) { - if (argv.length < 4) { - System.err.println("Not enough parameters specified for cmd " + cmd); - printUsage(cmd); - return exitCode; - } - } else if ("-update".equals(cmd)) { - if (argv.length < 4) { - System.err.println("Not enough parameters specified for cmd " + cmd); - printUsage(cmd); - return exitCode; - } - } else if ("-rm".equals(cmd)) { - if (argv.length < 2) { - System.err.println("Not enough parameters specified for cmd " + cmd); - printUsage(cmd); - return exitCode; - } - } else if ("-setQuota".equals(cmd)) { - if (argv.length < 4) { - System.err.println("Not enough parameters specified for cmd " + cmd); - printUsage(cmd); - return exitCode; - } - } else if ("-clrQuota".equals(cmd)) { - if (argv.length < 2) { - System.err.println("Not enough parameters specified for cmd " + cmd); - printUsage(cmd); - return exitCode; - } - } else if ("-safemode".equals(cmd)) { - if (argv.length < 2) { - System.err.println("Not enough parameters specified for cmd " + cmd); - printUsage(cmd); - return exitCode; - } - } else if ("-nameservice".equals(cmd)) { - if (argv.length < 3) { - System.err.println("Not enough parameters specificed for cmd " + cmd); - printUsage(cmd); - return exitCode; - } + if (!validateMin(argv)) { + System.err.println("Not enough parameters specificed for cmd " + cmd); + printUsage(cmd); + return exitCode; } - // Initialize RouterClient try { String address = getConf().getTrimmed( @@ -273,8 +263,17 @@ public int run(String[] argv) throws Exception { exitCode = -1; } } else if ("-rm".equals(cmd)) { - if (removeMount(argv[i])) { - System.out.println("Successfully removed mount point " + argv[i]); + while (i < argv.length) { + try { + if (removeMount(argv[i])) { + System.out.println("Successfully removed mount point " + argv[i]); + } + } catch (IOException e) { + exitCode = -1; + System.err + .println(cmd.substring(1) + ": " + e.getLocalizedMessage()); + } + i++; } } else if ("-ls".equals(cmd)) { if (argv.length > 1) { @@ -288,9 +287,12 @@ public int run(String[] argv) throws Exception { "Successfully set quota for mount point " + argv[i]); } } else if ("-clrQuota".equals(cmd)) { - if (clrQuota(argv[i])) { - System.out.println( - "Successfully clear quota for mount point " + argv[i]); + while (i < argv.length) { + if (clrQuota(argv[i])) { + System.out + .println("Successfully clear quota for mount point " + argv[i]); + i++; + } } } else if ("-safemode".equals(cmd)) { manageSafeMode(argv[i]); diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java index 80aca55d83..66429420ab 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java @@ -342,13 +342,43 @@ public void testRemoveMountTable() throws Exception { assertEquals(0, ToolRunner.run(admin, argv)); assertTrue(out.toString().contains( "Cannot remove mount point " + invalidPath)); + } - // test wrong number of arguments - System.setErr(new PrintStream(err)); - argv = new String[] {"-rm", src, "check" }; - ToolRunner.run(admin, argv); - assertTrue(err.toString() - .contains("Too many arguments, Max=1 argument allowed")); + @Test + public void testMultiArgsRemoveMountTable() throws Exception { + String nsId = "ns0"; + String src1 = "/test-rmmounttable1"; + String src2 = "/test-rmmounttable2"; + String dest1 = "/rmmounttable1"; + String dest2 = "/rmmounttable2"; + // Adding mount table entries + String[] argv = new String[] {"-add", src1, nsId, dest1}; + assertEquals(0, ToolRunner.run(admin, argv)); + argv = new String[] {"-add", src2, nsId, dest2}; + assertEquals(0, ToolRunner.run(admin, argv)); + + stateStore.loadCache(MountTableStoreImpl.class, true); + // Ensure mount table entries added successfully + GetMountTableEntriesRequest getRequest = + GetMountTableEntriesRequest.newInstance(src1); + GetMountTableEntriesResponse getResponse = + client.getMountTableManager().getMountTableEntries(getRequest); + MountTable mountTable = getResponse.getEntries().get(0); + getRequest = GetMountTableEntriesRequest.newInstance(src2); + getResponse = + client.getMountTableManager().getMountTableEntries(getRequest); + assertEquals(src1, mountTable.getSourcePath()); + mountTable = getResponse.getEntries().get(0); + assertEquals(src2, mountTable.getSourcePath()); + // Remove multiple mount table entries + argv = new String[] {"-rm", src1, src2}; + assertEquals(0, ToolRunner.run(admin, argv)); + + stateStore.loadCache(MountTableStoreImpl.class, true); + // Verify successful deletion of mount table entries + getResponse = + client.getMountTableManager().getMountTableEntries(getRequest); + assertEquals(0, getResponse.getEntries().size()); } @Test @@ -540,6 +570,7 @@ public void testInvalidArgumentMessage() throws Exception { public void testSetAndClearQuota() throws Exception { String nsId = "ns0"; String src = "/test-QuotaMounttable"; + String src1 = "/test-QuotaMounttable1"; String dest = "/QuotaMounttable"; String[] argv = new String[] {"-add", src, nsId, dest}; assertEquals(0, ToolRunner.run(admin, argv)); @@ -605,15 +636,42 @@ public void testSetAndClearQuota() throws Exception { assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getQuota()); assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getSpaceQuota()); + // verify multi args ClrQuota + String dest1 = "/QuotaMounttable1"; + // Add mount table entries. + argv = new String[] {"-add", src, nsId, dest}; + assertEquals(0, ToolRunner.run(admin, argv)); + argv = new String[] {"-add", src1, nsId, dest1}; + assertEquals(0, ToolRunner.run(admin, argv)); + + stateStore.loadCache(MountTableStoreImpl.class, true); + // SetQuota for the added entries + argv = new String[] {"-setQuota", src, "-nsQuota", String.valueOf(nsQuota), + "-ssQuota", String.valueOf(ssQuota)}; + assertEquals(0, ToolRunner.run(admin, argv)); + argv = new String[] {"-setQuota", src1, "-nsQuota", + String.valueOf(nsQuota), "-ssQuota", String.valueOf(ssQuota)}; + assertEquals(0, ToolRunner.run(admin, argv)); + stateStore.loadCache(MountTableStoreImpl.class, true); + // Clear quota for the added entries + argv = new String[] {"-clrQuota", src, src1}; + assertEquals(0, ToolRunner.run(admin, argv)); + + stateStore.loadCache(MountTableStoreImpl.class, true); + getResponse = + client.getMountTableManager().getMountTableEntries(getRequest); + + // Verify clear quota for the entries + for (int i = 0; i < 2; i++) { + mountTable = getResponse.getEntries().get(i); + quotaUsage = mountTable.getQuota(); + assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getQuota()); + assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getSpaceQuota()); + } + // verify wrong arguments System.setErr(new PrintStream(err)); - argv = new String[] {"-clrQuota", src, "check"}; - ToolRunner.run(admin, argv); - assertTrue(err.toString(), - err.toString().contains("Too many arguments, Max=1 argument allowed")); - argv = new String[] {"-setQuota", src, "check", "check2"}; - err.reset(); ToolRunner.run(admin, argv); assertTrue(err.toString().contains("Invalid argument : check")); }