diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/Quota.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/Quota.java index d8ed080236..5d0309fa57 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/Quota.java +++ b/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.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 @@ private List getValidQuotaLocations(String path) final List locations = getQuotaRemoteLocations(path); // NameService -> Locations - Map> validLocations = new HashMap<>(); - for (RemoteLocation loc : locations) { - String nsId = loc.getNameserviceId(); - List 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; - } - } + ListMultimap validLocations = + ArrayListMultimap.create(); - if (!isChildPath) { - dests.add(loc); + for (RemoteLocation loc : locations) { + final String nsId = loc.getNameserviceId(); + final Collection dests = validLocations.get(nsId); + + // Ensure the paths in the same nameservice is different. + // Do not include parent-child paths. + boolean isChildPath = false; + + for (RemoteLocation d : dests) { + if (StringUtils.startsWith(loc.getDest(), d.getDest())) { + isChildPath = true; + break; } } + + if (!isChildPath) { + validLocations.put(nsId, loc); + } } - List quotaLocs = new LinkedList<>(); - for (List locs : validLocations.values()) { - quotaLocs.addAll(locs); - } - - return quotaLocs; + return Collections + .unmodifiableList(new ArrayList<>(validLocations.values())); } /** @@ -209,7 +208,7 @@ private QuotaUsage aggregateQuota(Map results) { */ private List getQuotaRemoteLocations(String path) throws IOException { - List locations = new LinkedList<>(); + List locations = new ArrayList<>(); RouterQuotaManager manager = this.router.getQuotaManager(); if (manager != null) { Set childrenPaths = manager.getPaths(path); @@ -217,7 +216,6 @@ private List getQuotaRemoteLocations(String path) locations.addAll(rpcServer.getLocationsForPath(childPath, true, false)); } } - return locations; } }