[CDRIVER-1639] Rand used to pick Mongo server is not seeded Created: 07/Oct/16  Updated: 23/Feb/17  Resolved: 13/Oct/16

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: None
Fix Version/s: 1.5.0

Type: Bug Priority: Minor - P4
Reporter: Tom Hudson Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CDRIVER-1681 Backport CDRIVER-1639 to 1.3 Closed
Related
related to CDRIVER-2067 Server selection is not random on Win... Closed
is related to CDRIVER-1893 localThresholdMS calculated in micros... Closed

 Description   

The call to rand() used when selecting a server is unseeded (https://github.com/mongodb/mongo-c-driver/blob/a4bcfffa9c7ffa2e541a3351ec26cfad417cbdcf/src/mongoc/mongoc-topology-description.c#L693)

This tends to manifest itself as uneven balancing between nodes in a cluster.

As a specific example: when libmongoc is used as part of, say, the php-driver:

1. A sudden increase in traffic creates new children (e.g. Apache workers)
2. The new workers create new connections to mongo using the php-driver, and therefore libmongoc
3. All workers initially choose the same server from the cluster as the first value from rand() is always the same
4. One mongo node takes the vast majority of the queries - possibly causing it to become overloaded while other nodes in the cluster receive little traffic



 Comments   
Comment by Githook User [ 13/Oct/16 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1639 seed the PRNG used to select servers
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/5f33f7d78de8c8ff250461ca5d44fe49559efa61

Comment by A. Jesse Jiryu Davis [ 07/Oct/16 ]

We have rand_s on Windows and rand_r everywhere else, which are good enough for server selection.

Comment by Derick Rethans [ 07/Oct/16 ]

That would be random_r (Linux) and random_s (Windows). Sadly, it doesn't seem that Mac OSX has either, and recommends arc4random instead (which I don't think does what we want it to do).

Comment by A. Jesse Jiryu Davis [ 07/Oct/16 ]

derick and I think the right thing is for the C Driver to switch from rand to using rand_r (or rand_s on Windows), with a seed that's initially the system time. Then we have a properly seed PRNG that doesn't interfere with the global "rand" state.

Comment by Tom Hudson [ 07/Oct/16 ]

PR for potential fix on GitHub: https://github.com/mongodb/mongo-c-driver/pull/398

Generated at Wed Feb 07 21:13:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.