blob: 745b840b26ce7240fddc4d728e8580d9a80192b7 [file] [log] [blame]
import Queue
import json
import os
import signal
from subprocess import CalledProcessError
import sys
import threading
from mysos.common.cluster import ClusterListener, get_cluster_path
from mysos.common.zookeeper import parse
from .installer import PackageInstaller
from .state import StateManager
from .task_runner import TaskError, TaskRunner, TaskRunnerProvider
from .task_control import TaskControl
from kazoo.client import KazooClient
from twitter.common import log
from twitter.common.concurrent import defer
from twitter.common.zookeeper.serverset.endpoint import Endpoint, ServiceInstance
class MysosTaskRunnerProvider(TaskRunnerProvider):
def __init__(self, task_control_provider, installer_provider, backup_store_provider):
self._task_control_provider = task_control_provider
self._installer_provider = installer_provider
self._backup_store_provider = backup_store_provider
def from_task(self, task, sandbox):
data = json.loads(
cluster_name, host, port, zk_url = data['cluster'], data['host'], data['port'], data['zk_url']
_, servers, path = parse(zk_url)
kazoo = KazooClient(servers)
self_instance = ServiceInstance(Endpoint(host, port))
task_control = self._task_control_provider.from_task(task, sandbox)
installer = self._installer_provider.from_task(task, sandbox)
backup_store = self._backup_store_provider.from_task(task, sandbox)
except (TaskControl.Error, PackageInstaller.Error) as e:
raise TaskError(e.message)
state_manager = StateManager(sandbox, backup_store)
return MysosTaskRunner(
get_cluster_path(path, cluster_name),
class MysosTaskRunner(TaskRunner):
A runner that manages the lifecycle of a MySQL task (through the provided 'task_control').
The task is executed as a long-running process its return code can be obtained using 'join()'.
This class is accessed from the MysosExecutor thread (not the ExecutorDriver thread because
MysosExecutor invokes operations asynchronously) and the ClusterListener thread and is
TODO(jyx): Push the knowledge of the underlying subprocess down to the task control and stop the
the subprocess using the task control.
def __init__(self, self_instance, kazoo, cluster_root, installer, task_control, state_manager):
:param self_instance: The local ServiceInstance associated with this task runner.
:param kazoo: Kazoo client, it should be started before being passed in.
:param cluster_root: The ZooKeeper root path for *this cluster*.
:param installer: The PackageInstaller for MySQL.
:param task_control: The TaskControl that interacts with the task process.
:param state_manager: The StateManager for managing the executor state.
self._installer = installer
self._env = None # The environment variables for the 'task_control' commands. Set by the
# installer.
self._task_control = task_control
self._state_manager = state_manager
self._lock = threading.Lock()
self._popen = None # The singleton task process started by '_task_control'.
self._started = False # Indicates whether start() has already been called.
self._stopping = False # Indicates whether stop() has already been called.
self._exited = threading.Event() # Set when the task process has exited.
self._result = Queue.Queue() # The returncode returned by the task process or an exception.
# Public events and queue.
self.promoted = threading.Event()
self.demoted = threading.Event()
self.master = Queue.Queue() # Set when a master change is detected.
self._listener = ClusterListener(
master_callback=self._on_master_change) # Listener started by start().
# --- Public interface. ---
def start(self):
Start the runner in a separate thread and wait for the task process to be forked.
with self._lock:
if self._started:
raise TaskError("Runner already started")
self._started = True
# Can potentially hold the lock for a long time but it's OK since the runner is not accessed
# by multiple threads until after it's started; can be a noop as well, depending on the
# installer implementation.
# 1. Install the application.
self._env = self._installer.install()"Package installation completed. Resulting environment variables: %s" % self._env)
# 2. Restore/initialize the application state.
self._state_manager.bootstrap(self._task_control, self._env)"Executor state fully bootstrapped")
# 3. Start the task subprocess.
# Store the process so we can kill it if necessary.
self._popen = self._task_control.start(env=self._env)"Task started in subprocess %s" %
# 4. Start monitoring.
# Only start listening to ZK events after the task subprocess has been successfully started.
except (PackageInstaller.Error, StateManager.Error, CalledProcessError) as e:
raise TaskError("Failed to start MySQL task: %s" % e)
def _wait(self):
# Block until the subprocess exits and delivers the return code.
# Notify stop() if it is waiting.
def stop(self, timeout=10):
Stop the runner and wait for its thread (and the sub-processes) to exit.
:param timeout: The timeout that the process should die before a hard SIGKILL is issued
(SIGTERM is used initially).
:return: True if an active runner is stopped, False if the runner is not started or already
if not self._started:
log.warn("Cannot stop the runner because it's not started")
return False
if self._stopping:
log.warn("The runner is already stopping/stopped")
return False
with self._lock:"Stopping runner")
self._stopping = True
if not self._popen:"The runner task did not start successfully so no need to kill it")
return False
try:"Terminating process group: %s" %
os.killpg(, signal.SIGTERM)
except OSError as e:"The sub-processes are already terminated: %s" % e)
return False"Waiting for process to terminate due to SIGTERM")
# Escalate to SIGKILL if SIGTERM is not sufficient.
if not self._exited.wait(timeout=timeout):
with self._lock:
log.warn("Killing process group %s which failed to terminate cleanly within %s secs" %
(, timeout))
os.killpg(, signal.SIGKILL)
except OSError as e:"The sub-processes are already terminated: %s" % e)
return False"Waiting for process to terminate due to SIGKILL")
if not self._exited.wait(timeout=timeout):
raise TaskError("Failed to kill process group %s" %
return True
def get_log_position(self):
Get the log position of the MySQL slave. Return None if it cannot be obtained.
log_position = self._task_control.get_log_position(env=self._env)
return log_position
except CalledProcessError as e:
raise TaskError("Unable to get the slave's log position: %s" % e)
def join(self):
Wait for the runner to terminate.
:return: The return code of the subprocess. NOTE: A negative value -N indicates that the
child was terminated by signal N (on Unix).
:exception: The TaskError exception due to an error in task control operations.
# Using 'sys.maxint' makes this forever wait interruptible.
result = self._result.get(True, sys.maxint)
if isinstance(result, Exception):
raise result
return result
# --- ClusterListener handlers. ---
def _on_promote(self):
if not self._exited.is_set():
def _promote(self):
except CalledProcessError as e:
self._result.put(TaskError("Failed to promote the slave: %s" % e))
def _on_demote(self):
Executor shuts itself down when demoted.
# Stop the runner asynchronously.
if not self._exited.is_set():"Shutting down runner because it is demoted.")
# Call stop() asynchronously because this callback is invoked from the Kazoo thread which we
# don't want to block.
def _on_master_change(self, master):
if not self._exited.is_set():
defer(lambda: self._reparent(master))
def _reparent(self, master):
except CalledProcessError as e:
self._result.put(TaskError("Failed to reparent the slave: %s" % e))