def _consume(self, trial, workdirname): config_file = tempfile.NamedTemporaryFile(mode='w', prefix='trial_', suffix='.conf', dir=workdirname, delete=False) config_file.close() log.debug("## New temp config file: %s", config_file.name) results_file = tempfile.NamedTemporaryFile(mode='w', prefix='results_', suffix='.log', dir=workdirname, delete=False) results_file.close() log.debug("## New temp results file: %s", results_file.name) log.debug( "## Building command line argument and configuration for trial.") env = self.get_execution_environment(trial, results_file.name) cmd_args = self.template_builder.format(config_file.name, trial, self.experiment) log.debug( "## Launch user's script as a subprocess and wait for finish.") self.pacemaker = TrialPacemaker(trial) self.pacemaker.start() try: self.execute_process(cmd_args, env) finally: # merciless self.pacemaker.stop() return results_file
def test_trial_heartbeat_not_updated(exp, trial): """Test that the heartbeat of a trial is not updated when trial is not longer reserved.""" trial_monitor = TrialPacemaker(trial, wait_time=1) trial_monitor.start() time.sleep(2) trials = exp.fetch_trials_by_status("reserved") assert trial.heartbeat != trials[0].heartbeat get_storage().set_trial_status(trial, status="interrupted") time.sleep(2) # `join` blocks until all thread have finish executing. So, the test will hang if it fails. trial_monitor.join() assert 1
def test_trial_heartbeat_not_updated_inbetween(exp, trial): """Test that the heartbeat of a trial is not updated before wait time.""" trial_monitor = TrialPacemaker(trial, wait_time=5) trial_monitor.start() time.sleep(1) trials = exp.fetch_trials_by_status('reserved') assert trial.heartbeat.replace(microsecond=0) == trials[0].heartbeat.replace(microsecond=0) heartbeat = trials[0].heartbeat time.sleep(6) trials = exp.fetch_trials_by_status(status='reserved') assert heartbeat != trials[0].heartbeat trial_monitor.stop()
def test_trial_update_heartbeat(exp, trial): """Test that the heartbeat of a trial has been updated.""" trial_monitor = TrialPacemaker(trial, wait_time=1) trial_monitor.start() time.sleep(2) trials = exp.fetch_trials_by_status("reserved") assert trial.heartbeat != trials[0].heartbeat heartbeat = trials[0].heartbeat time.sleep(2) trials = exp.fetch_trials_by_status(status="reserved") assert heartbeat != trials[0].heartbeat trial_monitor.stop()
def test_trial_heartbeat_not_updated(exp, trial): """Test that the heartbeat of a trial is not updated when trial is not longer reserved.""" trial_monitor = TrialPacemaker(exp, trial.id, wait_time=1) trial_monitor.start() time.sleep(2) trials = exp.fetch_trials({'_id': trial.id, 'status': 'reserved'}) assert trial.heartbeat != trials[0].heartbeat data = {'status': 'interrupted'} Database().write('trials', data, query=dict(_id=trial.id)) time.sleep(2) # `join` blocks until all thread have finish executing. So, the test will hang if it fails. trial_monitor.join() assert 1
def test_trial_update_heartbeat(exp, trial): """Test that the heartbeat of a trial has been updated.""" trial_monitor = TrialPacemaker(exp, trial.id, wait_time=1) trial_monitor.start() time.sleep(2) trials = exp.fetch_trials({'_id': trial.id, 'status': 'reserved'}) assert trial.heartbeat != trials[0].heartbeat heartbeat = trials[0].heartbeat time.sleep(2) trials = exp.fetch_trials({'_id': trial.id, 'status': 'reserved'}) assert heartbeat != trials[0].heartbeat trial_monitor.stop()
class Consumer(object): """Consume a trial by using it to initialize a black-box box to evaluate it. It uses an `Experiment` object to push an evaluated trial, if results are delivered to the worker process successfully. It forks another process which executes user's script with the suggested options. It expects results to be written in a **JSON** file, whose path has been defined in a special orion environmental variable which is set into the child process' environment. """ def __init__(self, experiment): """Initialize a consumer. :param experiment: Manager of this experiment, provides convenient interface for interacting with the database. """ log.debug("Creating Consumer object.") self.experiment = experiment self.space = experiment.space if self.space is None: raise RuntimeError( "Experiment object provided to Consumer has not yet completed" " initialization.") # Fetch space builder self.template_builder = OrionCmdlineParser( orion.core.config.user_script_config) self.template_builder.set_state_dict(experiment.metadata['parser']) # Get path to user's script and infer trial configuration directory if experiment.working_dir: self.working_dir = os.path.abspath(experiment.working_dir) else: self.working_dir = os.path.join(tempfile.gettempdir(), 'orion') self.script_path = experiment.metadata['user_script'] self.pacemaker = None def consume(self, trial): """Execute user's script as a block box using the options contained within `trial`. :type trial: `orion.core.worker.trial.Trial` """ log.debug("### Create new directory at '%s':", self.working_dir) temp_dir = self.experiment.working_dir is None prefix = self.experiment.name + "_" suffix = trial.id try: with WorkingDir(self.working_dir, temp_dir, prefix=prefix, suffix=suffix) as workdirname: log.debug("## New consumer context: %s", workdirname) trial.working_dir = workdirname results_file = self._consume(trial, workdirname) log.debug( "## Parse results from file and fill corresponding Trial object." ) self.experiment.update_completed_trial(trial, results_file) except KeyboardInterrupt: log.debug("### Save %s as interrupted.", trial) self.experiment.set_trial_status(trial, status='interrupted') raise except ExecutionError: log.debug("### Save %s as broken.", trial) self.experiment.set_trial_status(trial, status='broken') def get_execution_environment(self, trial, results_file='results.log'): """Set a few environment variables to allow users and underlying processes to know if they are running under orion. Parameters ---------- results_file: str file used to store results, this is only used by the legacy protocol trial: Trial reference to the trial object that is going to be run Notes ----- This function defines the environment variables described below .. envvar:: ORION_EXPERIMENT_ID Current experiment that is being ran. .. envvar:: ORION_EXPERIMENT_NAME Name of the experiment the worker is currently working on. .. envvar:: ORION_EXPERIMENT_VERSION Version of the experiment the worker is currently working on. .. envvar:: ORION_TRIAL_ID Current trial id that is currently being executed in this process. .. envvar:: ORION_WORKING_DIRECTORY Trial's current working directory. .. envvar:: ORION_RESULTS_PATH Trial's results file that is read by the legacy protocol to get the results of the trial after a successful run. """ env = dict(os.environ) env['ORION_EXPERIMENT_ID'] = str(self.experiment.id) env['ORION_EXPERIMENT_NAME'] = str(self.experiment.name) env['ORION_EXPERIMENT_VERSION'] = str(self.experiment.version) env['ORION_TRIAL_ID'] = str(trial.id) env['ORION_WORKING_DIR'] = str(trial.working_dir) env['ORION_RESULTS_PATH'] = str(results_file) return env def _consume(self, trial, workdirname): config_file = tempfile.NamedTemporaryFile(mode='w', prefix='trial_', suffix='.conf', dir=workdirname, delete=False) config_file.close() log.debug("## New temp config file: %s", config_file.name) results_file = tempfile.NamedTemporaryFile(mode='w', prefix='results_', suffix='.log', dir=workdirname, delete=False) results_file.close() log.debug("## New temp results file: %s", results_file.name) log.debug( "## Building command line argument and configuration for trial.") env = self.get_execution_environment(trial, results_file.name) cmd_args = self.template_builder.format(config_file.name, trial, self.experiment) log.debug( "## Launch user's script as a subprocess and wait for finish.") self.pacemaker = TrialPacemaker(trial) self.pacemaker.start() try: self.execute_process(cmd_args, env) finally: # merciless self.pacemaker.stop() return results_file def execute_process(self, cmd_args, environ): """Facilitate launching a black-box trial.""" command = [self.script_path] + cmd_args signal.signal(signal.SIGTERM, _handler) process = subprocess.Popen(command, env=environ) return_code = process.wait() if return_code != 0: raise ExecutionError("Something went wrong. Check logs. Process " "returned with code {} !".format(return_code))
def _maintain_reservation(self, trial): self._pacemakers[trial.id] = TrialPacemaker(trial) self._pacemakers[trial.id].start()
class Consumer(object): """Consume a trial by using it to initialize a black-box box to evaluate it. It uses an `Experiment` object to push an evaluated trial, if results are delivered to the worker process successfully. It forks another process which executes user's script with the suggested options. It expects results to be written in a **JSON** file, whose path has been defined in a special orion environmental variable which is set into the child process' environment. Parameters ---------- experiment: `orion.core.worker.experiment.Experiment` Manager of this experiment, provides convenient interface for interacting with the database. heartbeat: int, optional Frequency (seconds) at which the heartbeat of the trial is updated. If the heartbeat of a `reserved` trial is larger than twice the configured heartbeat, Oríon will reset the status of the trial to `interrupted`. This allows restoring lost trials (ex: due to killed worker). Defaults to ``orion.core.config.worker.heartbeat``. user_script_config: str, optional Config argument name of user's script (--config). Defaults to ``orion.core.config.worker.user_script_config``. interrupt_signal_code: int, optional Signal returned by user script to signal to Oríon that it was interrupted. Defaults to ``orion.core.config.worker.interrupt_signal_code``. """ def __init__( self, experiment, heartbeat=None, user_script_config=None, interrupt_signal_code=None, ignore_code_changes=None, ): log.debug("Creating Consumer object.") self.experiment = experiment self.space = experiment.space if self.space is None: raise RuntimeError( "Experiment object provided to Consumer has not yet completed" " initialization.") if heartbeat is None: heartbeat = orion.core.config.worker.heartbeat if user_script_config is None: user_script_config = orion.core.config.worker.user_script_config if interrupt_signal_code is None: interrupt_signal_code = orion.core.config.worker.interrupt_signal_code if ignore_code_changes is None: ignore_code_changes = orion.core.config.evc.ignore_code_changes self.heartbeat = heartbeat self.interrupt_signal_code = interrupt_signal_code self.ignore_code_changes = ignore_code_changes # Fetch space builder self.template_builder = OrionCmdlineParser(user_script_config) self.template_builder.set_state_dict(experiment.metadata["parser"]) # Get path to user's script and infer trial configuration directory if experiment.working_dir: self.working_dir = os.path.abspath(experiment.working_dir) else: self.working_dir = os.path.join(tempfile.gettempdir(), "orion") self.pacemaker = None def consume(self, trial): """Execute user's script as a block box using the options contained within `trial`. Parameters ---------- trial: `orion.core.worker.trial.Trial` Orion trial to execute. Returns ------- bool True if the trial was successfully executed. False if the trial is broken. """ log.debug("### Create new directory at '%s':", self.working_dir) temp_dir = not bool(self.experiment.working_dir) prefix = self.experiment.name + "_" suffix = trial.id try: with WorkingDir(self.working_dir, temp_dir, prefix=prefix, suffix=suffix) as workdirname: log.debug("## New consumer context: %s", workdirname) trial.working_dir = workdirname results_file = self._consume(trial, workdirname) log.debug( "## Parse results from file and fill corresponding Trial object." ) self.experiment.update_completed_trial(trial, results_file) success = True except KeyboardInterrupt: log.debug("### Save %s as interrupted.", trial) self.experiment.set_trial_status(trial, status="interrupted") raise except ExecutionError: log.debug("### Save %s as broken.", trial) self.experiment.set_trial_status(trial, status="broken") success = False return success def get_execution_environment(self, trial, results_file="results.log"): """Set a few environment variables to allow users and underlying processes to know if they are running under orion. Parameters ---------- results_file: str file used to store results, this is only used by the legacy protocol trial: Trial reference to the trial object that is going to be run Notes ----- This function defines the environment variables described below .. envvar:: ORION_EXPERIMENT_ID :noindex: Current experiment that is being ran. .. envvar:: ORION_EXPERIMENT_NAME :noindex: Name of the experiment the worker is currently working on. .. envvar:: ORION_EXPERIMENT_VERSION :noindex: Version of the experiment the worker is currently working on. .. envvar:: ORION_TRIAL_ID :noindex: Current trial id that is currently being executed in this process. .. envvar:: ORION_WORKING_DIRECTORY :noindex: Trial's current working directory. .. envvar:: ORION_RESULTS_PATH :noindex: Trial's results file that is read by the legacy protocol to get the results of the trial after a successful run. """ env = dict(os.environ) env["ORION_EXPERIMENT_ID"] = str(self.experiment.id) env["ORION_EXPERIMENT_NAME"] = str(self.experiment.name) env["ORION_EXPERIMENT_VERSION"] = str(self.experiment.version) env["ORION_TRIAL_ID"] = str(trial.id) env["ORION_WORKING_DIR"] = str(trial.working_dir) env["ORION_RESULTS_PATH"] = str(results_file) env["ORION_INTERRUPT_CODE"] = str(self.interrupt_signal_code) return env def _consume(self, trial, workdirname): config_file = tempfile.NamedTemporaryFile(mode="w", prefix="trial_", suffix=".conf", dir=workdirname, delete=False) config_file.close() log.debug("## New temp config file: %s", config_file.name) results_file = tempfile.NamedTemporaryFile(mode="w", prefix="results_", suffix=".log", dir=workdirname, delete=False) results_file.close() log.debug("## New temp results file: %s", results_file.name) log.debug( "## Building command line argument and configuration for trial.") env = self.get_execution_environment(trial, results_file.name) cmd_args = self.template_builder.format(config_file.name, trial, self.experiment) log.debug( "## Launch user's script as a subprocess and wait for finish.") self._validate_code_version() self.pacemaker = TrialPacemaker(trial, self.heartbeat) self.pacemaker.start() try: self.execute_process(cmd_args, env) finally: # merciless self.pacemaker.stop() return results_file def _validate_code_version(self): if self.ignore_code_changes: return old_config = self.experiment.configuration new_config = copy.deepcopy(old_config) new_config["metadata"]["VCS"] = infer_versioning_metadata( old_config["metadata"]["user_script"]) # Circular import from orion.core.evc.conflicts import CodeConflict conflicts = list(CodeConflict.detect(old_config, new_config)) if conflicts: raise BranchingEvent( f"Code changed between execution of 2 trials:\n{conflicts[0]}") # pylint: disable = no-self-use def execute_process(self, cmd_args, environ): """Facilitate launching a black-box trial.""" command = cmd_args signal.signal(signal.SIGTERM, _handler) try: process = subprocess.Popen(command, env=environ) except PermissionError: log.debug("### Script is not executable") raise InexecutableUserScript(" ".join(cmd_args)) return_code = process.wait() if return_code == self.interrupt_signal_code: raise KeyboardInterrupt() elif return_code != 0: raise ExecutionError("Something went wrong. Check logs. Process " "returned with code {} !".format(return_code))
class Consumer(object): """Consume a trial by using it to initialize a black-box box to evaluate it. It uses an `Experiment` object to push an evaluated trial, if results are delivered to the worker process successfully. It forks another process which executes user's script with the suggested options. It expects results to be written in a **JSON** file, whose path has been defined in a special orion environmental variable which is set into the child process' environment. """ def __init__(self, experiment): """Initialize a consumer. :param experiment: Manager of this experiment, provides convenient interface for interacting with the database. """ log.debug("Creating Consumer object.") self.experiment = experiment self.space = experiment.space if self.space is None: raise RuntimeError( "Experiment object provided to Consumer has not yet completed" " initialization.") # Fetch space builder self.template_builder = SpaceBuilder() self.template_builder.build_from(experiment.metadata['user_args']) # Get path to user's script and infer trial configuration directory if experiment.working_dir: self.working_dir = os.path.abspath(experiment.working_dir) else: self.working_dir = os.path.join(tempfile.gettempdir(), 'orion') self.script_path = experiment.metadata['user_script'] self.pacemaker = None def consume(self, trial): """Execute user's script as a block box using the options contained within `trial`. :type trial: `orion.core.worker.trial.Trial` """ log.debug("### Create new directory at '%s':", self.working_dir) temp_dir = self.experiment.working_dir is None prefix = self.experiment.name + "_" suffix = trial.id try: with WorkingDir(self.working_dir, temp_dir, prefix=prefix, suffix=suffix) as workdirname: log.debug("## New consumer context: %s", workdirname) trial.working_dir = workdirname results_file = self._consume(trial, workdirname) log.debug( "## Parse results from file and fill corresponding Trial object." ) self.experiment.update_completed_trial(trial, results_file) except KeyboardInterrupt: log.debug("### Save %s as interrupted.", trial) trial.status = 'interrupted' self.experiment.update_trial(trial, status=trial.status) raise except RuntimeError: log.debug("### Save %s as broken.", trial) trial.status = 'broken' self.experiment.update_trial(trial, status=trial.status) def _consume(self, trial, workdirname): config_file = tempfile.NamedTemporaryFile(mode='w', prefix='trial_', suffix='.conf', dir=workdirname, delete=False) config_file.close() log.debug("## New temp config file: %s", config_file.name) results_file = tempfile.NamedTemporaryFile(mode='w', prefix='results_', suffix='.log', dir=workdirname, delete=False) results_file.close() log.debug("## New temp results file: %s", results_file.name) log.debug( "## Building command line argument and configuration for trial.") cmd_args = self.template_builder.build_to(config_file.name, trial, self.experiment) log.debug( "## Launch user's script as a subprocess and wait for finish.") self.pacemaker = TrialPacemaker(self.experiment, trial.id) self.pacemaker.start() try: self.execute_process(results_file.name, cmd_args) finally: # merciless self.pacemaker.stop() return results_file def execute_process(self, results_filename, cmd_args): """Facilitate launching a black-box trial.""" env = dict(os.environ) env['ORION_RESULTS_PATH'] = str(results_filename) command = [self.script_path] + cmd_args signal.signal(signal.SIGTERM, _handler) process = subprocess.Popen(command, env=env) return_code = process.wait() if return_code != 0: raise RuntimeError("Something went wrong. Check logs. Process " "returned with code {} !".format(return_code))