diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 2ad1c6e5c9e..7fb72babe47 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -58,6 +58,9 @@ Release 0.23.1 - UNRELEASED HDFS-2675. Reduce warning verbosity when double-closing edit logs (todd) + HDFS-2335. DataNodeCluster and NNStorage always pull fresh entropy. + (Uma Maheswara Rao G via eli) + OPTIMIZATIONS HDFS-2130. Switch default checksum to CRC32C. (todd) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSUtil.java index a6ec1f0b107..cadfcee5fed 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSUtil.java @@ -30,6 +30,7 @@ import java.io.UnsupportedEncodingException; import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -72,11 +73,23 @@ public class DFSUtil { return new Random(); } }; + + private static final ThreadLocal SECURE_RANDOM = new ThreadLocal() { + @Override + protected SecureRandom initialValue() { + return new SecureRandom(); + } + }; - /** @return a pseudorandom number generator. */ + /** @return a pseudo random number generator. */ public static Random getRandom() { return RANDOM.get(); } + + /** @return a pseudo secure random number generator. */ + public static SecureRandom getSecureRandom() { + return SECURE_RANDOM.get(); + } /** * Compartor for sorting DataNodeInfo[] based on decommissioned states. diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java index f8dedeaa8c8..9c7a6119876 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java @@ -65,7 +65,6 @@ import java.net.UnknownHostException; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.security.PrivilegedExceptionAction; -import java.security.SecureRandom; import java.util.AbstractList; import java.util.ArrayList; import java.util.Arrays; @@ -1056,7 +1055,7 @@ public class DataNode extends Configured LOG.warn("Could not find ip address of \"default\" inteface."); } - int rand = new SecureRandom().nextInt(Integer.MAX_VALUE); + int rand = DFSUtil.getSecureRandom().nextInt(Integer.MAX_VALUE); return "DS-" + rand + "-" + ip + "-" + port + "-" + System.currentTimeMillis(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java index 869922abb2d..31e8a7ec205 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java @@ -26,8 +26,6 @@ import java.io.RandomAccessFile; import java.io.OutputStream; import java.net.URI; import java.net.UnknownHostException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -955,13 +953,7 @@ public class NNStorage extends Storage implements Closeable { throw e; } - int rand = 0; - try { - rand = SecureRandom.getInstance("SHA1PRNG").nextInt(Integer.MAX_VALUE); - } catch (NoSuchAlgorithmException e) { - LOG.warn("Could not use SecureRandom"); - rand = DFSUtil.getRandom().nextInt(Integer.MAX_VALUE); - } + int rand = DFSUtil.getSecureRandom().nextInt(Integer.MAX_VALUE); String bpid = "BP-" + rand + "-"+ ip + "-" + System.currentTimeMillis(); return bpid; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/DataNodeCluster.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/DataNodeCluster.java index f82986f331b..f3350b988a7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/DataNodeCluster.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/DataNodeCluster.java @@ -19,10 +19,7 @@ package org.apache.hadoop.hdfs; import java.io.IOException; import java.net.UnknownHostException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.util.Arrays; -import java.util.Random; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; @@ -234,12 +231,7 @@ public class DataNodeCluster { System.out.println("Could not find ip address of \"default\" inteface."); } - int rand = 0; - try { - rand = SecureRandom.getInstance("SHA1PRNG").nextInt(Integer.MAX_VALUE); - } catch (NoSuchAlgorithmException e) { - rand = (new Random()).nextInt(Integer.MAX_VALUE); - } + int rand = DFSUtil.getSecureRandom().nextInt(Integer.MAX_VALUE); return "/Rack-" + rand + "-"+ ip + "-" + System.currentTimeMillis(); }