Commit 33f7e12b authored by Moti Cohen's avatar Moti Cohen Committed by Oran Agra
Browse files

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.

(cherry picked from commit 52b2fbe9)
parent 90891a7d
...@@ -6235,9 +6235,12 @@ int main(int argc, char **argv) { ...@@ -6235,9 +6235,12 @@ int main(int argc, char **argv) {
setlocale(LC_COLLATE,""); setlocale(LC_COLLATE,"");
tzset(); /* Populates 'timezone' global. */ tzset(); /* Populates 'timezone' global. */
zmalloc_set_oom_handler(redisOutOfMemoryHandler); 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); 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()); init_genrand64(((long long) tv.tv_sec * 1000000 + tv.tv_usec) ^ getpid());
crc64_init(); crc64_init();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment