Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 8BAD5200CC2 for ; Wed, 5 Jul 2017 14:23:38 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 8A1FF1631C7; Wed, 5 Jul 2017 12:23:38 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B09991631C2 for ; Wed, 5 Jul 2017 14:23:37 +0200 (CEST) Received: (qmail 80784 invoked by uid 500); 5 Jul 2017 12:23:36 -0000 Mailing-List: contact dev-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list dev@zookeeper.apache.org Received: (qmail 80773 invoked by uid 99); 5 Jul 2017 12:23:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Jul 2017 12:23:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B571AE037F; Wed, 5 Jul 2017 12:23:35 +0000 (UTC) From: asdf2014 To: dev@zookeeper.apache.org Reply-To: dev@zookeeper.apache.org Message-ID: Subject: [GitHub] zookeeper pull request #303: ZOOKEEPER-2840: Should using `System.nanoTime()... Content-Type: text/plain Date: Wed, 5 Jul 2017 12:23:35 +0000 (UTC) archived-at: Wed, 05 Jul 2017 12:23:38 -0000 GitHub user asdf2014 opened a pull request: https://github.com/apache/zookeeper/pull/303 ZOOKEEPER-2840: Should using `System.nanoTime() ^ this.hashCode()` fo… Should using `System.nanoTime() ^ this.hashCode()` for StaticHostProvider instead of `System.currentTimeMillis()`. Because if we have three Zookeeper server nodes and set the `zookeeper.leaderServes` as `no`, then those connections from client will always connect with the first Zookeeper server node. Due to... ```java @Test public void testShuffle() throws Exception { LinkedList inetSocketAddressesList = new LinkedList<>(); inetSocketAddressesList.add(new InetSocketAddress(0)); inetSocketAddressesList.add(new InetSocketAddress(1)); inetSocketAddressesList.add(new InetSocketAddress(2)); /* 1442045361 currentTime: 1499253530044, currentTime ^ hashCode: 1500143845389, Result: 1 2 0 currentTime: 1499253530044, currentTime ^ hashCode: 1500143845389, Result: 2 0 1 currentTime: 1499253530045, currentTime ^ hashCode: 1500143845388, Result: 0 1 2 currentTime: 1499253530045, currentTime ^ hashCode: 1500143845388, Result: 1 2 0 currentTime: 1499253530046, currentTime ^ hashCode: 1500143845391, Result: 1 2 0 currentTime: 1499253530046, currentTime ^ hashCode: 1500143845391, Result: 1 2 0 currentTime: 1499253530046, currentTime ^ hashCode: 1500143845391, Result: 1 2 0 currentTime: 1499253530046, currentTime ^ hashCode: 1500143845391, Result: 1 2 0 currentTime: 1499253530047, currentTime ^ hashCode: 1500143845390, Result: 1 2 0 currentTime: 1499253530047, currentTime ^ hashCode: 1500143845390, Result: 1 2 0 */ internalShuffleMillis(inetSocketAddressesList); /* 146611050 currentTime: 22618159623770, currentTime ^ hashCode: 22618302559536, Result: 2 1 0 currentTime: 22618159800738, currentTime ^ hashCode: 22618302085832, Result: 0 1 2 currentTime: 22618159967442, currentTime ^ hashCode: 22618302248888, Result: 1 0 2 currentTime: 22618160135080, currentTime ^ hashCode: 22618302013634, Result: 2 1 0 currentTime: 22618160302095, currentTime ^ hashCode: 22618301535077, Result: 2 1 0 currentTime: 22618160490260, currentTime ^ hashCode: 22618301725822, Result: 1 0 2 currentTime: 22618161566373, currentTime ^ hashCode: 22618300303823, Result: 1 0 2 currentTime: 22618161745518, currentTime ^ hashCode: 22618300355844, Result: 2 1 0 currentTime: 22618161910357, currentTime ^ hashCode: 22618291603775, Result: 2 1 0 currentTime: 22618162079549, currentTime ^ hashCode: 22618291387479, Result: 0 1 2 */ internalShuffleNano(inetSocketAddressesList); inetSocketAddressesList.clear(); inetSocketAddressesList.add(new InetSocketAddress(0)); inetSocketAddressesList.add(new InetSocketAddress(1)); /* 415138788 currentTime: 1499253530050, currentTime ^ hashCode: 1499124456998, Result: 0 1 currentTime: 1499253530050, currentTime ^ hashCode: 1499124456998, Result: 0 1 currentTime: 1499253530050, currentTime ^ hashCode: 1499124456998, Result: 0 1 currentTime: 1499253530050, currentTime ^ hashCode: 1499124456998, Result: 0 1 currentTime: 1499253530050, currentTime ^ hashCode: 1499124456998, Result: 0 1 currentTime: 1499253530050, currentTime ^ hashCode: 1499124456998, Result: 0 1 currentTime: 1499253530053, currentTime ^ hashCode: 1499124456993, Result: 0 1 currentTime: 1499253530055, currentTime ^ hashCode: 1499124456995, Result: 0 1 currentTime: 1499253530055, currentTime ^ hashCode: 1499124456995, Result: 0 1 currentTime: 1499253530055, currentTime ^ hashCode: 1499124456995, Result: 0 1 */ internalShuffleMillis(inetSocketAddressesList); /* 13326370 currentTime: 22618168292396, currentTime ^ hashCode: 22618156149774, Result: 1 0 currentTime: 22618168416181, currentTime ^ hashCode: 22618156535703, Result: 1 0 currentTime: 22618168534056, currentTime ^ hashCode: 22618156432394, Result: 0 1 currentTime: 22618168666548, currentTime ^ hashCode: 22618155774358, Result: 0 1 currentTime: 22618168818946, currentTime ^ hashCode: 22618155623712, Result: 0 1 currentTime: 22618168936821, currentTime ^ hashCode: 22618156011863, Result: 1 0 currentTime: 22618169056251, currentTime ^ hashCode: 22618155893721, Result: 1 0 currentTime: 22618169611103, currentTime ^ hashCode: 22618157370237, Result: 1 0 currentTime: 22618169744528, currentTime ^ hashCode: 22618156713138, Result: 1 0 currentTime: 22618171273170, currentTime ^ hashCode: 22618184562672, Result: 1 0 */ internalShuffleNano(inetSocketAddressesList); } private void internalShuffleMillis(LinkedList inetSocketAddressesList) throws Exception { int hashCode = new StaticHostProvider(inetSocketAddressesList).hashCode(); System.out.println(hashCode); int count = 10; Random r; while (count > 0) { long currentTime = System.currentTimeMillis(); r = new Random(currentTime ^ hashCode); System.out.print(String.format("currentTime: %s, currentTime ^ hashCode: %s, Result: ", currentTime, currentTime ^ hashCode)); Collections.shuffle(inetSocketAddressesList, r); for (InetSocketAddress inetSocketAddress : inetSocketAddressesList) { System.out.print(String.format("%s ", inetSocketAddress.getPort())); } System.out.println(); count--; } } private void internalShuffleNano(LinkedList inetSocketAddressesList) throws Exception { int hashCode = new StaticHostProvider(inetSocketAddressesList).hashCode(); System.out.println(hashCode); int count = 10; Random r; while (count > 0) { long currentTime = System.nanoTime(); r = new Random(currentTime ^ hashCode); System.out.print(String.format("currentTime: %s, currentTime ^ hashCode: %s, Result: ", currentTime, currentTime ^ hashCode)); Collections.shuffle(inetSocketAddressesList, r); for (InetSocketAddress inetSocketAddress : inetSocketAddressesList) { System.out.print(String.format("%s ", inetSocketAddress.getPort())); } System.out.println(); count--; } } ``` You can merge this pull request into a Git repository by running: $ git pull https://github.com/asdf2014/zookeeper ZOOKEEPER-2840 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/zookeeper/pull/303.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #303 ---- commit dad9cdba24c825a56dfe94a277ea4232af429bdb Author: asdf2014 <1571805553@qq.com> Date: 2017-07-05T12:17:44Z ZOOKEEPER-2840: Should using `System.nanoTime() ^ this.hashCode()` for StaticHostProvider ---- --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---