瀏覽代碼

HDFS-13967. HDFS Router Quota Class Review. Contributed by BELUGA BEHR.

Yiqun Lin 6 年之前
父節點
當前提交
d4626b4d18

+ 26 - 28
hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/Quota.java

@@ -18,12 +18,14 @@
 package org.apache.hadoop.hdfs.server.federation.router;
 
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.hadoop.fs.QuotaUsage;
 import org.apache.hadoop.fs.StorageType;
 import org.apache.hadoop.hdfs.protocol.ClientProtocol;
@@ -33,6 +35,9 @@ import org.apache.hadoop.hdfs.server.namenode.NameNode.OperationCategory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+
 /**
  * Module that implements the quota relevant RPC calls
  * {@link ClientProtocol#setQuota(String, long, long, StorageType)}
@@ -121,37 +126,31 @@ public class Quota {
     final List<RemoteLocation> locations = getQuotaRemoteLocations(path);
 
     // NameService -> Locations
-    Map<String, List<RemoteLocation>> validLocations = new HashMap<>();
+    ListMultimap<String, RemoteLocation> validLocations =
+        ArrayListMultimap.create();
+
     for (RemoteLocation loc : locations) {
-      String nsId = loc.getNameserviceId();
-      List<RemoteLocation> dests = validLocations.get(nsId);
-      if (dests == null) {
-        dests = new LinkedList<>();
-        dests.add(loc);
-        validLocations.put(nsId, dests);
-      } else {
-        // Ensure the paths in the same nameservice is different.
-        // Don't include parent-child paths.
-        boolean isChildPath = false;
-        for (RemoteLocation d : dests) {
-          if (loc.getDest().startsWith(d.getDest())) {
-            isChildPath = true;
-            break;
-          }
-        }
+      final String nsId = loc.getNameserviceId();
+      final Collection<RemoteLocation> dests = validLocations.get(nsId);
+
+      // Ensure the paths in the same nameservice is different.
+      // Do not include parent-child paths.
+      boolean isChildPath = false;
 
-        if (!isChildPath) {
-          dests.add(loc);
+      for (RemoteLocation d : dests) {
+        if (StringUtils.startsWith(loc.getDest(), d.getDest())) {
+          isChildPath = true;
+          break;
         }
       }
-    }
 
-    List<RemoteLocation> quotaLocs = new LinkedList<>();
-    for (List<RemoteLocation> locs : validLocations.values()) {
-      quotaLocs.addAll(locs);
+      if (!isChildPath) {
+        validLocations.put(nsId, loc);
+      }
     }
 
-    return quotaLocs;
+    return Collections
+        .unmodifiableList(new ArrayList<>(validLocations.values()));
   }
 
   /**
@@ -209,7 +208,7 @@ public class Quota {
    */
   private List<RemoteLocation> getQuotaRemoteLocations(String path)
       throws IOException {
-    List<RemoteLocation> locations = new LinkedList<>();
+    List<RemoteLocation> locations = new ArrayList<>();
     RouterQuotaManager manager = this.router.getQuotaManager();
     if (manager != null) {
       Set<String> childrenPaths = manager.getPaths(path);
@@ -217,7 +216,6 @@ public class Quota {
         locations.addAll(rpcServer.getLocationsForPath(childPath, true, false));
       }
     }
-
     return locations;
   }
 }