Question to Cache::Memcached

Joachim Bauernberger joachim@bauernberger.org
Mon, 7 Jun 2004 22:56:14 +0200


Hi Brad,

On Monday 07 June 2004 21:09, Brad Fitzpatrick wrote:
> > How does the module decide which backend to give it too (if no weight
> > value is specified)?
>
> It runs the key through a hashing function, then mods that number with the
> number of buckets (number of servers * each's weight) and uses the result
> as the bucket number.
>
> So if you have two servers:
>
> 10.0.0.1 weight 1   ->  1 bucket  (bucket 0)
> 10.0.0.2 weight 3   ->  3 buckets (bucket 1, 2, 3)
>
> Total:                  4 buckets
>
> So now we have key "foobar".  HashValue("foobar") = 238423432423434.
>
> Taking the hash value, mod 4 (4 buckets)...
>
>  238423432423434 % 4 = 2 (bucket 2)
>
> So it uses 10.0.0.2
>
> > Is there a restriction on how the keys may look?
>
> No spaces or newlines.
>
> > My keys are similar to this (without quotes):
> >
> > "010/dating/profilansicht/profilAnsicht.xmlindex=page1userid=2170797"
>
> That looks fine.
>
> So the mystery is:  what hash values is it calculating for your keys?
> It'd be way too weird if they were all 0.

I just added some debugging to Memcached.pm:

first in get_sock() just after asigning a value to $hv

print STDERR "[JMBDEBUG] hv = $hv bucketcount=".$self->{'bucketcount'}."\n";

this results in the following output:

[JMBDEBUG] hv = 8.25640843592502e+101 bucketcount=2


then in get_sock() just after $host gets calculated:
print STDERR "[JMBDEBUG] host=".$host." ".$self->{'buckets'}[0]." ".
$self->{'buckets'}[1]."\n";

which results in:
[JMBDEBUG] host=127.0.0.1:11211 127.0.0.1:11211 127.0.0.1:11212

Somehow I always get 0 from this:
 $hv % $self->{'bucketcount'}

Thanks again, 
~/joachim


-- 
http://www.bauernberger.org/