On Fri, Sep 26, 2014 at 12:53:49PM +0300, JCM wrote:
> On 25 September 2014 16:45, Gerd Müller <gmuel...@gmbd.de> wrote:
> > Hi list,
> >
> > we want to stress test our system. We have 8 nodes behind the haproxy and 8
> > server infront to generate the request. Since we are using source based
> > loadbalancing I would like to know how the hash is build so I can give the
> > requesting the proper ips.
> 
> I don't know this. However, in your position, I'd either
> 
> a) dive into the source code:
> http://git.haproxy.org/?p=haproxy-1.5.git;a=tree or
> b) do some very quick experiments with IPs taken from an RFC1918 /24
> until I got an even spread of requests to backends or
> c) change load balancing algorithm for the tests to something like
> round-robin as per
> http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#balance

JFYI, the hash code is available here, in src/backend.c :

struct server *get_server_sh(struct proxy *px, const char *addr, int len)

What the code does is the following :

  1) hash = XOR(all 32-bit network-order blocks of the input address)
          = IP address for IPv4 addresses

  2) hash = full_hash(hash) if avalanche hashing is enabled

  3) assuming you're not using consistent hashing, the resulting value
     above is passed to map_get_server_hash() which simply divides it
     by the total farm's weight (sum of each individual server's weight),
     and the remainder gives the server position, starting at zero for
     the first one.

So if all of your servers run with the same weight, you write your address
in network order, divide it by the number of servers and that should work.

Example: 10.1.2.3 is 0x0A010203 = 167838211. Let's assume you have 5 servers,
167838211 % 5 = 1 so you should direct it to the second server. That also
means that consecutive addresses will rotate through your servers.

Please note, I'm doing this via a quick read of the code, if you experience
a different result, just read the code and fix my errors :-)

Hoping this helps,
Willy


Reply via email to