Support server node weights.

Support setting a weighting value for each node that will determine
the proportion of load balancing requests that it receives in relation
to the other defined servers and their weights.

Also adds a file describing the worker JSON message versioning.

Fixes bug 1117349

Change-Id: Ie1460c165f03b0523ee28d864b74ea9e5858b256
This commit is contained in:
David Shrewsbury
2013-02-06 11:00:20 -05:00
parent 8316e49965
commit 53fedfca2a
6 changed files with 81 additions and 9 deletions

View File

@@ -75,7 +75,7 @@ class LBaaSController(object):
version this worker supports.
"""
# Version of the JSON message format that this worker understands.
msg_fmt_version = "1.0"
msg_fmt_version = "1.1"
self.msg['version'] = msg_fmt_version
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_SUCCESS
return self.msg

View File

@@ -48,7 +48,7 @@ class LoadBalancerDriver(object):
""" Add a supported protocol and listening port for the instance. """
raise NotImplementedError()
def add_server(self, protocol, host, port):
def add_server(self, protocol, host, port, weight):
""" Add a server for the protocol for which we will proxy. """
raise NotImplementedError()

View File

@@ -92,9 +92,9 @@ class HAProxyDriver(LoadBalancerDriver):
if proto == 'http':
output.append(' cookie SERVERID rewrite')
for (addr, port) in protocfg['servers']:
output.append(' server server%d %s:%s' %
(serv_num, addr, port))
for (addr, port, weight) in protocfg['servers']:
output.append(' server server%d %s:%s weight %d' %
(serv_num, addr, port, weight))
serv_num += 1
return '\n'.join(output) + '\n'
@@ -123,11 +123,20 @@ class HAProxyDriver(LoadBalancerDriver):
else:
self._bind(proto, '0.0.0.0', port)
def add_server(self, protocol, host, port):
def add_server(self, protocol, host, port, weight=1):
proto = protocol.lower()
try:
weight = int(weight)
except ValueError:
raise Exception("Non-integer 'weight' value: '%s'" % weight)
if weight > 256:
raise Exception("Server 'weight' %d exceeds max of 256" % weight)
if 'servers' not in self._config[proto]:
self._config[proto]['servers'] = []
self._config[proto]['servers'].append((host, port))
self._config[proto]['servers'].append((host, port, weight))
def set_algorithm(self, protocol, algo):
proto = protocol.lower()