[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: |
|
||||||||||||||||||||
| 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) |
| 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: |
| 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 |