MEDIUM: backend: avoid picking the same server twice in random draws
Aleksandar Lazic suggested [1] that the random() distribution could
be improved to avoid picking the same server twice, comparing it with
itself and choosing it, possibly even if suboptimal. His mesurements
showed that a slow server was picked more often than desired.
It turns out that we don't need extra draws to avoid this situation,
because get_server_rnd() uses chash_get_server_hash(), which supports
a server to avoid (normally used for redispatch, but usable here as
well to avoid picking the same server twice), so we can pass there the
pointer to the currently chosen best server and count on the function
to pick another one.
Before the patch, a config with 1 slow server responding in 50ms,
and N-1 fast servers responding in 5ms would give this distribution
for 100k requests at 100 concurrency:
It's clear that the distribution is much better and follows the ratios
of response times, with little to no difference in maximum server load,
thus being much closer to a leastconn algo. The average rate is also
better, +33% for 4 servers and +5% for 8.
Note that in order to avoid wasting CPU cycles in degenerate farm cases,
the two previous patches are strongly recommended:
OPTIM: lb-chash: don't look for more servers when there is only one
OPTIM: lb-chash: limit the scan to few consecutive nodes