You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by in...@apache.org on 2019/03/03 18:36:40 UTC

[hadoop] 01/45: HDFS-13906. RBF: Add multiple paths for dfsrouteradmin 'rm' and 'clrquota' commands. Contributed by Ayush Saxena.

This is an automated email from the ASF dual-hosted git repository.

inigoiri pushed a commit to branch HDFS-13891
in repository https://gitbox.apache.org/repos/asf/hadoop.git

commit 4cba216e01581d29fac231a8c4b8f4e162b258c1
Author: Vinayakumar B <vi...@apache.org>
AuthorDate: Fri Oct 12 17:19:55 2018 +0530

    HDFS-13906. RBF: Add multiple paths for dfsrouteradmin 'rm' and 'clrquota' commands. Contributed by Ayush Saxena.
---
 .../hadoop/hdfs/tools/federation/RouterAdmin.java  | 102 +++++++++++----------
 .../federation/router/TestRouterAdminCLI.java      |  82 ++++++++++++++---
 2 files changed, 122 insertions(+), 62 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 1aefe4f..4a9cc7a 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 @@ public class RouterAdmin extends Configured implements Tool {
    * @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,63 +173,63 @@ public class RouterAdmin extends Configured implements Tool {
     }
   }
 
-  @Override
-  public int run(String[] argv) throws Exception {
-    if (argv.length < 1) {
-      System.err.println("Not enough parameters specified");
-      printUsage();
-      return -1;
-    }
-
-    int exitCode = -1;
-    int i = 0;
-    String cmd = argv[i++];
-
-    // Verify that we have enough command line parameters
+  /**
+   * 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) {
-        System.err.println("Not enough parameters specified for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     } else if ("-update".equals(cmd)) {
       if (argv.length < 4) {
-        System.err.println("Not enough parameters specified for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     } else if ("-rm".equals(cmd)) {
       if (argv.length < 2) {
-        System.err.println("Not enough parameters specified for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     } else if ("-setQuota".equals(cmd)) {
       if (argv.length < 4) {
-        System.err.println("Not enough parameters specified for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     } else if ("-clrQuota".equals(cmd)) {
       if (argv.length < 2) {
-        System.err.println("Not enough parameters specified for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     } else if ("-safemode".equals(cmd)) {
       if (argv.length < 2) {
-        System.err.println("Not enough parameters specified for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     } else if ("-nameservice".equals(cmd)) {
       if (argv.length < 3) {
-        System.err.println("Not enough parameters specificed for cmd " + cmd);
-        printUsage(cmd);
-        return exitCode;
+        return false;
       }
     }
+    return true;
+  }
+
+  @Override
+  public int run(String[] argv) throws Exception {
+    if (argv.length < 1) {
+      System.err.println("Not enough parameters specified");
+      printUsage();
+      return -1;
+    }
+
+    int exitCode = -1;
+    int i = 0;
+    String cmd = argv[i++];
 
+    // Verify that we have enough command line parameters
+    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 class RouterAdmin extends Configured implements Tool {
           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 class RouterAdmin extends Configured implements Tool {
               "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 80aca55..6642942 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 class TestRouterAdminCLI {
     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 class TestRouterAdminCLI {
   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 class TestRouterAdminCLI {
     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"));
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org