HDFS-13987. RBF: Review of RandomResolver Class. Contributed by BELUGA BEHR.

This commit is contained in:
Inigo Goiri 2018-10-15 09:51:26 -07:00
parent f880ff418c
commit ee1c80ea32

View File

@ -17,15 +17,15 @@
*/ */
package org.apache.hadoop.hdfs.server.federation.resolver.order; package org.apache.hadoop.hdfs.server.federation.resolver.order;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.collections.CollectionUtils;
import org.apache.hadoop.hdfs.server.federation.resolver.PathLocation; import org.apache.hadoop.hdfs.server.federation.resolver.PathLocation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.collect.Iterables;
/** /**
* Order the destinations randomly. * Order the destinations randomly.
@ -35,10 +35,6 @@ public class RandomResolver implements OrderedResolver {
private static final Logger LOG = private static final Logger LOG =
LoggerFactory.getLogger(RandomResolver.class); LoggerFactory.getLogger(RandomResolver.class);
/** Random number generator. */
private static final Random RANDOM = new Random();
/** /**
* Get a random name space from the path. * Get a random name space from the path.
* *
@ -47,16 +43,12 @@ public class RandomResolver implements OrderedResolver {
* @return Random name space. * @return Random name space.
*/ */
public String getFirstNamespace(final String path, final PathLocation loc) { public String getFirstNamespace(final String path, final PathLocation loc) {
if (loc == null) { final Set<String> namespaces = (loc == null) ? null : loc.getNamespaces();
return null; if (CollectionUtils.isEmpty(namespaces)) {
}
Set<String> namespaces = loc.getNamespaces();
if (namespaces == null || namespaces.isEmpty()) {
LOG.error("Cannot get namespaces for {}", loc); LOG.error("Cannot get namespaces for {}", loc);
return null; return null;
} }
List<String> nssList = new ArrayList<>(namespaces); final int index = ThreadLocalRandom.current().nextInt(namespaces.size());
int index = RANDOM.nextInt(nssList.size()); return Iterables.get(namespaces, index);
return nssList.get(index);
} }
} }