You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Michael Smith <mj...@iii.co.uk> on 1999/11/25 17:21:11 UTC
load-balancing patch to mod_rewrite
Dear All,
I enclose a patch to mod_rewrite, which is intended to address load
balancing through randomized plain text files. In our experience, the
load balancing is not even when we use more than two backed servers,
which we attribute to the rounding through sprintf (with the result that
the first and last servers get less than their fair share of requests).
For example, in a test-run 300 requests to three servers and the line
test www1|www2|www3
we see the following numbers:
www1 102 hits, www2 205 hits www3 93 hits.
Over a long period of time it tends to a ratio of 1:2:1
The attached patch (to 1.3.6, but I don't think the routine affected,
rewrite_rand, has changed since.) dopts a slightly different way of
choosing a random server. In our testing the ration in the above
example (and in others) tends to 1:1:1.
Can anyone see any reason why this patch shouldn't be applied?
Michael Smith
Re: load-balancing patch to mod_rewrite
Posted by Greg Stein <gs...@lyra.org>.
On Fri, 26 Nov 1999, Jim Winstead wrote:
> On Nov 26, Greg Stein wrote:
> > There is no reason to use doubles or an sprintf/atoi combo. If the above
> > works for you, then please let me know and I'll apply the patch.
>
> >From rand(3) on Linux:
Aw, heck. I hit the man page, verified the prototype, but didn't go to the
bottom.
I updated the code (again :-) to use a double now. Still a single line.
Also added some comments to explain the situation.
thx,
-g
--
Greg Stein, http://www.lyra.org/
Re: load-balancing patch to mod_rewrite
Posted by Jim Winstead <ji...@trainedmonkey.com>.
On Nov 26, Greg Stein wrote:
> There is no reason to use doubles or an sprintf/atoi combo. If the above
> works for you, then please let me know and I'll apply the patch.
>From rand(3) on Linux:
The versions of rand() and srand() in the Linux C Library
use the same random number generator as random() and srandom(),
so the lower-order bits should be as random as the higher-order
bits. However, on older rand() implementations, the
lower-order bits are much less random than the higher-order
bits.
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed, p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
I don't have an explanation for the sprintf()/atoi() stuff that is
there in the mod_rewrite code, but the casting-to-double stuff is
legit.
Jim
Re: load-balancing patch to mod_rewrite
Posted by Michael Smith <mj...@iii.co.uk>.
Greg Stein wrote:
> > Can anyone see any reason why this patch shouldn't be applied?
Fair enough :)
> Yes, the routine is still much more complicated than it should be. Try
> this instead:
>
> {
> rewrite_rand_init();
> return rand() % (h - l + 1) + l;
> }
>
> There is no reason to use doubles or an sprintf/atoi combo. If the above
> works for you, then please let me know and I'll apply the patch.
Works for me - I tesed it out over a few hundred requests and it came out
very even.
Mike
Re: load-balancing patch to mod_rewrite
Posted by Greg Stein <gs...@lyra.org>.
On Thu, 25 Nov 1999, Michael Smith wrote:
>...
> www1 102 hits, www2 205 hits www3 93 hits.
>
> Over a long period of time it tends to a ratio of 1:2:1
Understandably.
1 = [1, 1.5)
2 = [1.5, 2.5)
3 = [2.5, 3)
You can see that 2 has much more weighting than the others.
> The attached patch (to 1.3.6, but I don't think the routine affected,
> rewrite_rand, has changed since.) dopts a slightly different way of
> choosing a random server. In our testing the ration in the above
> example (and in others) tends to 1:1:1.
>
> Can anyone see any reason why this patch shouldn't be applied?
Yes, the routine is still much more complicated than it should be. Try
this instead:
{
rewrite_rand_init();
return rand() % (h - l + 1) + l;
}
There is no reason to use doubles or an sprintf/atoi combo. If the above
works for you, then please let me know and I'll apply the patch.
Cheers,
-g
--
Greg Stein, http://www.lyra.org/