From 91db060cccc7c1efd7c3e071854ad351dce0c292 Mon Sep 17 00:00:00 2001 From: Arun Suresh Date: Thu, 30 Apr 2015 13:59:43 -0700 Subject: [PATCH] HADOOP-11891. OsSecureRandom should lazily fill its reservoir (asuresh) (cherry picked from commit f0db797be28ca221d540c6a3accd6bff9a7996fa) --- .../hadoop-common/CHANGES.txt | 2 ++ .../hadoop/crypto/random/OsSecureRandom.java | 19 ++++--------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index b1441774411..49168c74b64 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -24,6 +24,8 @@ Release 2.7.1 - UNRELEASED HADOOP-11802. DomainSocketWatcher thread terminates sometimes after there is an I/O error during requestShortCircuitShm (cmccabe) + HADOOP-11891. OsSecureRandom should lazily fill its reservoir (asuresh) + Release 2.7.0 - 2015-04-20 INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/random/OsSecureRandom.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/random/OsSecureRandom.java index fee418608dd..2010e6d84e5 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/random/OsSecureRandom.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/random/OsSecureRandom.java @@ -58,6 +58,9 @@ public class OsSecureRandom extends Random implements Closeable, Configurable { private void fillReservoir(int min) { if (pos >= reservoir.length - min) { try { + if (stream == null) { + stream = new FileInputStream(new File(randomDevPath)); + } IOUtils.readFully(stream, reservoir, 0, reservoir.length); } catch (IOException e) { throw new RuntimeException("failed to fill reservoir", e); @@ -75,21 +78,7 @@ public class OsSecureRandom extends Random implements Closeable, Configurable { this.randomDevPath = conf.get( HADOOP_SECURITY_SECURE_RANDOM_DEVICE_FILE_PATH_KEY, HADOOP_SECURITY_SECURE_RANDOM_DEVICE_FILE_PATH_DEFAULT); - File randomDevFile = new File(randomDevPath); - - try { - close(); - this.stream = new FileInputStream(randomDevFile); - } catch (IOException e) { - throw new RuntimeException(e); - } - - try { - fillReservoir(0); - } catch (RuntimeException e) { - close(); - throw e; - } + close(); } @Override