blob: 68eb2822d36212c36cda2f1eb14338732db02e66 [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
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080014from autotest_lib.server import utils
Chris Masone2d61ca22012-04-02 16:52:46 -070015
Dan Shic3fc5992016-12-08 11:12:30 -080016from chromite.lib import metrics
Dan Shi098d1e22015-09-02 10:00:24 -070017
Dan Shic3fc5992016-12-08 11:12:30 -080018POOL_SIZE = 32
Dan Shi098d1e22015-09-02 10:00:24 -070019
Chris Masone2d61ca22012-04-02 16:52:46 -070020class Driver(object):
21 """Implements the main loop of the suite_scheduler.
22
Chris Masonebf8775a2012-09-10 10:44:18 -070023 @var EVENT_CLASSES: list of the event classes Driver supports.
Chris Masonefe5a5092012-04-11 18:29:07 -070024 @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations.
Chris Masone2d61ca22012-04-02 16:52:46 -070025
26 @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
Chris Masone3fba86f2012-04-03 10:06:56 -070027 @var _enumerator: a BoardEnumerator, used to list plaforms known to
Chris Masone2d61ca22012-04-02 16:52:46 -070028 the AFE
Chris Masone855d86f2012-05-07 13:48:07 -070029 @var _events: dict of BaseEvents to be handled each time through main loop.
Chris Masone2d61ca22012-04-02 16:52:46 -070030 """
31
Chris Masonebf8775a2012-09-10 10:44:18 -070032 EVENT_CLASSES = [timed_event.Nightly, timed_event.Weekly,
33 build_event.NewBuild]
Chris Masonefe5a5092012-04-11 18:29:07 -070034 _LOOP_INTERVAL_SECONDS = 5 * 60
Chris Masone2d61ca22012-04-02 16:52:46 -070035
Dan Shifa705d22016-07-28 16:16:15 -070036 # Cache for known ChromeOS boards. The cache helps to avoid unnecessary
37 # repeated calls to Launch Control API.
38 _cros_boards = set()
Chris Masone2d61ca22012-04-02 16:52:46 -070039
Dan Shi23245142015-01-22 13:22:28 -080040 def __init__(self, scheduler, enumerator, is_sanity=False):
Chris Masone2d61ca22012-04-02 16:52:46 -070041 """Constructor
42
Chris Masone67f06d62012-04-12 15:16:56 -070043 @param scheduler: an instance of deduping_scheduler.DedupingScheduler.
44 @param enumerator: an instance of board_enumerator.BoardEnumerator.
Dan Shi23245142015-01-22 13:22:28 -080045 @param is_sanity: Set to True if the driver is created for sanity check.
46 Default is set to False.
Chris Masone2d61ca22012-04-02 16:52:46 -070047 """
Chris Masone67f06d62012-04-12 15:16:56 -070048 self._scheduler = scheduler
49 self._enumerator = enumerator
Dan Shi23245142015-01-22 13:22:28 -080050 task.TotMilestoneManager.is_sanity = is_sanity
Chris Masone2d61ca22012-04-02 16:52:46 -070051
Chris Masone2d61ca22012-04-02 16:52:46 -070052
Chris Masone855d86f2012-05-07 13:48:07 -070053 def RereadAndReprocessConfig(self, config, mv):
54 """Re-read config, re-populate self._events and recreate task lists.
55
56 @param config: an instance of ForgivingConfigParser.
57 @param mv: an instance of ManifestVersions.
58 """
59 config.reread()
60 new_events = self._CreateEventsWithTasks(config, mv)
61 for keyword, event in self._events.iteritems():
62 event.Merge(new_events[keyword])
63
64
Chris Masone93f51d42012-04-18 08:46:52 -070065 def SetUpEventsAndTasks(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -070066 """Populate self._events and create task lists from config.
67
Chris Masone96f16632012-04-04 18:36:03 -070068 @param config: an instance of ForgivingConfigParser.
Chris Masone93f51d42012-04-18 08:46:52 -070069 @param mv: an instance of ManifestVersions.
Chris Masone96f16632012-04-04 18:36:03 -070070 """
Chris Masone855d86f2012-05-07 13:48:07 -070071 self._events = self._CreateEventsWithTasks(config, mv)
72
73
74 def _CreateEventsWithTasks(self, config, mv):
75 """Create task lists from config, and assign to newly-minted events.
76
77 Calling multiple times should start afresh each time.
78
79 @param config: an instance of ForgivingConfigParser.
80 @param mv: an instance of ManifestVersions.
81 """
Chris Masone855d86f2012-05-07 13:48:07 -070082 events = {}
Chris Masonebf8775a2012-09-10 10:44:18 -070083 for klass in self.EVENT_CLASSES:
Chris Masone855d86f2012-05-07 13:48:07 -070084 events[klass.KEYWORD] = klass.CreateFromConfig(config, mv)
Chris Masone96f16632012-04-04 18:36:03 -070085
86 tasks = self.TasksFromConfig(config)
Chris Masone645c7e42012-05-17 17:28:40 -070087 for keyword, task_list in tasks.iteritems():
88 if keyword in events:
89 events[keyword].tasks = task_list
90 else:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -070091 logging.warning('%s, is an unknown keyword.', keyword)
Chris Masone855d86f2012-05-07 13:48:07 -070092 return events
Chris Masone96f16632012-04-04 18:36:03 -070093
94
95 def TasksFromConfig(self, config):
96 """Generate a dict of {event_keyword: [tasks]} mappings from |config|.
97
98 For each section in |config| that encodes a Task, instantiate a Task
99 object. Determine the event that Task is supposed to run_on and
100 append the object to a list associated with the appropriate event
101 keyword. Return a dictionary of these keyword: list of task mappings.
102
103 @param config: a ForgivingConfigParser containing tasks to be parsed.
104 @return dict of {event_keyword: [tasks]} mappings.
105 @raise MalformedConfigEntry on a task parsing error.
106 """
107 tasks = {}
108 for section in config.sections():
Chris Masone93f51d42012-04-18 08:46:52 -0700109 if not base_event.HonoredSection(section):
Chris Masone96f16632012-04-04 18:36:03 -0700110 try:
111 keyword, new_task = task.Task.CreateFromConfigSection(
Dan Shi84585362016-10-03 14:11:43 -0700112 config, section)
Chris Masone96f16632012-04-04 18:36:03 -0700113 except task.MalformedConfigEntry as e:
Dan Shi84585362016-10-03 14:11:43 -0700114 logging.warning('%s is malformed: %s', section, str(e))
Chris Masone96f16632012-04-04 18:36:03 -0700115 continue
116 tasks.setdefault(keyword, []).append(new_task)
117 return tasks
Chris Masone2d61ca22012-04-02 16:52:46 -0700118
119
Chris Masone855d86f2012-05-07 13:48:07 -0700120 def RunForever(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -0700121 """Main loop of the scheduler. Runs til the process is killed.
122
Chris Masone855d86f2012-05-07 13:48:07 -0700123 @param config: an instance of ForgivingConfigParser.
Chris Masone67f06d62012-04-12 15:16:56 -0700124 @param mv: an instance of manifest_versions.ManifestVersions.
125 """
Chris Masone855d86f2012-05-07 13:48:07 -0700126 for event in self._events.itervalues():
Chris Masone73a78382012-04-20 13:25:51 -0700127 event.Prepare()
Chris Masone2d61ca22012-04-02 16:52:46 -0700128 while True:
Chris Masone645c7e42012-05-17 17:28:40 -0700129 try:
130 self.HandleEventsOnce(mv)
Scott Zawalskic15c6b42012-07-09 13:16:05 -0400131 except board_enumerator.EnumeratorException as e:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700132 logging.warning('Failed to enumerate boards: %r', e)
Dan Shic3fc5992016-12-08 11:12:30 -0800133 mv.Update()
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
Dan Shifa705d22016-07-28 16:16:15 -0700153 # Try to get builds from LaunchControl first. If failed, the board could
154 # be ChromeOS. Use the cache Driver._cros_boards to avoid unnecessary
155 # repeated call to LaunchControl API.
156 launch_control_builds = None
157 if board not in Driver._cros_boards:
Dan Shi2121a332016-02-25 14:22:22 -0800158 launch_control_builds = event.GetLaunchControlBuildsForBoard(board)
Dan Shifa705d22016-07-28 16:16:15 -0700159 if launch_control_builds:
Dan Shi2121a332016-02-25 14:22:22 -0800160 event.Handle(scheduler, branch_builds=None, board=board,
161 launch_control_builds=launch_control_builds)
162 else:
163 branch_builds = event.GetBranchBuildsForBoard(board)
Dan Shifa705d22016-07-28 16:16:15 -0700164 if branch_builds:
165 Driver._cros_boards.add(board)
166 logging.info('Found ChromeOS build for board %s. This should '
167 'be a ChromeOS board.', board)
Dan Shi2121a332016-02-25 14:22:22 -0800168 event.Handle(scheduler, branch_builds, board)
Dan Shi15d42312015-12-15 15:37:28 -0800169 logging.info('Finished handling %s event for board %s', event.keyword,
170 board)
171
Dan Shic3fc5992016-12-08 11:12:30 -0800172 @metrics.SecondsTimerDecorator('chromeos/autotest/suite_scheduler/'
173 'handle_events_once_duration')
Chris Masone67f06d62012-04-12 15:16:56 -0700174 def HandleEventsOnce(self, mv):
175 """One turn through the loop. Separated out for unit testing.
176
177 @param mv: an instance of manifest_versions.ManifestVersions.
Chris Masone645c7e42012-05-17 17:28:40 -0700178 @raise EnumeratorException if we can't enumerate any supported boards.
Chris Masone67f06d62012-04-12 15:16:56 -0700179 """
Chris Masone92874d32012-04-03 10:13:04 -0700180 boards = self._enumerator.Enumerate()
Dan Shi15d42312015-12-15 15:37:28 -0800181 logging.info('%d boards currently in the lab: %r', len(boards), boards)
182 thread_pool = pool.ThreadPool(POOL_SIZE)
Dan Shi8d7f3562016-01-11 10:55:46 -0800183 with contextlib.closing(thread_pool):
184 for e in self._events.itervalues():
185 if not e.ShouldHandle():
186 continue
Dan Shi8446fac2016-03-02 22:07:39 -0800187 # Reset the value of delay_minutes, as this is the beginning of
188 # handling an event for all boards.
189 self._scheduler.delay_minutes = 0
190 self._scheduler.delay_minutes_interval = (
191 deduping_scheduler.DELAY_MINUTES_INTERVAL)
Dan Shi15d42312015-12-15 15:37:28 -0800192 logging.info('Handling %s event for %d boards', e.keyword,
193 len(boards))
194 args = []
Chris Masone96f16632012-04-04 18:36:03 -0700195 for board in boards:
Dan Shi15d42312015-12-15 15:37:28 -0800196 args.append({'scheduler': self._scheduler,
197 'event': e,
198 'board': board})
199 thread_pool.map(self.HandleBoard, args)
200 logging.info('Finished handling %s event for %d boards',
201 e.keyword, len(boards))
Chris Masonebbde3862012-05-07 14:29:51 -0700202 e.UpdateCriteria()
Chris Masone67f06d62012-04-12 15:16:56 -0700203
204
Dan Shi2121a332016-02-25 14:22:22 -0800205 def ForceEventsOnceForBuild(self, keywords, build_name,
206 os_type=task.OS_TYPE_CROS):
Chris Masone67f06d62012-04-12 15:16:56 -0700207 """Force events with provided keywords to happen, with given build.
208
209 @param keywords: iterable of event keywords to force
210 @param build_name: instead of looking up builds to test, test this one.
Dan Shi2121a332016-02-25 14:22:22 -0800211 @param os_type: Type of the OS to test, default to cros.
Chris Masone67f06d62012-04-12 15:16:56 -0700212 """
Dan Shi2121a332016-02-25 14:22:22 -0800213 branch_builds = None
214 launch_control_builds = None
215 if os_type == task.OS_TYPE_CROS:
216 board, type, milestone, manifest = utils.ParseBuildName(build_name)
217 branch_builds = {task.PickBranchName(type, milestone): [build_name]}
218 logging.info('Testing build R%s-%s on %s', milestone, manifest,
219 board)
220 else:
221 logging.info('Build is not a ChromeOS build, try to parse as a '
222 'Launch Control build.')
Dan Shi6450e142016-03-11 11:52:20 -0800223 _,target,_ = utils.parse_launch_control_build(build_name)
Dan Shi8db7e612016-07-21 12:55:16 -0700224 board = utils.parse_launch_control_target(target)[0]
Dan Shi43274402016-11-04 15:13:43 -0700225 # Translate board name in build target to the actual board name.
226 board = utils.ANDROID_TARGET_TO_BOARD_MAP.get(board, board)
Dan Shi2121a332016-02-25 14:22:22 -0800227 launch_control_builds = [build_name]
228 logging.info('Testing Launch Control build %s on %s', build_name,
229 board)
Chris Masone67f06d62012-04-12 15:16:56 -0700230
Chris Masone855d86f2012-05-07 13:48:07 -0700231 for e in self._events.itervalues():
Chris Masone67f06d62012-04-12 15:16:56 -0700232 if e.keyword in keywords:
Dan Shi2121a332016-02-25 14:22:22 -0800233 e.Handle(self._scheduler, branch_builds, board, force=True,
234 launch_control_builds=launch_control_builds)