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