blob: 44737ef3790e7afa64ed7ff4af47cd21c125f9d2 [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
Dan Shifa705d22016-07-28 16:16:15 -070037 # Cache for known ChromeOS boards. The cache helps to avoid unnecessary
38 # repeated calls to Launch Control API.
39 _cros_boards = set()
Chris Masone2d61ca22012-04-02 16:52:46 -070040
Dan Shi23245142015-01-22 13:22:28 -080041 def __init__(self, scheduler, enumerator, is_sanity=False):
Chris Masone2d61ca22012-04-02 16:52:46 -070042 """Constructor
43
Chris Masone67f06d62012-04-12 15:16:56 -070044 @param scheduler: an instance of deduping_scheduler.DedupingScheduler.
45 @param enumerator: an instance of board_enumerator.BoardEnumerator.
Dan Shi23245142015-01-22 13:22:28 -080046 @param is_sanity: Set to True if the driver is created for sanity check.
47 Default is set to False.
Chris Masone2d61ca22012-04-02 16:52:46 -070048 """
Chris Masone67f06d62012-04-12 15:16:56 -070049 self._scheduler = scheduler
50 self._enumerator = enumerator
Dan Shi23245142015-01-22 13:22:28 -080051 task.TotMilestoneManager.is_sanity = is_sanity
Chris Masone2d61ca22012-04-02 16:52:46 -070052
Chris Masone2d61ca22012-04-02 16:52:46 -070053
Chris Masone855d86f2012-05-07 13:48:07 -070054 def RereadAndReprocessConfig(self, config, mv):
55 """Re-read config, re-populate self._events and recreate task lists.
56
57 @param config: an instance of ForgivingConfigParser.
58 @param mv: an instance of ManifestVersions.
59 """
60 config.reread()
61 new_events = self._CreateEventsWithTasks(config, mv)
62 for keyword, event in self._events.iteritems():
63 event.Merge(new_events[keyword])
64
65
Chris Masone93f51d42012-04-18 08:46:52 -070066 def SetUpEventsAndTasks(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -070067 """Populate self._events and create task lists from config.
68
Chris Masone96f16632012-04-04 18:36:03 -070069 @param config: an instance of ForgivingConfigParser.
Chris Masone93f51d42012-04-18 08:46:52 -070070 @param mv: an instance of ManifestVersions.
Chris Masone96f16632012-04-04 18:36:03 -070071 """
Chris Masone855d86f2012-05-07 13:48:07 -070072 self._events = self._CreateEventsWithTasks(config, mv)
73
74
75 def _CreateEventsWithTasks(self, config, mv):
76 """Create task lists from config, and assign to newly-minted events.
77
78 Calling multiple times should start afresh each time.
79
80 @param config: an instance of ForgivingConfigParser.
81 @param mv: an instance of ManifestVersions.
82 """
Chris Masone855d86f2012-05-07 13:48:07 -070083 events = {}
Chris Masonebf8775a2012-09-10 10:44:18 -070084 for klass in self.EVENT_CLASSES:
Chris Masone855d86f2012-05-07 13:48:07 -070085 events[klass.KEYWORD] = klass.CreateFromConfig(config, mv)
Chris Masone96f16632012-04-04 18:36:03 -070086
87 tasks = self.TasksFromConfig(config)
Chris Masone645c7e42012-05-17 17:28:40 -070088 for keyword, task_list in tasks.iteritems():
89 if keyword in events:
90 events[keyword].tasks = task_list
91 else:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -070092 logging.warning('%s, is an unknown keyword.', keyword)
Chris Masone855d86f2012-05-07 13:48:07 -070093 return events
Chris Masone96f16632012-04-04 18:36:03 -070094
95
96 def TasksFromConfig(self, config):
97 """Generate a dict of {event_keyword: [tasks]} mappings from |config|.
98
99 For each section in |config| that encodes a Task, instantiate a Task
100 object. Determine the event that Task is supposed to run_on and
101 append the object to a list associated with the appropriate event
102 keyword. Return a dictionary of these keyword: list of task mappings.
103
104 @param config: a ForgivingConfigParser containing tasks to be parsed.
105 @return dict of {event_keyword: [tasks]} mappings.
106 @raise MalformedConfigEntry on a task parsing error.
107 """
108 tasks = {}
109 for section in config.sections():
Chris Masone93f51d42012-04-18 08:46:52 -0700110 if not base_event.HonoredSection(section):
Chris Masone96f16632012-04-04 18:36:03 -0700111 try:
112 keyword, new_task = task.Task.CreateFromConfigSection(
113 config, section)
114 except task.MalformedConfigEntry as e:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700115 logging.warning('%s is malformed: %s', section, e)
Chris Masone96f16632012-04-04 18:36:03 -0700116 continue
117 tasks.setdefault(keyword, []).append(new_task)
118 return tasks
Chris Masone2d61ca22012-04-02 16:52:46 -0700119
120
Chris Masone855d86f2012-05-07 13:48:07 -0700121 def RunForever(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -0700122 """Main loop of the scheduler. Runs til the process is killed.
123
Chris Masone855d86f2012-05-07 13:48:07 -0700124 @param config: an instance of ForgivingConfigParser.
Chris Masone67f06d62012-04-12 15:16:56 -0700125 @param mv: an instance of manifest_versions.ManifestVersions.
126 """
Chris Masone855d86f2012-05-07 13:48:07 -0700127 for event in self._events.itervalues():
Chris Masone73a78382012-04-20 13:25:51 -0700128 event.Prepare()
Chris Masone2d61ca22012-04-02 16:52:46 -0700129 while True:
Chris Masone645c7e42012-05-17 17:28:40 -0700130 try:
131 self.HandleEventsOnce(mv)
Scott Zawalskic15c6b42012-07-09 13:16:05 -0400132 except board_enumerator.EnumeratorException as e:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700133 logging.warning('Failed to enumerate boards: %r', e)
Dan Shi098d1e22015-09-02 10:00:24 -0700134 with _timer.get_client('manifest_versions_update'):
135 mv.Update()
136 with _timer.get_client('tot_milestone_manager_refresh'):
137 task.TotMilestoneManager().refresh()
Chris Masonefe5a5092012-04-11 18:29:07 -0700138 time.sleep(self._LOOP_INTERVAL_SECONDS)
Chris Masone855d86f2012-05-07 13:48:07 -0700139 self.RereadAndReprocessConfig(config, mv)
Chris Masone2d61ca22012-04-02 16:52:46 -0700140
141
Dan Shi15d42312015-12-15 15:37:28 -0800142 @staticmethod
143 def HandleBoard(inputs):
144 """Handle event based on given inputs.
145
146 @param inputs: A dictionary of the arguments needed to handle an event.
147 Keys include:
148 scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
149 event: An event object to be handled.
150 board: Name of the board.
151 """
152 scheduler = inputs['scheduler']
153 event = inputs['event']
154 board = inputs['board']
155
Dan Shifa705d22016-07-28 16:16:15 -0700156 # Try to get builds from LaunchControl first. If failed, the board could
157 # be ChromeOS. Use the cache Driver._cros_boards to avoid unnecessary
158 # repeated call to LaunchControl API.
159 launch_control_builds = None
160 if board not in Driver._cros_boards:
Dan Shi2121a332016-02-25 14:22:22 -0800161 launch_control_builds = event.GetLaunchControlBuildsForBoard(board)
Dan Shifa705d22016-07-28 16:16:15 -0700162 if launch_control_builds:
Dan Shi2121a332016-02-25 14:22:22 -0800163 event.Handle(scheduler, branch_builds=None, board=board,
164 launch_control_builds=launch_control_builds)
165 else:
166 branch_builds = event.GetBranchBuildsForBoard(board)
Dan Shifa705d22016-07-28 16:16:15 -0700167 if branch_builds:
168 Driver._cros_boards.add(board)
169 logging.info('Found ChromeOS build for board %s. This should '
170 'be a ChromeOS board.', board)
Dan Shi2121a332016-02-25 14:22:22 -0800171 event.Handle(scheduler, branch_builds, board)
Dan Shi15d42312015-12-15 15:37:28 -0800172 logging.info('Finished handling %s event for board %s', event.keyword,
173 board)
174
175
Dan Shi098d1e22015-09-02 10:00:24 -0700176 @_timer.decorate
Chris Masone67f06d62012-04-12 15:16:56 -0700177 def HandleEventsOnce(self, mv):
178 """One turn through the loop. Separated out for unit testing.
179
180 @param mv: an instance of manifest_versions.ManifestVersions.
Chris Masone645c7e42012-05-17 17:28:40 -0700181 @raise EnumeratorException if we can't enumerate any supported boards.
Chris Masone67f06d62012-04-12 15:16:56 -0700182 """
Chris Masone92874d32012-04-03 10:13:04 -0700183 boards = self._enumerator.Enumerate()
Dan Shi15d42312015-12-15 15:37:28 -0800184 logging.info('%d boards currently in the lab: %r', len(boards), boards)
185 thread_pool = pool.ThreadPool(POOL_SIZE)
Dan Shi8d7f3562016-01-11 10:55:46 -0800186 with contextlib.closing(thread_pool):
187 for e in self._events.itervalues():
188 if not e.ShouldHandle():
189 continue
Dan Shi8446fac2016-03-02 22:07:39 -0800190 # Reset the value of delay_minutes, as this is the beginning of
191 # handling an event for all boards.
192 self._scheduler.delay_minutes = 0
193 self._scheduler.delay_minutes_interval = (
194 deduping_scheduler.DELAY_MINUTES_INTERVAL)
Dan Shi15d42312015-12-15 15:37:28 -0800195 logging.info('Handling %s event for %d boards', e.keyword,
196 len(boards))
197 args = []
Chris Masone96f16632012-04-04 18:36:03 -0700198 for board in boards:
Dan Shi15d42312015-12-15 15:37:28 -0800199 args.append({'scheduler': self._scheduler,
200 'event': e,
201 'board': board})
202 thread_pool.map(self.HandleBoard, args)
203 logging.info('Finished handling %s event for %d boards',
204 e.keyword, len(boards))
Chris Masonebbde3862012-05-07 14:29:51 -0700205 e.UpdateCriteria()
Chris Masone67f06d62012-04-12 15:16:56 -0700206
207
Dan Shi2121a332016-02-25 14:22:22 -0800208 def ForceEventsOnceForBuild(self, keywords, build_name,
209 os_type=task.OS_TYPE_CROS):
Chris Masone67f06d62012-04-12 15:16:56 -0700210 """Force events with provided keywords to happen, with given build.
211
212 @param keywords: iterable of event keywords to force
213 @param build_name: instead of looking up builds to test, test this one.
Dan Shi2121a332016-02-25 14:22:22 -0800214 @param os_type: Type of the OS to test, default to cros.
Chris Masone67f06d62012-04-12 15:16:56 -0700215 """
Dan Shi2121a332016-02-25 14:22:22 -0800216 branch_builds = None
217 launch_control_builds = None
218 if os_type == task.OS_TYPE_CROS:
219 board, type, milestone, manifest = utils.ParseBuildName(build_name)
220 branch_builds = {task.PickBranchName(type, milestone): [build_name]}
221 logging.info('Testing build R%s-%s on %s', milestone, manifest,
222 board)
223 else:
224 logging.info('Build is not a ChromeOS build, try to parse as a '
225 'Launch Control build.')
Dan Shi6450e142016-03-11 11:52:20 -0800226 _,target,_ = utils.parse_launch_control_build(build_name)
Dan Shi8db7e612016-07-21 12:55:16 -0700227 board = utils.parse_launch_control_target(target)[0]
Dan Shi2121a332016-02-25 14:22:22 -0800228 launch_control_builds = [build_name]
229 logging.info('Testing Launch Control build %s on %s', build_name,
230 board)
Chris Masone67f06d62012-04-12 15:16:56 -0700231
Chris Masone855d86f2012-05-07 13:48:07 -0700232 for e in self._events.itervalues():
Chris Masone67f06d62012-04-12 15:16:56 -0700233 if e.keyword in keywords:
Dan Shi2121a332016-02-25 14:22:22 -0800234 e.Handle(self._scheduler, branch_builds, board, force=True,
235 launch_control_builds=launch_control_builds)