diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 4423b2f0ca8..0d780c8a49b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -648,6 +648,9 @@ Release 2.5.0 - UNRELEASED HDFS-6424. blockReport doesn't need to invalidate blocks on SBN. (Ming Ma via jing9) + HDFS-6497. Make TestAvailableSpaceVolumeChoosingPolicy deterministic + (cmccabe) + Release 2.4.1 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/AvailableSpaceVolumeChoosingPolicy.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/AvailableSpaceVolumeChoosingPolicy.java index e624effb779..ec19ec58601 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/AvailableSpaceVolumeChoosingPolicy.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/AvailableSpaceVolumeChoosingPolicy.java @@ -45,11 +45,19 @@ public class AvailableSpaceVolumeChoosingPolicy private static final Log LOG = LogFactory.getLog(AvailableSpaceVolumeChoosingPolicy.class); - private static final Random RAND = new Random(); + private final Random random; private long balancedSpaceThreshold = DFS_DATANODE_AVAILABLE_SPACE_VOLUME_CHOOSING_POLICY_BALANCED_SPACE_THRESHOLD_DEFAULT; private float balancedPreferencePercent = DFS_DATANODE_AVAILABLE_SPACE_VOLUME_CHOOSING_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_DEFAULT; + AvailableSpaceVolumeChoosingPolicy(Random random) { + this.random = random; + } + + public AvailableSpaceVolumeChoosingPolicy() { + this(new Random()); + } + @Override public synchronized void setConf(Configuration conf) { balancedSpaceThreshold = conf.getLong( @@ -128,7 +136,7 @@ public class AvailableSpaceVolumeChoosingPolicy (highAvailableVolumes.size() * balancedPreferencePercent) / preferencePercentScaler; if (mostAvailableAmongLowVolumes < replicaSize || - RAND.nextFloat() < scaledPreferencePercent) { + random.nextFloat() < scaledPreferencePercent) { volume = roundRobinPolicyHighAvailable.chooseVolume( highAvailableVolumes, replicaSize); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestAvailableSpaceVolumeChoosingPolicy.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestAvailableSpaceVolumeChoosingPolicy.java index 9326b6ad25f..9414a0e8a90 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestAvailableSpaceVolumeChoosingPolicy.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/TestAvailableSpaceVolumeChoosingPolicy.java @@ -22,6 +22,7 @@ import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_AVAILABLE_SPACE_ import java.util.ArrayList; import java.util.List; +import java.util.Random; import org.apache.hadoop.conf.Configurable; import org.apache.hadoop.conf.Configuration; @@ -251,10 +252,10 @@ public class TestAvailableSpaceVolumeChoosingPolicy { */ public void doRandomizedTest(float preferencePercent, int lowSpaceVolumes, int highSpaceVolumes) throws Exception { - @SuppressWarnings("unchecked") - final AvailableSpaceVolumeChoosingPolicy policy = - ReflectionUtils.newInstance(AvailableSpaceVolumeChoosingPolicy.class, null); - + Random random = new Random(123L); + final AvailableSpaceVolumeChoosingPolicy policy = + new AvailableSpaceVolumeChoosingPolicy(random); + List volumes = new ArrayList(); // Volumes with 1MB free space