Add set_algorithm() method to driver API.

HAProxy driver can now be given an algorithm to use for load
balancing.
This commit is contained in:
David Shrewsbury
2012-10-10 16:50:41 -04:00
parent abb1af8706
commit 294ef7d849
3 changed files with 39 additions and 1 deletions

View File

@@ -13,6 +13,7 @@
# under the License. # under the License.
from libra.common.faults import BadRequest from libra.common.faults import BadRequest
from libra.worker.drivers.base import LoadBalancerDriver
class LBaaSController(object): class LBaaSController(object):
@@ -91,6 +92,26 @@ class LBaaSController(object):
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg return self.msg
if 'algorithm' in self.msg:
algo = self.msg['algorithm'].upper()
if algo == 'ROUND_ROBIN':
algo = LoadBalancerDriver.ROUNDROBIN
elif algo == 'LEAST_CONNECTIONS':
algo = LoadBalancerDriver.LEASTCONN
try:
self.driver.set_algorithm(algo)
except NotImplementedError:
self.logger.error(
"Selected driver does not support setting algorithm."
)
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg
except Exception as e:
self.logger.error("Selected driver failed setting algorithm.")
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg
for lb_node in self.msg['nodes']: for lb_node in self.msg['nodes']:
port, address = None, None port, address = None, None

View File

@@ -30,6 +30,10 @@ class LoadBalancerDriver(object):
via these API calls until the activate() method is called. via these API calls until the activate() method is called.
""" """
# Load balancer algorithms
ROUNDROBIN = 1
LEASTCONN = 2
def init(self): def init(self):
""" Allows the driver to do any initialization for a new config. """ """ Allows the driver to do any initialization for a new config. """
raise NotImplementedError() raise NotImplementedError()
@@ -42,6 +46,10 @@ class LoadBalancerDriver(object):
""" Set the protocol of the instance. """ """ Set the protocol of the instance. """
raise NotImplementedError() raise NotImplementedError()
def set_algorithm(self, algo):
""" Set the algorithm used by the load balancer. """
raise NotImplementedError()
def create(self): def create(self):
""" Create the load balancer. """ """ Create the load balancer. """
raise NotImplementedError() raise NotImplementedError()

View File

@@ -28,6 +28,7 @@ class HAProxyDriver(LoadBalancerDriver):
def _init_config(self): def _init_config(self):
self._config = dict() self._config = dict()
self.set_protocol('HTTP', 80) self.set_protocol('HTTP', 80)
self.set_algorithm(self.ROUNDROBIN)
def _bind(self, address, port): def _bind(self, address, port):
self._config['bind_address'] = address self._config['bind_address'] = address
@@ -60,7 +61,7 @@ class HAProxyDriver(LoadBalancerDriver):
output.append(' timeout connect 5000ms') output.append(' timeout connect 5000ms')
output.append(' timeout client 50000ms') output.append(' timeout client 50000ms')
output.append(' timeout server 5000ms') output.append(' timeout server 5000ms')
output.append(' balance roundrobin') output.append(' balance %s' % self._config['algorithm'])
output.append(' cookie SERVERID rewrite') output.append(' cookie SERVERID rewrite')
output.append('frontend http-in') output.append('frontend http-in')
output.append(' bind %s:%s' % (self._config['bind_address'], output.append(' bind %s:%s' % (self._config['bind_address'],
@@ -171,6 +172,14 @@ class HAProxyDriver(LoadBalancerDriver):
else: else:
self._bind('0.0.0.0', port) self._bind('0.0.0.0', port)
def set_algorithm(self, algo):
if algo == self.ROUNDROBIN:
self._config['algorithm'] = 'roundrobin'
elif algo == self.LEASTCONN:
self._config['algorithm'] = 'leastconn'
else:
raise Exception('Invalid algorithm')
def create(self): def create(self):
self._write_config() self._write_config()
self._restart() self._restart()