From: phk Date: Mon, 8 Sep 2008 18:12:19 +0000 (+0000) Subject: Fix an off-by-one error in the random director, which made it unable to X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4027b3e4aa2670d576f36a09651e508ab2639a4b;p=varnish Fix an off-by-one error in the random director, which made it unable to use the single remaining healthy backend. Add regression test. Fixes: ticket #306 git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3174 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache_dir_random.c b/varnish-cache/bin/varnishd/cache_dir_random.c index f514cedc..bb386f79 100644 --- a/varnish-cache/bin/varnishd/cache_dir_random.c +++ b/varnish-cache/bin/varnishd/cache_dir_random.c @@ -96,14 +96,14 @@ vdi_random_getfd(struct sess *sp) r *= s1; s2 = 0; - j = 0; for (i = 0; i < vs->nhosts; i++) { if (!vs->hosts[i].backend->healthy) continue; s2 += vs->hosts[i].weight; - if (r > s2) - j = i + 1; + if (r < s2) + break; } + if (s2 != s1) { /* * Health bit changed in an unusable way while we @@ -112,7 +112,7 @@ vdi_random_getfd(struct sess *sp) */ continue; } - vbe = VBE_GetVbe(sp, vs->hosts[j].backend); + vbe = VBE_GetVbe(sp, vs->hosts[i].backend); if (vbe != NULL) return (vbe); k++; diff --git a/varnish-cache/bin/varnishtest/tests/r00306.vtc b/varnish-cache/bin/varnishtest/tests/r00306.vtc new file mode 100644 index 00000000..283769ea --- /dev/null +++ b/varnish-cache/bin/varnishtest/tests/r00306.vtc @@ -0,0 +1,53 @@ +# $Id: v00007.vtc 3060 2008-08-01 12:44:53Z phk $ + +test "Regression test for ticket #306, random director ignoring good backend" + +server s1 { + + rxreq + expect req.url == /foo + txresp -body "foo1" + + rxreq + expect req.url == /bar + txresp -body "bar1" + +} -start + +server s2 -listen 127.0.0.1:9180 { + rxreq + txresp -status 404 +} -start + +varnish v1 -vcl { + backend s1 { + .host = "127.0.0.1"; .port = "9080"; + } + backend s2 { + .host = "127.0.0.1"; .port = "9180"; + .probe = { + .url = "/"; + } + } + director foo random { + { .backend = s2; .weight = 1; } + { .backend = s1; .weight = 1; } + } + + sub vcl_recv { + set req.backend = foo; + } +} -start + +client c1 { + timeout 10 + + txreq -url "/foo" + rxresp + expect resp.status == 200 + + txreq -url "/bar" + rxresp + expect resp.status == 200 + +} -run