mirror of
https://github.com/redis/redis.git
synced 2026-06-04 06:07:43 -04:00
Improve srand entropy (and fix Sentinel failures) (#10197)
As Sentinel relies upon consensus algorithm, all sentinel instances, randomize a time to initiate their next attempt to become the leader of the group. But time after time, all raffled the same value. The problem is in the line `srand(time(NULL)^getpid())` such that all spinned up containers get same time (in seconds) and same pid which is always 1. Added material `tv_usec` and verify that even consecutive calls brings different values and makes the difference.
This commit is contained in:
parent
4e17154060
commit
52b2fbe970
1 changed files with 5 additions and 2 deletions
|
|
@ -6585,9 +6585,12 @@ int main(int argc, char **argv) {
|
|||
setlocale(LC_COLLATE,"");
|
||||
tzset(); /* Populates 'timezone' global. */
|
||||
zmalloc_set_oom_handler(redisOutOfMemoryHandler);
|
||||
srand(time(NULL)^getpid());
|
||||
srandom(time(NULL)^getpid());
|
||||
|
||||
/* To achieve entropy, in case of containers, their time() and getpid() can
|
||||
* be the same. But value of tv_usec is fast enough to make the difference */
|
||||
gettimeofday(&tv,NULL);
|
||||
srand(time(NULL)^getpid()^tv.tv_usec);
|
||||
srandom(time(NULL)^getpid()^tv.tv_usec);
|
||||
init_genrand64(((long long) tv.tv_sec * 1000000 + tv.tv_usec) ^ getpid());
|
||||
crc64_init();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue