blob: d3bebdcfa6b6524402c6b6afec4addd2fe32568e [file] [log] [blame]
Chris Masone2d61ca22012-04-02 16:52:46 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Dan Shi8d7f3562016-01-11 10:55:46 -08005import contextlib
Dan Shi15d42312015-12-15 15:37:28 -08006import logging
7import time
8from multiprocessing import pool
Chris Masone2d61ca22012-04-02 16:52:46 -07009
Dan Shi8446fac2016-03-02 22:07:39 -080010import base_event, board_enumerator, build_event, deduping_scheduler
Aviv Keshet4e7722b2013-02-14 15:07:46 -080011import task, timed_event
Chris Masone2d61ca22012-04-02 16:52:46 -070012
Aviv Keshet4e7722b2013-02-14 15:07:46 -080013import common
Dan Shi098d1e22015-09-02 10:00:24 -070014from autotest_lib.client.common_lib.cros.graphite import autotest_stats
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080015from autotest_lib.server import utils
Chris Masone2d61ca22012-04-02 16:52:46 -070016
Dan Shi15d42312015-12-15 15:37:28 -080017POOL_SIZE = 32
Dan Shi098d1e22015-09-02 10:00:24 -070018
19_timer = autotest_stats.Timer('suite_scheduler')
20
Chris Masone2d61ca22012-04-02 16:52:46 -070021class Driver(object):
22 """Implements the main loop of the suite_scheduler.
23
Chris Masonebf8775a2012-09-10 10:44:18 -070024 @var EVENT_CLASSES: list of the event classes Driver supports.
Chris Masonefe5a5092012-04-11 18:29:07 -070025 @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations.
Chris Masone2d61ca22012-04-02 16:52:46 -070026
27 @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
Chris Masone3fba86f2012-04-03 10:06:56 -070028 @var _enumerator: a BoardEnumerator, used to list plaforms known to
Chris Masone2d61ca22012-04-02 16:52:46 -070029 the AFE
Chris Masone855d86f2012-05-07 13:48:07 -070030 @var _events: dict of BaseEvents to be handled each time through main loop.
Chris Masone2d61ca22012-04-02 16:52:46 -070031 """
32
Chris Masonebf8775a2012-09-10 10:44:18 -070033 EVENT_CLASSES = [timed_event.Nightly, timed_event.Weekly,
34 build_event.NewBuild]
Chris Masonefe5a5092012-04-11 18:29:07 -070035 _LOOP_INTERVAL_SECONDS = 5 * 60
Chris Masone2d61ca22012-04-02 16:52:46 -070036
37
Dan Shi23245142015-01-22 13:22:28 -080038 def __init__(self, scheduler, enumerator, is_sanity=False):
Chris Masone2d61ca22012-04-02 16:52:46 -070039 """Constructor
40
Chris Masone67f06d62012-04-12 15:16:56 -070041 @param scheduler: an instance of deduping_scheduler.DedupingScheduler.
42 @param enumerator: an instance of board_enumerator.BoardEnumerator.
Dan Shi23245142015-01-22 13:22:28 -080043 @param is_sanity: Set to True if the driver is created for sanity check.
44 Default is set to False.
Chris Masone2d61ca22012-04-02 16:52:46 -070045 """
Chris Masone67f06d62012-04-12 15:16:56 -070046 self._scheduler = scheduler
47 self._enumerator = enumerator
Dan Shi23245142015-01-22 13:22:28 -080048 task.TotMilestoneManager.is_sanity = is_sanity
Chris Masone2d61ca22012-04-02 16:52:46 -070049
Chris Masone2d61ca22012-04-02 16:52:46 -070050
Chris Masone855d86f2012-05-07 13:48:07 -070051 def RereadAndReprocessConfig(self, config, mv):
52 """Re-read config, re-populate self._events and recreate task lists.
53
54 @param config: an instance of ForgivingConfigParser.
55 @param mv: an instance of ManifestVersions.
56 """
57 config.reread()
58 new_events = self._CreateEventsWithTasks(config, mv)
59 for keyword, event in self._events.iteritems():
60 event.Merge(new_events[keyword])
61
62
Chris Masone93f51d42012-04-18 08:46:52 -070063 def SetUpEventsAndTasks(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -070064 """Populate self._events and create task lists from config.
65
Chris Masone96f16632012-04-04 18:36:03 -070066 @param config: an instance of ForgivingConfigParser.
Chris Masone93f51d42012-04-18 08:46:52 -070067 @param mv: an instance of ManifestVersions.
Chris Masone96f16632012-04-04 18:36:03 -070068 """
Chris Masone855d86f2012-05-07 13:48:07 -070069 self._events = self._CreateEventsWithTasks(config, mv)
70
71
72 def _CreateEventsWithTasks(self, config, mv):
73 """Create task lists from config, and assign to newly-minted events.
74
75 Calling multiple times should start afresh each time.
76
77 @param config: an instance of ForgivingConfigParser.
78 @param mv: an instance of ManifestVersions.
79 """
Chris Masone855d86f2012-05-07 13:48:07 -070080 events = {}
Chris Masonebf8775a2012-09-10 10:44:18 -070081 for klass in self.EVENT_CLASSES:
Chris Masone855d86f2012-05-07 13:48:07 -070082 events[klass.KEYWORD] = klass.CreateFromConfig(config, mv)
Chris Masone96f16632012-04-04 18:36:03 -070083
84 tasks = self.TasksFromConfig(config)
Chris Masone645c7e42012-05-17 17:28:40 -070085 for keyword, task_list in tasks.iteritems():
86 if keyword in events:
87 events[keyword].tasks = task_list
88 else:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -070089 logging.warning('%s, is an unknown keyword.', keyword)
Chris Masone855d86f2012-05-07 13:48:07 -070090 return events
Chris Masone96f16632012-04-04 18:36:03 -070091
92
93 def TasksFromConfig(self, config):
94 """Generate a dict of {event_keyword: [tasks]} mappings from |config|.
95
96 For each section in |config| that encodes a Task, instantiate a Task
97 object. Determine the event that Task is supposed to run_on and
98 append the object to a list associated with the appropriate event
99 keyword. Return a dictionary of these keyword: list of task mappings.
100
101 @param config: a ForgivingConfigParser containing tasks to be parsed.
102 @return dict of {event_keyword: [tasks]} mappings.
103 @raise MalformedConfigEntry on a task parsing error.
104 """
105 tasks = {}
106 for section in config.sections():
Chris Masone93f51d42012-04-18 08:46:52 -0700107 if not base_event.HonoredSection(section):
Chris Masone96f16632012-04-04 18:36:03 -0700108 try:
109 keyword, new_task = task.Task.CreateFromConfigSection(
110 config, section)
111 except task.MalformedConfigEntry as e:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700112 logging.warning('%s is malformed: %s', section, e)
Chris Masone96f16632012-04-04 18:36:03 -0700113 continue
114 tasks.setdefault(keyword, []).append(new_task)
115 return tasks
Chris Masone2d61ca22012-04-02 16:52:46 -0700116
117
Chris Masone855d86f2012-05-07 13:48:07 -0700118 def RunForever(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -0700119 """Main loop of the scheduler. Runs til the process is killed.
120
Chris Masone855d86f2012-05-07 13:48:07 -0700121 @param config: an instance of ForgivingConfigParser.
Chris Masone67f06d62012-04-12 15:16:56 -0700122 @param mv: an instance of manifest_versions.ManifestVersions.
123 """
Chris Masone855d86f2012-05-07 13:48:07 -0700124 for event in self._events.itervalues():
Chris Masone73a78382012-04-20 13:25:51 -0700125 event.Prepare()
Chris Masone2d61ca22012-04-02 16:52:46 -0700126 while True:
Chris Masone645c7e42012-05-17 17:28:40 -0700127 try:
128 self.HandleEventsOnce(mv)
Scott Zawalskic15c6b42012-07-09 13:16:05 -0400129 except board_enumerator.EnumeratorException as e:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700130 logging.warning('Failed to enumerate boards: %r', e)
Dan Shi098d1e22015-09-02 10:00:24 -0700131 with _timer.get_client('manifest_versions_update'):
132 mv.Update()
133 with _timer.get_client('tot_milestone_manager_refresh'):
134 task.TotMilestoneManager().refresh()
Chris Masonefe5a5092012-04-11 18:29:07 -0700135 time.sleep(self._LOOP_INTERVAL_SECONDS)
Chris Masone855d86f2012-05-07 13:48:07 -0700136 self.RereadAndReprocessConfig(config, mv)
Chris Masone2d61ca22012-04-02 16:52:46 -0700137
138
Dan Shi15d42312015-12-15 15:37:28 -0800139 @staticmethod
140 def HandleBoard(inputs):
141 """Handle event based on given inputs.
142
143 @param inputs: A dictionary of the arguments needed to handle an event.
144 Keys include:
145 scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
146 event: An event object to be handled.
147 board: Name of the board.
148 """
149 scheduler = inputs['scheduler']
150 event = inputs['event']
151 board = inputs['board']
152
153 logging.info('Handling %s event for board %s', event.keyword, board)
Dan Shi2121a332016-02-25 14:22:22 -0800154 os_type, _ = utils.parse_android_board_label(board)
155 if os_type in {task.OS_TYPE_BRILLO, task.OS_TYPE_ANDROID}:
156 launch_control_builds = event.GetLaunchControlBuildsForBoard(board)
157 event.Handle(scheduler, branch_builds=None, board=board,
158 launch_control_builds=launch_control_builds)
159 else:
160 branch_builds = event.GetBranchBuildsForBoard(board)
161 event.Handle(scheduler, branch_builds, board)
Dan Shi15d42312015-12-15 15:37:28 -0800162 logging.info('Finished handling %s event for board %s', event.keyword,
163 board)
164
165
Dan Shi098d1e22015-09-02 10:00:24 -0700166 @_timer.decorate
Chris Masone67f06d62012-04-12 15:16:56 -0700167 def HandleEventsOnce(self, mv):
168 """One turn through the loop. Separated out for unit testing.
169
170 @param mv: an instance of manifest_versions.ManifestVersions.
Chris Masone645c7e42012-05-17 17:28:40 -0700171 @raise EnumeratorException if we can't enumerate any supported boards.
Chris Masone67f06d62012-04-12 15:16:56 -0700172 """
Chris Masone92874d32012-04-03 10:13:04 -0700173 boards = self._enumerator.Enumerate()
Dan Shi15d42312015-12-15 15:37:28 -0800174 logging.info('%d boards currently in the lab: %r', len(boards), boards)
175 thread_pool = pool.ThreadPool(POOL_SIZE)
Dan Shi8d7f3562016-01-11 10:55:46 -0800176 with contextlib.closing(thread_pool):
177 for e in self._events.itervalues():
178 if not e.ShouldHandle():
179 continue
Dan Shi8446fac2016-03-02 22:07:39 -0800180 # Reset the value of delay_minutes, as this is the beginning of
181 # handling an event for all boards.
182 self._scheduler.delay_minutes = 0
183 self._scheduler.delay_minutes_interval = (
184 deduping_scheduler.DELAY_MINUTES_INTERVAL)
Dan Shi15d42312015-12-15 15:37:28 -0800185 logging.info('Handling %s event for %d boards', e.keyword,
186 len(boards))
187 args = []
Chris Masone96f16632012-04-04 18:36:03 -0700188 for board in boards:
Dan Shi15d42312015-12-15 15:37:28 -0800189 args.append({'scheduler': self._scheduler,
190 'event': e,
191 'board': board})
192 thread_pool.map(self.HandleBoard, args)
193 logging.info('Finished handling %s event for %d boards',
194 e.keyword, len(boards))
Chris Masonebbde3862012-05-07 14:29:51 -0700195 e.UpdateCriteria()
Chris Masone67f06d62012-04-12 15:16:56 -0700196
197
Dan Shi2121a332016-02-25 14:22:22 -0800198 def ForceEventsOnceForBuild(self, keywords, build_name,
199 os_type=task.OS_TYPE_CROS):
Chris Masone67f06d62012-04-12 15:16:56 -0700200 """Force events with provided keywords to happen, with given build.
201
202 @param keywords: iterable of event keywords to force
203 @param build_name: instead of looking up builds to test, test this one.
Dan Shi2121a332016-02-25 14:22:22 -0800204 @param os_type: Type of the OS to test, default to cros.
Chris Masone67f06d62012-04-12 15:16:56 -0700205 """
Dan Shi2121a332016-02-25 14:22:22 -0800206 branch_builds = None
207 launch_control_builds = None
208 if os_type == task.OS_TYPE_CROS:
209 board, type, milestone, manifest = utils.ParseBuildName(build_name)
210 branch_builds = {task.PickBranchName(type, milestone): [build_name]}
211 logging.info('Testing build R%s-%s on %s', milestone, manifest,
212 board)
213 else:
214 logging.info('Build is not a ChromeOS build, try to parse as a '
215 'Launch Control build.')
Dan Shi6450e142016-03-11 11:52:20 -0800216 _,target,_ = utils.parse_launch_control_build(build_name)
Dan Shi2121a332016-02-25 14:22:22 -0800217 board = '%s-%s' % (os_type, utils.parse_launch_control_target(target)[0])
218 launch_control_builds = [build_name]
219 logging.info('Testing Launch Control build %s on %s', build_name,
220 board)
Chris Masone67f06d62012-04-12 15:16:56 -0700221
Chris Masone855d86f2012-05-07 13:48:07 -0700222 for e in self._events.itervalues():
Chris Masone67f06d62012-04-12 15:16:56 -0700223 if e.keyword in keywords:
Dan Shi2121a332016-02-25 14:22:22 -0800224 e.Handle(self._scheduler, branch_builds, board, force=True,
225 launch_control_builds=launch_control_builds)