From ee1c80ea324459852e8c1c73b2545a261cb02b6f Mon Sep 17 00:00:00 2001 From: Inigo Goiri Date: Mon, 15 Oct 2018 09:51:26 -0700 Subject: [PATCH] HDFS-13987. RBF: Review of RandomResolver Class. Contributed by BELUGA BEHR. --- .../resolver/order/RandomResolver.java | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/order/RandomResolver.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/order/RandomResolver.java index 022aa48ef1..13643e5e9f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/order/RandomResolver.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/order/RandomResolver.java @@ -17,15 +17,15 @@ */ 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.concurrent.ThreadLocalRandom; +import org.apache.commons.collections.CollectionUtils; import org.apache.hadoop.hdfs.server.federation.resolver.PathLocation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Iterables; /** * Order the destinations randomly. @@ -35,10 +35,6 @@ public class RandomResolver implements OrderedResolver { private static final Logger LOG = LoggerFactory.getLogger(RandomResolver.class); - - /** Random number generator. */ - private static final Random RANDOM = new Random(); - /** * Get a random name space from the path. * @@ -47,16 +43,12 @@ public class RandomResolver implements OrderedResolver { * @return Random name space. */ public String getFirstNamespace(final String path, final PathLocation loc) { - if (loc == null) { - return null; - } - Set namespaces = loc.getNamespaces(); - if (namespaces == null || namespaces.isEmpty()) { + final Set namespaces = (loc == null) ? null : loc.getNamespaces(); + if (CollectionUtils.isEmpty(namespaces)) { LOG.error("Cannot get namespaces for {}", loc); return null; } - List nssList = new ArrayList<>(namespaces); - int index = RANDOM.nextInt(nssList.size()); - return nssList.get(index); + final int index = ThreadLocalRandom.current().nextInt(namespaces.size()); + return Iterables.get(namespaces, index); } } \ No newline at end of file