You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by "Mark Fenes (JIRA)" <ji...@apache.org> on 2018/03/20 13:15:00 UTC

[jira] [Commented] (ZOOKEEPER-1990) suspicious instantiation of java Random instances

    [ https://issues.apache.org/jira/browse/ZOOKEEPER-1990?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16406301#comment-16406301 ] 

Mark Fenes commented on ZOOKEEPER-1990:
---------------------------------------

[~shralex] According to Random's source, the parameterless instantiation of Random (new Random()) does the following:
{code:java}
public Random() {
    this(seedUniquifier() ^ System.nanoTime());
}{code}

The above looks to me at least as random as

{code:java}
sourceOfRandomness = new Random(System.currentTimeMillis() ^ this.hashCode());
{code}

or probably new Random() is even more random, as seedUniquifier() seems to be different each time, but this.hashCode() returns the same value if the same StaticHostProvider instance is used.

I understand those cases, when in tests, we would like to have the same repeatable results (e.g. when new Random(1) is used), but what is the purpose of setting a less random seed (e.g. sessionId ^ superSecret) instead of using new Random(), which appears to be more random?


> suspicious instantiation of java Random instances
> -------------------------------------------------
>
>                 Key: ZOOKEEPER-1990
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-1990
>             Project: ZooKeeper
>          Issue Type: Bug
>    Affects Versions: 3.5.0
>            Reporter: Patrick Hunt
>            Assignee: Mark Fenes
>            Priority: Critical
>             Fix For: 3.5.4, 3.6.0
>
>
> It's not clear to me why we are doing this, but it looks very suspicious. Why aren't we just calling "new Random()" in these cases? (even for the tests I don't really see it - typically that would just be for repeatability)
> {noformat}
> ag "new Random[ \t]*\(" .
> src/java/main/org/apache/zookeeper/ClientCnxn.java
> 817:        private Random r = new Random(System.nanoTime());        
> src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
> 75:       sourceOfRandomness = new Random(System.currentTimeMillis() ^ this.hashCode());
> 98:        sourceOfRandomness = new Random(randomnessSeed);
> src/java/main/org/apache/zookeeper/server/quorum/AuthFastLeaderElection.java
> 420:                rand = new Random(java.lang.Thread.currentThread().getId()
> src/java/main/org/apache/zookeeper/server/SyncRequestProcessor.java
> 64:    private final Random r = new Random(System.nanoTime());
> src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java
> 537:        Random r = new Random(id ^ superSecret);
> 554:        Random r = new Random(sessionId ^ superSecret);
> src/java/test/org/apache/zookeeper/server/quorum/WatchLeakTest.java
> 271:        Random r = new Random(SESSION_ID ^ superSecret);
> src/java/test/org/apache/zookeeper/server/quorum/CommitProcessorTest.java
> 151:            Random rand = new Random(Thread.currentThread().getId());
> 258:            Random rand = new Random(Thread.currentThread().getId());
> 288:        Random rand = new Random(Thread.currentThread().getId());
> src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
> 40:    private Random r = new Random(1);
> {noformat}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)