[WORKER] Support cleaner HAProxy config reloading

Stopping and restarting the HAProxy service will rudely terminate
any active sessions in HAProxy. This new method allows existing
connections to finish whatever they are doing while the configuration
file is reloaded.

Change-Id: Ife5d3ccc66349ea3393541768f12aedddb5ac5c8
This commit is contained in:
David Shrewsbury
2013-10-07 11:32:55 -04:00
committed by Andrew Hutchings
parent fab4198f22
commit c5b094d3dc
4 changed files with 27 additions and 2 deletions

View File

@@ -19,6 +19,9 @@ class FakeOSServices(ServicesBase):
def service_start(self):
pass
def service_reload(self):
pass
def write_config(self, config_str):
pass
@@ -39,6 +42,9 @@ class FakeFaultingOSServices(ServicesBase):
def service_start(self):
raise Exception("fault")
def service_reload(self):
raise Exception("fault")
def service_restart(self):
raise Exception("fault")

View File

@@ -336,8 +336,7 @@ class HAProxyDriver(LoadBalancerDriver):
def create(self):
self.ossvc.write_config(self._config_to_string())
self.ossvc.service_stop()
self.ossvc.service_start()
self.ossvc.service_reload()
def suspend(self):
self.ossvc.service_stop()

View File

@@ -38,6 +38,10 @@ class ServicesBase:
""" Start the HAProxy service. """
raise NotImplementedError()
def service_reload(self):
""" Reload the HAProxy config file. """
raise NotImplementedError()
def write_config(self, config_str):
""" Write the HAProxy configuration file. """
raise NotImplementedError()

View File

@@ -58,6 +58,22 @@ class UbuntuServices(ServicesBase):
raise Exception("%s does not exist. Start failed." %
self._haproxy_pid)
def service_reload(self):
"""
Reload the HAProxy config file in a non-intrusive manner.
This assumes that /etc/init.d/haproxy is using the -sf option
to the haproxy process.
"""
cmd = '/usr/bin/sudo -n /usr/sbin/service haproxy reload'
try:
subprocess.check_output(cmd.split())
except subprocess.CalledProcessError as e:
raise Exception("Failed to reload HAProxy config: %s" % e)
if not os.path.exists(self._haproxy_pid):
raise Exception("%s does not exist. Reload failed." %
self._haproxy_pid)
def sudo_copy(self, from_file, to_file):
cmd = "/usr/bin/sudo -n /bin/cp %s %s" % (from_file, to_file)
try: