Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Richard Barnette | ffed172 | 2016-05-18 15:57:22 -0700 | [diff] [blame] | 2 | |
| 3 | #pylint: disable=C0111 |
| 4 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 5 | """ |
| 6 | Autotest scheduler |
| 7 | """ |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 8 | |
Dan Shi | f6c65bd | 2014-08-29 16:15:07 -0700 | [diff] [blame] | 9 | import datetime |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 10 | import functools |
Dan Shi | f6c65bd | 2014-08-29 16:15:07 -0700 | [diff] [blame] | 11 | import gc |
| 12 | import logging |
| 13 | import optparse |
| 14 | import os |
| 15 | import signal |
| 16 | import sys |
| 17 | import time |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 18 | |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 19 | import common |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 20 | from autotest_lib.frontend import setup_django_environment |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 21 | |
| 22 | import django.db |
| 23 | |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 24 | from autotest_lib.client.common_lib import control_data |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 25 | from autotest_lib.client.common_lib import global_config |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 26 | from autotest_lib.client.common_lib import utils |
xixuan | 4de4e74 | 2017-01-30 13:31:35 -0800 | [diff] [blame] | 27 | from autotest_lib.frontend.afe import models |
Fang Deng | c330bee | 2014-10-21 18:10:55 -0700 | [diff] [blame] | 28 | from autotest_lib.scheduler import agent_task, drone_manager |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 29 | from autotest_lib.scheduler import email_manager, gc_stats, host_scheduler |
| 30 | from autotest_lib.scheduler import monitor_db_cleanup, prejob_task |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 31 | from autotest_lib.scheduler import postjob_task |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 32 | from autotest_lib.scheduler import query_managers |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 33 | from autotest_lib.scheduler import scheduler_lib |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 34 | from autotest_lib.scheduler import scheduler_models |
Aviv Keshet | 6b4e3c2 | 2017-05-04 20:14:02 -0700 | [diff] [blame] | 35 | from autotest_lib.scheduler import scheduler_config |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 36 | from autotest_lib.server import autoserv_utils |
Dan Shi | 114e172 | 2016-01-10 18:12:53 -0800 | [diff] [blame] | 37 | from autotest_lib.server import system_utils |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 38 | from autotest_lib.server import utils as server_utils |
Dan Shi | cf2e8dd | 2015-05-07 17:18:48 -0700 | [diff] [blame] | 39 | from autotest_lib.site_utils import metadata_reporter |
Dan Shi | b9144a4 | 2014-12-01 16:09:32 -0800 | [diff] [blame] | 40 | from autotest_lib.site_utils import server_manager_utils |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 41 | |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 42 | try: |
| 43 | from chromite.lib import metrics |
| 44 | from chromite.lib import ts_mon_config |
| 45 | except ImportError: |
| 46 | metrics = utils.metrics_mock |
| 47 | ts_mon_config = utils.metrics_mock |
| 48 | |
Dan Shi | cf2e8dd | 2015-05-07 17:18:48 -0700 | [diff] [blame] | 49 | |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 50 | PID_FILE_PREFIX = 'monitor_db' |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 51 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 52 | RESULTS_DIR = '.' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 53 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 54 | |
| 55 | if os.environ.has_key('AUTOTEST_DIR'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 56 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 57 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 58 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 59 | |
| 60 | if AUTOTEST_SERVER_DIR not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 62 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 63 | # error message to leave in results dir when an autoserv process disappears |
| 64 | # mysteriously |
| 65 | _LOST_PROCESS_ERROR = """\ |
| 66 | Autoserv failed abnormally during execution for this job, probably due to a |
| 67 | system error on the Autotest server. Full results may not be available. Sorry. |
| 68 | """ |
| 69 | |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 70 | _db_manager = None |
Jakob Jülich | 36accc6 | 2014-07-23 10:26:55 -0700 | [diff] [blame] | 71 | _db = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 72 | _shutdown = False |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 73 | |
| 74 | # These 2 globals are replaced for testing |
| 75 | _autoserv_directory = autoserv_utils.autoserv_directory |
| 76 | _autoserv_path = autoserv_utils.autoserv_path |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 77 | _testing_mode = False |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 78 | _drone_manager = None |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 79 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 80 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 81 | def _verify_default_drone_set_exists(): |
| 82 | if (models.DroneSet.drone_sets_enabled() and |
| 83 | not models.DroneSet.default_drone_set_name()): |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 84 | raise scheduler_lib.SchedulerError( |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 85 | 'Drone sets are enabled, but no default is set') |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def _sanity_check(): |
| 89 | """Make sure the configs are consistent before starting the scheduler""" |
| 90 | _verify_default_drone_set_exists() |
| 91 | |
| 92 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 93 | def main(): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 94 | try: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 95 | try: |
| 96 | main_without_exception_handling() |
| 97 | except SystemExit: |
| 98 | raise |
| 99 | except: |
| 100 | logging.exception('Exception escaping in monitor_db') |
| 101 | raise |
| 102 | finally: |
| 103 | utils.delete_pid_file_if_exists(PID_FILE_PREFIX) |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def main_without_exception_handling(): |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 107 | scheduler_lib.setup_logging( |
| 108 | os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None), |
| 109 | os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None)) |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 110 | usage = 'usage: %prog [options] results_dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 111 | parser = optparse.OptionParser(usage) |
| 112 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
| 113 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 114 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 115 | 'test and should use dummy autoserv and no parsing', |
| 116 | action='store_true') |
Dan Shi | f6c65bd | 2014-08-29 16:15:07 -0700 | [diff] [blame] | 117 | parser.add_option('--production', |
| 118 | help=('Indicate that scheduler is running in production ' |
| 119 | 'environment and it can use database that is not ' |
| 120 | 'hosted in localhost. If it is set to False, ' |
| 121 | 'scheduler will fail if database is not in ' |
| 122 | 'localhost.'), |
Dan Shi | 06b09b7 | 2014-09-09 16:06:17 -0700 | [diff] [blame] | 123 | action='store_true', default=False) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 124 | (options, args) = parser.parse_args() |
| 125 | if len(args) != 1: |
| 126 | parser.print_usage() |
| 127 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 128 | |
Dan Shi | f6c65bd | 2014-08-29 16:15:07 -0700 | [diff] [blame] | 129 | scheduler_lib.check_production_settings(options) |
| 130 | |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 131 | scheduler_enabled = global_config.global_config.get_config_value( |
| 132 | scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool) |
| 133 | |
| 134 | if not scheduler_enabled: |
Aviv Keshet | 05c08ac | 2013-01-10 16:11:44 -0800 | [diff] [blame] | 135 | logging.error("Scheduler not enabled, set enable_scheduler to true in " |
| 136 | "the global_config's SCHEDULER section to enable it. " |
| 137 | "Exiting.") |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 138 | sys.exit(1) |
| 139 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 140 | global RESULTS_DIR |
| 141 | RESULTS_DIR = args[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 142 | |
showard | cca334f | 2009-03-12 20:38:34 +0000 | [diff] [blame] | 143 | # Change the cwd while running to avoid issues incase we were launched from |
| 144 | # somewhere odd (such as a random NFS home directory of the person running |
| 145 | # sudo to launch us as the appropriate user). |
| 146 | os.chdir(RESULTS_DIR) |
| 147 | |
jamesren | c7d387e | 2010-08-10 21:48:30 +0000 | [diff] [blame] | 148 | # This is helpful for debugging why stuff a scheduler launches is |
| 149 | # misbehaving. |
| 150 | logging.info('os.environ: %s', os.environ) |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 151 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 152 | if options.test: |
| 153 | global _autoserv_path |
| 154 | _autoserv_path = 'autoserv_dummy' |
| 155 | global _testing_mode |
| 156 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 157 | |
Dan Shi | cf2e8dd | 2015-05-07 17:18:48 -0700 | [diff] [blame] | 158 | # Start the thread to report metadata. |
| 159 | metadata_reporter.start() |
| 160 | |
Paul Hobbs | eedcb8b | 2016-10-05 16:44:27 -0700 | [diff] [blame] | 161 | with ts_mon_config.SetupTsMonGlobalState('autotest_scheduler', |
| 162 | indirect=True): |
| 163 | try: |
| 164 | initialize() |
| 165 | dispatcher = Dispatcher() |
| 166 | dispatcher.initialize(recover_hosts=options.recover_hosts) |
| 167 | minimum_tick_sec = global_config.global_config.get_config_value( |
| 168 | scheduler_config.CONFIG_SECTION, 'minimum_tick_sec', type=float) |
Richard Barnette | ffed172 | 2016-05-18 15:57:22 -0700 | [diff] [blame] | 169 | |
Aviv Keshet | 6b4e3c2 | 2017-05-04 20:14:02 -0700 | [diff] [blame] | 170 | while not _shutdown: |
Paul Hobbs | eedcb8b | 2016-10-05 16:44:27 -0700 | [diff] [blame] | 171 | start = time.time() |
| 172 | dispatcher.tick() |
| 173 | curr_tick_sec = time.time() - start |
| 174 | if minimum_tick_sec > curr_tick_sec: |
| 175 | time.sleep(minimum_tick_sec - curr_tick_sec) |
| 176 | else: |
| 177 | time.sleep(0.0001) |
| 178 | except server_manager_utils.ServerActionError as e: |
| 179 | # This error is expected when the server is not in primary status |
| 180 | # for scheduler role. Thus do not send email for it. |
| 181 | logging.exception(e) |
| 182 | except Exception: |
Aviv Keshet | ea8f851 | 2017-05-05 10:04:39 -0700 | [diff] [blame] | 183 | logging.exception('Uncaught exception, terminating monitor_db.') |
Aviv Keshet | 7f73f66 | 2017-04-27 11:48:45 -0700 | [diff] [blame] | 184 | metrics.Counter('chromeos/autotest/scheduler/uncaught_exception' |
| 185 | ).increment() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 186 | |
Paul Hobbs | abd3b05 | 2016-10-03 18:25:23 +0000 | [diff] [blame] | 187 | metadata_reporter.abort() |
| 188 | email_manager.manager.send_queued_emails() |
Paul Hobbs | abd3b05 | 2016-10-03 18:25:23 +0000 | [diff] [blame] | 189 | _drone_manager.shutdown() |
| 190 | _db_manager.disconnect() |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 191 | |
| 192 | |
Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 193 | def handle_signal(signum, frame): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 194 | global _shutdown |
| 195 | _shutdown = True |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 196 | logging.info("Shutdown request received.") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 197 | |
| 198 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 199 | def initialize(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 200 | logging.info("%s> dispatcher starting", time.strftime("%X %x")) |
| 201 | logging.info("My PID is %d", os.getpid()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 202 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 203 | if utils.program_is_alive(PID_FILE_PREFIX): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 204 | logging.critical("monitor_db already running, aborting!") |
| 205 | sys.exit(1) |
| 206 | utils.write_pid(PID_FILE_PREFIX) |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 207 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 208 | if _testing_mode: |
| 209 | global_config.global_config.override_config_value( |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 210 | scheduler_lib.DB_CONFIG_SECTION, 'database', |
| 211 | 'stresstest_autotest_web') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 212 | |
Dan Shi | b9144a4 | 2014-12-01 16:09:32 -0800 | [diff] [blame] | 213 | # If server database is enabled, check if the server has role `scheduler`. |
| 214 | # If the server does not have scheduler role, exception will be raised and |
| 215 | # scheduler will not continue to run. |
| 216 | if server_manager_utils.use_server_db(): |
| 217 | server_manager_utils.confirm_server_has_role(hostname='localhost', |
| 218 | role='scheduler') |
| 219 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 220 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 221 | global _db_manager |
| 222 | _db_manager = scheduler_lib.ConnectionManager() |
Jakob Jülich | 36accc6 | 2014-07-23 10:26:55 -0700 | [diff] [blame] | 223 | global _db |
| 224 | _db = _db_manager.get_connection() |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 225 | logging.info("Setting signal handler") |
Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 226 | signal.signal(signal.SIGINT, handle_signal) |
| 227 | signal.signal(signal.SIGTERM, handle_signal) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 228 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 229 | initialize_globals() |
| 230 | scheduler_models.initialize() |
| 231 | |
Dan Shi | 114e172 | 2016-01-10 18:12:53 -0800 | [diff] [blame] | 232 | drone_list = system_utils.get_drones() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 233 | results_host = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 234 | scheduler_config.CONFIG_SECTION, 'results_host', default='localhost') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 235 | _drone_manager.initialize(RESULTS_DIR, drone_list, results_host) |
| 236 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 237 | logging.info("Connected! Running...") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 238 | |
| 239 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 240 | def initialize_globals(): |
| 241 | global _drone_manager |
| 242 | _drone_manager = drone_manager.instance() |
| 243 | |
| 244 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 245 | def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None, |
| 246 | verbose=True): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 247 | """ |
| 248 | @returns The autoserv command line as a list of executable + parameters. |
| 249 | |
| 250 | @param machines - string - A machine or comma separated list of machines |
| 251 | for the (-m) flag. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 252 | @param extra_args - list - Additional arguments to pass to autoserv. |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 253 | @param job - Job object - If supplied, -u owner, -l name, --test-retry, |
| 254 | and client -c or server -s parameters will be added. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 255 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 256 | object was supplied, this will be used to lookup the Job object. |
| 257 | """ |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 258 | command = autoserv_utils.autoserv_run_job_command(_autoserv_directory, |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 259 | machines, results_directory=drone_manager.WORKING_DIRECTORY, |
| 260 | extra_args=extra_args, job=job, queue_entry=queue_entry, |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 261 | verbose=verbose, in_lab=True) |
| 262 | return command |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 263 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 264 | def _calls_log_tick_msg(func): |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 265 | """Used to trace functions called by Dispatcher.tick.""" |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 266 | @functools.wraps(func) |
| 267 | def wrapper(self, *args, **kwargs): |
| 268 | self._log_tick_msg('Starting %s' % func.__name__) |
| 269 | return func(self, *args, **kwargs) |
| 270 | |
| 271 | return wrapper |
| 272 | |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 273 | |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 274 | class Dispatcher(object): |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 275 | |
| 276 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 277 | def __init__(self): |
| 278 | self._agents = [] |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 279 | self._last_clean_time = time.time() |
Alex Miller | ac189f3 | 2014-06-23 13:55:23 -0700 | [diff] [blame] | 280 | user_cleanup_time = scheduler_config.config.clean_interval_minutes |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 281 | self._periodic_cleanup = monitor_db_cleanup.UserCleanup( |
Jakob Jülich | 36accc6 | 2014-07-23 10:26:55 -0700 | [diff] [blame] | 282 | _db, user_cleanup_time) |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 283 | self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep( |
Dan Shi | c458f66 | 2015-04-29 12:12:38 -0700 | [diff] [blame] | 284 | _db, _drone_manager) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 285 | self._host_agents = {} |
| 286 | self._queue_entry_agents = {} |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 287 | self._tick_count = 0 |
| 288 | self._last_garbage_stats_time = time.time() |
| 289 | self._seconds_between_garbage_stats = 60 * ( |
| 290 | global_config.global_config.get_config_value( |
| 291 | scheduler_config.CONFIG_SECTION, |
Dale Curtis | 456d3c1 | 2011-07-19 11:42:51 -0700 | [diff] [blame] | 292 | 'gc_stats_interval_mins', type=int, default=6*60)) |
Simran Basi | 0ec94dd | 2012-08-28 09:50:10 -0700 | [diff] [blame] | 293 | self._tick_debug = global_config.global_config.get_config_value( |
| 294 | scheduler_config.CONFIG_SECTION, 'tick_debug', type=bool, |
| 295 | default=False) |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 296 | self._extra_debugging = global_config.global_config.get_config_value( |
| 297 | scheduler_config.CONFIG_SECTION, 'extra_debugging', type=bool, |
| 298 | default=False) |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 299 | self._inline_host_acquisition = ( |
| 300 | global_config.global_config.get_config_value( |
| 301 | scheduler_config.CONFIG_SECTION, |
| 302 | 'inline_host_acquisition', type=bool, default=True)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 303 | |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 304 | # If _inline_host_acquisition is set the scheduler will acquire and |
| 305 | # release hosts against jobs inline, with the tick. Otherwise the |
| 306 | # scheduler will only focus on jobs that already have hosts, and |
| 307 | # will not explicitly unlease a host when a job finishes using it. |
| 308 | self._job_query_manager = query_managers.AFEJobQueryManager() |
| 309 | self._host_scheduler = (host_scheduler.BaseHostScheduler() |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 310 | if self._inline_host_acquisition else |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 311 | host_scheduler.DummyHostScheduler()) |
| 312 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 313 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 314 | def initialize(self, recover_hosts=True): |
| 315 | self._periodic_cleanup.initialize() |
| 316 | self._24hr_upkeep.initialize() |
Dan Shi | 55d5899 | 2015-05-05 09:10:02 -0700 | [diff] [blame] | 317 | # Execute all actions queued in the cleanup tasks. Scheduler tick will |
| 318 | # run a refresh task first. If there is any action in the queue, refresh |
| 319 | # will raise an exception. |
| 320 | _drone_manager.execute_actions() |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 321 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 322 | # always recover processes |
| 323 | self._recover_processes() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 324 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 325 | if recover_hosts: |
| 326 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 327 | |
| 328 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 329 | # TODO(pprabhu) Drop this metric once tick_times has been verified. |
Prathmesh Prabhu | c95a66a | 2016-11-22 19:10:56 -0800 | [diff] [blame] | 330 | @metrics.SecondsTimerDecorator( |
| 331 | 'chromeos/autotest/scheduler/tick_durations/tick') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 332 | def tick(self): |
Simran Basi | 0ec94dd | 2012-08-28 09:50:10 -0700 | [diff] [blame] | 333 | """ |
| 334 | This is an altered version of tick() where we keep track of when each |
| 335 | major step begins so we can try to figure out where we are using most |
| 336 | of the tick time. |
| 337 | """ |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 338 | with metrics.RuntimeBreakdownTimer( |
| 339 | 'chromeos/autotest/scheduler/tick_times') as breakdown_timer: |
| 340 | self._log_tick_msg('New tick') |
| 341 | system_utils.DroneCache.refresh() |
| 342 | |
| 343 | with breakdown_timer.Step('garbage_collection'): |
| 344 | self._garbage_collection() |
| 345 | with breakdown_timer.Step('trigger_refresh'): |
| 346 | self._log_tick_msg('Starting _drone_manager.trigger_refresh') |
| 347 | _drone_manager.trigger_refresh() |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 348 | with breakdown_timer.Step('schedule_running_host_queue_entries'): |
| 349 | self._schedule_running_host_queue_entries() |
| 350 | with breakdown_timer.Step('schedule_special_tasks'): |
| 351 | self._schedule_special_tasks() |
| 352 | with breakdown_timer.Step('schedule_new_jobs'): |
| 353 | self._schedule_new_jobs() |
| 354 | with breakdown_timer.Step('sync_refresh'): |
| 355 | self._log_tick_msg('Starting _drone_manager.sync_refresh') |
| 356 | _drone_manager.sync_refresh() |
| 357 | # _run_cleanup must be called between drone_manager.sync_refresh, |
| 358 | # and drone_manager.execute_actions, as sync_refresh will clear the |
| 359 | # calls queued in drones. Therefore, any action that calls |
| 360 | # drone.queue_call to add calls to the drone._calls, should be after |
| 361 | # drone refresh is completed and before |
| 362 | # drone_manager.execute_actions at the end of the tick. |
| 363 | with breakdown_timer.Step('run_cleanup'): |
| 364 | self._run_cleanup() |
| 365 | with breakdown_timer.Step('find_aborting'): |
| 366 | self._find_aborting() |
| 367 | with breakdown_timer.Step('find_aborted_special_tasks'): |
| 368 | self._find_aborted_special_tasks() |
| 369 | with breakdown_timer.Step('handle_agents'): |
| 370 | self._handle_agents() |
| 371 | with breakdown_timer.Step('host_scheduler_tick'): |
| 372 | self._log_tick_msg('Starting _host_scheduler.tick') |
| 373 | self._host_scheduler.tick() |
| 374 | with breakdown_timer.Step('drones_execute_actions'): |
| 375 | self._log_tick_msg('Starting _drone_manager.execute_actions') |
| 376 | _drone_manager.execute_actions() |
| 377 | with breakdown_timer.Step('send_queued_emails'): |
| 378 | self._log_tick_msg( |
| 379 | 'Starting email_manager.manager.send_queued_emails') |
| 380 | email_manager.manager.send_queued_emails() |
| 381 | with breakdown_timer.Step('db_reset_queries'): |
| 382 | self._log_tick_msg('Starting django.db.reset_queries') |
| 383 | django.db.reset_queries() |
| 384 | |
| 385 | self._tick_count += 1 |
| 386 | metrics.Counter('chromeos/autotest/scheduler/tick').increment() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 387 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 388 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 389 | @_calls_log_tick_msg |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 390 | def _run_cleanup(self): |
| 391 | self._periodic_cleanup.run_cleanup_maybe() |
| 392 | self._24hr_upkeep.run_cleanup_maybe() |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 393 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 394 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 395 | @_calls_log_tick_msg |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 396 | def _garbage_collection(self): |
| 397 | threshold_time = time.time() - self._seconds_between_garbage_stats |
| 398 | if threshold_time < self._last_garbage_stats_time: |
| 399 | # Don't generate these reports very often. |
| 400 | return |
| 401 | |
| 402 | self._last_garbage_stats_time = time.time() |
| 403 | # Force a full level 0 collection (because we can, it doesn't hurt |
| 404 | # at this interval). |
| 405 | gc.collect() |
| 406 | logging.info('Logging garbage collector stats on tick %d.', |
| 407 | self._tick_count) |
| 408 | gc_stats._log_garbage_collector_stats() |
| 409 | |
| 410 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 411 | def _register_agent_for_ids(self, agent_dict, object_ids, agent): |
| 412 | for object_id in object_ids: |
| 413 | agent_dict.setdefault(object_id, set()).add(agent) |
| 414 | |
| 415 | |
| 416 | def _unregister_agent_for_ids(self, agent_dict, object_ids, agent): |
| 417 | for object_id in object_ids: |
| 418 | assert object_id in agent_dict |
| 419 | agent_dict[object_id].remove(agent) |
Dan Shi | 76af802 | 2013-10-19 01:59:49 -0700 | [diff] [blame] | 420 | # If an ID has no more active agent associated, there is no need to |
| 421 | # keep it in the dictionary. Otherwise, scheduler will keep an |
| 422 | # unnecessarily big dictionary until being restarted. |
| 423 | if not agent_dict[object_id]: |
| 424 | agent_dict.pop(object_id) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 425 | |
| 426 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 427 | def add_agent_task(self, agent_task): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 428 | """ |
| 429 | Creates and adds an agent to the dispatchers list. |
| 430 | |
| 431 | In creating the agent we also pass on all the queue_entry_ids and |
| 432 | host_ids from the special agent task. For every agent we create, we |
| 433 | add it to 1. a dict against the queue_entry_ids given to it 2. A dict |
| 434 | against the host_ids given to it. So theoritically, a host can have any |
| 435 | number of agents associated with it, and each of them can have any |
| 436 | special agent task, though in practice we never see > 1 agent/task per |
| 437 | host at any time. |
| 438 | |
| 439 | @param agent_task: A SpecialTask for the agent to manage. |
| 440 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 441 | agent = Agent(agent_task) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 442 | self._agents.append(agent) |
| 443 | agent.dispatcher = self |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 444 | self._register_agent_for_ids(self._host_agents, agent.host_ids, agent) |
| 445 | self._register_agent_for_ids(self._queue_entry_agents, |
| 446 | agent.queue_entry_ids, agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 447 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 448 | |
| 449 | def get_agents_for_entry(self, queue_entry): |
| 450 | """ |
| 451 | Find agents corresponding to the specified queue_entry. |
| 452 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 453 | return list(self._queue_entry_agents.get(queue_entry.id, set())) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 454 | |
| 455 | |
| 456 | def host_has_agent(self, host): |
| 457 | """ |
| 458 | Determine if there is currently an Agent present using this host. |
| 459 | """ |
| 460 | return bool(self._host_agents.get(host.id, None)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 461 | |
| 462 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 463 | def remove_agent(self, agent): |
| 464 | self._agents.remove(agent) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 465 | self._unregister_agent_for_ids(self._host_agents, agent.host_ids, |
| 466 | agent) |
| 467 | self._unregister_agent_for_ids(self._queue_entry_agents, |
| 468 | agent.queue_entry_ids, agent) |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 469 | |
| 470 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 471 | def _host_has_scheduled_special_task(self, host): |
| 472 | return bool(models.SpecialTask.objects.filter(host__id=host.id, |
| 473 | is_active=False, |
| 474 | is_complete=False)) |
| 475 | |
| 476 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 477 | def _recover_processes(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 478 | agent_tasks = self._create_recovery_agent_tasks() |
| 479 | self._register_pidfiles(agent_tasks) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 480 | _drone_manager.refresh() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 481 | self._recover_tasks(agent_tasks) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 482 | self._recover_pending_entries() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 483 | self._check_for_unrecovered_verifying_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 484 | self._reverify_remaining_hosts() |
| 485 | # reinitialize drones after killing orphaned processes, since they can |
| 486 | # leave around files when they die |
| 487 | _drone_manager.execute_actions() |
| 488 | _drone_manager.reinitialize_drones() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 489 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 490 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 491 | def _create_recovery_agent_tasks(self): |
| 492 | return (self._get_queue_entry_agent_tasks() |
| 493 | + self._get_special_task_agent_tasks(is_active=True)) |
| 494 | |
| 495 | |
| 496 | def _get_queue_entry_agent_tasks(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 497 | """ |
| 498 | Get agent tasks for all hqe in the specified states. |
| 499 | |
| 500 | Loosely this translates to taking a hqe in one of the specified states, |
| 501 | say parsing, and getting an AgentTask for it, like the FinalReparseTask, |
| 502 | through _get_agent_task_for_queue_entry. Each queue entry can only have |
| 503 | one agent task at a time, but there might be multiple queue entries in |
| 504 | the group. |
| 505 | |
| 506 | @return: A list of AgentTasks. |
| 507 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 508 | # host queue entry statuses handled directly by AgentTasks (Verifying is |
| 509 | # handled through SpecialTasks, so is not listed here) |
| 510 | statuses = (models.HostQueueEntry.Status.STARTING, |
| 511 | models.HostQueueEntry.Status.RUNNING, |
| 512 | models.HostQueueEntry.Status.GATHERING, |
Allen Li | 8745b0d | 2017-06-22 16:09:52 -0700 | [diff] [blame] | 513 | models.HostQueueEntry.Status.PARSING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 514 | status_list = ','.join("'%s'" % status for status in statuses) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 515 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 516 | where='status IN (%s)' % status_list) |
| 517 | |
| 518 | agent_tasks = [] |
| 519 | used_queue_entries = set() |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 520 | hqe_count_by_status = {} |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 521 | for entry in queue_entries: |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 522 | hqe_count_by_status[entry.status] = ( |
| 523 | hqe_count_by_status.get(entry.status, 0) + 1) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 524 | if self.get_agents_for_entry(entry): |
| 525 | # already being handled |
| 526 | continue |
| 527 | if entry in used_queue_entries: |
| 528 | # already picked up by a synchronous job |
| 529 | continue |
| 530 | agent_task = self._get_agent_task_for_queue_entry(entry) |
| 531 | agent_tasks.append(agent_task) |
| 532 | used_queue_entries.update(agent_task.queue_entries) |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 533 | |
| 534 | for status, count in hqe_count_by_status.iteritems(): |
| 535 | metrics.Gauge( |
| 536 | 'chromeos/autotest/scheduler/active_host_queue_entries' |
| 537 | ).set(count, fields={'status': status}) |
| 538 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 539 | return agent_tasks |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 540 | |
| 541 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 542 | def _get_special_task_agent_tasks(self, is_active=False): |
| 543 | special_tasks = models.SpecialTask.objects.filter( |
| 544 | is_active=is_active, is_complete=False) |
| 545 | return [self._get_agent_task_for_special_task(task) |
| 546 | for task in special_tasks] |
| 547 | |
| 548 | |
| 549 | def _get_agent_task_for_queue_entry(self, queue_entry): |
| 550 | """ |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 551 | Construct an AgentTask instance for the given active HostQueueEntry. |
| 552 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 553 | @param queue_entry: a HostQueueEntry |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 554 | @return: an AgentTask to run the queue entry |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 555 | """ |
| 556 | task_entries = queue_entry.job.get_group_entries(queue_entry) |
| 557 | self._check_for_duplicate_host_entries(task_entries) |
| 558 | |
| 559 | if queue_entry.status in (models.HostQueueEntry.Status.STARTING, |
| 560 | models.HostQueueEntry.Status.RUNNING): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 561 | if queue_entry.is_hostless(): |
| 562 | return HostlessQueueTask(queue_entry=queue_entry) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 563 | return QueueTask(queue_entries=task_entries) |
| 564 | if queue_entry.status == models.HostQueueEntry.Status.GATHERING: |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 565 | return postjob_task.GatherLogsTask(queue_entries=task_entries) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 566 | if queue_entry.status == models.HostQueueEntry.Status.PARSING: |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 567 | return postjob_task.FinalReparseTask(queue_entries=task_entries) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 568 | |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 569 | raise scheduler_lib.SchedulerError( |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 570 | '_get_agent_task_for_queue_entry got entry with ' |
| 571 | 'invalid status %s: %s' % (queue_entry.status, queue_entry)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 572 | |
| 573 | |
| 574 | def _check_for_duplicate_host_entries(self, task_entries): |
Allen Li | 8745b0d | 2017-06-22 16:09:52 -0700 | [diff] [blame] | 575 | non_host_statuses = {models.HostQueueEntry.Status.PARSING} |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 576 | for task_entry in task_entries: |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 577 | using_host = (task_entry.host is not None |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 578 | and task_entry.status not in non_host_statuses) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 579 | if using_host: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 580 | self._assert_host_has_no_agent(task_entry) |
| 581 | |
| 582 | |
| 583 | def _assert_host_has_no_agent(self, entry): |
| 584 | """ |
| 585 | @param entry: a HostQueueEntry or a SpecialTask |
| 586 | """ |
| 587 | if self.host_has_agent(entry.host): |
| 588 | agent = tuple(self._host_agents.get(entry.host.id))[0] |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 589 | raise scheduler_lib.SchedulerError( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 590 | 'While scheduling %s, host %s already has a host agent %s' |
| 591 | % (entry, entry.host, agent.task)) |
| 592 | |
| 593 | |
| 594 | def _get_agent_task_for_special_task(self, special_task): |
| 595 | """ |
| 596 | Construct an AgentTask class to run the given SpecialTask and add it |
| 597 | to this dispatcher. |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 598 | |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 599 | A special task is created through schedule_special_tasks, but only if |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 600 | the host doesn't already have an agent. This happens through |
| 601 | add_agent_task. All special agent tasks are given a host on creation, |
| 602 | and a Null hqe. To create a SpecialAgentTask object, you need a |
| 603 | models.SpecialTask. If the SpecialTask used to create a SpecialAgentTask |
| 604 | object contains a hqe it's passed on to the special agent task, which |
| 605 | creates a HostQueueEntry and saves it as it's queue_entry. |
| 606 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 607 | @param special_task: a models.SpecialTask instance |
| 608 | @returns an AgentTask to run this SpecialTask |
| 609 | """ |
| 610 | self._assert_host_has_no_agent(special_task) |
| 611 | |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 612 | special_agent_task_classes = (prejob_task.CleanupTask, |
| 613 | prejob_task.VerifyTask, |
| 614 | prejob_task.RepairTask, |
| 615 | prejob_task.ResetTask, |
| 616 | prejob_task.ProvisionTask) |
| 617 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 618 | for agent_task_class in special_agent_task_classes: |
| 619 | if agent_task_class.TASK_TYPE == special_task.task: |
| 620 | return agent_task_class(task=special_task) |
| 621 | |
Prashanth B | 0e96028 | 2014-05-13 19:38:28 -0700 | [diff] [blame] | 622 | raise scheduler_lib.SchedulerError( |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 623 | 'No AgentTask class for task', str(special_task)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 624 | |
| 625 | |
| 626 | def _register_pidfiles(self, agent_tasks): |
| 627 | for agent_task in agent_tasks: |
| 628 | agent_task.register_necessary_pidfiles() |
| 629 | |
| 630 | |
| 631 | def _recover_tasks(self, agent_tasks): |
| 632 | orphans = _drone_manager.get_orphaned_autoserv_processes() |
| 633 | |
| 634 | for agent_task in agent_tasks: |
| 635 | agent_task.recover() |
| 636 | if agent_task.monitor and agent_task.monitor.has_process(): |
| 637 | orphans.discard(agent_task.monitor.get_process()) |
| 638 | self.add_agent_task(agent_task) |
| 639 | |
| 640 | self._check_for_remaining_orphan_processes(orphans) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 641 | |
| 642 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 643 | def _get_unassigned_entries(self, status): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 644 | for entry in scheduler_models.HostQueueEntry.fetch(where="status = '%s'" |
| 645 | % status): |
showard | 0db3d43 | 2009-10-12 20:29:15 +0000 | [diff] [blame] | 646 | if entry.status == status and not self.get_agents_for_entry(entry): |
| 647 | # The status can change during iteration, e.g., if job.run() |
| 648 | # sets a group of queue entries to Starting |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 649 | yield entry |
| 650 | |
| 651 | |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 652 | def _check_for_remaining_orphan_processes(self, orphans): |
Aviv Keshet | c29b4c7 | 2016-12-14 22:27:35 -0800 | [diff] [blame] | 653 | m = 'chromeos/autotest/errors/unrecovered_orphan_processes' |
| 654 | metrics.Gauge(m).set(len(orphans)) |
| 655 | |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 656 | if not orphans: |
| 657 | return |
| 658 | subject = 'Unrecovered orphan autoserv processes remain' |
| 659 | message = '\n'.join(str(process) for process in orphans) |
mbligh | 5fa9e11 | 2009-08-03 16:46:06 +0000 | [diff] [blame] | 660 | die_on_orphans = global_config.global_config.get_config_value( |
| 661 | scheduler_config.CONFIG_SECTION, 'die_on_orphans', type=bool) |
| 662 | |
| 663 | if die_on_orphans: |
| 664 | raise RuntimeError(subject + '\n' + message) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 665 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 666 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 667 | def _recover_pending_entries(self): |
| 668 | for entry in self._get_unassigned_entries( |
| 669 | models.HostQueueEntry.Status.PENDING): |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 670 | logging.info('Recovering Pending entry %s', entry) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 671 | entry.on_pending() |
| 672 | |
| 673 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 674 | def _check_for_unrecovered_verifying_entries(self): |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 675 | # Verify is replaced by Reset. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 676 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 677 | where='status = "%s"' % models.HostQueueEntry.Status.RESETTING) |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 678 | for queue_entry in queue_entries: |
| 679 | special_tasks = models.SpecialTask.objects.filter( |
| 680 | task__in=(models.SpecialTask.Task.CLEANUP, |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 681 | models.SpecialTask.Task.VERIFY, |
| 682 | models.SpecialTask.Task.RESET), |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 683 | queue_entry__id=queue_entry.id, |
| 684 | is_complete=False) |
| 685 | if special_tasks.count() == 0: |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 686 | logging.error('Unrecovered Resetting host queue entry: %s. ' |
| 687 | 'Setting status to Queued.', str(queue_entry)) |
| 688 | # Essentially this host queue entry was set to be Verifying |
| 689 | # however no special task exists for entry. This occurs if the |
| 690 | # scheduler dies between changing the status and creating the |
| 691 | # special task. By setting it to queued, the job can restart |
| 692 | # from the beginning and proceed correctly. This is much more |
| 693 | # preferable than having monitor_db not launching. |
| 694 | queue_entry.set_status('Queued') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 695 | |
| 696 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 697 | @_calls_log_tick_msg |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 698 | def _schedule_special_tasks(self): |
| 699 | """ |
| 700 | Execute queued SpecialTasks that are ready to run on idle hosts. |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 701 | |
| 702 | Special tasks include PreJobTasks like verify, reset and cleanup. |
| 703 | They are created through _schedule_new_jobs and associated with a hqe |
| 704 | This method translates SpecialTasks to the appropriate AgentTask and |
| 705 | adds them to the dispatchers agents list, so _handle_agents can execute |
| 706 | them. |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 707 | """ |
Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 708 | # When the host scheduler is responsible for acquisition we only want |
| 709 | # to run tasks with leased hosts. All hqe tasks will already have |
| 710 | # leased hosts, and we don't want to run frontend tasks till the host |
| 711 | # scheduler has vetted the assignment. Note that this doesn't include |
| 712 | # frontend tasks with hosts leased by other active hqes. |
| 713 | for task in self._job_query_manager.get_prioritized_special_tasks( |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 714 | only_tasks_with_leased_hosts=not self._inline_host_acquisition): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 715 | if self.host_has_agent(task.host): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 716 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 717 | self.add_agent_task(self._get_agent_task_for_special_task(task)) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 718 | |
| 719 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 720 | def _reverify_remaining_hosts(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 721 | # recover active hosts that have not yet been recovered, although this |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 722 | # should never happen |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 723 | message = ('Recovering active host %s - this probably indicates a ' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 724 | 'scheduler bug') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 725 | self._reverify_hosts_where( |
Alex Miller | dfff2fd | 2013-05-28 13:05:06 -0700 | [diff] [blame] | 726 | "status IN ('Repairing', 'Verifying', 'Cleaning', 'Provisioning')", |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 727 | print_message=message) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 728 | |
| 729 | |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 730 | DEFAULT_REQUESTED_BY_USER_ID = 1 |
| 731 | |
| 732 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 733 | def _reverify_hosts_where(self, where, |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 734 | print_message='Reverifying host %s'): |
Dale Curtis | 456d3c1 | 2011-07-19 11:42:51 -0700 | [diff] [blame] | 735 | full_where='locked = 0 AND invalid = 0 AND ' + where |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 736 | for host in scheduler_models.Host.fetch(where=full_where): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 737 | if self.host_has_agent(host): |
| 738 | # host has already been recovered in some way |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 739 | continue |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 740 | if self._host_has_scheduled_special_task(host): |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 741 | # host will have a special task scheduled on the next cycle |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 742 | continue |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 743 | if print_message: |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 744 | logging.error(print_message, host.hostname) |
| 745 | try: |
| 746 | user = models.User.objects.get(login='autotest_system') |
| 747 | except models.User.DoesNotExist: |
| 748 | user = models.User.objects.get( |
| 749 | id=self.DEFAULT_REQUESTED_BY_USER_ID) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 750 | models.SpecialTask.objects.create( |
Allen Li | 7bea6a0 | 2017-02-06 17:01:20 -0800 | [diff] [blame] | 751 | task=models.SpecialTask.Task.RESET, |
| 752 | host=models.Host.objects.get(id=host.id), |
| 753 | requested_by=user) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 754 | |
| 755 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 756 | def _recover_hosts(self): |
| 757 | # recover "Repair Failed" hosts |
| 758 | message = 'Reverifying dead host %s' |
| 759 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 760 | print_message=message) |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame] | 761 | |
| 762 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 763 | def _refresh_pending_queue_entries(self): |
| 764 | """ |
| 765 | Lookup the pending HostQueueEntries and call our HostScheduler |
| 766 | refresh() method given that list. Return the list. |
| 767 | |
| 768 | @returns A list of pending HostQueueEntries sorted in priority order. |
| 769 | """ |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 770 | queue_entries = self._job_query_manager.get_pending_queue_entries( |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 771 | only_hostless=not self._inline_host_acquisition) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 772 | if not queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 773 | return [] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 774 | return queue_entries |
| 775 | |
| 776 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 777 | def _schedule_hostless_job(self, queue_entry): |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 778 | """Schedule a hostless (suite) job. |
| 779 | |
| 780 | @param queue_entry: The queue_entry representing the hostless job. |
| 781 | """ |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 782 | self.add_agent_task(HostlessQueueTask(queue_entry)) |
Jakob Juelich | d615a1e | 2014-09-04 11:48:36 -0700 | [diff] [blame] | 783 | |
| 784 | # Need to set execution_subdir before setting the status: |
| 785 | # After a restart of the scheduler, agents will be restored for HQEs in |
| 786 | # Starting, Running, Gathering, Parsing or Archiving. To do this, the |
| 787 | # execution_subdir is needed. Therefore it must be set before entering |
| 788 | # one of these states. |
| 789 | # Otherwise, if the scheduler was interrupted between setting the status |
| 790 | # and the execution_subdir, upon it's restart restoring agents would |
| 791 | # fail. |
| 792 | # Is there a way to get a status in one of these states without going |
| 793 | # through this code? Following cases are possible: |
| 794 | # - If it's aborted before being started: |
| 795 | # active bit will be 0, so there's nothing to parse, it will just be |
| 796 | # set to completed by _find_aborting. Critical statuses are skipped. |
| 797 | # - If it's aborted or it fails after being started: |
| 798 | # It was started, so this code was executed. |
| 799 | queue_entry.update_field('execution_subdir', 'hostless') |
jamesren | 47bd737 | 2010-03-13 00:58:17 +0000 | [diff] [blame] | 800 | queue_entry.set_status(models.HostQueueEntry.Status.STARTING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 801 | |
| 802 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 803 | def _schedule_host_job(self, host, queue_entry): |
| 804 | """Schedules a job on the given host. |
| 805 | |
| 806 | 1. Assign the host to the hqe, if it isn't already assigned. |
| 807 | 2. Create a SpecialAgentTask for the hqe. |
| 808 | 3. Activate the hqe. |
| 809 | |
| 810 | @param queue_entry: The job to schedule. |
| 811 | @param host: The host to schedule the job on. |
| 812 | """ |
| 813 | if self.host_has_agent(host): |
| 814 | host_agent_task = list(self._host_agents.get(host.id))[0].task |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 815 | else: |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 816 | self._host_scheduler.schedule_host_job(host, queue_entry) |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 817 | |
| 818 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 819 | @_calls_log_tick_msg |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 820 | def _schedule_new_jobs(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 821 | """ |
| 822 | Find any new HQEs and call schedule_pre_job_tasks for it. |
| 823 | |
| 824 | This involves setting the status of the HQE and creating a row in the |
| 825 | db corresponding the the special task, through |
| 826 | scheduler_models._queue_special_task. The new db row is then added as |
| 827 | an agent to the dispatcher through _schedule_special_tasks and |
| 828 | scheduled for execution on the drone through _handle_agents. |
| 829 | """ |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 830 | queue_entries = self._refresh_pending_queue_entries() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 831 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 832 | key = 'scheduler.jobs_per_tick' |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 833 | new_hostless_jobs = 0 |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 834 | new_jobs_with_hosts = 0 |
| 835 | new_jobs_need_hosts = 0 |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 836 | host_jobs = [] |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 837 | logging.debug('Processing %d queue_entries', len(queue_entries)) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 838 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 839 | for queue_entry in queue_entries: |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 840 | if queue_entry.is_hostless(): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 841 | self._schedule_hostless_job(queue_entry) |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 842 | new_hostless_jobs = new_hostless_jobs + 1 |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 843 | else: |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 844 | host_jobs.append(queue_entry) |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 845 | new_jobs_need_hosts = new_jobs_need_hosts + 1 |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 846 | |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 847 | metrics.Counter( |
| 848 | 'chromeos/autotest/scheduler/scheduled_jobs_hostless' |
| 849 | ).increment_by(new_hostless_jobs) |
Aviv Keshet | c29b4c7 | 2016-12-14 22:27:35 -0800 | [diff] [blame] | 850 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 851 | if not host_jobs: |
| 852 | return |
Aviv Keshet | c29b4c7 | 2016-12-14 22:27:35 -0800 | [diff] [blame] | 853 | |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 854 | if not self._inline_host_acquisition: |
Prathmesh Prabhu | 3c6a3bf | 2016-12-20 11:29:02 -0800 | [diff] [blame] | 855 | # In this case, host_scheduler is responsible for scheduling |
| 856 | # host_jobs. Scheduling the jobs ourselves can lead to DB corruption |
| 857 | # since host_scheduler assumes it is the single process scheduling |
| 858 | # host jobs. |
| 859 | metrics.Gauge( |
| 860 | 'chromeos/autotest/errors/scheduler/unexpected_host_jobs').set( |
| 861 | len(host_jobs)) |
| 862 | return |
| 863 | |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame] | 864 | jobs_with_hosts = self._host_scheduler.find_hosts_for_jobs(host_jobs) |
| 865 | for host_assignment in jobs_with_hosts: |
| 866 | self._schedule_host_job(host_assignment.host, host_assignment.job) |
| 867 | new_jobs_with_hosts = new_jobs_with_hosts + 1 |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 868 | |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 869 | metrics.Counter( |
| 870 | 'chromeos/autotest/scheduler/scheduled_jobs_with_hosts' |
| 871 | ).increment_by(new_jobs_with_hosts) |
| 872 | # TODO(pprabhu): Decide what to do about this metric. Million dollar |
| 873 | # question: What happens to jobs that were not matched. Do they stay in |
| 874 | # the queue, and get processed right here in the next tick (then we want |
| 875 | # a guage corresponding to the number of outstanding unmatched host |
| 876 | # jobs), or are they handled somewhere else (then we need a counter |
| 877 | # corresponding to failed_to_match_with_hosts jobs). |
| 878 | #autotest_stats.Gauge(key).send('new_jobs_without_hosts', |
| 879 | # new_jobs_need_hosts - |
| 880 | # new_jobs_with_hosts) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 881 | |
| 882 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 883 | @_calls_log_tick_msg |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 884 | def _schedule_running_host_queue_entries(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 885 | """ |
| 886 | Adds agents to the dispatcher. |
| 887 | |
| 888 | Any AgentTask, like the QueueTask, is wrapped in an Agent. The |
| 889 | QueueTask for example, will have a job with a control file, and |
| 890 | the agent will have methods that poll, abort and check if the queue |
| 891 | task is finished. The dispatcher runs the agent_task, as well as |
| 892 | other agents in it's _agents member, through _handle_agents, by |
| 893 | calling the Agents tick(). |
| 894 | |
| 895 | This method creates an agent for each HQE in one of (starting, running, |
Allen Li | 8745b0d | 2017-06-22 16:09:52 -0700 | [diff] [blame] | 896 | gathering, parsing) states, and adds it to the dispatcher so |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 897 | it is handled by _handle_agents. |
| 898 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 899 | for agent_task in self._get_queue_entry_agent_tasks(): |
| 900 | self.add_agent_task(agent_task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 901 | |
| 902 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 903 | @_calls_log_tick_msg |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 904 | def _find_aborting(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 905 | """ |
| 906 | Looks through the afe_host_queue_entries for an aborted entry. |
| 907 | |
| 908 | The aborted bit is set on an HQE in many ways, the most common |
| 909 | being when a user requests an abort through the frontend, which |
| 910 | results in an rpc from the afe to abort_host_queue_entries. |
| 911 | """ |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 912 | jobs_to_stop = set() |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 913 | for entry in scheduler_models.HostQueueEntry.fetch( |
Alex Miller | 9fe3966 | 2013-08-09 11:53:09 -0700 | [diff] [blame] | 914 | where='aborted=1 and complete=0'): |
Prashanth Balasubramanian | 047e1c5 | 2014-12-22 19:25:23 -0800 | [diff] [blame] | 915 | |
| 916 | # If the job is running on a shard, let the shard handle aborting |
| 917 | # it and sync back the right status. |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 918 | if entry.job.shard_id is not None and not server_utils.is_shard(): |
Prashanth Balasubramanian | 047e1c5 | 2014-12-22 19:25:23 -0800 | [diff] [blame] | 919 | logging.info('Waiting for shard %s to abort hqe %s', |
| 920 | entry.job.shard_id, entry) |
| 921 | continue |
| 922 | |
showard | f4a2e50 | 2009-07-28 20:06:39 +0000 | [diff] [blame] | 923 | logging.info('Aborting %s', entry) |
beeps | e50d875 | 2013-11-20 18:23:02 -0800 | [diff] [blame] | 924 | |
| 925 | # The task would have started off with both is_complete and |
| 926 | # is_active = False. Aborted tasks are neither active nor complete. |
| 927 | # For all currently active tasks this will happen through the agent, |
| 928 | # but we need to manually update the special tasks that haven't |
| 929 | # started yet, because they don't have agents. |
| 930 | models.SpecialTask.objects.filter(is_active=False, |
| 931 | queue_entry_id=entry.id).update(is_complete=True) |
| 932 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 933 | for agent in self.get_agents_for_entry(entry): |
| 934 | agent.abort() |
| 935 | entry.abort(self) |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 936 | jobs_to_stop.add(entry.job) |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 937 | logging.debug('Aborting %d jobs this tick.', len(jobs_to_stop)) |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 938 | for job in jobs_to_stop: |
| 939 | job.stop_if_necessary() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 940 | |
| 941 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 942 | @_calls_log_tick_msg |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 943 | def _find_aborted_special_tasks(self): |
| 944 | """ |
| 945 | Find SpecialTasks that have been marked for abortion. |
| 946 | |
| 947 | Poll the database looking for SpecialTasks that are active |
| 948 | and have been marked for abortion, then abort them. |
| 949 | """ |
| 950 | |
| 951 | # The completed and active bits are very important when it comes |
| 952 | # to scheduler correctness. The active bit is set through the prolog |
| 953 | # of a special task, and reset through the cleanup method of the |
| 954 | # SpecialAgentTask. The cleanup is called both through the abort and |
| 955 | # epilog. The complete bit is set in several places, and in general |
| 956 | # a hanging job will have is_active=1 is_complete=0, while a special |
| 957 | # task which completed will have is_active=0 is_complete=1. To check |
| 958 | # aborts we directly check active because the complete bit is set in |
| 959 | # several places, including the epilog of agent tasks. |
| 960 | aborted_tasks = models.SpecialTask.objects.filter(is_active=True, |
| 961 | is_aborted=True) |
| 962 | for task in aborted_tasks: |
| 963 | # There are 2 ways to get the agent associated with a task, |
| 964 | # through the host and through the hqe. A special task |
| 965 | # always needs a host, but doesn't always need a hqe. |
| 966 | for agent in self._host_agents.get(task.host.id, []): |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 967 | if isinstance(agent.task, agent_task.SpecialAgentTask): |
Prashanth B | f47a6bb | 2014-08-29 18:07:06 +0000 | [diff] [blame] | 968 | |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 969 | # The epilog preforms critical actions such as |
| 970 | # queueing the next SpecialTask, requeuing the |
| 971 | # hqe etc, however it doesn't actually kill the |
| 972 | # monitor process and set the 'done' bit. Epilogs |
| 973 | # assume that the job failed, and that the monitor |
| 974 | # process has already written an exit code. The |
| 975 | # done bit is a necessary condition for |
| 976 | # _handle_agents to schedule any more special |
| 977 | # tasks against the host, and it must be set |
| 978 | # in addition to is_active, is_complete and success. |
| 979 | agent.task.epilog() |
Prashanth B | f47a6bb | 2014-08-29 18:07:06 +0000 | [diff] [blame] | 980 | agent.task.abort() |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 981 | |
| 982 | |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 983 | def _can_start_agent(self, agent, have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 984 | # always allow zero-process agents to run |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 985 | if agent.task.num_processes == 0: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 986 | return True |
| 987 | # don't allow any nonzero-process agents to run after we've reached a |
| 988 | # limit (this avoids starvation of many-process agents) |
| 989 | if have_reached_limit: |
| 990 | return False |
| 991 | # total process throttling |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 992 | max_runnable_processes = _drone_manager.max_runnable_processes( |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 993 | agent.task.owner_username, |
| 994 | agent.task.get_drone_hostnames_allowed()) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 995 | if agent.task.num_processes > max_runnable_processes: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 996 | return False |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 997 | return True |
| 998 | |
| 999 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 1000 | @_calls_log_tick_msg |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1001 | def _handle_agents(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 1002 | """ |
| 1003 | Handles agents of the dispatcher. |
| 1004 | |
| 1005 | Appropriate Agents are added to the dispatcher through |
| 1006 | _schedule_running_host_queue_entries. These agents each |
| 1007 | have a task. This method runs the agents task through |
| 1008 | agent.tick() leading to: |
| 1009 | agent.start |
| 1010 | prolog -> AgentTasks prolog |
| 1011 | For each queue entry: |
| 1012 | sets host status/status to Running |
| 1013 | set started_on in afe_host_queue_entries |
| 1014 | run -> AgentTasks run |
| 1015 | Creates PidfileRunMonitor |
| 1016 | Queues the autoserv command line for this AgentTask |
| 1017 | via the drone manager. These commands are executed |
| 1018 | through the drone managers execute actions. |
| 1019 | poll -> AgentTasks/BaseAgentTask poll |
| 1020 | checks the monitors exit_code. |
| 1021 | Executes epilog if task is finished. |
| 1022 | Executes AgentTasks _finish_task |
| 1023 | finish_task is usually responsible for setting the status |
| 1024 | of the HQE/host, and updating it's active and complete fileds. |
| 1025 | |
| 1026 | agent.is_done |
| 1027 | Removed the agent from the dispatchers _agents queue. |
| 1028 | Is_done checks the finished bit on the agent, that is |
| 1029 | set based on the Agents task. During the agents poll |
| 1030 | we check to see if the monitor process has exited in |
| 1031 | it's finish method, and set the success member of the |
| 1032 | task based on this exit code. |
| 1033 | """ |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 1034 | num_started_this_tick = 0 |
| 1035 | num_finished_this_tick = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1036 | have_reached_limit = False |
| 1037 | # iterate over copy, so we can remove agents during iteration |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 1038 | logging.debug('Handling %d Agents', len(self._agents)) |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1039 | for agent in list(self._agents): |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1040 | self._log_extra_msg('Processing Agent with Host Ids: %s and ' |
| 1041 | 'queue_entry ids:%s' % (agent.host_ids, |
| 1042 | agent.queue_entry_ids)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1043 | if not agent.started: |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 1044 | if not self._can_start_agent(agent, have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1045 | have_reached_limit = True |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 1046 | logging.debug('Reached Limit of allowed running Agents.') |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1047 | continue |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 1048 | num_started_this_tick += agent.task.num_processes |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1049 | self._log_extra_msg('Starting Agent') |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1050 | agent.tick() |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1051 | self._log_extra_msg('Agent tick completed.') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1052 | if agent.is_done(): |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 1053 | num_finished_this_tick += agent.task.num_processes |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1054 | self._log_extra_msg("Agent finished") |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1055 | self.remove_agent(agent) |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 1056 | |
| 1057 | metrics.Counter( |
| 1058 | 'chromeos/autotest/scheduler/agent_processes_started' |
| 1059 | ).increment_by(num_started_this_tick) |
| 1060 | metrics.Counter( |
| 1061 | 'chromeos/autotest/scheduler/agent_processes_finished' |
| 1062 | ).increment_by(num_finished_this_tick) |
| 1063 | num_agent_processes = _drone_manager.total_running_processes() |
| 1064 | metrics.Gauge( |
| 1065 | 'chromeos/autotest/scheduler/agent_processes' |
| 1066 | ).set(num_agent_processes) |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 1067 | logging.info('%d running processes. %d added this tick.', |
Prathmesh Prabhu | ed7ece9 | 2016-11-23 11:19:43 -0800 | [diff] [blame] | 1068 | num_agent_processes, num_started_this_tick) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1069 | |
| 1070 | |
Prathmesh Prabhu | 7ab1ef6 | 2016-12-15 13:43:22 -0800 | [diff] [blame] | 1071 | def _log_tick_msg(self, msg): |
| 1072 | if self._tick_debug: |
| 1073 | logging.debug(msg) |
| 1074 | |
| 1075 | |
| 1076 | def _log_extra_msg(self, msg): |
| 1077 | if self._extra_debugging: |
| 1078 | logging.debug(msg) |
| 1079 | |
| 1080 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1081 | class Agent(object): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1082 | """ |
Alex Miller | 47715eb | 2013-07-24 03:34:01 -0700 | [diff] [blame] | 1083 | An agent for use by the Dispatcher class to perform a task. An agent wraps |
| 1084 | around an AgentTask mainly to associate the AgentTask with the queue_entry |
| 1085 | and host ids. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1086 | |
| 1087 | The following methods are required on all task objects: |
| 1088 | poll() - Called periodically to let the task check its status and |
| 1089 | update its internal state. If the task succeeded. |
| 1090 | is_done() - Returns True if the task is finished. |
| 1091 | abort() - Called when an abort has been requested. The task must |
| 1092 | set its aborted attribute to True if it actually aborted. |
| 1093 | |
| 1094 | The following attributes are required on all task objects: |
| 1095 | aborted - bool, True if this task was aborted. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1096 | success - bool, True if this task succeeded. |
| 1097 | queue_entry_ids - A sequence of HostQueueEntry ids this task handles. |
| 1098 | host_ids - A sequence of Host ids this task represents. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1099 | """ |
| 1100 | |
| 1101 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1102 | def __init__(self, task): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1103 | """ |
Alex Miller | 47715eb | 2013-07-24 03:34:01 -0700 | [diff] [blame] | 1104 | @param task: An instance of an AgentTask. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1105 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1106 | self.task = task |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1107 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1108 | # This is filled in by Dispatcher.add_agent() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1109 | self.dispatcher = None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1110 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1111 | self.queue_entry_ids = task.queue_entry_ids |
| 1112 | self.host_ids = task.host_ids |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1113 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1114 | self.started = False |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1115 | self.finished = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1116 | |
| 1117 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1118 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1119 | self.started = True |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1120 | if not self.finished: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1121 | self.task.poll() |
| 1122 | if self.task.is_done(): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1123 | self.finished = True |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 1124 | |
| 1125 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1126 | def is_done(self): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1127 | return self.finished |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1128 | |
| 1129 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1130 | def abort(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1131 | if self.task: |
| 1132 | self.task.abort() |
| 1133 | if self.task.aborted: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1134 | # tasks can choose to ignore aborts |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1135 | self.finished = True |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1136 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1137 | |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 1138 | class AbstractQueueTask(agent_task.AgentTask, agent_task.TaskWithJobKeyvals): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1139 | """ |
| 1140 | Common functionality for QueueTask and HostlessQueueTask |
| 1141 | """ |
| 1142 | def __init__(self, queue_entries): |
| 1143 | super(AbstractQueueTask, self).__init__() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1144 | self.job = queue_entries[0].job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1145 | self.queue_entries = queue_entries |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1146 | |
| 1147 | |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1148 | def _keyval_path(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1149 | return os.path.join(self._working_directory(), self._KEYVAL_FILE) |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1150 | |
| 1151 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1152 | def _write_control_file(self, execution_path): |
| 1153 | control_path = _drone_manager.attach_file_to_execution( |
| 1154 | execution_path, self.job.control_file) |
| 1155 | return control_path |
| 1156 | |
| 1157 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 1158 | # TODO: Refactor into autoserv_utils. crbug.com/243090 |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1159 | def _command_line(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1160 | execution_path = self.queue_entries[0].execution_path() |
| 1161 | control_path = self._write_control_file(execution_path) |
| 1162 | hostnames = ','.join(entry.host.hostname |
| 1163 | for entry in self.queue_entries |
| 1164 | if not entry.is_hostless()) |
| 1165 | |
| 1166 | execution_tag = self.queue_entries[0].execution_tag() |
| 1167 | params = _autoserv_command_line( |
| 1168 | hostnames, |
Alex Miller | 9f01d5d | 2013-08-08 02:26:01 -0700 | [diff] [blame] | 1169 | ['-P', execution_tag, '-n', |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1170 | _drone_manager.absolute_path(control_path)], |
| 1171 | job=self.job, verbose=False) |
Dale Curtis | 30cb8eb | 2011-06-09 12:22:26 -0700 | [diff] [blame] | 1172 | if self.job.is_image_update_job(): |
| 1173 | params += ['--image', self.job.update_image_path] |
| 1174 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1175 | return params |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | @property |
| 1179 | def num_processes(self): |
| 1180 | return len(self.queue_entries) |
| 1181 | |
| 1182 | |
| 1183 | @property |
| 1184 | def owner_username(self): |
| 1185 | return self.job.owner |
| 1186 | |
| 1187 | |
| 1188 | def _working_directory(self): |
| 1189 | return self._get_consistent_execution_path(self.queue_entries) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1190 | |
| 1191 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1192 | def prolog(self): |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1193 | queued_key, queued_time = self._job_queued_keyval(self.job) |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1194 | keyval_dict = self.job.keyval_dict() |
| 1195 | keyval_dict[queued_key] = queued_time |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1196 | self._write_keyvals_before_job(keyval_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1197 | for queue_entry in self.queue_entries: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1198 | queue_entry.set_status(models.HostQueueEntry.Status.RUNNING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1199 | queue_entry.set_started_on_now() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1200 | |
| 1201 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1202 | def _write_lost_process_error_file(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1203 | error_file_path = os.path.join(self._working_directory(), 'job_failure') |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1204 | _drone_manager.write_lines_to_file(error_file_path, |
| 1205 | [_LOST_PROCESS_ERROR]) |
| 1206 | |
| 1207 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1208 | def _finish_task(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1209 | if not self.monitor: |
| 1210 | return |
| 1211 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1212 | self._write_job_finished() |
| 1213 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1214 | if self.monitor.lost_process: |
| 1215 | self._write_lost_process_error_file() |
showard | 4ac4754 | 2009-08-31 18:32:19 +0000 | [diff] [blame] | 1216 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1217 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1218 | def _write_status_comment(self, comment): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1219 | _drone_manager.write_lines_to_file( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1220 | os.path.join(self._working_directory(), 'status.log'), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1221 | ['INFO\t----\t----\t' + comment], |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1222 | paired_with_process=self.monitor.get_process()) |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1223 | |
| 1224 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1225 | def _log_abort(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1226 | if not self.monitor or not self.monitor.has_process(): |
| 1227 | return |
| 1228 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1229 | # build up sets of all the aborted_by and aborted_on values |
| 1230 | aborted_by, aborted_on = set(), set() |
| 1231 | for queue_entry in self.queue_entries: |
| 1232 | if queue_entry.aborted_by: |
| 1233 | aborted_by.add(queue_entry.aborted_by) |
| 1234 | t = int(time.mktime(queue_entry.aborted_on.timetuple())) |
| 1235 | aborted_on.add(t) |
| 1236 | |
| 1237 | # extract some actual, unique aborted by value and write it out |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1238 | # TODO(showard): this conditional is now obsolete, we just need to leave |
| 1239 | # it in temporarily for backwards compatibility over upgrades. delete |
| 1240 | # soon. |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1241 | assert len(aborted_by) <= 1 |
| 1242 | if len(aborted_by) == 1: |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1243 | aborted_by_value = aborted_by.pop() |
| 1244 | aborted_on_value = max(aborted_on) |
| 1245 | else: |
| 1246 | aborted_by_value = 'autotest_system' |
| 1247 | aborted_on_value = int(time.time()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1248 | |
showard | a038235 | 2009-02-11 23:36:43 +0000 | [diff] [blame] | 1249 | self._write_keyval_after_job("aborted_by", aborted_by_value) |
| 1250 | self._write_keyval_after_job("aborted_on", aborted_on_value) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1251 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1252 | aborted_on_string = str(datetime.datetime.fromtimestamp( |
| 1253 | aborted_on_value)) |
| 1254 | self._write_status_comment('Job aborted by %s on %s' % |
| 1255 | (aborted_by_value, aborted_on_string)) |
jadmanski | c2ac77f | 2008-05-16 21:44:04 +0000 | [diff] [blame] | 1256 | |
| 1257 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1258 | def abort(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1259 | super(AbstractQueueTask, self).abort() |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1260 | self._log_abort() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1261 | self._finish_task() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1262 | |
| 1263 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1264 | def epilog(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1265 | super(AbstractQueueTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1266 | self._finish_task() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1267 | |
| 1268 | |
| 1269 | class QueueTask(AbstractQueueTask): |
| 1270 | def __init__(self, queue_entries): |
| 1271 | super(QueueTask, self).__init__(queue_entries) |
| 1272 | self._set_ids(queue_entries=queue_entries) |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 1273 | self._enable_ssp_container = ( |
| 1274 | global_config.global_config.get_config_value( |
| 1275 | 'AUTOSERV', 'enable_ssp_container', type=bool, |
| 1276 | default=True)) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1277 | |
| 1278 | |
| 1279 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1280 | self._check_queue_entry_statuses( |
| 1281 | self.queue_entries, |
| 1282 | allowed_hqe_statuses=(models.HostQueueEntry.Status.STARTING, |
| 1283 | models.HostQueueEntry.Status.RUNNING), |
| 1284 | allowed_host_statuses=(models.Host.Status.PENDING, |
| 1285 | models.Host.Status.RUNNING)) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1286 | |
| 1287 | super(QueueTask, self).prolog() |
| 1288 | |
| 1289 | for queue_entry in self.queue_entries: |
| 1290 | self._write_host_keyvals(queue_entry.host) |
| 1291 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
| 1292 | queue_entry.host.update_field('dirty', 1) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1293 | |
| 1294 | |
| 1295 | def _finish_task(self): |
| 1296 | super(QueueTask, self)._finish_task() |
| 1297 | |
| 1298 | for queue_entry in self.queue_entries: |
| 1299 | queue_entry.set_status(models.HostQueueEntry.Status.GATHERING) |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1300 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1301 | |
| 1302 | |
Alex Miller | 9f01d5d | 2013-08-08 02:26:01 -0700 | [diff] [blame] | 1303 | def _command_line(self): |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 1304 | invocation = super(QueueTask, self)._command_line() |
| 1305 | # Check if server-side packaging is needed. |
Prathmesh Prabhu | 468c32c | 2016-12-20 15:12:30 -0800 | [diff] [blame] | 1306 | if (self._enable_ssp_container and |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 1307 | self.job.control_type == control_data.CONTROL_TYPE.SERVER and |
| 1308 | self.job.require_ssp != False): |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 1309 | invocation += ['--require-ssp'] |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 1310 | keyval_dict = self.job.keyval_dict() |
| 1311 | test_source_build = keyval_dict.get('test_source_build', None) |
| 1312 | if test_source_build: |
| 1313 | invocation += ['--test_source_build', test_source_build] |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 1314 | if self.job.parent_job_id: |
| 1315 | invocation += ['--parent_job_id', str(self.job.parent_job_id)] |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 1316 | return invocation + ['--verify_job_repo_url'] |
Alex Miller | 9f01d5d | 2013-08-08 02:26:01 -0700 | [diff] [blame] | 1317 | |
| 1318 | |
Dan Shi | 1a18905 | 2013-10-28 14:41:35 -0700 | [diff] [blame] | 1319 | class HostlessQueueTask(AbstractQueueTask): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1320 | def __init__(self, queue_entry): |
| 1321 | super(HostlessQueueTask, self).__init__([queue_entry]) |
| 1322 | self.queue_entry_ids = [queue_entry.id] |
| 1323 | |
| 1324 | |
| 1325 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1326 | super(HostlessQueueTask, self).prolog() |
| 1327 | |
| 1328 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1329 | def _finish_task(self): |
| 1330 | super(HostlessQueueTask, self)._finish_task() |
Dan Shi | 76af802 | 2013-10-19 01:59:49 -0700 | [diff] [blame] | 1331 | |
| 1332 | # When a job is added to database, its initial status is always |
| 1333 | # Starting. In a scheduler tick, scheduler finds all jobs in Starting |
| 1334 | # status, check if any of them can be started. If scheduler hits some |
Paul Hobbs | b92af21b | 2015-04-09 15:12:41 -0700 | [diff] [blame] | 1335 | # limit, e.g., max_hostless_jobs_per_drone, scheduler will |
| 1336 | # leave these jobs in Starting status. Otherwise, the jobs' |
| 1337 | # status will be changed to Running, and an autoserv process |
| 1338 | # will be started in drone for each of these jobs. |
Dan Shi | 76af802 | 2013-10-19 01:59:49 -0700 | [diff] [blame] | 1339 | # If the entry is still in status Starting, the process has not started |
| 1340 | # yet. Therefore, there is no need to parse and collect log. Without |
| 1341 | # this check, exception will be raised by scheduler as execution_subdir |
| 1342 | # for this queue entry does not have a value yet. |
| 1343 | hqe = self.queue_entries[0] |
| 1344 | if hqe.status != models.HostQueueEntry.Status.STARTING: |
| 1345 | hqe.set_status(models.HostQueueEntry.Status.PARSING) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1346 | |
| 1347 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1348 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1349 | main() |