blob: 9e4a6548038083d0d318839e6bc0b2dca331a5be [file] [log] [blame]
mbligh36768f02008-02-22 18:28:33 +00001#!/usr/bin/python -u
2
3"""
4Autotest scheduler
5"""
showard909c7a62008-07-15 21:52:38 +00006
mbligh36768f02008-02-22 18:28:33 +00007
showard402934a2009-12-21 22:20:47 +00008import common
showardef519212009-05-08 02:29:53 +00009import datetime, errno, optparse, os, pwd, Queue, re, shutil, signal
showard542e8402008-09-19 20:16:18 +000010import smtplib, socket, stat, subprocess, sys, tempfile, time, traceback
showardf13a9e22009-12-18 22:54:09 +000011import itertools, logging, weakref, gc
showard402934a2009-12-21 22:20:47 +000012
mbligh8bcd23a2009-02-03 19:14:06 +000013import MySQLdb
showard402934a2009-12-21 22:20:47 +000014
showard043c62a2009-06-10 19:48:57 +000015from autotest_lib.scheduler import scheduler_logging_config
showard21baa452008-10-21 00:08:39 +000016from autotest_lib.frontend import setup_django_environment
showard402934a2009-12-21 22:20:47 +000017
18import django.db
19
showard136e6dc2009-06-10 19:38:49 +000020from autotest_lib.client.common_lib import global_config, logging_manager
showardb18134f2009-03-20 20:52:18 +000021from autotest_lib.client.common_lib import host_protections, utils
showardb1e51872008-10-07 11:08:18 +000022from autotest_lib.database import database_connection
showard844960a2009-05-29 18:41:18 +000023from autotest_lib.frontend.afe import models, rpc_utils, readonly_connection
showard170873e2009-01-07 00:22:26 +000024from autotest_lib.scheduler import drone_manager, drones, email_manager
showard043c62a2009-06-10 19:48:57 +000025from autotest_lib.scheduler import monitor_db_cleanup
showardd1ee1dd2009-01-07 21:33:08 +000026from autotest_lib.scheduler import status_server, scheduler_config
showardf13a9e22009-12-18 22:54:09 +000027from autotest_lib.scheduler import gc_stats
mbligh70feeee2008-06-11 16:20:49 +000028
showard549afad2009-08-20 23:33:36 +000029BABYSITTER_PID_FILE_PREFIX = 'monitor_db_babysitter'
30PID_FILE_PREFIX = 'monitor_db'
mblighb090f142008-02-27 21:33:46 +000031
mbligh36768f02008-02-22 18:28:33 +000032RESULTS_DIR = '.'
33AUTOSERV_NICE_LEVEL = 10
showard170873e2009-01-07 00:22:26 +000034DB_CONFIG_SECTION = 'AUTOTEST_WEB'
mbligh36768f02008-02-22 18:28:33 +000035AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..')
36
37if os.environ.has_key('AUTOTEST_DIR'):
jadmanski0afbb632008-06-06 21:10:57 +000038 AUTOTEST_PATH = os.environ['AUTOTEST_DIR']
mbligh36768f02008-02-22 18:28:33 +000039AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server')
40AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko')
41
42if AUTOTEST_SERVER_DIR not in sys.path:
jadmanski0afbb632008-06-06 21:10:57 +000043 sys.path.insert(0, AUTOTEST_SERVER_DIR)
mbligh36768f02008-02-22 18:28:33 +000044
showardd3dc1992009-04-22 21:01:40 +000045_AUTOSERV_PID_FILE = '.autoserv_execute'
46_CRASHINFO_PID_FILE = '.collect_crashinfo_execute'
47_PARSER_PID_FILE = '.parser_execute'
mbligh4608b002010-01-05 18:22:35 +000048_ARCHIVER_PID_FILE = '.archiver_execute'
showardd3dc1992009-04-22 21:01:40 +000049
50_ALL_PIDFILE_NAMES = (_AUTOSERV_PID_FILE, _CRASHINFO_PID_FILE,
mbligh4608b002010-01-05 18:22:35 +000051 _PARSER_PID_FILE, _ARCHIVER_PID_FILE)
showardd3dc1992009-04-22 21:01:40 +000052
showard35162b02009-03-03 02:17:30 +000053# error message to leave in results dir when an autoserv process disappears
54# mysteriously
55_LOST_PROCESS_ERROR = """\
56Autoserv failed abnormally during execution for this job, probably due to a
57system error on the Autotest server. Full results may not be available. Sorry.
58"""
59
mbligh6f8bab42008-02-29 22:45:14 +000060_db = None
mbligh36768f02008-02-22 18:28:33 +000061_shutdown = False
showard170873e2009-01-07 00:22:26 +000062_autoserv_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'server', 'autoserv')
63_parser_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'tko', 'parse')
mbligh4314a712008-02-29 22:44:30 +000064_testing_mode = False
showard542e8402008-09-19 20:16:18 +000065_base_url = None
showardc85c21b2008-11-24 22:17:37 +000066_notify_email_statuses = []
showard170873e2009-01-07 00:22:26 +000067_drone_manager = drone_manager.DroneManager()
mbligh36768f02008-02-22 18:28:33 +000068
69
showardec6a3b92009-09-25 20:29:13 +000070def _get_pidfile_timeout_secs():
71 """@returns How long to wait for autoserv to write pidfile."""
72 pidfile_timeout_mins = global_config.global_config.get_config_value(
73 scheduler_config.CONFIG_SECTION, 'pidfile_timeout_mins', type=int)
74 return pidfile_timeout_mins * 60
75
76
mbligh83c1e9e2009-05-01 23:10:41 +000077def _site_init_monitor_db_dummy():
78 return {}
79
80
mbligh36768f02008-02-22 18:28:33 +000081def main():
showard27f33872009-04-07 18:20:53 +000082 try:
showard549afad2009-08-20 23:33:36 +000083 try:
84 main_without_exception_handling()
85 except SystemExit:
86 raise
87 except:
88 logging.exception('Exception escaping in monitor_db')
89 raise
90 finally:
91 utils.delete_pid_file_if_exists(PID_FILE_PREFIX)
showard27f33872009-04-07 18:20:53 +000092
93
94def main_without_exception_handling():
showard136e6dc2009-06-10 19:38:49 +000095 setup_logging()
mbligh36768f02008-02-22 18:28:33 +000096
showard136e6dc2009-06-10 19:38:49 +000097 usage = 'usage: %prog [options] results_dir'
jadmanski0afbb632008-06-06 21:10:57 +000098 parser = optparse.OptionParser(usage)
99 parser.add_option('--recover-hosts', help='Try to recover dead hosts',
100 action='store_true')
jadmanski0afbb632008-06-06 21:10:57 +0000101 parser.add_option('--test', help='Indicate that scheduler is under ' +
102 'test and should use dummy autoserv and no parsing',
103 action='store_true')
104 (options, args) = parser.parse_args()
105 if len(args) != 1:
106 parser.print_usage()
107 return
mbligh36768f02008-02-22 18:28:33 +0000108
showard5613c662009-06-08 23:30:33 +0000109 scheduler_enabled = global_config.global_config.get_config_value(
110 scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool)
111
112 if not scheduler_enabled:
113 msg = ("Scheduler not enabled, set enable_scheduler to true in the "
114 "global_config's SCHEDULER section to enabled it. Exiting.")
mbligh6fbdb802009-08-03 16:42:55 +0000115 logging.error(msg)
showard5613c662009-06-08 23:30:33 +0000116 sys.exit(1)
117
jadmanski0afbb632008-06-06 21:10:57 +0000118 global RESULTS_DIR
119 RESULTS_DIR = args[0]
mbligh36768f02008-02-22 18:28:33 +0000120
mbligh83c1e9e2009-05-01 23:10:41 +0000121 site_init = utils.import_site_function(__file__,
122 "autotest_lib.scheduler.site_monitor_db", "site_init_monitor_db",
123 _site_init_monitor_db_dummy)
124 site_init()
125
showardcca334f2009-03-12 20:38:34 +0000126 # Change the cwd while running to avoid issues incase we were launched from
127 # somewhere odd (such as a random NFS home directory of the person running
128 # sudo to launch us as the appropriate user).
129 os.chdir(RESULTS_DIR)
130
jadmanski0afbb632008-06-06 21:10:57 +0000131 c = global_config.global_config
showardd1ee1dd2009-01-07 21:33:08 +0000132 notify_statuses_list = c.get_config_value(scheduler_config.CONFIG_SECTION,
133 "notify_email_statuses",
134 default='')
showardc85c21b2008-11-24 22:17:37 +0000135 global _notify_email_statuses
showard170873e2009-01-07 00:22:26 +0000136 _notify_email_statuses = [status for status in
137 re.split(r'[\s,;:]', notify_statuses_list.lower())
138 if status]
showardc85c21b2008-11-24 22:17:37 +0000139
jadmanski0afbb632008-06-06 21:10:57 +0000140 if options.test:
141 global _autoserv_path
142 _autoserv_path = 'autoserv_dummy'
143 global _testing_mode
144 _testing_mode = True
mbligh36768f02008-02-22 18:28:33 +0000145
mbligh37eceaa2008-12-15 22:56:37 +0000146 # AUTOTEST_WEB.base_url is still a supported config option as some people
147 # may wish to override the entire url.
showard542e8402008-09-19 20:16:18 +0000148 global _base_url
showard170873e2009-01-07 00:22:26 +0000149 config_base_url = c.get_config_value(DB_CONFIG_SECTION, 'base_url',
150 default='')
mbligh37eceaa2008-12-15 22:56:37 +0000151 if config_base_url:
152 _base_url = config_base_url
showard542e8402008-09-19 20:16:18 +0000153 else:
mbligh37eceaa2008-12-15 22:56:37 +0000154 # For the common case of everything running on a single server you
155 # can just set the hostname in a single place in the config file.
156 server_name = c.get_config_value('SERVER', 'hostname')
157 if not server_name:
showardb18134f2009-03-20 20:52:18 +0000158 logging.critical('[SERVER] hostname missing from the config file.')
mbligh37eceaa2008-12-15 22:56:37 +0000159 sys.exit(1)
160 _base_url = 'http://%s/afe/' % server_name
showard542e8402008-09-19 20:16:18 +0000161
showardc5afc462009-01-13 00:09:39 +0000162 server = status_server.StatusServer(_drone_manager)
showardd1ee1dd2009-01-07 21:33:08 +0000163 server.start()
164
jadmanski0afbb632008-06-06 21:10:57 +0000165 try:
showard136e6dc2009-06-10 19:38:49 +0000166 init()
showardc5afc462009-01-13 00:09:39 +0000167 dispatcher = Dispatcher()
showard915958d2009-04-22 21:00:58 +0000168 dispatcher.initialize(recover_hosts=options.recover_hosts)
showardc5afc462009-01-13 00:09:39 +0000169
jadmanski0afbb632008-06-06 21:10:57 +0000170 while not _shutdown:
171 dispatcher.tick()
showardd1ee1dd2009-01-07 21:33:08 +0000172 time.sleep(scheduler_config.config.tick_pause_sec)
jadmanski0afbb632008-06-06 21:10:57 +0000173 except:
showard170873e2009-01-07 00:22:26 +0000174 email_manager.manager.log_stacktrace(
175 "Uncaught exception; terminating monitor_db")
jadmanski0afbb632008-06-06 21:10:57 +0000176
showard170873e2009-01-07 00:22:26 +0000177 email_manager.manager.send_queued_emails()
showard55b4b542009-01-08 23:30:30 +0000178 server.shutdown()
showard170873e2009-01-07 00:22:26 +0000179 _drone_manager.shutdown()
jadmanski0afbb632008-06-06 21:10:57 +0000180 _db.disconnect()
mbligh36768f02008-02-22 18:28:33 +0000181
182
showard136e6dc2009-06-10 19:38:49 +0000183def setup_logging():
184 log_dir = os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None)
185 log_name = os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None)
186 logging_manager.configure_logging(
187 scheduler_logging_config.SchedulerLoggingConfig(), log_dir=log_dir,
188 logfile_name=log_name)
189
190
mbligh36768f02008-02-22 18:28:33 +0000191def handle_sigint(signum, frame):
jadmanski0afbb632008-06-06 21:10:57 +0000192 global _shutdown
193 _shutdown = True
showardb18134f2009-03-20 20:52:18 +0000194 logging.info("Shutdown request received.")
mbligh36768f02008-02-22 18:28:33 +0000195
196
showard136e6dc2009-06-10 19:38:49 +0000197def init():
showardb18134f2009-03-20 20:52:18 +0000198 logging.info("%s> dispatcher starting", time.strftime("%X %x"))
199 logging.info("My PID is %d", os.getpid())
mbligh36768f02008-02-22 18:28:33 +0000200
showard8de37132009-08-31 18:33:08 +0000201 if utils.program_is_alive(PID_FILE_PREFIX):
showard549afad2009-08-20 23:33:36 +0000202 logging.critical("monitor_db already running, aborting!")
203 sys.exit(1)
204 utils.write_pid(PID_FILE_PREFIX)
mblighfb676032009-04-01 18:25:38 +0000205
showardb1e51872008-10-07 11:08:18 +0000206 if _testing_mode:
207 global_config.global_config.override_config_value(
showard170873e2009-01-07 00:22:26 +0000208 DB_CONFIG_SECTION, 'database', 'stresstest_autotest_web')
showardb1e51872008-10-07 11:08:18 +0000209
jadmanski0afbb632008-06-06 21:10:57 +0000210 os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH']
211 global _db
showard170873e2009-01-07 00:22:26 +0000212 _db = database_connection.DatabaseConnection(DB_CONFIG_SECTION)
showardb21b8c82009-12-07 19:39:39 +0000213 _db.connect(db_type='django')
mbligh36768f02008-02-22 18:28:33 +0000214
showardfa8629c2008-11-04 16:51:23 +0000215 # ensure Django connection is in autocommit
216 setup_django_environment.enable_autocommit()
showard844960a2009-05-29 18:41:18 +0000217 # bypass the readonly connection
218 readonly_connection.ReadOnlyConnection.set_globally_disabled(True)
showardfa8629c2008-11-04 16:51:23 +0000219
showardb18134f2009-03-20 20:52:18 +0000220 logging.info("Setting signal handler")
jadmanski0afbb632008-06-06 21:10:57 +0000221 signal.signal(signal.SIGINT, handle_sigint)
222
showardd1ee1dd2009-01-07 21:33:08 +0000223 drones = global_config.global_config.get_config_value(
224 scheduler_config.CONFIG_SECTION, 'drones', default='localhost')
225 drone_list = [hostname.strip() for hostname in drones.split(',')]
showard170873e2009-01-07 00:22:26 +0000226 results_host = global_config.global_config.get_config_value(
showardd1ee1dd2009-01-07 21:33:08 +0000227 scheduler_config.CONFIG_SECTION, 'results_host', default='localhost')
showard170873e2009-01-07 00:22:26 +0000228 _drone_manager.initialize(RESULTS_DIR, drone_list, results_host)
229
showardb18134f2009-03-20 20:52:18 +0000230 logging.info("Connected! Running...")
mbligh36768f02008-02-22 18:28:33 +0000231
232
showarded2afea2009-07-07 20:54:07 +0000233def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None,
234 verbose=True):
showardf1ae3542009-05-11 19:26:02 +0000235 """
236 @returns The autoserv command line as a list of executable + parameters.
237
238 @param machines - string - A machine or comma separated list of machines
239 for the (-m) flag.
showardf1ae3542009-05-11 19:26:02 +0000240 @param extra_args - list - Additional arguments to pass to autoserv.
241 @param job - Job object - If supplied, -u owner and -l name parameters
242 will be added.
243 @param queue_entry - A HostQueueEntry object - If supplied and no Job
244 object was supplied, this will be used to lookup the Job object.
245 """
showarda9545c02009-12-18 22:44:26 +0000246 autoserv_argv = [_autoserv_path, '-p',
showarded2afea2009-07-07 20:54:07 +0000247 '-r', drone_manager.WORKING_DIRECTORY]
showarda9545c02009-12-18 22:44:26 +0000248 if machines:
249 autoserv_argv += ['-m', machines]
showard87ba02a2009-04-20 19:37:32 +0000250 if job or queue_entry:
251 if not job:
252 job = queue_entry.job
253 autoserv_argv += ['-u', job.owner, '-l', job.name]
showarde9c69362009-06-30 01:58:03 +0000254 if verbose:
255 autoserv_argv.append('--verbose')
showard87ba02a2009-04-20 19:37:32 +0000256 return autoserv_argv + extra_args
257
258
showard89f84db2009-03-12 20:39:13 +0000259class SchedulerError(Exception):
260 """Raised by HostScheduler when an inconsistent state occurs."""
261
262
showard63a34772008-08-18 19:32:50 +0000263class HostScheduler(object):
264 def _get_ready_hosts(self):
265 # avoid any host with a currently active queue entry against it
266 hosts = Host.fetch(
showardeab66ce2009-12-23 00:03:56 +0000267 joins='LEFT JOIN afe_host_queue_entries AS active_hqe '
268 'ON (afe_hosts.id = active_hqe.host_id AND '
showardb1e51872008-10-07 11:08:18 +0000269 'active_hqe.active)',
showard63a34772008-08-18 19:32:50 +0000270 where="active_hqe.host_id IS NULL "
showardeab66ce2009-12-23 00:03:56 +0000271 "AND NOT afe_hosts.locked "
272 "AND (afe_hosts.status IS NULL "
273 "OR afe_hosts.status = 'Ready')")
showard63a34772008-08-18 19:32:50 +0000274 return dict((host.id, host) for host in hosts)
275
276
277 @staticmethod
278 def _get_sql_id_list(id_list):
279 return ','.join(str(item_id) for item_id in id_list)
280
281
282 @classmethod
showard989f25d2008-10-01 11:38:11 +0000283 def _get_many2many_dict(cls, query, id_list, flip=False):
mbligh849a0f62008-08-28 20:12:19 +0000284 if not id_list:
285 return {}
showard63a34772008-08-18 19:32:50 +0000286 query %= cls._get_sql_id_list(id_list)
287 rows = _db.execute(query)
showard989f25d2008-10-01 11:38:11 +0000288 return cls._process_many2many_dict(rows, flip)
289
290
291 @staticmethod
292 def _process_many2many_dict(rows, flip=False):
showard63a34772008-08-18 19:32:50 +0000293 result = {}
294 for row in rows:
showard89f84db2009-03-12 20:39:13 +0000295 left_id, right_id = int(row[0]), int(row[1])
showard989f25d2008-10-01 11:38:11 +0000296 if flip:
297 left_id, right_id = right_id, left_id
showard63a34772008-08-18 19:32:50 +0000298 result.setdefault(left_id, set()).add(right_id)
299 return result
300
301
302 @classmethod
303 def _get_job_acl_groups(cls, job_ids):
304 query = """
showardeab66ce2009-12-23 00:03:56 +0000305 SELECT afe_jobs.id, afe_acl_groups_users.aclgroup_id
306 FROM afe_jobs
307 INNER JOIN afe_users ON afe_users.login = afe_jobs.owner
308 INNER JOIN afe_acl_groups_users ON
309 afe_acl_groups_users.user_id = afe_users.id
310 WHERE afe_jobs.id IN (%s)
showard63a34772008-08-18 19:32:50 +0000311 """
312 return cls._get_many2many_dict(query, job_ids)
313
314
315 @classmethod
316 def _get_job_ineligible_hosts(cls, job_ids):
317 query = """
318 SELECT job_id, host_id
showardeab66ce2009-12-23 00:03:56 +0000319 FROM afe_ineligible_host_queues
showard63a34772008-08-18 19:32:50 +0000320 WHERE job_id IN (%s)
321 """
322 return cls._get_many2many_dict(query, job_ids)
323
324
325 @classmethod
showard989f25d2008-10-01 11:38:11 +0000326 def _get_job_dependencies(cls, job_ids):
327 query = """
328 SELECT job_id, label_id
showardeab66ce2009-12-23 00:03:56 +0000329 FROM afe_jobs_dependency_labels
showard989f25d2008-10-01 11:38:11 +0000330 WHERE job_id IN (%s)
331 """
332 return cls._get_many2many_dict(query, job_ids)
333
334
335 @classmethod
showard63a34772008-08-18 19:32:50 +0000336 def _get_host_acls(cls, host_ids):
337 query = """
showardd9ac4452009-02-07 02:04:37 +0000338 SELECT host_id, aclgroup_id
showardeab66ce2009-12-23 00:03:56 +0000339 FROM afe_acl_groups_hosts
showard63a34772008-08-18 19:32:50 +0000340 WHERE host_id IN (%s)
341 """
342 return cls._get_many2many_dict(query, host_ids)
343
344
345 @classmethod
346 def _get_label_hosts(cls, host_ids):
showardfa8629c2008-11-04 16:51:23 +0000347 if not host_ids:
348 return {}, {}
showard63a34772008-08-18 19:32:50 +0000349 query = """
350 SELECT label_id, host_id
showardeab66ce2009-12-23 00:03:56 +0000351 FROM afe_hosts_labels
showard63a34772008-08-18 19:32:50 +0000352 WHERE host_id IN (%s)
showard989f25d2008-10-01 11:38:11 +0000353 """ % cls._get_sql_id_list(host_ids)
354 rows = _db.execute(query)
355 labels_to_hosts = cls._process_many2many_dict(rows)
356 hosts_to_labels = cls._process_many2many_dict(rows, flip=True)
357 return labels_to_hosts, hosts_to_labels
358
359
360 @classmethod
361 def _get_labels(cls):
362 return dict((label.id, label) for label in Label.fetch())
showard63a34772008-08-18 19:32:50 +0000363
364
365 def refresh(self, pending_queue_entries):
366 self._hosts_available = self._get_ready_hosts()
367
368 relevant_jobs = [queue_entry.job_id
369 for queue_entry in pending_queue_entries]
370 self._job_acls = self._get_job_acl_groups(relevant_jobs)
371 self._ineligible_hosts = self._get_job_ineligible_hosts(relevant_jobs)
showard989f25d2008-10-01 11:38:11 +0000372 self._job_dependencies = self._get_job_dependencies(relevant_jobs)
showard63a34772008-08-18 19:32:50 +0000373
374 host_ids = self._hosts_available.keys()
375 self._host_acls = self._get_host_acls(host_ids)
showard989f25d2008-10-01 11:38:11 +0000376 self._label_hosts, self._host_labels = self._get_label_hosts(host_ids)
377
378 self._labels = self._get_labels()
showard63a34772008-08-18 19:32:50 +0000379
380
381 def _is_acl_accessible(self, host_id, queue_entry):
382 job_acls = self._job_acls.get(queue_entry.job_id, set())
383 host_acls = self._host_acls.get(host_id, set())
384 return len(host_acls.intersection(job_acls)) > 0
385
386
showard989f25d2008-10-01 11:38:11 +0000387 def _check_job_dependencies(self, job_dependencies, host_labels):
388 missing = job_dependencies - host_labels
showard89f84db2009-03-12 20:39:13 +0000389 return len(missing) == 0
showard989f25d2008-10-01 11:38:11 +0000390
391
392 def _check_only_if_needed_labels(self, job_dependencies, host_labels,
393 queue_entry):
showardade14e22009-01-26 22:38:32 +0000394 if not queue_entry.meta_host:
395 # bypass only_if_needed labels when a specific host is selected
396 return True
397
showard989f25d2008-10-01 11:38:11 +0000398 for label_id in host_labels:
399 label = self._labels[label_id]
400 if not label.only_if_needed:
401 # we don't care about non-only_if_needed labels
402 continue
403 if queue_entry.meta_host == label_id:
404 # if the label was requested in a metahost it's OK
405 continue
406 if label_id not in job_dependencies:
407 return False
408 return True
409
410
showard89f84db2009-03-12 20:39:13 +0000411 def _check_atomic_group_labels(self, host_labels, queue_entry):
412 """
413 Determine if the given HostQueueEntry's atomic group settings are okay
414 to schedule on a host with the given labels.
415
showard6157c632009-07-06 20:19:31 +0000416 @param host_labels: A list of label ids that the host has.
417 @param queue_entry: The HostQueueEntry being considered for the host.
showard89f84db2009-03-12 20:39:13 +0000418
419 @returns True if atomic group settings are okay, False otherwise.
420 """
showard6157c632009-07-06 20:19:31 +0000421 return (self._get_host_atomic_group_id(host_labels, queue_entry) ==
showard89f84db2009-03-12 20:39:13 +0000422 queue_entry.atomic_group_id)
423
424
showard6157c632009-07-06 20:19:31 +0000425 def _get_host_atomic_group_id(self, host_labels, queue_entry=None):
showard89f84db2009-03-12 20:39:13 +0000426 """
427 Return the atomic group label id for a host with the given set of
428 labels if any, or None otherwise. Raises an exception if more than
429 one atomic group are found in the set of labels.
430
showard6157c632009-07-06 20:19:31 +0000431 @param host_labels: A list of label ids that the host has.
432 @param queue_entry: The HostQueueEntry we're testing. Only used for
433 extra info in a potential logged error message.
showard89f84db2009-03-12 20:39:13 +0000434
435 @returns The id of the atomic group found on a label in host_labels
436 or None if no atomic group label is found.
showard89f84db2009-03-12 20:39:13 +0000437 """
showard6157c632009-07-06 20:19:31 +0000438 atomic_labels = [self._labels[label_id] for label_id in host_labels
439 if self._labels[label_id].atomic_group_id is not None]
440 atomic_ids = set(label.atomic_group_id for label in atomic_labels)
showard89f84db2009-03-12 20:39:13 +0000441 if not atomic_ids:
442 return None
443 if len(atomic_ids) > 1:
showard6157c632009-07-06 20:19:31 +0000444 logging.error('More than one Atomic Group on HQE "%s" via: %r',
445 queue_entry, atomic_labels)
446 return atomic_ids.pop()
showard89f84db2009-03-12 20:39:13 +0000447
448
449 def _get_atomic_group_labels(self, atomic_group_id):
450 """
451 Lookup the label ids that an atomic_group is associated with.
452
453 @param atomic_group_id - The id of the AtomicGroup to look up.
454
455 @returns A generator yeilding Label ids for this atomic group.
456 """
457 return (id for id, label in self._labels.iteritems()
458 if label.atomic_group_id == atomic_group_id
459 and not label.invalid)
460
461
showard54c1ea92009-05-20 00:32:58 +0000462 def _get_eligible_host_ids_in_group(self, group_hosts, queue_entry):
showard89f84db2009-03-12 20:39:13 +0000463 """
464 @param group_hosts - A sequence of Host ids to test for usability
465 and eligibility against the Job associated with queue_entry.
466 @param queue_entry - The HostQueueEntry that these hosts are being
467 tested for eligibility against.
468
469 @returns A subset of group_hosts Host ids that are eligible for the
470 supplied queue_entry.
471 """
472 return set(host_id for host_id in group_hosts
473 if self._is_host_usable(host_id)
474 and self._is_host_eligible_for_job(host_id, queue_entry))
475
476
showard989f25d2008-10-01 11:38:11 +0000477 def _is_host_eligible_for_job(self, host_id, queue_entry):
showard2924b0a2009-06-18 23:16:15 +0000478 if self._is_host_invalid(host_id):
479 # if an invalid host is scheduled for a job, it's a one-time host
480 # and it therefore bypasses eligibility checks. note this can only
481 # happen for non-metahosts, because invalid hosts have their label
482 # relationships cleared.
483 return True
484
showard989f25d2008-10-01 11:38:11 +0000485 job_dependencies = self._job_dependencies.get(queue_entry.job_id, set())
486 host_labels = self._host_labels.get(host_id, set())
mblighc993bee2008-10-03 03:42:34 +0000487
showard89f84db2009-03-12 20:39:13 +0000488 return (self._is_acl_accessible(host_id, queue_entry) and
489 self._check_job_dependencies(job_dependencies, host_labels) and
490 self._check_only_if_needed_labels(
491 job_dependencies, host_labels, queue_entry) and
492 self._check_atomic_group_labels(host_labels, queue_entry))
showard989f25d2008-10-01 11:38:11 +0000493
494
showard2924b0a2009-06-18 23:16:15 +0000495 def _is_host_invalid(self, host_id):
496 host_object = self._hosts_available.get(host_id, None)
497 return host_object and host_object.invalid
498
499
showard63a34772008-08-18 19:32:50 +0000500 def _schedule_non_metahost(self, queue_entry):
showard989f25d2008-10-01 11:38:11 +0000501 if not self._is_host_eligible_for_job(queue_entry.host_id, queue_entry):
showard63a34772008-08-18 19:32:50 +0000502 return None
503 return self._hosts_available.pop(queue_entry.host_id, None)
504
505
506 def _is_host_usable(self, host_id):
507 if host_id not in self._hosts_available:
508 # host was already used during this scheduling cycle
509 return False
510 if self._hosts_available[host_id].invalid:
511 # Invalid hosts cannot be used for metahosts. They're included in
512 # the original query because they can be used by non-metahosts.
513 return False
514 return True
515
516
517 def _schedule_metahost(self, queue_entry):
518 label_id = queue_entry.meta_host
519 hosts_in_label = self._label_hosts.get(label_id, set())
520 ineligible_host_ids = self._ineligible_hosts.get(queue_entry.job_id,
521 set())
522
523 # must iterate over a copy so we can mutate the original while iterating
524 for host_id in list(hosts_in_label):
525 if not self._is_host_usable(host_id):
526 hosts_in_label.remove(host_id)
527 continue
528 if host_id in ineligible_host_ids:
529 continue
showard989f25d2008-10-01 11:38:11 +0000530 if not self._is_host_eligible_for_job(host_id, queue_entry):
showard63a34772008-08-18 19:32:50 +0000531 continue
532
showard89f84db2009-03-12 20:39:13 +0000533 # Remove the host from our cached internal state before returning
534 # the host object.
showard63a34772008-08-18 19:32:50 +0000535 hosts_in_label.remove(host_id)
536 return self._hosts_available.pop(host_id)
537 return None
538
539
540 def find_eligible_host(self, queue_entry):
541 if not queue_entry.meta_host:
showard89f84db2009-03-12 20:39:13 +0000542 assert queue_entry.host_id is not None
showard63a34772008-08-18 19:32:50 +0000543 return self._schedule_non_metahost(queue_entry)
showard89f84db2009-03-12 20:39:13 +0000544 assert queue_entry.atomic_group_id is None
showard63a34772008-08-18 19:32:50 +0000545 return self._schedule_metahost(queue_entry)
546
547
showard89f84db2009-03-12 20:39:13 +0000548 def find_eligible_atomic_group(self, queue_entry):
549 """
550 Given an atomic group host queue entry, locate an appropriate group
551 of hosts for the associated job to run on.
552
553 The caller is responsible for creating new HQEs for the additional
554 hosts returned in order to run the actual job on them.
555
556 @returns A list of Host instances in a ready state to satisfy this
557 atomic group scheduling. Hosts will all belong to the same
558 atomic group label as specified by the queue_entry.
559 An empty list will be returned if no suitable atomic
560 group could be found.
561
562 TODO(gps): what is responsible for kicking off any attempted repairs on
563 a group of hosts? not this function, but something needs to. We do
564 not communicate that reason for returning [] outside of here...
565 For now, we'll just be unschedulable if enough hosts within one group
566 enter Repair Failed state.
567 """
568 assert queue_entry.atomic_group_id is not None
569 job = queue_entry.job
570 assert job.synch_count and job.synch_count > 0
showard77182562009-06-10 00:16:05 +0000571 atomic_group = queue_entry.atomic_group
showard89f84db2009-03-12 20:39:13 +0000572 if job.synch_count > atomic_group.max_number_of_machines:
573 # Such a Job and HostQueueEntry should never be possible to
574 # create using the frontend. Regardless, we can't process it.
575 # Abort it immediately and log an error on the scheduler.
576 queue_entry.set_status(models.HostQueueEntry.Status.ABORTED)
showard7629f142009-03-27 21:02:02 +0000577 logging.error(
578 'Error: job %d synch_count=%d > requested atomic_group %d '
579 'max_number_of_machines=%d. Aborted host_queue_entry %d.',
580 job.id, job.synch_count, atomic_group.id,
581 atomic_group.max_number_of_machines, queue_entry.id)
showard89f84db2009-03-12 20:39:13 +0000582 return []
583 hosts_in_label = self._label_hosts.get(queue_entry.meta_host, set())
584 ineligible_host_ids = self._ineligible_hosts.get(queue_entry.job_id,
585 set())
586
587 # Look in each label associated with atomic_group until we find one with
588 # enough hosts to satisfy the job.
589 for atomic_label_id in self._get_atomic_group_labels(atomic_group.id):
590 group_hosts = set(self._label_hosts.get(atomic_label_id, set()))
591 if queue_entry.meta_host is not None:
592 # If we have a metahost label, only allow its hosts.
593 group_hosts.intersection_update(hosts_in_label)
594 group_hosts -= ineligible_host_ids
showard54c1ea92009-05-20 00:32:58 +0000595 eligible_host_ids_in_group = self._get_eligible_host_ids_in_group(
showard89f84db2009-03-12 20:39:13 +0000596 group_hosts, queue_entry)
597
598 # Job.synch_count is treated as "minimum synch count" when
599 # scheduling for an atomic group of hosts. The atomic group
600 # number of machines is the maximum to pick out of a single
601 # atomic group label for scheduling at one time.
602 min_hosts = job.synch_count
603 max_hosts = atomic_group.max_number_of_machines
604
showard54c1ea92009-05-20 00:32:58 +0000605 if len(eligible_host_ids_in_group) < min_hosts:
showard89f84db2009-03-12 20:39:13 +0000606 # Not enough eligible hosts in this atomic group label.
607 continue
608
showard54c1ea92009-05-20 00:32:58 +0000609 eligible_hosts_in_group = [self._hosts_available[id]
610 for id in eligible_host_ids_in_group]
showardef519212009-05-08 02:29:53 +0000611 # So that they show up in a sane order when viewing the job.
showard54c1ea92009-05-20 00:32:58 +0000612 eligible_hosts_in_group.sort(cmp=Host.cmp_for_sort)
showardef519212009-05-08 02:29:53 +0000613
showard89f84db2009-03-12 20:39:13 +0000614 # Limit ourselves to scheduling the atomic group size.
615 if len(eligible_hosts_in_group) > max_hosts:
showardef519212009-05-08 02:29:53 +0000616 eligible_hosts_in_group = eligible_hosts_in_group[:max_hosts]
showard89f84db2009-03-12 20:39:13 +0000617
618 # Remove the selected hosts from our cached internal state
619 # of available hosts in order to return the Host objects.
620 host_list = []
showard54c1ea92009-05-20 00:32:58 +0000621 for host in eligible_hosts_in_group:
622 hosts_in_label.discard(host.id)
623 self._hosts_available.pop(host.id)
624 host_list.append(host)
showard89f84db2009-03-12 20:39:13 +0000625 return host_list
626
627 return []
628
629
showard170873e2009-01-07 00:22:26 +0000630class Dispatcher(object):
jadmanski0afbb632008-06-06 21:10:57 +0000631 def __init__(self):
632 self._agents = []
showard3bb499f2008-07-03 19:42:20 +0000633 self._last_clean_time = time.time()
showard63a34772008-08-18 19:32:50 +0000634 self._host_scheduler = HostScheduler()
mblighf3294cc2009-04-08 21:17:38 +0000635 user_cleanup_time = scheduler_config.config.clean_interval
636 self._periodic_cleanup = monitor_db_cleanup.UserCleanup(
637 _db, user_cleanup_time)
638 self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep(_db)
showard170873e2009-01-07 00:22:26 +0000639 self._host_agents = {}
640 self._queue_entry_agents = {}
showardf13a9e22009-12-18 22:54:09 +0000641 self._tick_count = 0
642 self._last_garbage_stats_time = time.time()
643 self._seconds_between_garbage_stats = 60 * (
644 global_config.global_config.get_config_value(
645 scheduler_config.CONFIG_SECTION,
646 'gc_stats_interval_mins', type=int, default=6*60))
mbligh36768f02008-02-22 18:28:33 +0000647
mbligh36768f02008-02-22 18:28:33 +0000648
showard915958d2009-04-22 21:00:58 +0000649 def initialize(self, recover_hosts=True):
650 self._periodic_cleanup.initialize()
651 self._24hr_upkeep.initialize()
652
jadmanski0afbb632008-06-06 21:10:57 +0000653 # always recover processes
654 self._recover_processes()
mblighbb421852008-03-11 22:36:16 +0000655
jadmanski0afbb632008-06-06 21:10:57 +0000656 if recover_hosts:
657 self._recover_hosts()
mbligh36768f02008-02-22 18:28:33 +0000658
659
jadmanski0afbb632008-06-06 21:10:57 +0000660 def tick(self):
showardf13a9e22009-12-18 22:54:09 +0000661 self._garbage_collection()
showard170873e2009-01-07 00:22:26 +0000662 _drone_manager.refresh()
mblighf3294cc2009-04-08 21:17:38 +0000663 self._run_cleanup()
jadmanski0afbb632008-06-06 21:10:57 +0000664 self._find_aborting()
showard29f7cd22009-04-29 21:16:24 +0000665 self._process_recurring_runs()
showard8cc058f2009-09-08 16:26:33 +0000666 self._schedule_delay_tasks()
showard8cc058f2009-09-08 16:26:33 +0000667 self._schedule_running_host_queue_entries()
668 self._schedule_special_tasks()
showard65db3932009-10-28 19:54:35 +0000669 self._schedule_new_jobs()
jadmanski0afbb632008-06-06 21:10:57 +0000670 self._handle_agents()
showard170873e2009-01-07 00:22:26 +0000671 _drone_manager.execute_actions()
672 email_manager.manager.send_queued_emails()
showard402934a2009-12-21 22:20:47 +0000673 django.db.reset_queries()
showardf13a9e22009-12-18 22:54:09 +0000674 self._tick_count += 1
mbligh36768f02008-02-22 18:28:33 +0000675
showard97aed502008-11-04 02:01:24 +0000676
mblighf3294cc2009-04-08 21:17:38 +0000677 def _run_cleanup(self):
678 self._periodic_cleanup.run_cleanup_maybe()
679 self._24hr_upkeep.run_cleanup_maybe()
showarda3ab0d52008-11-03 19:03:47 +0000680
mbligh36768f02008-02-22 18:28:33 +0000681
showardf13a9e22009-12-18 22:54:09 +0000682 def _garbage_collection(self):
683 threshold_time = time.time() - self._seconds_between_garbage_stats
684 if threshold_time < self._last_garbage_stats_time:
685 # Don't generate these reports very often.
686 return
687
688 self._last_garbage_stats_time = time.time()
689 # Force a full level 0 collection (because we can, it doesn't hurt
690 # at this interval).
691 gc.collect()
692 logging.info('Logging garbage collector stats on tick %d.',
693 self._tick_count)
694 gc_stats._log_garbage_collector_stats()
695
696
showard170873e2009-01-07 00:22:26 +0000697 def _register_agent_for_ids(self, agent_dict, object_ids, agent):
698 for object_id in object_ids:
699 agent_dict.setdefault(object_id, set()).add(agent)
700
701
702 def _unregister_agent_for_ids(self, agent_dict, object_ids, agent):
703 for object_id in object_ids:
704 assert object_id in agent_dict
705 agent_dict[object_id].remove(agent)
706
707
showardd1195652009-12-08 22:21:02 +0000708 def add_agent_task(self, agent_task):
709 agent = Agent(agent_task)
jadmanski0afbb632008-06-06 21:10:57 +0000710 self._agents.append(agent)
711 agent.dispatcher = self
showard170873e2009-01-07 00:22:26 +0000712 self._register_agent_for_ids(self._host_agents, agent.host_ids, agent)
713 self._register_agent_for_ids(self._queue_entry_agents,
714 agent.queue_entry_ids, agent)
mblighd5c95802008-03-05 00:33:46 +0000715
showard170873e2009-01-07 00:22:26 +0000716
717 def get_agents_for_entry(self, queue_entry):
718 """
719 Find agents corresponding to the specified queue_entry.
720 """
showardd3dc1992009-04-22 21:01:40 +0000721 return list(self._queue_entry_agents.get(queue_entry.id, set()))
showard170873e2009-01-07 00:22:26 +0000722
723
724 def host_has_agent(self, host):
725 """
726 Determine if there is currently an Agent present using this host.
727 """
728 return bool(self._host_agents.get(host.id, None))
mbligh36768f02008-02-22 18:28:33 +0000729
730
jadmanski0afbb632008-06-06 21:10:57 +0000731 def remove_agent(self, agent):
732 self._agents.remove(agent)
showard170873e2009-01-07 00:22:26 +0000733 self._unregister_agent_for_ids(self._host_agents, agent.host_ids,
734 agent)
735 self._unregister_agent_for_ids(self._queue_entry_agents,
736 agent.queue_entry_ids, agent)
showardec113162008-05-08 00:52:49 +0000737
738
showard8cc058f2009-09-08 16:26:33 +0000739 def _host_has_scheduled_special_task(self, host):
740 return bool(models.SpecialTask.objects.filter(host__id=host.id,
741 is_active=False,
742 is_complete=False))
743
744
jadmanski0afbb632008-06-06 21:10:57 +0000745 def _recover_processes(self):
showardd1195652009-12-08 22:21:02 +0000746 agent_tasks = self._create_recovery_agent_tasks()
747 self._register_pidfiles(agent_tasks)
showard170873e2009-01-07 00:22:26 +0000748 _drone_manager.refresh()
showardd1195652009-12-08 22:21:02 +0000749 self._recover_tasks(agent_tasks)
showard8cc058f2009-09-08 16:26:33 +0000750 self._recover_pending_entries()
showardb8900452009-10-12 20:31:01 +0000751 self._check_for_unrecovered_verifying_entries()
showard170873e2009-01-07 00:22:26 +0000752 self._reverify_remaining_hosts()
753 # reinitialize drones after killing orphaned processes, since they can
754 # leave around files when they die
755 _drone_manager.execute_actions()
756 _drone_manager.reinitialize_drones()
mblighbb421852008-03-11 22:36:16 +0000757
showard170873e2009-01-07 00:22:26 +0000758
showardd1195652009-12-08 22:21:02 +0000759 def _create_recovery_agent_tasks(self):
760 return (self._get_queue_entry_agent_tasks()
761 + self._get_special_task_agent_tasks(is_active=True))
762
763
764 def _get_queue_entry_agent_tasks(self):
765 # host queue entry statuses handled directly by AgentTasks (Verifying is
766 # handled through SpecialTasks, so is not listed here)
767 statuses = (models.HostQueueEntry.Status.STARTING,
768 models.HostQueueEntry.Status.RUNNING,
769 models.HostQueueEntry.Status.GATHERING,
mbligh4608b002010-01-05 18:22:35 +0000770 models.HostQueueEntry.Status.PARSING,
771 models.HostQueueEntry.Status.ARCHIVING)
showardd1195652009-12-08 22:21:02 +0000772 status_list = ','.join("'%s'" % status for status in statuses)
showard170873e2009-01-07 00:22:26 +0000773 queue_entries = HostQueueEntry.fetch(
showardd1195652009-12-08 22:21:02 +0000774 where='status IN (%s)' % status_list)
775
776 agent_tasks = []
777 used_queue_entries = set()
778 for entry in queue_entries:
779 if self.get_agents_for_entry(entry):
780 # already being handled
781 continue
782 if entry in used_queue_entries:
783 # already picked up by a synchronous job
784 continue
785 agent_task = self._get_agent_task_for_queue_entry(entry)
786 agent_tasks.append(agent_task)
787 used_queue_entries.update(agent_task.queue_entries)
788 return agent_tasks
showard170873e2009-01-07 00:22:26 +0000789
790
showardd1195652009-12-08 22:21:02 +0000791 def _get_special_task_agent_tasks(self, is_active=False):
792 special_tasks = models.SpecialTask.objects.filter(
793 is_active=is_active, is_complete=False)
794 return [self._get_agent_task_for_special_task(task)
795 for task in special_tasks]
796
797
798 def _get_agent_task_for_queue_entry(self, queue_entry):
799 """
800 Construct an AgentTask instance for the given active HostQueueEntry,
801 if one can currently run it.
802 @param queue_entry: a HostQueueEntry
803 @returns an AgentTask to run the queue entry
804 """
805 task_entries = queue_entry.job.get_group_entries(queue_entry)
806 self._check_for_duplicate_host_entries(task_entries)
807
808 if queue_entry.status in (models.HostQueueEntry.Status.STARTING,
809 models.HostQueueEntry.Status.RUNNING):
showarda9545c02009-12-18 22:44:26 +0000810 if queue_entry.is_hostless():
811 return HostlessQueueTask(queue_entry=queue_entry)
showardd1195652009-12-08 22:21:02 +0000812 return QueueTask(queue_entries=task_entries)
813 if queue_entry.status == models.HostQueueEntry.Status.GATHERING:
814 return GatherLogsTask(queue_entries=task_entries)
815 if queue_entry.status == models.HostQueueEntry.Status.PARSING:
816 return FinalReparseTask(queue_entries=task_entries)
mbligh4608b002010-01-05 18:22:35 +0000817 if queue_entry.status == models.HostQueueEntry.Status.ARCHIVING:
818 return ArchiveResultsTask(queue_entries=task_entries)
showardd1195652009-12-08 22:21:02 +0000819
820 raise SchedulerError('_get_agent_task_for_queue_entry got entry with '
821 'invalid status %s: %s' % (entry.status, entry))
822
823
824 def _check_for_duplicate_host_entries(self, task_entries):
mbligh4608b002010-01-05 18:22:35 +0000825 non_host_statuses = (models.HostQueueEntry.Status.PARSING,
826 models.HostQueueEntry.Status.ARCHIVING)
showardd1195652009-12-08 22:21:02 +0000827 for task_entry in task_entries:
showarda9545c02009-12-18 22:44:26 +0000828 using_host = (task_entry.host is not None
mbligh4608b002010-01-05 18:22:35 +0000829 and task_entry.status not in non_host_statuses)
showarda9545c02009-12-18 22:44:26 +0000830 if using_host:
showardd1195652009-12-08 22:21:02 +0000831 self._assert_host_has_no_agent(task_entry)
832
833
834 def _assert_host_has_no_agent(self, entry):
835 """
836 @param entry: a HostQueueEntry or a SpecialTask
837 """
838 if self.host_has_agent(entry.host):
839 agent = tuple(self._host_agents.get(entry.host.id))[0]
840 raise SchedulerError(
841 'While scheduling %s, host %s already has a host agent %s'
842 % (entry, entry.host, agent.task))
843
844
845 def _get_agent_task_for_special_task(self, special_task):
846 """
847 Construct an AgentTask class to run the given SpecialTask and add it
848 to this dispatcher.
849 @param special_task: a models.SpecialTask instance
850 @returns an AgentTask to run this SpecialTask
851 """
852 self._assert_host_has_no_agent(special_task)
853
854 special_agent_task_classes = (CleanupTask, VerifyTask, RepairTask)
855 for agent_task_class in special_agent_task_classes:
856 if agent_task_class.TASK_TYPE == special_task.task:
857 return agent_task_class(task=special_task)
858
859 raise SchedulerError('No AgentTask class for task', str(special_task))
860
861
862 def _register_pidfiles(self, agent_tasks):
863 for agent_task in agent_tasks:
864 agent_task.register_necessary_pidfiles()
865
866
867 def _recover_tasks(self, agent_tasks):
868 orphans = _drone_manager.get_orphaned_autoserv_processes()
869
870 for agent_task in agent_tasks:
871 agent_task.recover()
872 if agent_task.monitor and agent_task.monitor.has_process():
873 orphans.discard(agent_task.monitor.get_process())
874 self.add_agent_task(agent_task)
875
876 self._check_for_remaining_orphan_processes(orphans)
showarded2afea2009-07-07 20:54:07 +0000877
878
showard8cc058f2009-09-08 16:26:33 +0000879 def _get_unassigned_entries(self, status):
880 for entry in HostQueueEntry.fetch(where="status = '%s'" % status):
showard0db3d432009-10-12 20:29:15 +0000881 if entry.status == status and not self.get_agents_for_entry(entry):
882 # The status can change during iteration, e.g., if job.run()
883 # sets a group of queue entries to Starting
showard8cc058f2009-09-08 16:26:33 +0000884 yield entry
885
886
showard6878e8b2009-07-20 22:37:45 +0000887 def _check_for_remaining_orphan_processes(self, orphans):
888 if not orphans:
889 return
890 subject = 'Unrecovered orphan autoserv processes remain'
891 message = '\n'.join(str(process) for process in orphans)
892 email_manager.manager.enqueue_notify_email(subject, message)
mbligh5fa9e112009-08-03 16:46:06 +0000893
894 die_on_orphans = global_config.global_config.get_config_value(
895 scheduler_config.CONFIG_SECTION, 'die_on_orphans', type=bool)
896
897 if die_on_orphans:
898 raise RuntimeError(subject + '\n' + message)
jadmanski0afbb632008-06-06 21:10:57 +0000899
showard170873e2009-01-07 00:22:26 +0000900
showard8cc058f2009-09-08 16:26:33 +0000901 def _recover_pending_entries(self):
902 for entry in self._get_unassigned_entries(
903 models.HostQueueEntry.Status.PENDING):
showard56824072009-10-12 20:30:21 +0000904 logging.info('Recovering Pending entry %s', entry)
showard8cc058f2009-09-08 16:26:33 +0000905 entry.on_pending()
906
907
showardb8900452009-10-12 20:31:01 +0000908 def _check_for_unrecovered_verifying_entries(self):
showard170873e2009-01-07 00:22:26 +0000909 queue_entries = HostQueueEntry.fetch(
showardb8900452009-10-12 20:31:01 +0000910 where='status = "%s"' % models.HostQueueEntry.Status.VERIFYING)
911 unrecovered_hqes = []
912 for queue_entry in queue_entries:
913 special_tasks = models.SpecialTask.objects.filter(
914 task__in=(models.SpecialTask.Task.CLEANUP,
915 models.SpecialTask.Task.VERIFY),
916 queue_entry__id=queue_entry.id,
917 is_complete=False)
918 if special_tasks.count() == 0:
919 unrecovered_hqes.append(queue_entry)
showardd3dc1992009-04-22 21:01:40 +0000920
showardb8900452009-10-12 20:31:01 +0000921 if unrecovered_hqes:
922 message = '\n'.join(str(hqe) for hqe in unrecovered_hqes)
showarde8e37072009-08-20 23:31:30 +0000923 raise SchedulerError(
showard37757f32009-10-19 18:34:24 +0000924 '%d unrecovered verifying host queue entries:\n%s' %
showardb8900452009-10-12 20:31:01 +0000925 (len(unrecovered_hqes), message))
showard170873e2009-01-07 00:22:26 +0000926
927
showard65db3932009-10-28 19:54:35 +0000928 def _get_prioritized_special_tasks(self):
929 """
930 Returns all queued SpecialTasks prioritized for repair first, then
931 cleanup, then verify.
932 """
933 queued_tasks = models.SpecialTask.objects.filter(is_active=False,
934 is_complete=False,
935 host__locked=False)
936 # exclude hosts with active queue entries unless the SpecialTask is for
937 # that queue entry
938 queued_tasks = models.Host.objects.add_join(
showardeab66ce2009-12-23 00:03:56 +0000939 queued_tasks, 'afe_host_queue_entries', 'host_id',
940 join_condition='afe_host_queue_entries.active',
showard65db3932009-10-28 19:54:35 +0000941 force_left_join=True)
942 queued_tasks = queued_tasks.extra(
showardeab66ce2009-12-23 00:03:56 +0000943 where=['(afe_host_queue_entries.id IS NULL OR '
944 'afe_host_queue_entries.id = '
945 'afe_special_tasks.queue_entry_id)'])
showard6d7b2ff2009-06-10 00:16:47 +0000946
showard65db3932009-10-28 19:54:35 +0000947 # reorder tasks by priority
948 task_priority_order = [models.SpecialTask.Task.REPAIR,
949 models.SpecialTask.Task.CLEANUP,
950 models.SpecialTask.Task.VERIFY]
951 def task_priority_key(task):
952 return task_priority_order.index(task.task)
953 return sorted(queued_tasks, key=task_priority_key)
954
955
showard65db3932009-10-28 19:54:35 +0000956 def _schedule_special_tasks(self):
957 """
958 Execute queued SpecialTasks that are ready to run on idle hosts.
959 """
960 for task in self._get_prioritized_special_tasks():
showard8cc058f2009-09-08 16:26:33 +0000961 if self.host_has_agent(task.host):
showard2fe3f1d2009-07-06 20:19:11 +0000962 continue
showardd1195652009-12-08 22:21:02 +0000963 self.add_agent_task(self._get_agent_task_for_special_task(task))
showard1ff7b2e2009-05-15 23:17:18 +0000964
965
showard170873e2009-01-07 00:22:26 +0000966 def _reverify_remaining_hosts(self):
showarded2afea2009-07-07 20:54:07 +0000967 # recover active hosts that have not yet been recovered, although this
showard170873e2009-01-07 00:22:26 +0000968 # should never happen
showarded2afea2009-07-07 20:54:07 +0000969 message = ('Recovering active host %s - this probably indicates a '
showard170873e2009-01-07 00:22:26 +0000970 'scheduler bug')
showarded2afea2009-07-07 20:54:07 +0000971 self._reverify_hosts_where(
showard8cc058f2009-09-08 16:26:33 +0000972 "status IN ('Repairing', 'Verifying', 'Cleaning')",
showarded2afea2009-07-07 20:54:07 +0000973 print_message=message)
mblighbb421852008-03-11 22:36:16 +0000974
975
jadmanski0afbb632008-06-06 21:10:57 +0000976 def _reverify_hosts_where(self, where,
showard2fe3f1d2009-07-06 20:19:11 +0000977 print_message='Reverifying host %s'):
showard170873e2009-01-07 00:22:26 +0000978 full_where='locked = 0 AND invalid = 0 AND ' + where
979 for host in Host.fetch(where=full_where):
980 if self.host_has_agent(host):
981 # host has already been recovered in some way
jadmanski0afbb632008-06-06 21:10:57 +0000982 continue
showard8cc058f2009-09-08 16:26:33 +0000983 if self._host_has_scheduled_special_task(host):
984 # host will have a special task scheduled on the next cycle
985 continue
showard170873e2009-01-07 00:22:26 +0000986 if print_message:
showardb18134f2009-03-20 20:52:18 +0000987 logging.info(print_message, host.hostname)
showard8cc058f2009-09-08 16:26:33 +0000988 models.SpecialTask.objects.create(
989 task=models.SpecialTask.Task.CLEANUP,
showard9bb960b2009-11-19 01:02:11 +0000990 host=models.Host.objects.get(id=host.id))
mbligh36768f02008-02-22 18:28:33 +0000991
992
jadmanski0afbb632008-06-06 21:10:57 +0000993 def _recover_hosts(self):
994 # recover "Repair Failed" hosts
995 message = 'Reverifying dead host %s'
996 self._reverify_hosts_where("status = 'Repair Failed'",
997 print_message=message)
mbligh62ba2ed2008-04-30 17:09:25 +0000998
999
showard04c82c52008-05-29 19:38:12 +00001000
showardb95b1bd2008-08-15 18:11:04 +00001001 def _get_pending_queue_entries(self):
showard63a34772008-08-18 19:32:50 +00001002 # prioritize by job priority, then non-metahost over metahost, then FIFO
1003 return list(HostQueueEntry.fetch(
showardeab66ce2009-12-23 00:03:56 +00001004 joins='INNER JOIN afe_jobs ON (job_id=afe_jobs.id)',
showardac9ce222008-12-03 18:19:44 +00001005 where='NOT complete AND NOT active AND status="Queued"',
showardeab66ce2009-12-23 00:03:56 +00001006 order_by='afe_jobs.priority DESC, meta_host, job_id'))
mbligh36768f02008-02-22 18:28:33 +00001007
1008
showard89f84db2009-03-12 20:39:13 +00001009 def _refresh_pending_queue_entries(self):
1010 """
1011 Lookup the pending HostQueueEntries and call our HostScheduler
1012 refresh() method given that list. Return the list.
1013
1014 @returns A list of pending HostQueueEntries sorted in priority order.
1015 """
showard63a34772008-08-18 19:32:50 +00001016 queue_entries = self._get_pending_queue_entries()
1017 if not queue_entries:
showard89f84db2009-03-12 20:39:13 +00001018 return []
showardb95b1bd2008-08-15 18:11:04 +00001019
showard63a34772008-08-18 19:32:50 +00001020 self._host_scheduler.refresh(queue_entries)
showardb95b1bd2008-08-15 18:11:04 +00001021
showard89f84db2009-03-12 20:39:13 +00001022 return queue_entries
1023
1024
1025 def _schedule_atomic_group(self, queue_entry):
1026 """
1027 Schedule the given queue_entry on an atomic group of hosts.
1028
1029 Returns immediately if there are insufficient available hosts.
1030
1031 Creates new HostQueueEntries based off of queue_entry for the
1032 scheduled hosts and starts them all running.
1033 """
1034 # This is a virtual host queue entry representing an entire
1035 # atomic group, find a group and schedule their hosts.
1036 group_hosts = self._host_scheduler.find_eligible_atomic_group(
1037 queue_entry)
1038 if not group_hosts:
1039 return
showardcbe6f942009-06-17 19:33:49 +00001040
1041 logging.info('Expanding atomic group entry %s with hosts %s',
1042 queue_entry,
1043 ', '.join(host.hostname for host in group_hosts))
showard89f84db2009-03-12 20:39:13 +00001044 # The first assigned host uses the original HostQueueEntry
1045 group_queue_entries = [queue_entry]
1046 for assigned_host in group_hosts[1:]:
1047 # Create a new HQE for every additional assigned_host.
1048 new_hqe = HostQueueEntry.clone(queue_entry)
1049 new_hqe.save()
1050 group_queue_entries.append(new_hqe)
1051 assert len(group_queue_entries) == len(group_hosts)
1052 for queue_entry, host in itertools.izip(group_queue_entries,
1053 group_hosts):
1054 self._run_queue_entry(queue_entry, host)
1055
1056
showarda9545c02009-12-18 22:44:26 +00001057 def _schedule_hostless_job(self, queue_entry):
1058 self.add_agent_task(HostlessQueueTask(queue_entry))
1059
1060
showard89f84db2009-03-12 20:39:13 +00001061 def _schedule_new_jobs(self):
1062 queue_entries = self._refresh_pending_queue_entries()
1063 if not queue_entries:
1064 return
1065
showard63a34772008-08-18 19:32:50 +00001066 for queue_entry in queue_entries:
showarde55955f2009-10-07 20:48:58 +00001067 is_unassigned_atomic_group = (
1068 queue_entry.atomic_group_id is not None
1069 and queue_entry.host_id is None)
1070 if is_unassigned_atomic_group:
1071 self._schedule_atomic_group(queue_entry)
showarda9545c02009-12-18 22:44:26 +00001072 elif queue_entry.is_hostless():
1073 self._schedule_hostless_job(queue_entry)
showarde55955f2009-10-07 20:48:58 +00001074 else:
showard89f84db2009-03-12 20:39:13 +00001075 assigned_host = self._host_scheduler.find_eligible_host(
1076 queue_entry)
showard65db3932009-10-28 19:54:35 +00001077 if assigned_host and not self.host_has_agent(assigned_host):
showard89f84db2009-03-12 20:39:13 +00001078 self._run_queue_entry(queue_entry, assigned_host)
showardb95b1bd2008-08-15 18:11:04 +00001079
1080
showard8cc058f2009-09-08 16:26:33 +00001081 def _schedule_running_host_queue_entries(self):
showardd1195652009-12-08 22:21:02 +00001082 for agent_task in self._get_queue_entry_agent_tasks():
1083 self.add_agent_task(agent_task)
showard8cc058f2009-09-08 16:26:33 +00001084
1085
1086 def _schedule_delay_tasks(self):
showardd2014822009-10-12 20:26:58 +00001087 for entry in HostQueueEntry.fetch(where='status = "%s"' %
1088 models.HostQueueEntry.Status.WAITING):
showard8cc058f2009-09-08 16:26:33 +00001089 task = entry.job.schedule_delayed_callback_task(entry)
1090 if task:
showardd1195652009-12-08 22:21:02 +00001091 self.add_agent_task(task)
showard8cc058f2009-09-08 16:26:33 +00001092
1093
showardb95b1bd2008-08-15 18:11:04 +00001094 def _run_queue_entry(self, queue_entry, host):
showard8cc058f2009-09-08 16:26:33 +00001095 queue_entry.schedule_pre_job_tasks(assigned_host=host)
mblighd5c95802008-03-05 00:33:46 +00001096
1097
jadmanski0afbb632008-06-06 21:10:57 +00001098 def _find_aborting(self):
showardd3dc1992009-04-22 21:01:40 +00001099 for entry in HostQueueEntry.fetch(where='aborted and not complete'):
showardf4a2e502009-07-28 20:06:39 +00001100 logging.info('Aborting %s', entry)
showardd3dc1992009-04-22 21:01:40 +00001101 for agent in self.get_agents_for_entry(entry):
1102 agent.abort()
1103 entry.abort(self)
jadmanski0afbb632008-06-06 21:10:57 +00001104
1105
showard324bf812009-01-20 23:23:38 +00001106 def _can_start_agent(self, agent, num_started_this_cycle,
1107 have_reached_limit):
showard4c5374f2008-09-04 17:02:56 +00001108 # always allow zero-process agents to run
showardd1195652009-12-08 22:21:02 +00001109 if agent.task.num_processes == 0:
showard4c5374f2008-09-04 17:02:56 +00001110 return True
1111 # don't allow any nonzero-process agents to run after we've reached a
1112 # limit (this avoids starvation of many-process agents)
1113 if have_reached_limit:
1114 return False
1115 # total process throttling
showard9bb960b2009-11-19 01:02:11 +00001116 max_runnable_processes = _drone_manager.max_runnable_processes(
showardd1195652009-12-08 22:21:02 +00001117 agent.task.owner_username)
1118 if agent.task.num_processes > max_runnable_processes:
showard4c5374f2008-09-04 17:02:56 +00001119 return False
1120 # if a single agent exceeds the per-cycle throttling, still allow it to
1121 # run when it's the first agent in the cycle
1122 if num_started_this_cycle == 0:
1123 return True
1124 # per-cycle throttling
showardd1195652009-12-08 22:21:02 +00001125 if (num_started_this_cycle + agent.task.num_processes >
1126 scheduler_config.config.max_processes_started_per_cycle):
showard4c5374f2008-09-04 17:02:56 +00001127 return False
1128 return True
1129
1130
jadmanski0afbb632008-06-06 21:10:57 +00001131 def _handle_agents(self):
jadmanski0afbb632008-06-06 21:10:57 +00001132 num_started_this_cycle = 0
showard4c5374f2008-09-04 17:02:56 +00001133 have_reached_limit = False
1134 # iterate over copy, so we can remove agents during iteration
1135 for agent in list(self._agents):
showard8cc058f2009-09-08 16:26:33 +00001136 if not agent.started:
showard324bf812009-01-20 23:23:38 +00001137 if not self._can_start_agent(agent, num_started_this_cycle,
showard4c5374f2008-09-04 17:02:56 +00001138 have_reached_limit):
1139 have_reached_limit = True
1140 continue
showardd1195652009-12-08 22:21:02 +00001141 num_started_this_cycle += agent.task.num_processes
showard4c5374f2008-09-04 17:02:56 +00001142 agent.tick()
showard8cc058f2009-09-08 16:26:33 +00001143 if agent.is_done():
1144 logging.info("agent finished")
1145 self.remove_agent(agent)
showarda9435c02009-05-13 21:28:17 +00001146 logging.info('%d running processes',
showardb18134f2009-03-20 20:52:18 +00001147 _drone_manager.total_running_processes())
mbligh36768f02008-02-22 18:28:33 +00001148
1149
showard29f7cd22009-04-29 21:16:24 +00001150 def _process_recurring_runs(self):
1151 recurring_runs = models.RecurringRun.objects.filter(
1152 start_date__lte=datetime.datetime.now())
1153 for rrun in recurring_runs:
1154 # Create job from template
1155 job = rrun.job
1156 info = rpc_utils.get_job_info(job)
showarda9435c02009-05-13 21:28:17 +00001157 options = job.get_object_dict()
showard29f7cd22009-04-29 21:16:24 +00001158
1159 host_objects = info['hosts']
1160 one_time_hosts = info['one_time_hosts']
1161 metahost_objects = info['meta_hosts']
1162 dependencies = info['dependencies']
1163 atomic_group = info['atomic_group']
1164
1165 for host in one_time_hosts or []:
1166 this_host = models.Host.create_one_time_host(host.hostname)
1167 host_objects.append(this_host)
1168
1169 try:
1170 rpc_utils.create_new_job(owner=rrun.owner.login,
showarda9435c02009-05-13 21:28:17 +00001171 options=options,
showard29f7cd22009-04-29 21:16:24 +00001172 host_objects=host_objects,
1173 metahost_objects=metahost_objects,
showard29f7cd22009-04-29 21:16:24 +00001174 atomic_group=atomic_group)
1175
1176 except Exception, ex:
1177 logging.exception(ex)
1178 #TODO send email
1179
1180 if rrun.loop_count == 1:
1181 rrun.delete()
1182 else:
1183 if rrun.loop_count != 0: # if not infinite loop
1184 # calculate new start_date
1185 difference = datetime.timedelta(seconds=rrun.loop_period)
1186 rrun.start_date = rrun.start_date + difference
1187 rrun.loop_count -= 1
1188 rrun.save()
1189
1190
showard170873e2009-01-07 00:22:26 +00001191class PidfileRunMonitor(object):
1192 """
1193 Client must call either run() to start a new process or
1194 attach_to_existing_process().
1195 """
mbligh36768f02008-02-22 18:28:33 +00001196
showard170873e2009-01-07 00:22:26 +00001197 class _PidfileException(Exception):
1198 """
1199 Raised when there's some unexpected behavior with the pid file, but only
1200 used internally (never allowed to escape this class).
1201 """
mbligh36768f02008-02-22 18:28:33 +00001202
1203
showard170873e2009-01-07 00:22:26 +00001204 def __init__(self):
showard35162b02009-03-03 02:17:30 +00001205 self.lost_process = False
showard170873e2009-01-07 00:22:26 +00001206 self._start_time = None
1207 self.pidfile_id = None
1208 self._state = drone_manager.PidfileContents()
showard2bab8f42008-11-12 18:15:22 +00001209
1210
showard170873e2009-01-07 00:22:26 +00001211 def _add_nice_command(self, command, nice_level):
1212 if not nice_level:
1213 return command
1214 return ['nice', '-n', str(nice_level)] + command
1215
1216
1217 def _set_start_time(self):
1218 self._start_time = time.time()
1219
1220
showard418785b2009-11-23 20:19:59 +00001221 def run(self, command, working_directory, num_processes, nice_level=None,
1222 log_file=None, pidfile_name=None, paired_with_pidfile=None,
1223 username=None):
showard170873e2009-01-07 00:22:26 +00001224 assert command is not None
1225 if nice_level is not None:
1226 command = ['nice', '-n', str(nice_level)] + command
1227 self._set_start_time()
1228 self.pidfile_id = _drone_manager.execute_command(
showardd3dc1992009-04-22 21:01:40 +00001229 command, working_directory, pidfile_name=pidfile_name,
showard418785b2009-11-23 20:19:59 +00001230 num_processes=num_processes, log_file=log_file,
1231 paired_with_pidfile=paired_with_pidfile, username=username)
showard170873e2009-01-07 00:22:26 +00001232
1233
showarded2afea2009-07-07 20:54:07 +00001234 def attach_to_existing_process(self, execution_path,
showardd1195652009-12-08 22:21:02 +00001235 pidfile_name=_AUTOSERV_PID_FILE,
1236 num_processes=None):
showard170873e2009-01-07 00:22:26 +00001237 self._set_start_time()
showardd3dc1992009-04-22 21:01:40 +00001238 self.pidfile_id = _drone_manager.get_pidfile_id_from(
showarded2afea2009-07-07 20:54:07 +00001239 execution_path, pidfile_name=pidfile_name)
showardd1195652009-12-08 22:21:02 +00001240 if num_processes is not None:
1241 _drone_manager.declare_process_count(self.pidfile_id, num_processes)
mblighbb421852008-03-11 22:36:16 +00001242
1243
jadmanski0afbb632008-06-06 21:10:57 +00001244 def kill(self):
showard170873e2009-01-07 00:22:26 +00001245 if self.has_process():
1246 _drone_manager.kill_process(self.get_process())
mblighbb421852008-03-11 22:36:16 +00001247
mbligh36768f02008-02-22 18:28:33 +00001248
showard170873e2009-01-07 00:22:26 +00001249 def has_process(self):
showard21baa452008-10-21 00:08:39 +00001250 self._get_pidfile_info()
showard170873e2009-01-07 00:22:26 +00001251 return self._state.process is not None
showard21baa452008-10-21 00:08:39 +00001252
1253
showard170873e2009-01-07 00:22:26 +00001254 def get_process(self):
showard21baa452008-10-21 00:08:39 +00001255 self._get_pidfile_info()
showard35162b02009-03-03 02:17:30 +00001256 assert self._state.process is not None
showard170873e2009-01-07 00:22:26 +00001257 return self._state.process
mblighbb421852008-03-11 22:36:16 +00001258
1259
showard170873e2009-01-07 00:22:26 +00001260 def _read_pidfile(self, use_second_read=False):
1261 assert self.pidfile_id is not None, (
1262 'You must call run() or attach_to_existing_process()')
1263 contents = _drone_manager.get_pidfile_contents(
1264 self.pidfile_id, use_second_read=use_second_read)
1265 if contents.is_invalid():
1266 self._state = drone_manager.PidfileContents()
1267 raise self._PidfileException(contents)
1268 self._state = contents
mbligh90a549d2008-03-25 23:52:34 +00001269
1270
showard21baa452008-10-21 00:08:39 +00001271 def _handle_pidfile_error(self, error, message=''):
showard170873e2009-01-07 00:22:26 +00001272 message = error + '\nProcess: %s\nPidfile: %s\n%s' % (
1273 self._state.process, self.pidfile_id, message)
showard170873e2009-01-07 00:22:26 +00001274 email_manager.manager.enqueue_notify_email(error, message)
showard35162b02009-03-03 02:17:30 +00001275 self.on_lost_process(self._state.process)
showard21baa452008-10-21 00:08:39 +00001276
1277
1278 def _get_pidfile_info_helper(self):
showard35162b02009-03-03 02:17:30 +00001279 if self.lost_process:
showard21baa452008-10-21 00:08:39 +00001280 return
mblighbb421852008-03-11 22:36:16 +00001281
showard21baa452008-10-21 00:08:39 +00001282 self._read_pidfile()
mblighbb421852008-03-11 22:36:16 +00001283
showard170873e2009-01-07 00:22:26 +00001284 if self._state.process is None:
1285 self._handle_no_process()
showard21baa452008-10-21 00:08:39 +00001286 return
mbligh90a549d2008-03-25 23:52:34 +00001287
showard21baa452008-10-21 00:08:39 +00001288 if self._state.exit_status is None:
jadmanski0afbb632008-06-06 21:10:57 +00001289 # double check whether or not autoserv is running
showard170873e2009-01-07 00:22:26 +00001290 if _drone_manager.is_process_running(self._state.process):
showard21baa452008-10-21 00:08:39 +00001291 return
mbligh90a549d2008-03-25 23:52:34 +00001292
showard170873e2009-01-07 00:22:26 +00001293 # pid but no running process - maybe process *just* exited
1294 self._read_pidfile(use_second_read=True)
showard21baa452008-10-21 00:08:39 +00001295 if self._state.exit_status is None:
jadmanski0afbb632008-06-06 21:10:57 +00001296 # autoserv exited without writing an exit code
1297 # to the pidfile
showard21baa452008-10-21 00:08:39 +00001298 self._handle_pidfile_error(
1299 'autoserv died without writing exit code')
mblighbb421852008-03-11 22:36:16 +00001300
showard21baa452008-10-21 00:08:39 +00001301
1302 def _get_pidfile_info(self):
1303 """\
1304 After completion, self._state will contain:
1305 pid=None, exit_status=None if autoserv has not yet run
1306 pid!=None, exit_status=None if autoserv is running
1307 pid!=None, exit_status!=None if autoserv has completed
1308 """
1309 try:
1310 self._get_pidfile_info_helper()
showard170873e2009-01-07 00:22:26 +00001311 except self._PidfileException, exc:
showard21baa452008-10-21 00:08:39 +00001312 self._handle_pidfile_error('Pidfile error', traceback.format_exc())
mblighbb421852008-03-11 22:36:16 +00001313
1314
showard170873e2009-01-07 00:22:26 +00001315 def _handle_no_process(self):
jadmanski0afbb632008-06-06 21:10:57 +00001316 """\
1317 Called when no pidfile is found or no pid is in the pidfile.
1318 """
showard170873e2009-01-07 00:22:26 +00001319 message = 'No pid found at %s' % self.pidfile_id
showardec6a3b92009-09-25 20:29:13 +00001320 if time.time() - self._start_time > _get_pidfile_timeout_secs():
showard170873e2009-01-07 00:22:26 +00001321 email_manager.manager.enqueue_notify_email(
jadmanski0afbb632008-06-06 21:10:57 +00001322 'Process has failed to write pidfile', message)
showard35162b02009-03-03 02:17:30 +00001323 self.on_lost_process()
mbligh90a549d2008-03-25 23:52:34 +00001324
1325
showard35162b02009-03-03 02:17:30 +00001326 def on_lost_process(self, process=None):
jadmanski0afbb632008-06-06 21:10:57 +00001327 """\
1328 Called when autoserv has exited without writing an exit status,
1329 or we've timed out waiting for autoserv to write a pid to the
1330 pidfile. In either case, we just return failure and the caller
1331 should signal some kind of warning.
mbligh90a549d2008-03-25 23:52:34 +00001332
showard170873e2009-01-07 00:22:26 +00001333 process is unimportant here, as it shouldn't be used by anyone.
jadmanski0afbb632008-06-06 21:10:57 +00001334 """
1335 self.lost_process = True
showard170873e2009-01-07 00:22:26 +00001336 self._state.process = process
showard21baa452008-10-21 00:08:39 +00001337 self._state.exit_status = 1
1338 self._state.num_tests_failed = 0
mbligh90a549d2008-03-25 23:52:34 +00001339
1340
jadmanski0afbb632008-06-06 21:10:57 +00001341 def exit_code(self):
showard21baa452008-10-21 00:08:39 +00001342 self._get_pidfile_info()
1343 return self._state.exit_status
1344
1345
1346 def num_tests_failed(self):
showard6bba3d12009-08-20 23:31:41 +00001347 """@returns The number of tests that failed or -1 if unknown."""
showard21baa452008-10-21 00:08:39 +00001348 self._get_pidfile_info()
showard6bba3d12009-08-20 23:31:41 +00001349 if self._state.num_tests_failed is None:
1350 return -1
showard21baa452008-10-21 00:08:39 +00001351 return self._state.num_tests_failed
mblighbb421852008-03-11 22:36:16 +00001352
1353
showardcdaeae82009-08-31 18:32:48 +00001354 def try_copy_results_on_drone(self, **kwargs):
1355 if self.has_process():
1356 # copy results logs into the normal place for job results
1357 _drone_manager.copy_results_on_drone(self.get_process(), **kwargs)
1358
1359
1360 def try_copy_to_results_repository(self, source, **kwargs):
1361 if self.has_process():
1362 _drone_manager.copy_to_results_repository(self.get_process(),
1363 source, **kwargs)
1364
1365
mbligh36768f02008-02-22 18:28:33 +00001366class Agent(object):
showard77182562009-06-10 00:16:05 +00001367 """
showard8cc058f2009-09-08 16:26:33 +00001368 An agent for use by the Dispatcher class to perform a task.
showard77182562009-06-10 00:16:05 +00001369
1370 The following methods are required on all task objects:
1371 poll() - Called periodically to let the task check its status and
1372 update its internal state. If the task succeeded.
1373 is_done() - Returns True if the task is finished.
1374 abort() - Called when an abort has been requested. The task must
1375 set its aborted attribute to True if it actually aborted.
1376
1377 The following attributes are required on all task objects:
1378 aborted - bool, True if this task was aborted.
showard77182562009-06-10 00:16:05 +00001379 success - bool, True if this task succeeded.
1380 queue_entry_ids - A sequence of HostQueueEntry ids this task handles.
1381 host_ids - A sequence of Host ids this task represents.
showard77182562009-06-10 00:16:05 +00001382 """
1383
1384
showard418785b2009-11-23 20:19:59 +00001385 def __init__(self, task):
showard77182562009-06-10 00:16:05 +00001386 """
showard8cc058f2009-09-08 16:26:33 +00001387 @param task: A task as described in the class docstring.
showard77182562009-06-10 00:16:05 +00001388 """
showard8cc058f2009-09-08 16:26:33 +00001389 self.task = task
showard8cc058f2009-09-08 16:26:33 +00001390
showard77182562009-06-10 00:16:05 +00001391 # This is filled in by Dispatcher.add_agent()
jadmanski0afbb632008-06-06 21:10:57 +00001392 self.dispatcher = None
jadmanski0afbb632008-06-06 21:10:57 +00001393
showard8cc058f2009-09-08 16:26:33 +00001394 self.queue_entry_ids = task.queue_entry_ids
1395 self.host_ids = task.host_ids
showard170873e2009-01-07 00:22:26 +00001396
showard8cc058f2009-09-08 16:26:33 +00001397 self.started = False
showard9bb960b2009-11-19 01:02:11 +00001398 self.finished = False
mbligh36768f02008-02-22 18:28:33 +00001399
1400
jadmanski0afbb632008-06-06 21:10:57 +00001401 def tick(self):
showard8cc058f2009-09-08 16:26:33 +00001402 self.started = True
showard9bb960b2009-11-19 01:02:11 +00001403 if not self.finished:
showard8cc058f2009-09-08 16:26:33 +00001404 self.task.poll()
1405 if self.task.is_done():
showard9bb960b2009-11-19 01:02:11 +00001406 self.finished = True
showardec113162008-05-08 00:52:49 +00001407
1408
jadmanski0afbb632008-06-06 21:10:57 +00001409 def is_done(self):
showard9bb960b2009-11-19 01:02:11 +00001410 return self.finished
mbligh36768f02008-02-22 18:28:33 +00001411
1412
showardd3dc1992009-04-22 21:01:40 +00001413 def abort(self):
showard8cc058f2009-09-08 16:26:33 +00001414 if self.task:
1415 self.task.abort()
1416 if self.task.aborted:
showard08a36412009-05-05 01:01:13 +00001417 # tasks can choose to ignore aborts
showard9bb960b2009-11-19 01:02:11 +00001418 self.finished = True
showard20f9bdd2009-04-29 19:48:33 +00001419
showardd3dc1992009-04-22 21:01:40 +00001420
showard77182562009-06-10 00:16:05 +00001421class DelayedCallTask(object):
1422 """
1423 A task object like AgentTask for an Agent to run that waits for the
1424 specified amount of time to have elapsed before calling the supplied
1425 callback once and finishing. If the callback returns anything, it is
1426 assumed to be a new Agent instance and will be added to the dispatcher.
1427
1428 @attribute end_time: The absolute posix time after which this task will
1429 call its callback when it is polled and be finished.
1430
1431 Also has all attributes required by the Agent class.
1432 """
1433 def __init__(self, delay_seconds, callback, now_func=None):
1434 """
1435 @param delay_seconds: The delay in seconds from now that this task
1436 will call the supplied callback and be done.
1437 @param callback: A callable to be called by this task once after at
1438 least delay_seconds time has elapsed. It must return None
1439 or a new Agent instance.
1440 @param now_func: A time.time like function. Default: time.time.
1441 Used for testing.
1442 """
1443 assert delay_seconds > 0
1444 assert callable(callback)
1445 if not now_func:
1446 now_func = time.time
1447 self._now_func = now_func
1448 self._callback = callback
1449
1450 self.end_time = self._now_func() + delay_seconds
1451
1452 # These attributes are required by Agent.
1453 self.aborted = False
showard77182562009-06-10 00:16:05 +00001454 self.host_ids = ()
1455 self.success = False
1456 self.queue_entry_ids = ()
showard418785b2009-11-23 20:19:59 +00001457 self.num_processes = 0
showard77182562009-06-10 00:16:05 +00001458
1459
1460 def poll(self):
showard8cc058f2009-09-08 16:26:33 +00001461 if not self.is_done() and self._now_func() >= self.end_time:
1462 self._callback()
showard77182562009-06-10 00:16:05 +00001463 self.success = True
1464
1465
1466 def is_done(self):
showard8cc058f2009-09-08 16:26:33 +00001467 return self.success or self.aborted
showard77182562009-06-10 00:16:05 +00001468
1469
1470 def abort(self):
1471 self.aborted = True
showard77182562009-06-10 00:16:05 +00001472
1473
mbligh36768f02008-02-22 18:28:33 +00001474class AgentTask(object):
showardd1195652009-12-08 22:21:02 +00001475 class _NullMonitor(object):
1476 pidfile_id = None
1477
1478 def has_process(self):
1479 return True
1480
1481
1482 def __init__(self, log_file_name=None):
showard9bb960b2009-11-19 01:02:11 +00001483 """
showardd1195652009-12-08 22:21:02 +00001484 @param log_file_name: (optional) name of file to log command output to
showard9bb960b2009-11-19 01:02:11 +00001485 """
jadmanski0afbb632008-06-06 21:10:57 +00001486 self.done = False
showardd1195652009-12-08 22:21:02 +00001487 self.started = False
jadmanski0afbb632008-06-06 21:10:57 +00001488 self.success = None
showardd3dc1992009-04-22 21:01:40 +00001489 self.aborted = False
showardd1195652009-12-08 22:21:02 +00001490 self.monitor = None
showard170873e2009-01-07 00:22:26 +00001491 self.queue_entry_ids = []
1492 self.host_ids = []
showardd1195652009-12-08 22:21:02 +00001493 self._log_file_name = log_file_name
showard170873e2009-01-07 00:22:26 +00001494
1495
1496 def _set_ids(self, host=None, queue_entries=None):
1497 if queue_entries and queue_entries != [None]:
1498 self.host_ids = [entry.host.id for entry in queue_entries]
1499 self.queue_entry_ids = [entry.id for entry in queue_entries]
1500 else:
1501 assert host
1502 self.host_ids = [host.id]
mbligh36768f02008-02-22 18:28:33 +00001503
1504
jadmanski0afbb632008-06-06 21:10:57 +00001505 def poll(self):
showard08a36412009-05-05 01:01:13 +00001506 if not self.started:
1507 self.start()
showardd1195652009-12-08 22:21:02 +00001508 if not self.done:
1509 self.tick()
showard08a36412009-05-05 01:01:13 +00001510
1511
1512 def tick(self):
showardd1195652009-12-08 22:21:02 +00001513 assert self.monitor
1514 exit_code = self.monitor.exit_code()
1515 if exit_code is None:
1516 return
mbligh36768f02008-02-22 18:28:33 +00001517
showardd1195652009-12-08 22:21:02 +00001518 success = (exit_code == 0)
jadmanski0afbb632008-06-06 21:10:57 +00001519 self.finished(success)
mbligh36768f02008-02-22 18:28:33 +00001520
1521
jadmanski0afbb632008-06-06 21:10:57 +00001522 def is_done(self):
1523 return self.done
mbligh36768f02008-02-22 18:28:33 +00001524
1525
jadmanski0afbb632008-06-06 21:10:57 +00001526 def finished(self, success):
showard08a36412009-05-05 01:01:13 +00001527 if self.done:
showardd1195652009-12-08 22:21:02 +00001528 assert self.started
showard08a36412009-05-05 01:01:13 +00001529 return
showardd1195652009-12-08 22:21:02 +00001530 self.started = True
jadmanski0afbb632008-06-06 21:10:57 +00001531 self.done = True
1532 self.success = success
1533 self.epilog()
mbligh36768f02008-02-22 18:28:33 +00001534
1535
jadmanski0afbb632008-06-06 21:10:57 +00001536 def prolog(self):
showardd1195652009-12-08 22:21:02 +00001537 """
1538 To be overridden.
1539 """
showarded2afea2009-07-07 20:54:07 +00001540 assert not self.monitor
showardd1195652009-12-08 22:21:02 +00001541 self.register_necessary_pidfiles()
1542
1543
1544 def _log_file(self):
1545 if not self._log_file_name:
1546 return None
1547 return os.path.join(self._working_directory(), self._log_file_name)
mblighd64e5702008-04-04 21:39:28 +00001548
mbligh36768f02008-02-22 18:28:33 +00001549
jadmanski0afbb632008-06-06 21:10:57 +00001550 def cleanup(self):
showardd1195652009-12-08 22:21:02 +00001551 log_file = self._log_file()
1552 if self.monitor and log_file:
1553 self.monitor.try_copy_to_results_repository(log_file)
mbligh36768f02008-02-22 18:28:33 +00001554
1555
jadmanski0afbb632008-06-06 21:10:57 +00001556 def epilog(self):
showardd1195652009-12-08 22:21:02 +00001557 """
1558 To be overridden.
1559 """
jadmanski0afbb632008-06-06 21:10:57 +00001560 self.cleanup()
showarda9545c02009-12-18 22:44:26 +00001561 logging.info("%s finished with success=%s", type(self).__name__,
1562 self.success)
1563
mbligh36768f02008-02-22 18:28:33 +00001564
1565
jadmanski0afbb632008-06-06 21:10:57 +00001566 def start(self):
jadmanski0afbb632008-06-06 21:10:57 +00001567 if not self.started:
1568 self.prolog()
1569 self.run()
1570
1571 self.started = True
1572
1573
1574 def abort(self):
1575 if self.monitor:
1576 self.monitor.kill()
1577 self.done = True
showardd3dc1992009-04-22 21:01:40 +00001578 self.aborted = True
jadmanski0afbb632008-06-06 21:10:57 +00001579 self.cleanup()
1580
1581
showarded2afea2009-07-07 20:54:07 +00001582 def _get_consistent_execution_path(self, execution_entries):
1583 first_execution_path = execution_entries[0].execution_path()
1584 for execution_entry in execution_entries[1:]:
1585 assert execution_entry.execution_path() == first_execution_path, (
1586 '%s (%s) != %s (%s)' % (execution_entry.execution_path(),
1587 execution_entry,
1588 first_execution_path,
1589 execution_entries[0]))
1590 return first_execution_path
showard170873e2009-01-07 00:22:26 +00001591
1592
showarded2afea2009-07-07 20:54:07 +00001593 def _copy_results(self, execution_entries, use_monitor=None):
1594 """
1595 @param execution_entries: list of objects with execution_path() method
1596 """
showard6d1c1432009-08-20 23:30:39 +00001597 if use_monitor is not None and not use_monitor.has_process():
1598 return
1599
showarded2afea2009-07-07 20:54:07 +00001600 assert len(execution_entries) > 0
showard6b733412009-04-27 20:09:18 +00001601 if use_monitor is None:
1602 assert self.monitor
1603 use_monitor = self.monitor
1604 assert use_monitor.has_process()
showarded2afea2009-07-07 20:54:07 +00001605 execution_path = self._get_consistent_execution_path(execution_entries)
1606 results_path = execution_path + '/'
showardcdaeae82009-08-31 18:32:48 +00001607 use_monitor.try_copy_to_results_repository(results_path)
showardde634ee2009-01-30 01:44:24 +00001608
showarda1e74b32009-05-12 17:32:04 +00001609
1610 def _parse_results(self, queue_entries):
showard8cc058f2009-09-08 16:26:33 +00001611 for queue_entry in queue_entries:
1612 queue_entry.set_status(models.HostQueueEntry.Status.PARSING)
showardde634ee2009-01-30 01:44:24 +00001613
1614
mbligh4608b002010-01-05 18:22:35 +00001615 def _archive_results(self, queue_entries):
1616 for queue_entry in queue_entries:
1617 queue_entry.set_status(models.HostQueueEntry.Status.ARCHIVING)
showarda1e74b32009-05-12 17:32:04 +00001618
1619
showardd1195652009-12-08 22:21:02 +00001620 def _command_line(self):
1621 """
1622 Return the command line to run. Must be overridden.
1623 """
1624 raise NotImplementedError
1625
1626
1627 @property
1628 def num_processes(self):
1629 """
1630 Return the number of processes forked by this AgentTask's process. It
1631 may only be approximate. To be overridden if necessary.
1632 """
1633 return 1
1634
1635
1636 def _paired_with_monitor(self):
1637 """
1638 If this AgentTask's process must run on the same machine as some
1639 previous process, this method should be overridden to return a
1640 PidfileRunMonitor for that process.
1641 """
1642 return self._NullMonitor()
1643
1644
1645 @property
1646 def owner_username(self):
1647 """
1648 Return login of user responsible for this task. May be None. Must be
1649 overridden.
1650 """
1651 raise NotImplementedError
1652
1653
1654 def _working_directory(self):
1655 """
1656 Return the directory where this AgentTask's process executes. Must be
1657 overridden.
1658 """
1659 raise NotImplementedError
1660
1661
1662 def _pidfile_name(self):
1663 """
1664 Return the name of the pidfile this AgentTask's process uses. To be
1665 overridden if necessary.
1666 """
1667 return _AUTOSERV_PID_FILE
1668
1669
1670 def _check_paired_results_exist(self):
1671 if not self._paired_with_monitor().has_process():
1672 email_manager.manager.enqueue_notify_email(
1673 'No paired results in task',
1674 'No paired results in task %s at %s'
1675 % (self, self._paired_with_monitor().pidfile_id))
1676 self.finished(False)
1677 return False
1678 return True
1679
1680
1681 def _create_monitor(self):
showarded2afea2009-07-07 20:54:07 +00001682 assert not self.monitor
showardd1195652009-12-08 22:21:02 +00001683 self.monitor = PidfileRunMonitor()
1684
1685
1686 def run(self):
1687 if not self._check_paired_results_exist():
1688 return
1689
1690 self._create_monitor()
1691 self.monitor.run(
1692 self._command_line(), self._working_directory(),
1693 num_processes=self.num_processes,
1694 nice_level=AUTOSERV_NICE_LEVEL, log_file=self._log_file(),
1695 pidfile_name=self._pidfile_name(),
1696 paired_with_pidfile=self._paired_with_monitor().pidfile_id,
1697 username=self.owner_username)
1698
1699
1700 def register_necessary_pidfiles(self):
1701 pidfile_id = _drone_manager.get_pidfile_id_from(
1702 self._working_directory(), self._pidfile_name())
1703 _drone_manager.register_pidfile(pidfile_id)
1704
1705 paired_pidfile_id = self._paired_with_monitor().pidfile_id
1706 if paired_pidfile_id:
1707 _drone_manager.register_pidfile(paired_pidfile_id)
1708
1709
1710 def recover(self):
1711 if not self._check_paired_results_exist():
1712 return
1713
1714 self._create_monitor()
1715 self.monitor.attach_to_existing_process(
1716 self._working_directory(), pidfile_name=self._pidfile_name(),
1717 num_processes=self.num_processes)
1718 if not self.monitor.has_process():
1719 # no process to recover; wait to be started normally
1720 self.monitor = None
1721 return
1722
1723 self.started = True
1724 logging.info('Recovering process %s for %s at %s'
1725 % (self.monitor.get_process(), type(self).__name__,
1726 self._working_directory()))
mbligh36768f02008-02-22 18:28:33 +00001727
1728
mbligh4608b002010-01-05 18:22:35 +00001729 def _check_queue_entry_statuses(self, queue_entries, allowed_hqe_statuses,
1730 allowed_host_statuses=None):
1731 for entry in queue_entries:
1732 if entry.status not in allowed_hqe_statuses:
1733 raise SchedulerError('Queue task attempting to start '
1734 'entry with invalid status %s: %s'
1735 % (entry.status, entry))
1736 invalid_host_status = (
1737 allowed_host_statuses is not None
1738 and entry.host.status not in allowed_host_statuses)
1739 if invalid_host_status:
1740 raise SchedulerError('Queue task attempting to start on queue '
1741 'entry with invalid host status %s: %s'
1742 % (entry.host.status, entry))
1743
1744
showardd9205182009-04-27 20:09:55 +00001745class TaskWithJobKeyvals(object):
1746 """AgentTask mixin providing functionality to help with job keyval files."""
1747 _KEYVAL_FILE = 'keyval'
1748 def _format_keyval(self, key, value):
1749 return '%s=%s' % (key, value)
1750
1751
1752 def _keyval_path(self):
1753 """Subclasses must override this"""
1754 raise NotImplemented
1755
1756
1757 def _write_keyval_after_job(self, field, value):
1758 assert self.monitor
1759 if not self.monitor.has_process():
1760 return
1761 _drone_manager.write_lines_to_file(
1762 self._keyval_path(), [self._format_keyval(field, value)],
1763 paired_with_process=self.monitor.get_process())
1764
1765
1766 def _job_queued_keyval(self, job):
1767 return 'job_queued', int(time.mktime(job.created_on.timetuple()))
1768
1769
1770 def _write_job_finished(self):
1771 self._write_keyval_after_job("job_finished", int(time.time()))
1772
1773
showarddb502762009-09-09 15:31:20 +00001774 def _write_keyvals_before_job_helper(self, keyval_dict, keyval_path):
1775 keyval_contents = '\n'.join(self._format_keyval(key, value)
1776 for key, value in keyval_dict.iteritems())
1777 # always end with a newline to allow additional keyvals to be written
1778 keyval_contents += '\n'
showard493beaa2009-12-18 22:44:45 +00001779 _drone_manager.attach_file_to_execution(self._working_directory(),
showarddb502762009-09-09 15:31:20 +00001780 keyval_contents,
1781 file_path=keyval_path)
1782
1783
1784 def _write_keyvals_before_job(self, keyval_dict):
1785 self._write_keyvals_before_job_helper(keyval_dict, self._keyval_path())
1786
1787
1788 def _write_host_keyvals(self, host):
showardd1195652009-12-08 22:21:02 +00001789 keyval_path = os.path.join(self._working_directory(), 'host_keyvals',
showarddb502762009-09-09 15:31:20 +00001790 host.hostname)
1791 platform, all_labels = host.platform_and_labels()
1792 keyval_dict = dict(platform=platform, labels=','.join(all_labels))
1793 self._write_keyvals_before_job_helper(keyval_dict, keyval_path)
1794
1795
showard8cc058f2009-09-08 16:26:33 +00001796class SpecialAgentTask(AgentTask, TaskWithJobKeyvals):
showarded2afea2009-07-07 20:54:07 +00001797 """
1798 Subclass for AgentTasks that correspond to a SpecialTask entry in the DB.
1799 """
1800
1801 TASK_TYPE = None
1802 host = None
1803 queue_entry = None
1804
showardd1195652009-12-08 22:21:02 +00001805 def __init__(self, task, extra_command_args):
1806 super(SpecialAgentTask, self).__init__()
1807
showarded2afea2009-07-07 20:54:07 +00001808 assert (self.TASK_TYPE is not None,
1809 'self.TASK_TYPE must be overridden')
showard8cc058f2009-09-08 16:26:33 +00001810
1811 self.host = Host(id=task.host.id)
1812 self.queue_entry = None
1813 if task.queue_entry:
1814 self.queue_entry = HostQueueEntry(id=task.queue_entry.id)
1815
showarded2afea2009-07-07 20:54:07 +00001816 self.task = task
1817 self._extra_command_args = extra_command_args
showarded2afea2009-07-07 20:54:07 +00001818
1819
showard8cc058f2009-09-08 16:26:33 +00001820 def _keyval_path(self):
showardd1195652009-12-08 22:21:02 +00001821 return os.path.join(self._working_directory(), self._KEYVAL_FILE)
1822
1823
1824 def _command_line(self):
1825 return _autoserv_command_line(self.host.hostname,
1826 self._extra_command_args,
1827 queue_entry=self.queue_entry)
1828
1829
1830 def _working_directory(self):
1831 return self.task.execution_path()
1832
1833
1834 @property
1835 def owner_username(self):
1836 if self.task.requested_by:
1837 return self.task.requested_by.login
1838 return None
showard8cc058f2009-09-08 16:26:33 +00001839
1840
showarded2afea2009-07-07 20:54:07 +00001841 def prolog(self):
1842 super(SpecialAgentTask, self).prolog()
showarded2afea2009-07-07 20:54:07 +00001843 self.task.activate()
showarddb502762009-09-09 15:31:20 +00001844 self._write_host_keyvals(self.host)
showarded2afea2009-07-07 20:54:07 +00001845
1846
showardde634ee2009-01-30 01:44:24 +00001847 def _fail_queue_entry(self):
showard2fe3f1d2009-07-06 20:19:11 +00001848 assert self.queue_entry
showardccbd6c52009-03-21 00:10:21 +00001849
showard2fe3f1d2009-07-06 20:19:11 +00001850 if self.queue_entry.meta_host:
showardccbd6c52009-03-21 00:10:21 +00001851 return # don't fail metahost entries, they'll be reassigned
1852
showard2fe3f1d2009-07-06 20:19:11 +00001853 self.queue_entry.update_from_database()
showard8cc058f2009-09-08 16:26:33 +00001854 if self.queue_entry.status != models.HostQueueEntry.Status.QUEUED:
showardccbd6c52009-03-21 00:10:21 +00001855 return # entry has been aborted
1856
showard2fe3f1d2009-07-06 20:19:11 +00001857 self.queue_entry.set_execution_subdir()
showardd9205182009-04-27 20:09:55 +00001858 queued_key, queued_time = self._job_queued_keyval(
showard2fe3f1d2009-07-06 20:19:11 +00001859 self.queue_entry.job)
showardd9205182009-04-27 20:09:55 +00001860 self._write_keyval_after_job(queued_key, queued_time)
1861 self._write_job_finished()
showardcdaeae82009-08-31 18:32:48 +00001862
showard8cc058f2009-09-08 16:26:33 +00001863 # copy results logs into the normal place for job results
showardcdaeae82009-08-31 18:32:48 +00001864 self.monitor.try_copy_results_on_drone(
showardd1195652009-12-08 22:21:02 +00001865 source_path=self._working_directory() + '/',
showardcdaeae82009-08-31 18:32:48 +00001866 destination_path=self.queue_entry.execution_path() + '/')
showard678df4f2009-02-04 21:36:39 +00001867
showard8cc058f2009-09-08 16:26:33 +00001868 pidfile_id = _drone_manager.get_pidfile_id_from(
1869 self.queue_entry.execution_path(),
1870 pidfile_name=_AUTOSERV_PID_FILE)
1871 _drone_manager.register_pidfile(pidfile_id)
mbligh4608b002010-01-05 18:22:35 +00001872
1873 if self.queue_entry.job.parse_failed_repair:
1874 self._parse_results([self.queue_entry])
1875 else:
1876 self._archive_results([self.queue_entry])
showard8cc058f2009-09-08 16:26:33 +00001877
1878
1879 def cleanup(self):
1880 super(SpecialAgentTask, self).cleanup()
showarde60e44e2009-11-13 20:45:38 +00001881
1882 # We will consider an aborted task to be "Failed"
1883 self.task.finish(bool(self.success))
1884
showardf85a0b72009-10-07 20:48:45 +00001885 if self.monitor:
1886 if self.monitor.has_process():
1887 self._copy_results([self.task])
1888 if self.monitor.pidfile_id is not None:
1889 _drone_manager.unregister_pidfile(self.monitor.pidfile_id)
showard8cc058f2009-09-08 16:26:33 +00001890
1891
1892class RepairTask(SpecialAgentTask):
1893 TASK_TYPE = models.SpecialTask.Task.REPAIR
1894
1895
showardd1195652009-12-08 22:21:02 +00001896 def __init__(self, task):
showard8cc058f2009-09-08 16:26:33 +00001897 """\
1898 queue_entry: queue entry to mark failed if this repair fails.
1899 """
1900 protection = host_protections.Protection.get_string(
1901 task.host.protection)
1902 # normalize the protection name
1903 protection = host_protections.Protection.get_attr_name(protection)
1904
1905 super(RepairTask, self).__init__(
showardd1195652009-12-08 22:21:02 +00001906 task, ['-R', '--host-protection', protection])
showard8cc058f2009-09-08 16:26:33 +00001907
1908 # *don't* include the queue entry in IDs -- if the queue entry is
1909 # aborted, we want to leave the repair task running
1910 self._set_ids(host=self.host)
1911
1912
1913 def prolog(self):
1914 super(RepairTask, self).prolog()
1915 logging.info("repair_task starting")
1916 self.host.set_status(models.Host.Status.REPAIRING)
showardde634ee2009-01-30 01:44:24 +00001917
1918
jadmanski0afbb632008-06-06 21:10:57 +00001919 def epilog(self):
1920 super(RepairTask, self).epilog()
showard6d7b2ff2009-06-10 00:16:47 +00001921
jadmanski0afbb632008-06-06 21:10:57 +00001922 if self.success:
showard8cc058f2009-09-08 16:26:33 +00001923 self.host.set_status(models.Host.Status.READY)
jadmanski0afbb632008-06-06 21:10:57 +00001924 else:
showard8cc058f2009-09-08 16:26:33 +00001925 self.host.set_status(models.Host.Status.REPAIR_FAILED)
showard2fe3f1d2009-07-06 20:19:11 +00001926 if self.queue_entry:
showardde634ee2009-01-30 01:44:24 +00001927 self._fail_queue_entry()
mbligh36768f02008-02-22 18:28:33 +00001928
1929
showarded2afea2009-07-07 20:54:07 +00001930class PreJobTask(SpecialAgentTask):
showard775300b2009-09-09 15:30:50 +00001931 def _copy_to_results_repository(self):
1932 if not self.queue_entry or self.queue_entry.meta_host:
1933 return
1934
1935 self.queue_entry.set_execution_subdir()
1936 log_name = os.path.basename(self.task.execution_path())
1937 source = os.path.join(self.task.execution_path(), 'debug',
1938 'autoserv.DEBUG')
1939 destination = os.path.join(
1940 self.queue_entry.execution_path(), log_name)
1941
1942 self.monitor.try_copy_to_results_repository(
1943 source, destination_path=destination)
1944
1945
showard170873e2009-01-07 00:22:26 +00001946 def epilog(self):
1947 super(PreJobTask, self).epilog()
showardcdaeae82009-08-31 18:32:48 +00001948
showard775300b2009-09-09 15:30:50 +00001949 if self.success:
1950 return
showard8fe93b52008-11-18 17:53:22 +00001951
showard775300b2009-09-09 15:30:50 +00001952 self._copy_to_results_repository()
showard8cc058f2009-09-08 16:26:33 +00001953
showard775300b2009-09-09 15:30:50 +00001954 if self.host.protection == host_protections.Protection.DO_NOT_VERIFY:
showard7b2d7cb2009-10-28 19:53:03 +00001955 # effectively ignore failure for these hosts
1956 self.success = True
showard775300b2009-09-09 15:30:50 +00001957 return
1958
1959 if self.queue_entry:
1960 self.queue_entry.requeue()
1961
1962 if models.SpecialTask.objects.filter(
showard8cc058f2009-09-08 16:26:33 +00001963 task=models.SpecialTask.Task.REPAIR,
showard775300b2009-09-09 15:30:50 +00001964 queue_entry__id=self.queue_entry.id):
1965 self.host.set_status(models.Host.Status.REPAIR_FAILED)
1966 self._fail_queue_entry()
1967 return
1968
showard9bb960b2009-11-19 01:02:11 +00001969 queue_entry = models.HostQueueEntry.objects.get(
1970 id=self.queue_entry.id)
showard775300b2009-09-09 15:30:50 +00001971 else:
1972 queue_entry = None
1973
1974 models.SpecialTask.objects.create(
showard9bb960b2009-11-19 01:02:11 +00001975 host=models.Host.objects.get(id=self.host.id),
showard775300b2009-09-09 15:30:50 +00001976 task=models.SpecialTask.Task.REPAIR,
showard9bb960b2009-11-19 01:02:11 +00001977 queue_entry=queue_entry,
1978 requested_by=self.task.requested_by)
showard58721a82009-08-20 23:32:40 +00001979
showard8fe93b52008-11-18 17:53:22 +00001980
1981class VerifyTask(PreJobTask):
showarded2afea2009-07-07 20:54:07 +00001982 TASK_TYPE = models.SpecialTask.Task.VERIFY
1983
1984
showardd1195652009-12-08 22:21:02 +00001985 def __init__(self, task):
1986 super(VerifyTask, self).__init__(task, ['-v'])
showard8cc058f2009-09-08 16:26:33 +00001987 self._set_ids(host=self.host, queue_entries=[self.queue_entry])
mblighe2586682008-02-29 22:45:46 +00001988
1989
jadmanski0afbb632008-06-06 21:10:57 +00001990 def prolog(self):
showard8fe93b52008-11-18 17:53:22 +00001991 super(VerifyTask, self).prolog()
showarded2afea2009-07-07 20:54:07 +00001992
showardb18134f2009-03-20 20:52:18 +00001993 logging.info("starting verify on %s", self.host.hostname)
jadmanski0afbb632008-06-06 21:10:57 +00001994 if self.queue_entry:
showard8cc058f2009-09-08 16:26:33 +00001995 self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING)
1996 self.host.set_status(models.Host.Status.VERIFYING)
mbligh36768f02008-02-22 18:28:33 +00001997
showarded2afea2009-07-07 20:54:07 +00001998 # Delete any other queued verifies for this host. One verify will do
1999 # and there's no need to keep records of other requests.
2000 queued_verifies = models.SpecialTask.objects.filter(
showard2fe3f1d2009-07-06 20:19:11 +00002001 host__id=self.host.id,
2002 task=models.SpecialTask.Task.VERIFY,
showarded2afea2009-07-07 20:54:07 +00002003 is_active=False, is_complete=False)
2004 queued_verifies = queued_verifies.exclude(id=self.task.id)
2005 queued_verifies.delete()
showard2fe3f1d2009-07-06 20:19:11 +00002006
mbligh36768f02008-02-22 18:28:33 +00002007
jadmanski0afbb632008-06-06 21:10:57 +00002008 def epilog(self):
2009 super(VerifyTask, self).epilog()
showard2fe3f1d2009-07-06 20:19:11 +00002010 if self.success:
showard8cc058f2009-09-08 16:26:33 +00002011 if self.queue_entry:
2012 self.queue_entry.on_pending()
2013 else:
2014 self.host.set_status(models.Host.Status.READY)
mbligh36768f02008-02-22 18:28:33 +00002015
2016
mbligh4608b002010-01-05 18:22:35 +00002017class CleanupTask(PreJobTask):
2018 # note this can also run post-job, but when it does, it's running standalone
2019 # against the host (not related to the job), so it's not considered a
2020 # PostJobTask
2021
2022 TASK_TYPE = models.SpecialTask.Task.CLEANUP
2023
2024
2025 def __init__(self, task, recover_run_monitor=None):
2026 super(CleanupTask, self).__init__(task, ['--cleanup'])
2027 self._set_ids(host=self.host, queue_entries=[self.queue_entry])
2028
2029
2030 def prolog(self):
2031 super(CleanupTask, self).prolog()
2032 logging.info("starting cleanup task for host: %s", self.host.hostname)
2033 self.host.set_status(models.Host.Status.CLEANING)
2034 if self.queue_entry:
2035 self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING)
2036
2037
2038 def _finish_epilog(self):
2039 if not self.queue_entry or not self.success:
2040 return
2041
2042 do_not_verify_protection = host_protections.Protection.DO_NOT_VERIFY
2043 should_run_verify = (
2044 self.queue_entry.job.run_verify
2045 and self.host.protection != do_not_verify_protection)
2046 if should_run_verify:
2047 entry = models.HostQueueEntry.objects.get(id=self.queue_entry.id)
2048 models.SpecialTask.objects.create(
2049 host=models.Host.objects.get(id=self.host.id),
2050 queue_entry=entry,
2051 task=models.SpecialTask.Task.VERIFY)
2052 else:
2053 self.queue_entry.on_pending()
2054
2055
2056 def epilog(self):
2057 super(CleanupTask, self).epilog()
2058
2059 if self.success:
2060 self.host.update_field('dirty', 0)
2061 self.host.set_status(models.Host.Status.READY)
2062
2063 self._finish_epilog()
2064
2065
showarda9545c02009-12-18 22:44:26 +00002066class AbstractQueueTask(AgentTask, TaskWithJobKeyvals):
2067 """
2068 Common functionality for QueueTask and HostlessQueueTask
2069 """
2070 def __init__(self, queue_entries):
2071 super(AbstractQueueTask, self).__init__()
showardd1195652009-12-08 22:21:02 +00002072 self.job = queue_entries[0].job
jadmanski0afbb632008-06-06 21:10:57 +00002073 self.queue_entries = queue_entries
mbligh36768f02008-02-22 18:28:33 +00002074
2075
showard73ec0442009-02-07 02:05:20 +00002076 def _keyval_path(self):
showardd1195652009-12-08 22:21:02 +00002077 return os.path.join(self._working_directory(), self._KEYVAL_FILE)
showard73ec0442009-02-07 02:05:20 +00002078
2079
showardd1195652009-12-08 22:21:02 +00002080 def _command_line(self):
2081 return self.job.get_autoserv_params(self.queue_entries)
2082
2083
2084 @property
2085 def num_processes(self):
2086 return len(self.queue_entries)
2087
2088
2089 @property
2090 def owner_username(self):
2091 return self.job.owner
2092
2093
2094 def _working_directory(self):
2095 return self._get_consistent_execution_path(self.queue_entries)
mblighbb421852008-03-11 22:36:16 +00002096
2097
jadmanski0afbb632008-06-06 21:10:57 +00002098 def prolog(self):
showardd9205182009-04-27 20:09:55 +00002099 queued_key, queued_time = self._job_queued_keyval(self.job)
showardf1ae3542009-05-11 19:26:02 +00002100 keyval_dict = {queued_key: queued_time}
showardd1195652009-12-08 22:21:02 +00002101 group_name = self.queue_entries[0].get_group_name()
2102 if group_name:
2103 keyval_dict['host_group_name'] = group_name
showardf1ae3542009-05-11 19:26:02 +00002104 self._write_keyvals_before_job(keyval_dict)
jadmanski0afbb632008-06-06 21:10:57 +00002105 for queue_entry in self.queue_entries:
showard8cc058f2009-09-08 16:26:33 +00002106 queue_entry.set_status(models.HostQueueEntry.Status.RUNNING)
showarda9545c02009-12-18 22:44:26 +00002107 queue_entry.set_started_on_now()
mbligh36768f02008-02-22 18:28:33 +00002108
2109
showard35162b02009-03-03 02:17:30 +00002110 def _write_lost_process_error_file(self):
showardd1195652009-12-08 22:21:02 +00002111 error_file_path = os.path.join(self._working_directory(), 'job_failure')
showard35162b02009-03-03 02:17:30 +00002112 _drone_manager.write_lines_to_file(error_file_path,
2113 [_LOST_PROCESS_ERROR])
2114
2115
showardd3dc1992009-04-22 21:01:40 +00002116 def _finish_task(self):
showard08a36412009-05-05 01:01:13 +00002117 if not self.monitor:
2118 return
2119
showardd9205182009-04-27 20:09:55 +00002120 self._write_job_finished()
2121
showard35162b02009-03-03 02:17:30 +00002122 if self.monitor.lost_process:
2123 self._write_lost_process_error_file()
showard4ac47542009-08-31 18:32:19 +00002124
jadmanskif7fa2cc2008-10-01 14:13:23 +00002125
showardcbd74612008-11-19 21:42:02 +00002126 def _write_status_comment(self, comment):
showard170873e2009-01-07 00:22:26 +00002127 _drone_manager.write_lines_to_file(
showardd1195652009-12-08 22:21:02 +00002128 os.path.join(self._working_directory(), 'status.log'),
showard170873e2009-01-07 00:22:26 +00002129 ['INFO\t----\t----\t' + comment],
showard35162b02009-03-03 02:17:30 +00002130 paired_with_process=self.monitor.get_process())
showardcbd74612008-11-19 21:42:02 +00002131
2132
jadmanskif7fa2cc2008-10-01 14:13:23 +00002133 def _log_abort(self):
showard170873e2009-01-07 00:22:26 +00002134 if not self.monitor or not self.monitor.has_process():
2135 return
2136
jadmanskif7fa2cc2008-10-01 14:13:23 +00002137 # build up sets of all the aborted_by and aborted_on values
2138 aborted_by, aborted_on = set(), set()
2139 for queue_entry in self.queue_entries:
2140 if queue_entry.aborted_by:
2141 aborted_by.add(queue_entry.aborted_by)
2142 t = int(time.mktime(queue_entry.aborted_on.timetuple()))
2143 aborted_on.add(t)
2144
2145 # extract some actual, unique aborted by value and write it out
2146 assert len(aborted_by) <= 1
2147 if len(aborted_by) == 1:
showardcbd74612008-11-19 21:42:02 +00002148 aborted_by_value = aborted_by.pop()
2149 aborted_on_value = max(aborted_on)
2150 else:
2151 aborted_by_value = 'autotest_system'
2152 aborted_on_value = int(time.time())
showard170873e2009-01-07 00:22:26 +00002153
showarda0382352009-02-11 23:36:43 +00002154 self._write_keyval_after_job("aborted_by", aborted_by_value)
2155 self._write_keyval_after_job("aborted_on", aborted_on_value)
showard170873e2009-01-07 00:22:26 +00002156
showardcbd74612008-11-19 21:42:02 +00002157 aborted_on_string = str(datetime.datetime.fromtimestamp(
2158 aborted_on_value))
2159 self._write_status_comment('Job aborted by %s on %s' %
2160 (aborted_by_value, aborted_on_string))
jadmanskic2ac77f2008-05-16 21:44:04 +00002161
2162
jadmanski0afbb632008-06-06 21:10:57 +00002163 def abort(self):
showarda9545c02009-12-18 22:44:26 +00002164 super(AbstractQueueTask, self).abort()
jadmanskif7fa2cc2008-10-01 14:13:23 +00002165 self._log_abort()
showardd3dc1992009-04-22 21:01:40 +00002166 self._finish_task()
showard21baa452008-10-21 00:08:39 +00002167
2168
jadmanski0afbb632008-06-06 21:10:57 +00002169 def epilog(self):
showarda9545c02009-12-18 22:44:26 +00002170 super(AbstractQueueTask, self).epilog()
showardd3dc1992009-04-22 21:01:40 +00002171 self._finish_task()
showarda9545c02009-12-18 22:44:26 +00002172
2173
2174class QueueTask(AbstractQueueTask):
2175 def __init__(self, queue_entries):
2176 super(QueueTask, self).__init__(queue_entries)
2177 self._set_ids(queue_entries=queue_entries)
2178
2179
2180 def prolog(self):
mbligh4608b002010-01-05 18:22:35 +00002181 self._check_queue_entry_statuses(
2182 self.queue_entries,
2183 allowed_hqe_statuses=(models.HostQueueEntry.Status.STARTING,
2184 models.HostQueueEntry.Status.RUNNING),
2185 allowed_host_statuses=(models.Host.Status.PENDING,
2186 models.Host.Status.RUNNING))
showarda9545c02009-12-18 22:44:26 +00002187
2188 super(QueueTask, self).prolog()
2189
2190 for queue_entry in self.queue_entries:
2191 self._write_host_keyvals(queue_entry.host)
2192 queue_entry.host.set_status(models.Host.Status.RUNNING)
2193 queue_entry.host.update_field('dirty', 1)
2194 if self.job.synch_count == 1 and len(self.queue_entries) == 1:
2195 # TODO(gps): Remove this if nothing needs it anymore.
2196 # A potential user is: tko/parser
2197 self.job.write_to_machines_file(self.queue_entries[0])
2198
2199
2200 def _finish_task(self):
2201 super(QueueTask, self)._finish_task()
2202
2203 for queue_entry in self.queue_entries:
2204 queue_entry.set_status(models.HostQueueEntry.Status.GATHERING)
mbligh36768f02008-02-22 18:28:33 +00002205
2206
mbligh4608b002010-01-05 18:22:35 +00002207class HostlessQueueTask(AbstractQueueTask):
2208 def __init__(self, queue_entry):
2209 super(HostlessQueueTask, self).__init__([queue_entry])
2210 self.queue_entry_ids = [queue_entry.id]
2211
2212
2213 def prolog(self):
2214 self.queue_entries[0].update_field('execution_subdir', 'hostless')
2215 super(HostlessQueueTask, self).prolog()
2216
2217
2218 def _final_status(self):
2219 if self.queue_entries[0].aborted:
2220 return models.HostQueueEntry.Status.ABORTED
2221 if self.monitor.exit_code() == 0:
2222 return models.HostQueueEntry.Status.COMPLETED
2223 return models.HostQueueEntry.Status.FAILED
2224
2225
2226 def _finish_task(self):
2227 super(HostlessQueueTask, self)._finish_task()
2228 self.queue_entries[0].set_status(self._final_status())
2229
2230
showardd3dc1992009-04-22 21:01:40 +00002231class PostJobTask(AgentTask):
showardd1195652009-12-08 22:21:02 +00002232 def __init__(self, queue_entries, log_file_name):
2233 super(PostJobTask, self).__init__(log_file_name=log_file_name)
showardd3dc1992009-04-22 21:01:40 +00002234
showardd1195652009-12-08 22:21:02 +00002235 self.queue_entries = queue_entries
2236
showardd3dc1992009-04-22 21:01:40 +00002237 self._autoserv_monitor = PidfileRunMonitor()
showardd1195652009-12-08 22:21:02 +00002238 self._autoserv_monitor.attach_to_existing_process(
2239 self._working_directory())
showardd3dc1992009-04-22 21:01:40 +00002240
showardd1195652009-12-08 22:21:02 +00002241
2242 def _command_line(self):
showardd3dc1992009-04-22 21:01:40 +00002243 if _testing_mode:
showardd1195652009-12-08 22:21:02 +00002244 return 'true'
2245 return self._generate_command(
2246 _drone_manager.absolute_path(self._working_directory()))
showardd3dc1992009-04-22 21:01:40 +00002247
2248
2249 def _generate_command(self, results_dir):
2250 raise NotImplementedError('Subclasses must override this')
2251
2252
showardd1195652009-12-08 22:21:02 +00002253 @property
2254 def owner_username(self):
2255 return self.queue_entries[0].job.owner
2256
2257
2258 def _working_directory(self):
2259 return self._get_consistent_execution_path(self.queue_entries)
2260
2261
2262 def _paired_with_monitor(self):
2263 return self._autoserv_monitor
2264
2265
showardd3dc1992009-04-22 21:01:40 +00002266 def _job_was_aborted(self):
2267 was_aborted = None
showardd1195652009-12-08 22:21:02 +00002268 for queue_entry in self.queue_entries:
showardd3dc1992009-04-22 21:01:40 +00002269 queue_entry.update_from_database()
2270 if was_aborted is None: # first queue entry
2271 was_aborted = bool(queue_entry.aborted)
2272 elif was_aborted != bool(queue_entry.aborted): # subsequent entries
2273 email_manager.manager.enqueue_notify_email(
2274 'Inconsistent abort state',
2275 'Queue entries have inconsistent abort state: ' +
2276 ', '.join('%s (%s)' % (queue_entry, queue_entry.aborted)))
2277 # don't crash here, just assume true
2278 return True
2279 return was_aborted
2280
2281
showardd1195652009-12-08 22:21:02 +00002282 def _final_status(self):
showardd3dc1992009-04-22 21:01:40 +00002283 if self._job_was_aborted():
2284 return models.HostQueueEntry.Status.ABORTED
2285
2286 # we'll use a PidfileRunMonitor to read the autoserv exit status
2287 if self._autoserv_monitor.exit_code() == 0:
2288 return models.HostQueueEntry.Status.COMPLETED
2289 return models.HostQueueEntry.Status.FAILED
2290
2291
showardd3dc1992009-04-22 21:01:40 +00002292 def _set_all_statuses(self, status):
showardd1195652009-12-08 22:21:02 +00002293 for queue_entry in self.queue_entries:
showardd3dc1992009-04-22 21:01:40 +00002294 queue_entry.set_status(status)
2295
2296
2297 def abort(self):
2298 # override AgentTask.abort() to avoid killing the process and ending
2299 # the task. post-job tasks continue when the job is aborted.
2300 pass
2301
2302
mbligh4608b002010-01-05 18:22:35 +00002303 def _pidfile_label(self):
2304 # '.autoserv_execute' -> 'autoserv'
2305 return self._pidfile_name()[1:-len('_execute')]
2306
2307
showard9bb960b2009-11-19 01:02:11 +00002308class GatherLogsTask(PostJobTask):
showardd3dc1992009-04-22 21:01:40 +00002309 """
2310 Task responsible for
2311 * gathering uncollected logs (if Autoserv crashed hard or was killed)
2312 * copying logs to the results repository
2313 * spawning CleanupTasks for hosts, if necessary
2314 * spawning a FinalReparseTask for the job
2315 """
showardd1195652009-12-08 22:21:02 +00002316 def __init__(self, queue_entries, recover_run_monitor=None):
2317 self._job = queue_entries[0].job
showardd3dc1992009-04-22 21:01:40 +00002318 super(GatherLogsTask, self).__init__(
showardd1195652009-12-08 22:21:02 +00002319 queue_entries, log_file_name='.collect_crashinfo.log')
showardd3dc1992009-04-22 21:01:40 +00002320 self._set_ids(queue_entries=queue_entries)
2321
2322
2323 def _generate_command(self, results_dir):
2324 host_list = ','.join(queue_entry.host.hostname
showardd1195652009-12-08 22:21:02 +00002325 for queue_entry in self.queue_entries)
mbligh4608b002010-01-05 18:22:35 +00002326 return [_autoserv_path , '-p',
2327 '--pidfile-label=%s' % self._pidfile_label(),
2328 '--use-existing-results', '--collect-crashinfo',
2329 '-m', host_list, '-r', results_dir]
showardd3dc1992009-04-22 21:01:40 +00002330
2331
showardd1195652009-12-08 22:21:02 +00002332 @property
2333 def num_processes(self):
2334 return len(self.queue_entries)
2335
2336
2337 def _pidfile_name(self):
2338 return _CRASHINFO_PID_FILE
2339
2340
showardd3dc1992009-04-22 21:01:40 +00002341 def prolog(self):
mbligh4608b002010-01-05 18:22:35 +00002342 self._check_queue_entry_statuses(
2343 self.queue_entries,
2344 allowed_hqe_statuses=(models.HostQueueEntry.Status.GATHERING,),
2345 allowed_host_statuses=(models.Host.Status.RUNNING,))
showard8cc058f2009-09-08 16:26:33 +00002346
showardd3dc1992009-04-22 21:01:40 +00002347 super(GatherLogsTask, self).prolog()
showardd3dc1992009-04-22 21:01:40 +00002348
2349
showardd3dc1992009-04-22 21:01:40 +00002350 def epilog(self):
2351 super(GatherLogsTask, self).epilog()
mbligh4608b002010-01-05 18:22:35 +00002352 self._parse_results(self.queue_entries)
showard9bb960b2009-11-19 01:02:11 +00002353 self._reboot_hosts()
showard6d1c1432009-08-20 23:30:39 +00002354
showard9bb960b2009-11-19 01:02:11 +00002355
2356 def _reboot_hosts(self):
showard6d1c1432009-08-20 23:30:39 +00002357 if self._autoserv_monitor.has_process():
showardd1195652009-12-08 22:21:02 +00002358 final_success = (self._final_status() ==
showard6d1c1432009-08-20 23:30:39 +00002359 models.HostQueueEntry.Status.COMPLETED)
2360 num_tests_failed = self._autoserv_monitor.num_tests_failed()
2361 else:
2362 final_success = False
2363 num_tests_failed = 0
2364
showard9bb960b2009-11-19 01:02:11 +00002365 reboot_after = self._job.reboot_after
2366 do_reboot = (
2367 # always reboot after aborted jobs
showardd1195652009-12-08 22:21:02 +00002368 self._final_status() == models.HostQueueEntry.Status.ABORTED
showard9bb960b2009-11-19 01:02:11 +00002369 or reboot_after == models.RebootAfter.ALWAYS
2370 or (reboot_after == models.RebootAfter.IF_ALL_TESTS_PASSED
2371 and final_success and num_tests_failed == 0))
2372
showardd1195652009-12-08 22:21:02 +00002373 for queue_entry in self.queue_entries:
showard9bb960b2009-11-19 01:02:11 +00002374 if do_reboot:
2375 # don't pass the queue entry to the CleanupTask. if the cleanup
2376 # fails, the job doesn't care -- it's over.
2377 models.SpecialTask.objects.create(
2378 host=models.Host.objects.get(id=queue_entry.host.id),
2379 task=models.SpecialTask.Task.CLEANUP,
2380 requested_by=self._job.owner_model())
2381 else:
2382 queue_entry.host.set_status(models.Host.Status.READY)
showardd3dc1992009-04-22 21:01:40 +00002383
2384
showard0bbfc212009-04-29 21:06:13 +00002385 def run(self):
showard597bfd32009-05-08 18:22:50 +00002386 autoserv_exit_code = self._autoserv_monitor.exit_code()
2387 # only run if Autoserv exited due to some signal. if we have no exit
2388 # code, assume something bad (and signal-like) happened.
2389 if autoserv_exit_code is None or os.WIFSIGNALED(autoserv_exit_code):
showard0bbfc212009-04-29 21:06:13 +00002390 super(GatherLogsTask, self).run()
showard597bfd32009-05-08 18:22:50 +00002391 else:
2392 self.finished(True)
showard0bbfc212009-04-29 21:06:13 +00002393
2394
mbligh4608b002010-01-05 18:22:35 +00002395class SelfThrottledPostJobTask(PostJobTask):
2396 """
2397 Special AgentTask subclass that maintains its own global process limit.
2398 """
2399 _num_running_processes = 0
showarded2afea2009-07-07 20:54:07 +00002400
2401
mbligh4608b002010-01-05 18:22:35 +00002402 @classmethod
2403 def _increment_running_processes(cls):
2404 cls._num_running_processes += 1
mbligh16c722d2008-03-05 00:58:44 +00002405
mblighd5c95802008-03-05 00:33:46 +00002406
mbligh4608b002010-01-05 18:22:35 +00002407 @classmethod
2408 def _decrement_running_processes(cls):
2409 cls._num_running_processes -= 1
showard8cc058f2009-09-08 16:26:33 +00002410
2411
mbligh4608b002010-01-05 18:22:35 +00002412 @classmethod
2413 def _max_processes(cls):
2414 raise NotImplementedError
2415
2416
2417 @classmethod
2418 def _can_run_new_process(cls):
2419 return cls._num_running_processes < cls._max_processes()
2420
2421
2422 def _process_started(self):
2423 return bool(self.monitor)
2424
2425
2426 def tick(self):
2427 # override tick to keep trying to start until the process count goes
2428 # down and we can, at which point we revert to default behavior
2429 if self._process_started():
2430 super(SelfThrottledPostJobTask, self).tick()
2431 else:
2432 self._try_starting_process()
2433
2434
2435 def run(self):
2436 # override run() to not actually run unless we can
2437 self._try_starting_process()
2438
2439
2440 def _try_starting_process(self):
2441 if not self._can_run_new_process():
showard775300b2009-09-09 15:30:50 +00002442 return
2443
mbligh4608b002010-01-05 18:22:35 +00002444 # actually run the command
2445 super(SelfThrottledPostJobTask, self).run()
2446 self._increment_running_processes()
mblighd5c95802008-03-05 00:33:46 +00002447
mblighd5c95802008-03-05 00:33:46 +00002448
mbligh4608b002010-01-05 18:22:35 +00002449 def finished(self, success):
2450 super(SelfThrottledPostJobTask, self).finished(success)
2451 if self._process_started():
2452 self._decrement_running_processes()
showard8cc058f2009-09-08 16:26:33 +00002453
showard21baa452008-10-21 00:08:39 +00002454
mbligh4608b002010-01-05 18:22:35 +00002455class FinalReparseTask(SelfThrottledPostJobTask):
showardd1195652009-12-08 22:21:02 +00002456 def __init__(self, queue_entries):
2457 super(FinalReparseTask, self).__init__(queue_entries,
2458 log_file_name='.parse.log')
showard170873e2009-01-07 00:22:26 +00002459 # don't use _set_ids, since we don't want to set the host_ids
2460 self.queue_entry_ids = [entry.id for entry in queue_entries]
showardd1195652009-12-08 22:21:02 +00002461
2462
2463 def _generate_command(self, results_dir):
mbligh4608b002010-01-05 18:22:35 +00002464 return [_parser_path, '--write-pidfile', '-l', '2', '-r', '-o',
showardd1195652009-12-08 22:21:02 +00002465 results_dir]
2466
2467
2468 @property
2469 def num_processes(self):
2470 return 0 # don't include parser processes in accounting
2471
2472
2473 def _pidfile_name(self):
2474 return _PARSER_PID_FILE
2475
2476
showard97aed502008-11-04 02:01:24 +00002477 @classmethod
mbligh4608b002010-01-05 18:22:35 +00002478 def _max_processes(cls):
2479 return scheduler_config.config.max_parse_processes
showard97aed502008-11-04 02:01:24 +00002480
2481
2482 def prolog(self):
mbligh4608b002010-01-05 18:22:35 +00002483 self._check_queue_entry_statuses(
2484 self.queue_entries,
2485 allowed_hqe_statuses=(models.HostQueueEntry.Status.PARSING,))
showard8cc058f2009-09-08 16:26:33 +00002486
showard97aed502008-11-04 02:01:24 +00002487 super(FinalReparseTask, self).prolog()
showard97aed502008-11-04 02:01:24 +00002488
2489
2490 def epilog(self):
2491 super(FinalReparseTask, self).epilog()
mbligh4608b002010-01-05 18:22:35 +00002492 self._archive_results(self.queue_entries)
showard97aed502008-11-04 02:01:24 +00002493
2494
mbligh4608b002010-01-05 18:22:35 +00002495class ArchiveResultsTask(SelfThrottledPostJobTask):
2496 def __init__(self, queue_entries):
2497 super(ArchiveResultsTask, self).__init__(queue_entries,
2498 log_file_name='.archiving.log')
2499 # don't use _set_ids, since we don't want to set the host_ids
2500 self.queue_entry_ids = [entry.id for entry in queue_entries]
showard97aed502008-11-04 02:01:24 +00002501
2502
mbligh4608b002010-01-05 18:22:35 +00002503 def _pidfile_name(self):
2504 return _ARCHIVER_PID_FILE
showard97aed502008-11-04 02:01:24 +00002505
2506
mbligh4608b002010-01-05 18:22:35 +00002507 def _generate_command(self, results_dir):
2508 return [_autoserv_path , '-p',
2509 '--pidfile-label=%s' % self._pidfile_label(), '-r', results_dir,
2510 '--use-existing-results',
2511 os.path.join('..', 'scheduler', 'archive_results.control.srv')]
showard97aed502008-11-04 02:01:24 +00002512
2513
mbligh4608b002010-01-05 18:22:35 +00002514 @classmethod
2515 def _max_processes(cls):
2516 return scheduler_config.config.max_transfer_processes
showarda9545c02009-12-18 22:44:26 +00002517
2518
2519 def prolog(self):
mbligh4608b002010-01-05 18:22:35 +00002520 self._check_queue_entry_statuses(
2521 self.queue_entries,
2522 allowed_hqe_statuses=(models.HostQueueEntry.Status.ARCHIVING,))
2523
2524 super(ArchiveResultsTask, self).prolog()
showarda9545c02009-12-18 22:44:26 +00002525
2526
mbligh4608b002010-01-05 18:22:35 +00002527 def epilog(self):
2528 super(ArchiveResultsTask, self).epilog()
2529 self._set_all_statuses(self._final_status())
showarda9545c02009-12-18 22:44:26 +00002530
2531
showarda3c58572009-03-12 20:36:59 +00002532class DBError(Exception):
2533 """Raised by the DBObject constructor when its select fails."""
2534
2535
mbligh36768f02008-02-22 18:28:33 +00002536class DBObject(object):
showarda3c58572009-03-12 20:36:59 +00002537 """A miniature object relational model for the database."""
showard6ae5ea92009-02-25 00:11:51 +00002538
2539 # Subclasses MUST override these:
2540 _table_name = ''
2541 _fields = ()
2542
showarda3c58572009-03-12 20:36:59 +00002543 # A mapping from (type, id) to the instance of the object for that
2544 # particular id. This prevents us from creating new Job() and Host()
2545 # instances for every HostQueueEntry object that we instantiate as
2546 # multiple HQEs often share the same Job.
2547 _instances_by_type_and_id = weakref.WeakValueDictionary()
2548 _initialized = False
showard6ae5ea92009-02-25 00:11:51 +00002549
showarda3c58572009-03-12 20:36:59 +00002550
2551 def __new__(cls, id=None, **kwargs):
2552 """
2553 Look to see if we already have an instance for this particular type
2554 and id. If so, use it instead of creating a duplicate instance.
2555 """
2556 if id is not None:
2557 instance = cls._instances_by_type_and_id.get((cls, id))
2558 if instance:
2559 return instance
2560 return super(DBObject, cls).__new__(cls, id=id, **kwargs)
2561
2562
2563 def __init__(self, id=None, row=None, new_record=False, always_query=True):
showard8cc058f2009-09-08 16:26:33 +00002564 assert bool(id) or bool(row)
2565 if id is not None and row is not None:
2566 assert id == row[0]
showard6ae5ea92009-02-25 00:11:51 +00002567 assert self._table_name, '_table_name must be defined in your class'
2568 assert self._fields, '_fields must be defined in your class'
showarda3c58572009-03-12 20:36:59 +00002569 if not new_record:
2570 if self._initialized and not always_query:
2571 return # We've already been initialized.
2572 if id is None:
2573 id = row[0]
2574 # Tell future constructors to use us instead of re-querying while
2575 # this instance is still around.
2576 self._instances_by_type_and_id[(type(self), id)] = self
mbligh36768f02008-02-22 18:28:33 +00002577
showard6ae5ea92009-02-25 00:11:51 +00002578 self.__table = self._table_name
mbligh36768f02008-02-22 18:28:33 +00002579
jadmanski0afbb632008-06-06 21:10:57 +00002580 self.__new_record = new_record
mbligh36768f02008-02-22 18:28:33 +00002581
jadmanski0afbb632008-06-06 21:10:57 +00002582 if row is None:
showardccbd6c52009-03-21 00:10:21 +00002583 row = self._fetch_row_from_db(id)
mbligh36768f02008-02-22 18:28:33 +00002584
showarda3c58572009-03-12 20:36:59 +00002585 if self._initialized:
2586 differences = self._compare_fields_in_row(row)
2587 if differences:
showard7629f142009-03-27 21:02:02 +00002588 logging.warn(
2589 'initialized %s %s instance requery is updating: %s',
2590 type(self), self.id, differences)
showard2bab8f42008-11-12 18:15:22 +00002591 self._update_fields_from_row(row)
showarda3c58572009-03-12 20:36:59 +00002592 self._initialized = True
2593
2594
2595 @classmethod
2596 def _clear_instance_cache(cls):
2597 """Used for testing, clear the internal instance cache."""
2598 cls._instances_by_type_and_id.clear()
2599
2600
showardccbd6c52009-03-21 00:10:21 +00002601 def _fetch_row_from_db(self, row_id):
2602 sql = 'SELECT * FROM %s WHERE ID=%%s' % self.__table
2603 rows = _db.execute(sql, (row_id,))
2604 if not rows:
showard76e29d12009-04-15 21:53:10 +00002605 raise DBError("row not found (table=%s, row id=%s)"
2606 % (self.__table, row_id))
showardccbd6c52009-03-21 00:10:21 +00002607 return rows[0]
2608
2609
showarda3c58572009-03-12 20:36:59 +00002610 def _assert_row_length(self, row):
2611 assert len(row) == len(self._fields), (
2612 "table = %s, row = %s/%d, fields = %s/%d" % (
2613 self.__table, row, len(row), self._fields, len(self._fields)))
2614
2615
2616 def _compare_fields_in_row(self, row):
2617 """
showarddae680a2009-10-12 20:26:43 +00002618 Given a row as returned by a SELECT query, compare it to our existing in
2619 memory fields. Fractional seconds are stripped from datetime values
2620 before comparison.
showarda3c58572009-03-12 20:36:59 +00002621
2622 @param row - A sequence of values corresponding to fields named in
2623 The class attribute _fields.
2624
2625 @returns A dictionary listing the differences keyed by field name
2626 containing tuples of (current_value, row_value).
2627 """
2628 self._assert_row_length(row)
2629 differences = {}
showarddae680a2009-10-12 20:26:43 +00002630 datetime_cmp_fmt = '%Y-%m-%d %H:%M:%S' # Leave off the microseconds.
showarda3c58572009-03-12 20:36:59 +00002631 for field, row_value in itertools.izip(self._fields, row):
2632 current_value = getattr(self, field)
showarddae680a2009-10-12 20:26:43 +00002633 if (isinstance(current_value, datetime.datetime)
2634 and isinstance(row_value, datetime.datetime)):
2635 current_value = current_value.strftime(datetime_cmp_fmt)
2636 row_value = row_value.strftime(datetime_cmp_fmt)
showarda3c58572009-03-12 20:36:59 +00002637 if current_value != row_value:
2638 differences[field] = (current_value, row_value)
2639 return differences
showard2bab8f42008-11-12 18:15:22 +00002640
2641
2642 def _update_fields_from_row(self, row):
showarda3c58572009-03-12 20:36:59 +00002643 """
2644 Update our field attributes using a single row returned by SELECT.
2645
2646 @param row - A sequence of values corresponding to fields named in
2647 the class fields list.
2648 """
2649 self._assert_row_length(row)
mbligh36768f02008-02-22 18:28:33 +00002650
showard2bab8f42008-11-12 18:15:22 +00002651 self._valid_fields = set()
showarda3c58572009-03-12 20:36:59 +00002652 for field, value in itertools.izip(self._fields, row):
showard2bab8f42008-11-12 18:15:22 +00002653 setattr(self, field, value)
2654 self._valid_fields.add(field)
mbligh36768f02008-02-22 18:28:33 +00002655
showard2bab8f42008-11-12 18:15:22 +00002656 self._valid_fields.remove('id')
mbligh36768f02008-02-22 18:28:33 +00002657
mblighe2586682008-02-29 22:45:46 +00002658
showardccbd6c52009-03-21 00:10:21 +00002659 def update_from_database(self):
2660 assert self.id is not None
2661 row = self._fetch_row_from_db(self.id)
2662 self._update_fields_from_row(row)
2663
2664
jadmanski0afbb632008-06-06 21:10:57 +00002665 def count(self, where, table = None):
2666 if not table:
2667 table = self.__table
mbligh36768f02008-02-22 18:28:33 +00002668
jadmanski0afbb632008-06-06 21:10:57 +00002669 rows = _db.execute("""
2670 SELECT count(*) FROM %s
2671 WHERE %s
2672 """ % (table, where))
mbligh6f8bab42008-02-29 22:45:14 +00002673
jadmanski0afbb632008-06-06 21:10:57 +00002674 assert len(rows) == 1
2675
2676 return int(rows[0][0])
mbligh36768f02008-02-22 18:28:33 +00002677
2678
showardd3dc1992009-04-22 21:01:40 +00002679 def update_field(self, field, value):
showard2bab8f42008-11-12 18:15:22 +00002680 assert field in self._valid_fields
mbligh36768f02008-02-22 18:28:33 +00002681
showard2bab8f42008-11-12 18:15:22 +00002682 if getattr(self, field) == value:
jadmanski0afbb632008-06-06 21:10:57 +00002683 return
mbligh36768f02008-02-22 18:28:33 +00002684
mblighf8c624d2008-07-03 16:58:45 +00002685 query = "UPDATE %s SET %s = %%s WHERE id = %%s" % (self.__table, field)
jadmanski0afbb632008-06-06 21:10:57 +00002686 _db.execute(query, (value, self.id))
2687
showard2bab8f42008-11-12 18:15:22 +00002688 setattr(self, field, value)
mbligh36768f02008-02-22 18:28:33 +00002689
2690
jadmanski0afbb632008-06-06 21:10:57 +00002691 def save(self):
2692 if self.__new_record:
showard6ae5ea92009-02-25 00:11:51 +00002693 keys = self._fields[1:] # avoid id
jadmanski0afbb632008-06-06 21:10:57 +00002694 columns = ','.join([str(key) for key in keys])
showard76e29d12009-04-15 21:53:10 +00002695 values = []
2696 for key in keys:
2697 value = getattr(self, key)
2698 if value is None:
2699 values.append('NULL')
2700 else:
2701 values.append('"%s"' % value)
showard89f84db2009-03-12 20:39:13 +00002702 values_str = ','.join(values)
2703 query = ('INSERT INTO %s (%s) VALUES (%s)' %
2704 (self.__table, columns, values_str))
jadmanski0afbb632008-06-06 21:10:57 +00002705 _db.execute(query)
showard89f84db2009-03-12 20:39:13 +00002706 # Update our id to the one the database just assigned to us.
2707 self.id = _db.execute('SELECT LAST_INSERT_ID()')[0][0]
mbligh36768f02008-02-22 18:28:33 +00002708
2709
jadmanski0afbb632008-06-06 21:10:57 +00002710 def delete(self):
showarda3c58572009-03-12 20:36:59 +00002711 self._instances_by_type_and_id.pop((type(self), id), None)
2712 self._initialized = False
2713 self._valid_fields.clear()
jadmanski0afbb632008-06-06 21:10:57 +00002714 query = 'DELETE FROM %s WHERE id=%%s' % self.__table
2715 _db.execute(query, (self.id,))
mblighe2586682008-02-29 22:45:46 +00002716
2717
showard63a34772008-08-18 19:32:50 +00002718 @staticmethod
2719 def _prefix_with(string, prefix):
2720 if string:
2721 string = prefix + string
2722 return string
2723
2724
jadmanski0afbb632008-06-06 21:10:57 +00002725 @classmethod
showard989f25d2008-10-01 11:38:11 +00002726 def fetch(cls, where='', params=(), joins='', order_by=''):
showard89f84db2009-03-12 20:39:13 +00002727 """
2728 Construct instances of our class based on the given database query.
2729
2730 @yields One class instance for each row fetched.
2731 """
showard63a34772008-08-18 19:32:50 +00002732 order_by = cls._prefix_with(order_by, 'ORDER BY ')
2733 where = cls._prefix_with(where, 'WHERE ')
2734 query = ('SELECT %(table)s.* FROM %(table)s %(joins)s '
showard6ae5ea92009-02-25 00:11:51 +00002735 '%(where)s %(order_by)s' % {'table' : cls._table_name,
showard63a34772008-08-18 19:32:50 +00002736 'joins' : joins,
2737 'where' : where,
2738 'order_by' : order_by})
2739 rows = _db.execute(query, params)
showard8cc058f2009-09-08 16:26:33 +00002740 return [cls(id=row[0], row=row) for row in rows]
mblighe2586682008-02-29 22:45:46 +00002741
mbligh36768f02008-02-22 18:28:33 +00002742
2743class IneligibleHostQueue(DBObject):
showardeab66ce2009-12-23 00:03:56 +00002744 _table_name = 'afe_ineligible_host_queues'
showard6ae5ea92009-02-25 00:11:51 +00002745 _fields = ('id', 'job_id', 'host_id')
showard04c82c52008-05-29 19:38:12 +00002746
2747
showard89f84db2009-03-12 20:39:13 +00002748class AtomicGroup(DBObject):
showardeab66ce2009-12-23 00:03:56 +00002749 _table_name = 'afe_atomic_groups'
showard205fd602009-03-21 00:17:35 +00002750 _fields = ('id', 'name', 'description', 'max_number_of_machines',
2751 'invalid')
showard89f84db2009-03-12 20:39:13 +00002752
2753
showard989f25d2008-10-01 11:38:11 +00002754class Label(DBObject):
showardeab66ce2009-12-23 00:03:56 +00002755 _table_name = 'afe_labels'
showard6ae5ea92009-02-25 00:11:51 +00002756 _fields = ('id', 'name', 'kernel_config', 'platform', 'invalid',
showard89f84db2009-03-12 20:39:13 +00002757 'only_if_needed', 'atomic_group_id')
showard989f25d2008-10-01 11:38:11 +00002758
2759
showard6157c632009-07-06 20:19:31 +00002760 def __repr__(self):
2761 return 'Label(name=%r, id=%d, atomic_group_id=%r)' % (
2762 self.name, self.id, self.atomic_group_id)
2763
2764
mbligh36768f02008-02-22 18:28:33 +00002765class Host(DBObject):
showardeab66ce2009-12-23 00:03:56 +00002766 _table_name = 'afe_hosts'
showard6ae5ea92009-02-25 00:11:51 +00002767 _fields = ('id', 'hostname', 'locked', 'synch_id', 'status',
2768 'invalid', 'protection', 'locked_by_id', 'lock_time', 'dirty')
2769
2770
jadmanski0afbb632008-06-06 21:10:57 +00002771 def set_status(self,status):
showardb18134f2009-03-20 20:52:18 +00002772 logging.info('%s -> %s', self.hostname, status)
jadmanski0afbb632008-06-06 21:10:57 +00002773 self.update_field('status',status)
mbligh36768f02008-02-22 18:28:33 +00002774
2775
showard170873e2009-01-07 00:22:26 +00002776 def platform_and_labels(self):
showardd8e548a2008-09-09 03:04:57 +00002777 """
showard170873e2009-01-07 00:22:26 +00002778 Returns a tuple (platform_name, list_of_all_label_names).
showardd8e548a2008-09-09 03:04:57 +00002779 """
2780 rows = _db.execute("""
showardeab66ce2009-12-23 00:03:56 +00002781 SELECT afe_labels.name, afe_labels.platform
2782 FROM afe_labels
2783 INNER JOIN afe_hosts_labels ON
2784 afe_labels.id = afe_hosts_labels.label_id
2785 WHERE afe_hosts_labels.host_id = %s
2786 ORDER BY afe_labels.name
showardd8e548a2008-09-09 03:04:57 +00002787 """, (self.id,))
showard170873e2009-01-07 00:22:26 +00002788 platform = None
2789 all_labels = []
2790 for label_name, is_platform in rows:
2791 if is_platform:
2792 platform = label_name
2793 all_labels.append(label_name)
2794 return platform, all_labels
2795
2796
showard54c1ea92009-05-20 00:32:58 +00002797 _ALPHANUM_HOST_RE = re.compile(r'^([a-z-]+)(\d+)$', re.IGNORECASE)
2798
2799
2800 @classmethod
2801 def cmp_for_sort(cls, a, b):
2802 """
2803 A comparison function for sorting Host objects by hostname.
2804
2805 This strips any trailing numeric digits, ignores leading 0s and
2806 compares hostnames by the leading name and the trailing digits as a
2807 number. If both hostnames do not match this pattern, they are simply
2808 compared as lower case strings.
2809
2810 Example of how hostnames will be sorted:
2811
2812 alice, host1, host2, host09, host010, host10, host11, yolkfolk
2813
2814 This hopefully satisfy most people's hostname sorting needs regardless
2815 of their exact naming schemes. Nobody sane should have both a host10
2816 and host010 (but the algorithm works regardless).
2817 """
2818 lower_a = a.hostname.lower()
2819 lower_b = b.hostname.lower()
2820 match_a = cls._ALPHANUM_HOST_RE.match(lower_a)
2821 match_b = cls._ALPHANUM_HOST_RE.match(lower_b)
2822 if match_a and match_b:
2823 name_a, number_a_str = match_a.groups()
2824 name_b, number_b_str = match_b.groups()
2825 number_a = int(number_a_str.lstrip('0'))
2826 number_b = int(number_b_str.lstrip('0'))
2827 result = cmp((name_a, number_a), (name_b, number_b))
2828 if result == 0 and lower_a != lower_b:
2829 # If they compared equal above but the lower case names are
2830 # indeed different, don't report equality. abc012 != abc12.
2831 return cmp(lower_a, lower_b)
2832 return result
2833 else:
2834 return cmp(lower_a, lower_b)
2835
2836
mbligh36768f02008-02-22 18:28:33 +00002837class HostQueueEntry(DBObject):
showardeab66ce2009-12-23 00:03:56 +00002838 _table_name = 'afe_host_queue_entries'
showard6ae5ea92009-02-25 00:11:51 +00002839 _fields = ('id', 'job_id', 'host_id', 'status', 'meta_host',
showard89f84db2009-03-12 20:39:13 +00002840 'active', 'complete', 'deleted', 'execution_subdir',
showard12f3e322009-05-13 21:27:42 +00002841 'atomic_group_id', 'aborted', 'started_on')
showard6ae5ea92009-02-25 00:11:51 +00002842
2843
showarda3c58572009-03-12 20:36:59 +00002844 def __init__(self, id=None, row=None, **kwargs):
jadmanski0afbb632008-06-06 21:10:57 +00002845 assert id or row
showarda3c58572009-03-12 20:36:59 +00002846 super(HostQueueEntry, self).__init__(id=id, row=row, **kwargs)
jadmanski0afbb632008-06-06 21:10:57 +00002847 self.job = Job(self.job_id)
mbligh36768f02008-02-22 18:28:33 +00002848
jadmanski0afbb632008-06-06 21:10:57 +00002849 if self.host_id:
2850 self.host = Host(self.host_id)
2851 else:
2852 self.host = None
mbligh36768f02008-02-22 18:28:33 +00002853
showard77182562009-06-10 00:16:05 +00002854 if self.atomic_group_id:
2855 self.atomic_group = AtomicGroup(self.atomic_group_id,
2856 always_query=False)
2857 else:
2858 self.atomic_group = None
2859
showard170873e2009-01-07 00:22:26 +00002860 self.queue_log_path = os.path.join(self.job.tag(),
jadmanski0afbb632008-06-06 21:10:57 +00002861 'queue.log.' + str(self.id))
mbligh36768f02008-02-22 18:28:33 +00002862
2863
showard89f84db2009-03-12 20:39:13 +00002864 @classmethod
2865 def clone(cls, template):
2866 """
2867 Creates a new row using the values from a template instance.
2868
2869 The new instance will not exist in the database or have a valid
2870 id attribute until its save() method is called.
2871 """
2872 assert isinstance(template, cls)
2873 new_row = [getattr(template, field) for field in cls._fields]
2874 clone = cls(row=new_row, new_record=True)
2875 clone.id = None
2876 return clone
2877
2878
showardc85c21b2008-11-24 22:17:37 +00002879 def _view_job_url(self):
2880 return "%s#tab_id=view_job&object_id=%s" % (_base_url, self.job.id)
2881
2882
showardf1ae3542009-05-11 19:26:02 +00002883 def get_labels(self):
2884 """
2885 Get all labels associated with this host queue entry (either via the
2886 meta_host or as a job dependency label). The labels yielded are not
2887 guaranteed to be unique.
2888
2889 @yields Label instances associated with this host_queue_entry.
2890 """
2891 if self.meta_host:
2892 yield Label(id=self.meta_host, always_query=False)
2893 labels = Label.fetch(
showardeab66ce2009-12-23 00:03:56 +00002894 joins="JOIN afe_jobs_dependency_labels AS deps "
2895 "ON (afe_labels.id = deps.label_id)",
showardf1ae3542009-05-11 19:26:02 +00002896 where="deps.job_id = %d" % self.job.id)
2897 for label in labels:
2898 yield label
2899
2900
jadmanski0afbb632008-06-06 21:10:57 +00002901 def set_host(self, host):
2902 if host:
2903 self.queue_log_record('Assigning host ' + host.hostname)
2904 self.update_field('host_id', host.id)
2905 self.update_field('active', True)
2906 self.block_host(host.id)
2907 else:
2908 self.queue_log_record('Releasing host')
2909 self.unblock_host(self.host.id)
2910 self.update_field('host_id', None)
mbligh36768f02008-02-22 18:28:33 +00002911
jadmanski0afbb632008-06-06 21:10:57 +00002912 self.host = host
mbligh36768f02008-02-22 18:28:33 +00002913
2914
jadmanski0afbb632008-06-06 21:10:57 +00002915 def queue_log_record(self, log_line):
2916 now = str(datetime.datetime.now())
showard170873e2009-01-07 00:22:26 +00002917 _drone_manager.write_lines_to_file(self.queue_log_path,
2918 [now + ' ' + log_line])
mbligh36768f02008-02-22 18:28:33 +00002919
2920
jadmanski0afbb632008-06-06 21:10:57 +00002921 def block_host(self, host_id):
showardb18134f2009-03-20 20:52:18 +00002922 logging.info("creating block %s/%s", self.job.id, host_id)
jadmanski0afbb632008-06-06 21:10:57 +00002923 row = [0, self.job.id, host_id]
2924 block = IneligibleHostQueue(row=row, new_record=True)
2925 block.save()
mblighe2586682008-02-29 22:45:46 +00002926
2927
jadmanski0afbb632008-06-06 21:10:57 +00002928 def unblock_host(self, host_id):
showardb18134f2009-03-20 20:52:18 +00002929 logging.info("removing block %s/%s", self.job.id, host_id)
jadmanski0afbb632008-06-06 21:10:57 +00002930 blocks = IneligibleHostQueue.fetch(
2931 'job_id=%d and host_id=%d' % (self.job.id, host_id))
2932 for block in blocks:
2933 block.delete()
mblighe2586682008-02-29 22:45:46 +00002934
2935
showard2bab8f42008-11-12 18:15:22 +00002936 def set_execution_subdir(self, subdir=None):
2937 if subdir is None:
showarda9545c02009-12-18 22:44:26 +00002938 assert self.host
2939 subdir = self.host.hostname
showard2bab8f42008-11-12 18:15:22 +00002940 self.update_field('execution_subdir', subdir)
mbligh36768f02008-02-22 18:28:33 +00002941
2942
showard6355f6b2008-12-05 18:52:13 +00002943 def _get_hostname(self):
2944 if self.host:
2945 return self.host.hostname
2946 return 'no host'
2947
2948
showard170873e2009-01-07 00:22:26 +00002949 def __str__(self):
showard828fc4c2009-09-14 20:31:00 +00002950 flags = []
2951 if self.active:
2952 flags.append('active')
2953 if self.complete:
2954 flags.append('complete')
2955 if self.deleted:
2956 flags.append('deleted')
2957 if self.aborted:
2958 flags.append('aborted')
2959 flags_str = ','.join(flags)
2960 if flags_str:
2961 flags_str = ' [%s]' % flags_str
2962 return "%s/%d (%d) %s%s" % (self._get_hostname(), self.job.id, self.id,
2963 self.status, flags_str)
showard170873e2009-01-07 00:22:26 +00002964
2965
jadmanski0afbb632008-06-06 21:10:57 +00002966 def set_status(self, status):
showard56824072009-10-12 20:30:21 +00002967 logging.info("%s -> %s", self, status)
mblighf8c624d2008-07-03 16:58:45 +00002968
showard56824072009-10-12 20:30:21 +00002969 self.update_field('status', status)
mblighf8c624d2008-07-03 16:58:45 +00002970
mbligh4608b002010-01-05 18:22:35 +00002971 active = (status in models.HostQueueEntry.ACTIVE_STATUSES)
2972 complete = (status in models.HostQueueEntry.COMPLETE_STATUSES)
2973 assert not (active and complete)
mbligh36768f02008-02-22 18:28:33 +00002974
mbligh4608b002010-01-05 18:22:35 +00002975 self.update_field('active', active)
2976 self.update_field('complete', complete)
mbligh36768f02008-02-22 18:28:33 +00002977
mbligh4608b002010-01-05 18:22:35 +00002978 if complete:
showardf85a0b72009-10-07 20:48:45 +00002979 self._on_complete()
mbligh4608b002010-01-05 18:22:35 +00002980 self._email_on_job_complete()
showardc85c21b2008-11-24 22:17:37 +00002981
2982 should_email_status = (status.lower() in _notify_email_statuses or
2983 'all' in _notify_email_statuses)
2984 if should_email_status:
2985 self._email_on_status(status)
2986
showardc85c21b2008-11-24 22:17:37 +00002987
showardf85a0b72009-10-07 20:48:45 +00002988 def _on_complete(self):
showardd1195652009-12-08 22:21:02 +00002989 self.job.stop_if_necessary()
showardf85a0b72009-10-07 20:48:45 +00002990 if not self.execution_subdir:
2991 return
2992 # unregister any possible pidfiles associated with this queue entry
2993 for pidfile_name in _ALL_PIDFILE_NAMES:
2994 pidfile_id = _drone_manager.get_pidfile_id_from(
2995 self.execution_path(), pidfile_name=pidfile_name)
2996 _drone_manager.unregister_pidfile(pidfile_id)
2997
2998
showardc85c21b2008-11-24 22:17:37 +00002999 def _email_on_status(self, status):
showard6355f6b2008-12-05 18:52:13 +00003000 hostname = self._get_hostname()
showardc85c21b2008-11-24 22:17:37 +00003001
3002 subject = 'Autotest: Job ID: %s "%s" Host: %s %s' % (
3003 self.job.id, self.job.name, hostname, status)
3004 body = "Job ID: %s\nJob Name: %s\nHost: %s\nStatus: %s\n%s\n" % (
3005 self.job.id, self.job.name, hostname, status,
3006 self._view_job_url())
showard170873e2009-01-07 00:22:26 +00003007 email_manager.manager.send_email(self.job.email_list, subject, body)
showard542e8402008-09-19 20:16:18 +00003008
3009
3010 def _email_on_job_complete(self):
showardc85c21b2008-11-24 22:17:37 +00003011 if not self.job.is_finished():
3012 return
showard542e8402008-09-19 20:16:18 +00003013
showardc85c21b2008-11-24 22:17:37 +00003014 summary_text = []
showard6355f6b2008-12-05 18:52:13 +00003015 hosts_queue = HostQueueEntry.fetch('job_id = %s' % self.job.id)
showardc85c21b2008-11-24 22:17:37 +00003016 for queue_entry in hosts_queue:
3017 summary_text.append("Host: %s Status: %s" %
showard6355f6b2008-12-05 18:52:13 +00003018 (queue_entry._get_hostname(),
showardc85c21b2008-11-24 22:17:37 +00003019 queue_entry.status))
3020
3021 summary_text = "\n".join(summary_text)
3022 status_counts = models.Job.objects.get_status_counts(
3023 [self.job.id])[self.job.id]
3024 status = ', '.join('%d %s' % (count, status) for status, count
3025 in status_counts.iteritems())
3026
3027 subject = 'Autotest: Job ID: %s "%s" %s' % (
3028 self.job.id, self.job.name, status)
3029 body = "Job ID: %s\nJob Name: %s\nStatus: %s\n%s\nSummary:\n%s" % (
3030 self.job.id, self.job.name, status, self._view_job_url(),
3031 summary_text)
showard170873e2009-01-07 00:22:26 +00003032 email_manager.manager.send_email(self.job.email_list, subject, body)
mbligh36768f02008-02-22 18:28:33 +00003033
3034
showard8cc058f2009-09-08 16:26:33 +00003035 def schedule_pre_job_tasks(self, assigned_host=None):
showard77182562009-06-10 00:16:05 +00003036 if self.meta_host is not None or self.atomic_group:
jadmanski0afbb632008-06-06 21:10:57 +00003037 assert assigned_host
3038 # ensure results dir exists for the queue log
showard08356c12009-06-15 20:24:01 +00003039 if self.host_id is None:
3040 self.set_host(assigned_host)
3041 else:
3042 assert assigned_host.id == self.host_id
mbligh36768f02008-02-22 18:28:33 +00003043
showard2ca64c92009-12-10 21:41:02 +00003044 logging.info("%s/%s/%s (job %s, entry %s) scheduled on %s, status=%s",
showardb18134f2009-03-20 20:52:18 +00003045 self.job.name, self.meta_host, self.atomic_group_id,
showard2ca64c92009-12-10 21:41:02 +00003046 self.job.id, self.id, self.host.hostname, self.status)
mbligh36768f02008-02-22 18:28:33 +00003047
showard8cc058f2009-09-08 16:26:33 +00003048 self._do_schedule_pre_job_tasks()
showard77182562009-06-10 00:16:05 +00003049
3050
showard8cc058f2009-09-08 16:26:33 +00003051 def _do_schedule_pre_job_tasks(self):
showard77182562009-06-10 00:16:05 +00003052 # Every host goes thru the Verifying stage (which may or may not
3053 # actually do anything as determined by get_pre_job_tasks).
3054 self.set_status(models.HostQueueEntry.Status.VERIFYING)
showard8cc058f2009-09-08 16:26:33 +00003055 self.job.schedule_pre_job_tasks(queue_entry=self)
mblighe2586682008-02-29 22:45:46 +00003056
showard6ae5ea92009-02-25 00:11:51 +00003057
jadmanski0afbb632008-06-06 21:10:57 +00003058 def requeue(self):
showardcfd4a7e2009-07-11 01:47:33 +00003059 assert self.host
showard8cc058f2009-09-08 16:26:33 +00003060 self.set_status(models.HostQueueEntry.Status.QUEUED)
showard12f3e322009-05-13 21:27:42 +00003061 self.update_field('started_on', None)
showardde634ee2009-01-30 01:44:24 +00003062 # verify/cleanup failure sets the execution subdir, so reset it here
3063 self.set_execution_subdir('')
jadmanski0afbb632008-06-06 21:10:57 +00003064 if self.meta_host:
3065 self.set_host(None)
mbligh36768f02008-02-22 18:28:33 +00003066
3067
jadmanskif7fa2cc2008-10-01 14:13:23 +00003068 @property
3069 def aborted_by(self):
3070 self._load_abort_info()
3071 return self._aborted_by
3072
3073
3074 @property
3075 def aborted_on(self):
3076 self._load_abort_info()
3077 return self._aborted_on
3078
3079
3080 def _load_abort_info(self):
3081 """ Fetch info about who aborted the job. """
3082 if hasattr(self, "_aborted_by"):
3083 return
3084 rows = _db.execute("""
showardeab66ce2009-12-23 00:03:56 +00003085 SELECT afe_users.login,
3086 afe_aborted_host_queue_entries.aborted_on
3087 FROM afe_aborted_host_queue_entries
3088 INNER JOIN afe_users
3089 ON afe_users.id = afe_aborted_host_queue_entries.aborted_by_id
3090 WHERE afe_aborted_host_queue_entries.queue_entry_id = %s
jadmanskif7fa2cc2008-10-01 14:13:23 +00003091 """, (self.id,))
3092 if rows:
3093 self._aborted_by, self._aborted_on = rows[0]
3094 else:
3095 self._aborted_by = self._aborted_on = None
3096
3097
showardb2e2c322008-10-14 17:33:55 +00003098 def on_pending(self):
3099 """
3100 Called when an entry in a synchronous job has passed verify. If the
showard8cc058f2009-09-08 16:26:33 +00003101 job is ready to run, sets the entries to STARTING. Otherwise, it leaves
3102 them in PENDING.
showardb2e2c322008-10-14 17:33:55 +00003103 """
showard8cc058f2009-09-08 16:26:33 +00003104 self.set_status(models.HostQueueEntry.Status.PENDING)
3105 self.host.set_status(models.Host.Status.PENDING)
showardb000a8d2009-07-28 20:02:07 +00003106
3107 # Some debug code here: sends an email if an asynchronous job does not
3108 # immediately enter Starting.
3109 # TODO: Remove this once we figure out why asynchronous jobs are getting
3110 # stuck in Pending.
showard8cc058f2009-09-08 16:26:33 +00003111 self.job.run_if_ready(queue_entry=self)
3112 if (self.job.synch_count == 1 and
3113 self.status == models.HostQueueEntry.Status.PENDING):
showardb000a8d2009-07-28 20:02:07 +00003114 subject = 'Job %s (id %s)' % (self.job.name, self.job.id)
3115 message = 'Asynchronous job stuck in Pending'
3116 email_manager.manager.enqueue_notify_email(subject, message)
showardb2e2c322008-10-14 17:33:55 +00003117
3118
showardd3dc1992009-04-22 21:01:40 +00003119 def abort(self, dispatcher):
3120 assert self.aborted and not self.complete
showard1be97432008-10-17 15:30:45 +00003121
showardd3dc1992009-04-22 21:01:40 +00003122 Status = models.HostQueueEntry.Status
mbligh4608b002010-01-05 18:22:35 +00003123 if self.status in (Status.GATHERING, Status.PARSING, Status.ARCHIVING):
showardd3dc1992009-04-22 21:01:40 +00003124 # do nothing; post-job tasks will finish and then mark this entry
3125 # with status "Aborted" and take care of the host
3126 return
3127
showard8cc058f2009-09-08 16:26:33 +00003128 if self.status in (Status.STARTING, Status.PENDING, Status.RUNNING):
3129 assert not dispatcher.get_agents_for_entry(self)
showardd3dc1992009-04-22 21:01:40 +00003130 self.host.set_status(models.Host.Status.READY)
3131 elif self.status == Status.VERIFYING:
showard8cc058f2009-09-08 16:26:33 +00003132 models.SpecialTask.objects.create(
3133 task=models.SpecialTask.Task.CLEANUP,
showard9bb960b2009-11-19 01:02:11 +00003134 host=models.Host.objects.get(id=self.host.id),
3135 requested_by=self.job.owner_model())
showardd3dc1992009-04-22 21:01:40 +00003136
3137 self.set_status(Status.ABORTED)
showardd2014822009-10-12 20:26:58 +00003138 self.job.abort_delay_ready_task()
showard170873e2009-01-07 00:22:26 +00003139
showard8cc058f2009-09-08 16:26:33 +00003140
3141 def get_group_name(self):
3142 atomic_group = self.atomic_group
3143 if not atomic_group:
3144 return ''
3145
3146 # Look at any meta_host and dependency labels and pick the first
3147 # one that also specifies this atomic group. Use that label name
3148 # as the group name if possible (it is more specific).
3149 for label in self.get_labels():
3150 if label.atomic_group_id:
3151 assert label.atomic_group_id == atomic_group.id
3152 return label.name
3153 return atomic_group.name
3154
3155
showard170873e2009-01-07 00:22:26 +00003156 def execution_tag(self):
3157 assert self.execution_subdir
mblighe7d9c602009-07-02 19:02:33 +00003158 return "%s/%s" % (self.job.tag(), self.execution_subdir)
showard1be97432008-10-17 15:30:45 +00003159
3160
showarded2afea2009-07-07 20:54:07 +00003161 def execution_path(self):
3162 return self.execution_tag()
3163
3164
showarda9545c02009-12-18 22:44:26 +00003165 def set_started_on_now(self):
3166 self.update_field('started_on', datetime.datetime.now())
3167
3168
3169 def is_hostless(self):
3170 return (self.host_id is None
3171 and self.meta_host is None
3172 and self.atomic_group_id is None)
3173
3174
mbligh36768f02008-02-22 18:28:33 +00003175class Job(DBObject):
showardeab66ce2009-12-23 00:03:56 +00003176 _table_name = 'afe_jobs'
showard6ae5ea92009-02-25 00:11:51 +00003177 _fields = ('id', 'owner', 'name', 'priority', 'control_file',
3178 'control_type', 'created_on', 'synch_count', 'timeout',
showarda1e74b32009-05-12 17:32:04 +00003179 'run_verify', 'email_list', 'reboot_before', 'reboot_after',
showard12f3e322009-05-13 21:27:42 +00003180 'parse_failed_repair', 'max_runtime_hrs')
showard6ae5ea92009-02-25 00:11:51 +00003181
showard77182562009-06-10 00:16:05 +00003182 # This does not need to be a column in the DB. The delays are likely to
3183 # be configured short. If the scheduler is stopped and restarted in
3184 # the middle of a job's delay cycle, the delay cycle will either be
3185 # repeated or skipped depending on the number of Pending machines found
3186 # when the restarted scheduler recovers to track it. Not a problem.
3187 #
3188 # A reference to the DelayedCallTask that will wake up the job should
3189 # no other HQEs change state in time. Its end_time attribute is used
3190 # by our run_with_ready_delay() method to determine if the wait is over.
3191 _delay_ready_task = None
3192
3193 # TODO(gps): On scheduler start/recovery we need to call HQE.on_pending() on
3194 # all status='Pending' atomic group HQEs incase a delay was running when the
3195 # scheduler was restarted and no more hosts ever successfully exit Verify.
showard6ae5ea92009-02-25 00:11:51 +00003196
showarda3c58572009-03-12 20:36:59 +00003197 def __init__(self, id=None, row=None, **kwargs):
jadmanski0afbb632008-06-06 21:10:57 +00003198 assert id or row
showarda3c58572009-03-12 20:36:59 +00003199 super(Job, self).__init__(id=id, row=row, **kwargs)
showard9bb960b2009-11-19 01:02:11 +00003200 self._owner_model = None # caches model instance of owner
3201
3202
3203 def owner_model(self):
3204 # work around the fact that the Job owner field is a string, not a
3205 # foreign key
3206 if not self._owner_model:
3207 self._owner_model = models.User.objects.get(login=self.owner)
3208 return self._owner_model
mbligh36768f02008-02-22 18:28:33 +00003209
mblighe2586682008-02-29 22:45:46 +00003210
jadmanski0afbb632008-06-06 21:10:57 +00003211 def is_server_job(self):
3212 return self.control_type != 2
mbligh36768f02008-02-22 18:28:33 +00003213
3214
showard170873e2009-01-07 00:22:26 +00003215 def tag(self):
3216 return "%s-%s" % (self.id, self.owner)
3217
3218
jadmanski0afbb632008-06-06 21:10:57 +00003219 def get_host_queue_entries(self):
3220 rows = _db.execute("""
showardeab66ce2009-12-23 00:03:56 +00003221 SELECT * FROM afe_host_queue_entries
jadmanski0afbb632008-06-06 21:10:57 +00003222 WHERE job_id= %s
3223 """, (self.id,))
3224 entries = [HostQueueEntry(row=i) for i in rows]
mbligh36768f02008-02-22 18:28:33 +00003225
jadmanski0afbb632008-06-06 21:10:57 +00003226 assert len(entries)>0
mbligh36768f02008-02-22 18:28:33 +00003227
jadmanski0afbb632008-06-06 21:10:57 +00003228 return entries
mbligh36768f02008-02-22 18:28:33 +00003229
3230
jadmanski0afbb632008-06-06 21:10:57 +00003231 def set_status(self, status, update_queues=False):
3232 self.update_field('status',status)
3233
3234 if update_queues:
3235 for queue_entry in self.get_host_queue_entries():
3236 queue_entry.set_status(status)
mbligh36768f02008-02-22 18:28:33 +00003237
3238
showard77182562009-06-10 00:16:05 +00003239 def _atomic_and_has_started(self):
3240 """
3241 @returns True if any of the HostQueueEntries associated with this job
3242 have entered the Status.STARTING state or beyond.
3243 """
3244 atomic_entries = models.HostQueueEntry.objects.filter(
3245 job=self.id, atomic_group__isnull=False)
3246 if atomic_entries.count() <= 0:
3247 return False
3248
showardaf8b4ca2009-06-16 18:47:26 +00003249 # These states may *only* be reached if Job.run() has been called.
3250 started_statuses = (models.HostQueueEntry.Status.STARTING,
3251 models.HostQueueEntry.Status.RUNNING,
3252 models.HostQueueEntry.Status.COMPLETED)
3253
3254 started_entries = atomic_entries.filter(status__in=started_statuses)
showard77182562009-06-10 00:16:05 +00003255 return started_entries.count() > 0
3256
3257
showard708b3522009-08-20 23:26:15 +00003258 def _hosts_assigned_count(self):
3259 """The number of HostQueueEntries assigned a Host for this job."""
3260 entries = models.HostQueueEntry.objects.filter(job=self.id,
3261 host__isnull=False)
3262 return entries.count()
3263
3264
showard77182562009-06-10 00:16:05 +00003265 def _pending_count(self):
3266 """The number of HostQueueEntries for this job in the Pending state."""
3267 pending_entries = models.HostQueueEntry.objects.filter(
3268 job=self.id, status=models.HostQueueEntry.Status.PENDING)
3269 return pending_entries.count()
3270
3271
showardd07a5f32009-12-07 19:36:20 +00003272 def _max_hosts_needed_to_run(self, atomic_group):
showardd2014822009-10-12 20:26:58 +00003273 """
3274 @param atomic_group: The AtomicGroup associated with this job that we
showardd07a5f32009-12-07 19:36:20 +00003275 are using to set an upper bound on the threshold.
3276 @returns The maximum number of HostQueueEntries assigned a Host before
showardd2014822009-10-12 20:26:58 +00003277 this job can run.
3278 """
3279 return min(self._hosts_assigned_count(),
3280 atomic_group.max_number_of_machines)
3281
3282
showardd07a5f32009-12-07 19:36:20 +00003283 def _min_hosts_needed_to_run(self):
3284 """Return the minumum number of hsots needed to run this job."""
3285 return self.synch_count
3286
3287
jadmanski0afbb632008-06-06 21:10:57 +00003288 def is_ready(self):
showard77182562009-06-10 00:16:05 +00003289 # NOTE: Atomic group jobs stop reporting ready after they have been
3290 # started to avoid launching multiple copies of one atomic job.
3291 # Only possible if synch_count is less than than half the number of
3292 # machines in the atomic group.
showardb000a8d2009-07-28 20:02:07 +00003293 pending_count = self._pending_count()
3294 atomic_and_has_started = self._atomic_and_has_started()
3295 ready = (pending_count >= self.synch_count
showardd2014822009-10-12 20:26:58 +00003296 and not atomic_and_has_started)
showardb000a8d2009-07-28 20:02:07 +00003297
3298 if not ready:
3299 logging.info(
3300 'Job %s not ready: %s pending, %s required '
3301 '(Atomic and started: %s)',
3302 self, pending_count, self.synch_count,
3303 atomic_and_has_started)
3304
3305 return ready
mbligh36768f02008-02-22 18:28:33 +00003306
3307
jadmanski0afbb632008-06-06 21:10:57 +00003308 def num_machines(self, clause = None):
3309 sql = "job_id=%s" % self.id
3310 if clause:
3311 sql += " AND (%s)" % clause
showardeab66ce2009-12-23 00:03:56 +00003312 return self.count(sql, table='afe_host_queue_entries')
mbligh36768f02008-02-22 18:28:33 +00003313
3314
jadmanski0afbb632008-06-06 21:10:57 +00003315 def num_queued(self):
3316 return self.num_machines('not complete')
mbligh36768f02008-02-22 18:28:33 +00003317
3318
jadmanski0afbb632008-06-06 21:10:57 +00003319 def num_active(self):
3320 return self.num_machines('active')
mbligh36768f02008-02-22 18:28:33 +00003321
3322
jadmanski0afbb632008-06-06 21:10:57 +00003323 def num_complete(self):
3324 return self.num_machines('complete')
mbligh36768f02008-02-22 18:28:33 +00003325
3326
jadmanski0afbb632008-06-06 21:10:57 +00003327 def is_finished(self):
showardc85c21b2008-11-24 22:17:37 +00003328 return self.num_complete() == self.num_machines()
mbligh36768f02008-02-22 18:28:33 +00003329
mbligh36768f02008-02-22 18:28:33 +00003330
showard6bb7c292009-01-30 01:44:51 +00003331 def _not_yet_run_entries(self, include_verifying=True):
3332 statuses = [models.HostQueueEntry.Status.QUEUED,
3333 models.HostQueueEntry.Status.PENDING]
3334 if include_verifying:
3335 statuses.append(models.HostQueueEntry.Status.VERIFYING)
3336 return models.HostQueueEntry.objects.filter(job=self.id,
3337 status__in=statuses)
3338
3339
3340 def _stop_all_entries(self):
3341 entries_to_stop = self._not_yet_run_entries(
3342 include_verifying=False)
3343 for child_entry in entries_to_stop:
showard4f9e5372009-01-07 21:33:38 +00003344 assert not child_entry.complete, (
3345 '%s status=%s, active=%s, complete=%s' %
3346 (child_entry.id, child_entry.status, child_entry.active,
3347 child_entry.complete))
showard2bab8f42008-11-12 18:15:22 +00003348 if child_entry.status == models.HostQueueEntry.Status.PENDING:
3349 child_entry.host.status = models.Host.Status.READY
3350 child_entry.host.save()
3351 child_entry.status = models.HostQueueEntry.Status.STOPPED
3352 child_entry.save()
3353
showard2bab8f42008-11-12 18:15:22 +00003354 def stop_if_necessary(self):
showard6bb7c292009-01-30 01:44:51 +00003355 not_yet_run = self._not_yet_run_entries()
showard2bab8f42008-11-12 18:15:22 +00003356 if not_yet_run.count() < self.synch_count:
showard6bb7c292009-01-30 01:44:51 +00003357 self._stop_all_entries()
mblighe2586682008-02-29 22:45:46 +00003358
3359
jadmanski0afbb632008-06-06 21:10:57 +00003360 def write_to_machines_file(self, queue_entry):
showarda9545c02009-12-18 22:44:26 +00003361 hostname = queue_entry.host.hostname
showard170873e2009-01-07 00:22:26 +00003362 file_path = os.path.join(self.tag(), '.machines')
3363 _drone_manager.write_lines_to_file(file_path, [hostname])
mbligh36768f02008-02-22 18:28:33 +00003364
3365
showardf1ae3542009-05-11 19:26:02 +00003366 def _next_group_name(self, group_name=''):
3367 """@returns a directory name to use for the next host group results."""
3368 if group_name:
3369 # Sanitize for use as a pathname.
3370 group_name = group_name.replace(os.path.sep, '_')
3371 if group_name.startswith('.'):
3372 group_name = '_' + group_name[1:]
3373 # Add a separator between the group name and 'group%d'.
3374 group_name += '.'
3375 group_count_re = re.compile(r'%sgroup(\d+)' % re.escape(group_name))
showard2bab8f42008-11-12 18:15:22 +00003376 query = models.HostQueueEntry.objects.filter(
3377 job=self.id).values('execution_subdir').distinct()
3378 subdirs = (entry['execution_subdir'] for entry in query)
showardf1ae3542009-05-11 19:26:02 +00003379 group_matches = (group_count_re.match(subdir) for subdir in subdirs)
3380 ids = [int(match.group(1)) for match in group_matches if match]
showard2bab8f42008-11-12 18:15:22 +00003381 if ids:
3382 next_id = max(ids) + 1
3383 else:
3384 next_id = 0
showardf1ae3542009-05-11 19:26:02 +00003385 return '%sgroup%d' % (group_name, next_id)
showard2bab8f42008-11-12 18:15:22 +00003386
3387
showarddb502762009-09-09 15:31:20 +00003388 def _write_control_file(self, execution_path):
showard170873e2009-01-07 00:22:26 +00003389 control_path = _drone_manager.attach_file_to_execution(
showarddb502762009-09-09 15:31:20 +00003390 execution_path, self.control_file)
showard170873e2009-01-07 00:22:26 +00003391 return control_path
mbligh36768f02008-02-22 18:28:33 +00003392
showardb2e2c322008-10-14 17:33:55 +00003393
showard2bab8f42008-11-12 18:15:22 +00003394 def get_group_entries(self, queue_entry_from_group):
showard8375ce02009-10-12 20:35:13 +00003395 """
3396 @param queue_entry_from_group: A HostQueueEntry instance to find other
3397 group entries on this job for.
3398
3399 @returns A list of HostQueueEntry objects all executing this job as
3400 part of the same group as the one supplied (having the same
3401 execution_subdir).
3402 """
showard2bab8f42008-11-12 18:15:22 +00003403 execution_subdir = queue_entry_from_group.execution_subdir
showarde788ea62008-11-17 21:02:47 +00003404 return list(HostQueueEntry.fetch(
3405 where='job_id=%s AND execution_subdir=%s',
3406 params=(self.id, execution_subdir)))
showard2bab8f42008-11-12 18:15:22 +00003407
3408
showard8cc058f2009-09-08 16:26:33 +00003409 def get_autoserv_params(self, queue_entries):
showard170873e2009-01-07 00:22:26 +00003410 assert queue_entries
showarddb502762009-09-09 15:31:20 +00003411 execution_path = queue_entries[0].execution_path()
3412 control_path = self._write_control_file(execution_path)
showarda9545c02009-12-18 22:44:26 +00003413 hostnames = ','.join(entry.host.hostname
3414 for entry in queue_entries
3415 if not entry.is_hostless())
mbligh36768f02008-02-22 18:28:33 +00003416
showarddb502762009-09-09 15:31:20 +00003417 execution_tag = queue_entries[0].execution_tag()
showard87ba02a2009-04-20 19:37:32 +00003418 params = _autoserv_command_line(
showarded2afea2009-07-07 20:54:07 +00003419 hostnames,
showard87ba02a2009-04-20 19:37:32 +00003420 ['-P', execution_tag, '-n',
3421 _drone_manager.absolute_path(control_path)],
showarde9c69362009-06-30 01:58:03 +00003422 job=self, verbose=False)
mbligh36768f02008-02-22 18:28:33 +00003423
jadmanski0afbb632008-06-06 21:10:57 +00003424 if not self.is_server_job():
3425 params.append('-c')
mbligh36768f02008-02-22 18:28:33 +00003426
showardb2e2c322008-10-14 17:33:55 +00003427 return params
mblighe2586682008-02-29 22:45:46 +00003428
mbligh36768f02008-02-22 18:28:33 +00003429
showardc9ae1782009-01-30 01:42:37 +00003430 def _should_run_cleanup(self, queue_entry):
showard0fc38302008-10-23 00:44:07 +00003431 if self.reboot_before == models.RebootBefore.ALWAYS:
showardc9ae1782009-01-30 01:42:37 +00003432 return True
showard0fc38302008-10-23 00:44:07 +00003433 elif self.reboot_before == models.RebootBefore.IF_DIRTY:
showarda9545c02009-12-18 22:44:26 +00003434 return queue_entry.host.dirty
showardc9ae1782009-01-30 01:42:37 +00003435 return False
showard21baa452008-10-21 00:08:39 +00003436
showardc9ae1782009-01-30 01:42:37 +00003437
showard8cc058f2009-09-08 16:26:33 +00003438 def _should_run_verify(self, queue_entry):
showardc9ae1782009-01-30 01:42:37 +00003439 do_not_verify = (queue_entry.host.protection ==
3440 host_protections.Protection.DO_NOT_VERIFY)
3441 if do_not_verify:
3442 return False
3443 return self.run_verify
3444
3445
showard8cc058f2009-09-08 16:26:33 +00003446 def schedule_pre_job_tasks(self, queue_entry):
showard77182562009-06-10 00:16:05 +00003447 """
3448 Get a list of tasks to perform before the host_queue_entry
3449 may be used to run this Job (such as Cleanup & Verify).
3450
3451 @returns A list of tasks to be done to the given queue_entry before
mbligh6fbdb802009-08-03 16:42:55 +00003452 it should be considered be ready to run this job. The last
showard77182562009-06-10 00:16:05 +00003453 task in the list calls HostQueueEntry.on_pending(), which
3454 continues the flow of the job.
3455 """
showardc9ae1782009-01-30 01:42:37 +00003456 if self._should_run_cleanup(queue_entry):
showard8cc058f2009-09-08 16:26:33 +00003457 task = models.SpecialTask.Task.CLEANUP
3458 elif self._should_run_verify(queue_entry):
3459 task = models.SpecialTask.Task.VERIFY
3460 else:
3461 queue_entry.on_pending()
3462 return
3463
showard9bb960b2009-11-19 01:02:11 +00003464 queue_entry = models.HostQueueEntry.objects.get(id=queue_entry.id)
showard8cc058f2009-09-08 16:26:33 +00003465 models.SpecialTask.objects.create(
showard9bb960b2009-11-19 01:02:11 +00003466 host=models.Host.objects.get(id=queue_entry.host_id),
3467 queue_entry=queue_entry, task=task)
showard21baa452008-10-21 00:08:39 +00003468
3469
showardf1ae3542009-05-11 19:26:02 +00003470 def _assign_new_group(self, queue_entries, group_name=''):
showard2bab8f42008-11-12 18:15:22 +00003471 if len(queue_entries) == 1:
showarda9545c02009-12-18 22:44:26 +00003472 group_subdir_name = queue_entries[0].host.hostname
showard2bab8f42008-11-12 18:15:22 +00003473 else:
showardf1ae3542009-05-11 19:26:02 +00003474 group_subdir_name = self._next_group_name(group_name)
showardb18134f2009-03-20 20:52:18 +00003475 logging.info('Running synchronous job %d hosts %s as %s',
showard2bab8f42008-11-12 18:15:22 +00003476 self.id, [entry.host.hostname for entry in queue_entries],
showardf1ae3542009-05-11 19:26:02 +00003477 group_subdir_name)
showard2bab8f42008-11-12 18:15:22 +00003478
3479 for queue_entry in queue_entries:
showardf1ae3542009-05-11 19:26:02 +00003480 queue_entry.set_execution_subdir(group_subdir_name)
showard2bab8f42008-11-12 18:15:22 +00003481
3482
3483 def _choose_group_to_run(self, include_queue_entry):
showardf1ae3542009-05-11 19:26:02 +00003484 """
3485 @returns A tuple containing a list of HostQueueEntry instances to be
3486 used to run this Job, a string group name to suggest giving
showard54c1ea92009-05-20 00:32:58 +00003487 to this job in the results database.
showardf1ae3542009-05-11 19:26:02 +00003488 """
showard77182562009-06-10 00:16:05 +00003489 atomic_group = include_queue_entry.atomic_group
showard2bab8f42008-11-12 18:15:22 +00003490 chosen_entries = [include_queue_entry]
showardf1ae3542009-05-11 19:26:02 +00003491 if atomic_group:
3492 num_entries_wanted = atomic_group.max_number_of_machines
3493 else:
3494 num_entries_wanted = self.synch_count
3495 num_entries_wanted -= len(chosen_entries)
showard2bab8f42008-11-12 18:15:22 +00003496
showardf1ae3542009-05-11 19:26:02 +00003497 if num_entries_wanted > 0:
3498 where_clause = 'job_id = %s AND status = "Pending" AND id != %s'
showard54c1ea92009-05-20 00:32:58 +00003499 pending_entries = list(HostQueueEntry.fetch(
showardf1ae3542009-05-11 19:26:02 +00003500 where=where_clause,
showard54c1ea92009-05-20 00:32:58 +00003501 params=(self.id, include_queue_entry.id)))
3502
3503 # Sort the chosen hosts by hostname before slicing.
3504 def cmp_queue_entries_by_hostname(entry_a, entry_b):
3505 return Host.cmp_for_sort(entry_a.host, entry_b.host)
3506 pending_entries.sort(cmp=cmp_queue_entries_by_hostname)
3507 chosen_entries += pending_entries[:num_entries_wanted]
showard2bab8f42008-11-12 18:15:22 +00003508
showardf1ae3542009-05-11 19:26:02 +00003509 # Sanity check. We'll only ever be called if this can be met.
showard828fc4c2009-09-14 20:31:00 +00003510 if len(chosen_entries) < self.synch_count:
3511 message = ('job %s got less than %s chosen entries: %s' % (
3512 self.id, self.synch_count, chosen_entries))
3513 logging.error(message)
3514 email_manager.manager.enqueue_notify_email(
3515 'Job not started, too few chosen entries', message)
3516 return []
showardf1ae3542009-05-11 19:26:02 +00003517
showard8cc058f2009-09-08 16:26:33 +00003518 group_name = include_queue_entry.get_group_name()
showardf1ae3542009-05-11 19:26:02 +00003519
3520 self._assign_new_group(chosen_entries, group_name=group_name)
showard8cc058f2009-09-08 16:26:33 +00003521 return chosen_entries
showard2bab8f42008-11-12 18:15:22 +00003522
3523
showard77182562009-06-10 00:16:05 +00003524 def run_if_ready(self, queue_entry):
3525 """
showard8375ce02009-10-12 20:35:13 +00003526 Run this job by kicking its HQEs into status='Starting' if enough
3527 hosts are ready for it to run.
3528
3529 Cleans up by kicking HQEs into status='Stopped' if this Job is not
3530 ready to run.
showard77182562009-06-10 00:16:05 +00003531 """
showardb2e2c322008-10-14 17:33:55 +00003532 if not self.is_ready():
showard77182562009-06-10 00:16:05 +00003533 self.stop_if_necessary()
showard8cc058f2009-09-08 16:26:33 +00003534 elif queue_entry.atomic_group:
3535 self.run_with_ready_delay(queue_entry)
3536 else:
3537 self.run(queue_entry)
showard77182562009-06-10 00:16:05 +00003538
3539
3540 def run_with_ready_delay(self, queue_entry):
3541 """
3542 Start a delay to wait for more hosts to enter Pending state before
3543 launching an atomic group job. Once set, the a delay cannot be reset.
3544
3545 @param queue_entry: The HostQueueEntry object to get atomic group
3546 info from and pass to run_if_ready when the delay is up.
3547
3548 @returns An Agent to run the job as appropriate or None if a delay
3549 has already been set.
3550 """
3551 assert queue_entry.job_id == self.id
3552 assert queue_entry.atomic_group
3553 delay = scheduler_config.config.secs_to_wait_for_atomic_group_hosts
showardd2014822009-10-12 20:26:58 +00003554 over_max_threshold = (self._pending_count() >=
showardd07a5f32009-12-07 19:36:20 +00003555 self._max_hosts_needed_to_run(queue_entry.atomic_group))
showard77182562009-06-10 00:16:05 +00003556 delay_expired = (self._delay_ready_task and
3557 time.time() >= self._delay_ready_task.end_time)
3558
3559 # Delay is disabled or we already have enough? Do not wait to run.
3560 if not delay or over_max_threshold or delay_expired:
showard8cc058f2009-09-08 16:26:33 +00003561 self.run(queue_entry)
3562 else:
3563 queue_entry.set_status(models.HostQueueEntry.Status.WAITING)
showard77182562009-06-10 00:16:05 +00003564
showard8cc058f2009-09-08 16:26:33 +00003565
showardd07a5f32009-12-07 19:36:20 +00003566 def request_abort(self):
3567 """Request that this Job be aborted on the next scheduler cycle."""
3568 queue_entries = HostQueueEntry.fetch(where="job_id=%s" % self.id)
3569 for hqe in queue_entries:
3570 hqe.update_field('aborted', True)
3571
3572
showard8cc058f2009-09-08 16:26:33 +00003573 def schedule_delayed_callback_task(self, queue_entry):
3574 queue_entry.set_status(models.HostQueueEntry.Status.PENDING)
3575
showard77182562009-06-10 00:16:05 +00003576 if self._delay_ready_task:
3577 return None
3578
showard8cc058f2009-09-08 16:26:33 +00003579 delay = scheduler_config.config.secs_to_wait_for_atomic_group_hosts
3580
showard77182562009-06-10 00:16:05 +00003581 def run_job_after_delay():
showardd2014822009-10-12 20:26:58 +00003582 logging.info('Job %s done waiting for extra hosts.', self)
3583 # Check to see if the job is still relevant. It could have aborted
3584 # while we were waiting or hosts could have disappearred, etc.
showardd07a5f32009-12-07 19:36:20 +00003585 if self._pending_count() < self._min_hosts_needed_to_run():
showardd2014822009-10-12 20:26:58 +00003586 logging.info('Job %s had too few Pending hosts after waiting '
3587 'for extras. Not running.', self)
showardd07a5f32009-12-07 19:36:20 +00003588 self.request_abort()
showardd2014822009-10-12 20:26:58 +00003589 return
showard77182562009-06-10 00:16:05 +00003590 return self.run(queue_entry)
3591
showard708b3522009-08-20 23:26:15 +00003592 logging.info('Job %s waiting up to %s seconds for more hosts.',
3593 self.id, delay)
showard77182562009-06-10 00:16:05 +00003594 self._delay_ready_task = DelayedCallTask(delay_seconds=delay,
3595 callback=run_job_after_delay)
showard8cc058f2009-09-08 16:26:33 +00003596 return self._delay_ready_task
showard77182562009-06-10 00:16:05 +00003597
3598
3599 def run(self, queue_entry):
3600 """
3601 @param queue_entry: The HostQueueEntry instance calling this method.
showard77182562009-06-10 00:16:05 +00003602 """
3603 if queue_entry.atomic_group and self._atomic_and_has_started():
3604 logging.error('Job.run() called on running atomic Job %d '
3605 'with HQE %s.', self.id, queue_entry)
showard8cc058f2009-09-08 16:26:33 +00003606 return
3607 queue_entries = self._choose_group_to_run(queue_entry)
showard828fc4c2009-09-14 20:31:00 +00003608 if queue_entries:
3609 self._finish_run(queue_entries)
showardb2e2c322008-10-14 17:33:55 +00003610
3611
showard8cc058f2009-09-08 16:26:33 +00003612 def _finish_run(self, queue_entries):
showardb2ccdda2008-10-28 20:39:05 +00003613 for queue_entry in queue_entries:
showard8cc058f2009-09-08 16:26:33 +00003614 queue_entry.set_status(models.HostQueueEntry.Status.STARTING)
showardd2014822009-10-12 20:26:58 +00003615 self.abort_delay_ready_task()
3616
3617
3618 def abort_delay_ready_task(self):
3619 """Abort the delayed task associated with this job, if any."""
showard77182562009-06-10 00:16:05 +00003620 if self._delay_ready_task:
3621 # Cancel any pending callback that would try to run again
3622 # as we are already running.
3623 self._delay_ready_task.abort()
showardb2e2c322008-10-14 17:33:55 +00003624
showardd2014822009-10-12 20:26:58 +00003625
showardb000a8d2009-07-28 20:02:07 +00003626 def __str__(self):
3627 return '%s-%s' % (self.id, self.owner)
3628
3629
mbligh36768f02008-02-22 18:28:33 +00003630if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +00003631 main()