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

Stephen Tyree commented on ZOOKEEPER-1018:
------------------------------------------

Running 1000 times with the old way, I get the following distribution:

308 The connected host is: (address 1):2181
369 The connected host is: (address 2):2181
324 The connected host is: (address 3):2181

Running 1000 times with the new way, I get the following distribution:

333 The connected host is: (address 1):2181
325 The connected host is: (address 2):2181
343 The connected host is: (address 3):2181

Hardly scientific, but an indication that the new way is better or at the least 
as good.

> The connection permutation in get_addrs uses a weak and inefficient shuffle
> ---------------------------------------------------------------------------
>
>                 Key: ZOOKEEPER-1018
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-1018
>             Project: ZooKeeper
>          Issue Type: Improvement
>          Components: c client
>    Affects Versions: 3.3.2
>            Reporter: Stephen Tyree
>            Priority: Minor
>         Attachments: zookeeper.c.patch
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> After determining all of the addresses in the get_addrs function in the C 
> client, the connection is permuted using the following code:
>         setup_random();
>         /* Permute */
>         for(i = 0; i < zh->addrs_count; i++) {
>             struct sockaddr_storage *s1 = zh->addrs + 
> random()%zh->addrs_count;
>             struct sockaddr_storage *s2 = zh->addrs + 
> random()%zh->addrs_count;
>             if (s1 != s2) {
>                 struct sockaddr_storage t = *s1;
>                 *s1 = *s2;
>                 *s2 = t;
>             }
>         }
> Not only does this shuffle produce an uneven permutation, but it is half as 
> efficient as the Fisher-Yates shuffle which produces an unbiased one. It 
> seems like it would be a simple fix to increase the randomness and efficiency 
> of the shuffle by switching over to using Fisher-Yates.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to