James E. Blair | dbfe1cd | 2015-02-07 11:41:19 -0800 | [diff] [blame] | 1 | # Copyright 2012-2015 Hewlett-Packard Development Company, L.P. |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 2 | # Copyright 2013 OpenStack Foundation |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 3 | # Copyright 2013 Antoine "hashar" Musso |
| 4 | # Copyright 2013 Wikimedia Foundation Inc. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 18 | import extras |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 19 | import json |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 20 | import logging |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 21 | import os |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 22 | import pickle |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 23 | import six |
Christian Berendt | 12d4d72 | 2014-06-07 21:03:45 +0200 | [diff] [blame] | 24 | from six.moves import queue as Queue |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 25 | import sys |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 26 | import threading |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 27 | import time |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 28 | |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 29 | from zuul import configloader |
Morgan Fainberg | 9c4700a | 2016-05-30 14:25:19 -0700 | [diff] [blame] | 30 | from zuul import model |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 31 | from zuul import exceptions |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 32 | from zuul import version as zuul_version |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 33 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 34 | |
James E. Blair | af17a97 | 2016-02-03 15:07:18 -0800 | [diff] [blame] | 35 | class MutexHandler(object): |
| 36 | log = logging.getLogger("zuul.MutexHandler") |
| 37 | |
| 38 | def __init__(self): |
| 39 | self.mutexes = {} |
| 40 | |
| 41 | def acquire(self, item, job): |
| 42 | if not job.mutex: |
| 43 | return True |
| 44 | mutex_name = job.mutex |
| 45 | m = self.mutexes.get(mutex_name) |
| 46 | if not m: |
| 47 | # The mutex is not held, acquire it |
| 48 | self._acquire(mutex_name, item, job.name) |
| 49 | return True |
| 50 | held_item, held_job_name = m |
| 51 | if held_item is item and held_job_name == job.name: |
| 52 | # This item already holds the mutex |
| 53 | return True |
| 54 | held_build = held_item.current_build_set.getBuild(held_job_name) |
| 55 | if held_build and held_build.result: |
| 56 | # The build that held the mutex is complete, release it |
| 57 | # and let the new item have it. |
| 58 | self.log.error("Held mutex %s being released because " |
| 59 | "the build that holds it is complete" % |
| 60 | (mutex_name,)) |
| 61 | self._release(mutex_name, item, job.name) |
| 62 | self._acquire(mutex_name, item, job.name) |
| 63 | return True |
| 64 | return False |
| 65 | |
| 66 | def release(self, item, job): |
| 67 | if not job.mutex: |
| 68 | return |
| 69 | mutex_name = job.mutex |
| 70 | m = self.mutexes.get(mutex_name) |
| 71 | if not m: |
| 72 | # The mutex is not held, nothing to do |
| 73 | self.log.error("Mutex can not be released for %s " |
| 74 | "because the mutex is not held" % |
| 75 | (item,)) |
| 76 | return |
| 77 | held_item, held_job_name = m |
| 78 | if held_item is item and held_job_name == job.name: |
| 79 | # This item holds the mutex |
| 80 | self._release(mutex_name, item, job.name) |
| 81 | return |
| 82 | self.log.error("Mutex can not be released for %s " |
| 83 | "which does not hold it" % |
| 84 | (item,)) |
| 85 | |
| 86 | def _acquire(self, mutex_name, item, job_name): |
| 87 | self.log.debug("Job %s of item %s acquiring mutex %s" % |
| 88 | (job_name, item, mutex_name)) |
| 89 | self.mutexes[mutex_name] = (item, job_name) |
| 90 | |
| 91 | def _release(self, mutex_name, item, job_name): |
| 92 | self.log.debug("Job %s of item %s releasing mutex %s" % |
| 93 | (job_name, item, mutex_name)) |
| 94 | del self.mutexes[mutex_name] |
| 95 | |
| 96 | |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 97 | class ManagementEvent(object): |
| 98 | """An event that should be processed within the main queue run loop""" |
| 99 | def __init__(self): |
| 100 | self._wait_event = threading.Event() |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 101 | self._exc_info = None |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 102 | |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 103 | def exception(self, exc_info): |
| 104 | self._exc_info = exc_info |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 105 | self._wait_event.set() |
| 106 | |
| 107 | def done(self): |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 108 | self._wait_event.set() |
| 109 | |
| 110 | def wait(self, timeout=None): |
| 111 | self._wait_event.wait(timeout) |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 112 | if self._exc_info: |
| 113 | six.reraise(*self._exc_info) |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 114 | return self._wait_event.is_set() |
| 115 | |
| 116 | |
| 117 | class ReconfigureEvent(ManagementEvent): |
| 118 | """Reconfigure the scheduler. The layout will be (re-)loaded from |
| 119 | the path specified in the configuration. |
| 120 | |
| 121 | :arg ConfigParser config: the new configuration |
| 122 | """ |
| 123 | def __init__(self, config): |
| 124 | super(ReconfigureEvent, self).__init__() |
| 125 | self.config = config |
| 126 | |
| 127 | |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 128 | class PromoteEvent(ManagementEvent): |
| 129 | """Promote one or more changes to the head of the queue. |
| 130 | |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 131 | :arg str tenant_name: the name of the tenant |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 132 | :arg str pipeline_name: the name of the pipeline |
| 133 | :arg list change_ids: a list of strings of change ids in the form |
| 134 | 1234,1 |
| 135 | """ |
| 136 | |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 137 | def __init__(self, tenant_name, pipeline_name, change_ids): |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 138 | super(PromoteEvent, self).__init__() |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 139 | self.tenant_name = tenant_name |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 140 | self.pipeline_name = pipeline_name |
| 141 | self.change_ids = change_ids |
| 142 | |
| 143 | |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 144 | class EnqueueEvent(ManagementEvent): |
| 145 | """Enqueue a change into a pipeline |
| 146 | |
| 147 | :arg TriggerEvent trigger_event: a TriggerEvent describing the |
| 148 | trigger, pipeline, and change to enqueue |
| 149 | """ |
| 150 | |
| 151 | def __init__(self, trigger_event): |
| 152 | super(EnqueueEvent, self).__init__() |
| 153 | self.trigger_event = trigger_event |
| 154 | |
| 155 | |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 156 | class ResultEvent(object): |
| 157 | """An event that needs to modify the pipeline state due to a |
| 158 | result from an external system.""" |
| 159 | |
| 160 | pass |
| 161 | |
| 162 | |
| 163 | class BuildStartedEvent(ResultEvent): |
| 164 | """A build has started. |
| 165 | |
| 166 | :arg Build build: The build which has started. |
| 167 | """ |
| 168 | |
| 169 | def __init__(self, build): |
| 170 | self.build = build |
| 171 | |
| 172 | |
| 173 | class BuildCompletedEvent(ResultEvent): |
| 174 | """A build has completed |
| 175 | |
| 176 | :arg Build build: The build which has completed. |
| 177 | """ |
| 178 | |
| 179 | def __init__(self, build): |
| 180 | self.build = build |
| 181 | |
| 182 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 183 | class MergeCompletedEvent(ResultEvent): |
| 184 | """A remote merge operation has completed |
| 185 | |
| 186 | :arg BuildSet build_set: The build_set which is ready. |
| 187 | :arg str zuul_url: The URL of the Zuul Merger. |
| 188 | :arg bool merged: Whether the merge succeeded (changes with refs). |
| 189 | :arg bool updated: Whether the repo was updated (changes without refs). |
| 190 | :arg str commit: The SHA of the merged commit (changes with refs). |
| 191 | """ |
| 192 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 193 | def __init__(self, build_set, zuul_url, merged, updated, commit, |
| 194 | files): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 195 | self.build_set = build_set |
| 196 | self.zuul_url = zuul_url |
| 197 | self.merged = merged |
| 198 | self.updated = updated |
| 199 | self.commit = commit |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 200 | self.files = files |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 201 | |
| 202 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 203 | class NodesProvisionedEvent(ResultEvent): |
| 204 | """Nodes have been provisioned for a build_set |
| 205 | |
| 206 | :arg BuildSet build_set: The build_set which has nodes. |
| 207 | :arg list of Node objects nodes: The provisioned nodes |
| 208 | """ |
| 209 | |
| 210 | def __init__(self, request): |
| 211 | self.request = request |
| 212 | |
| 213 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 214 | def toList(item): |
| 215 | if not item: |
| 216 | return [] |
| 217 | if isinstance(item, list): |
| 218 | return item |
| 219 | return [item] |
| 220 | |
| 221 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 222 | class Scheduler(threading.Thread): |
James E. Blair | e4de4f4 | 2017-01-19 10:35:24 -0800 | [diff] [blame^] | 223 | """The engine of Zuul. |
| 224 | |
| 225 | The Scheduler is reponsible for recieving events and dispatching |
| 226 | them to appropriate components (including pipeline managers, |
| 227 | mergers and launchers). |
| 228 | |
| 229 | It runs a single threaded main loop which processes events |
| 230 | received one at a time and takes action as appropriate. Other |
| 231 | parts of Zuul may run in their own thread, but synchronization is |
| 232 | performed within the scheduler to reduce or eliminate the need for |
| 233 | locking in most circumstances. |
| 234 | |
| 235 | The main daemon will have one instance of the Scheduler class |
| 236 | running which will persist for the life of the process. The |
| 237 | Scheduler instance is supplied to other Zuul components so that |
| 238 | they can submit events or otherwise communicate with other |
| 239 | components. |
| 240 | |
| 241 | """ |
| 242 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 243 | log = logging.getLogger("zuul.Scheduler") |
| 244 | |
James E. Blair | e4d229c | 2016-05-25 15:25:41 -0700 | [diff] [blame] | 245 | def __init__(self, config, testonly=False): |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 246 | threading.Thread.__init__(self) |
James E. Blair | 8a6f0c2 | 2013-07-01 12:31:34 -0400 | [diff] [blame] | 247 | self.daemon = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 248 | self.wake_event = threading.Event() |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 249 | self.layout_lock = threading.Lock() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 250 | self.run_handler_lock = threading.Lock() |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 251 | self._pause = False |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 252 | self._exit = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 253 | self._stopped = False |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 254 | self.launcher = None |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 255 | self.merger = None |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 256 | self.connections = None |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 257 | self.statsd = extras.try_import('statsd.statsd') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 258 | # TODO(jeblair): fix this |
James E. Blair | af17a97 | 2016-02-03 15:07:18 -0800 | [diff] [blame] | 259 | self.mutex = MutexHandler() |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 260 | # Despite triggers being part of the pipeline, there is one trigger set |
| 261 | # per scheduler. The pipeline handles the trigger filters but since |
| 262 | # the events are handled by the scheduler itself it needs to handle |
| 263 | # the loading of the triggers. |
| 264 | # self.triggers['connection_name'] = triggerObject |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 265 | self.triggers = dict() |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 266 | self.config = config |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 267 | |
| 268 | self.trigger_event_queue = Queue.Queue() |
| 269 | self.result_event_queue = Queue.Queue() |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 270 | self.management_event_queue = Queue.Queue() |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 271 | self.abide = model.Abide() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 272 | |
James E. Blair | e4d229c | 2016-05-25 15:25:41 -0700 | [diff] [blame] | 273 | if not testonly: |
| 274 | time_dir = self._get_time_database_dir() |
| 275 | self.time_database = model.TimeDataBase(time_dir) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 276 | |
Jeremy Stanley | 98b38de | 2015-06-04 21:20:43 +0000 | [diff] [blame] | 277 | self.zuul_version = zuul_version.version_info.release_string() |
Sergey Lukjanov | 5d0438d | 2013-12-24 03:36:39 +0400 | [diff] [blame] | 278 | self.last_reconfigured = None |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 279 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 280 | def stop(self): |
| 281 | self._stopped = True |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 282 | self.stopConnections() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 283 | self.wake_event.set() |
| 284 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 285 | def testConfig(self, config_path, connections): |
| 286 | # Take the list of set up connections directly here rather than with |
| 287 | # registerConnections as we don't want to do the onLoad event yet. |
| 288 | return self._parseConfig(config_path, connections) |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 289 | |
Joshua Hesketh | 9a25675 | 2016-04-04 13:38:51 +1000 | [diff] [blame] | 290 | def registerConnections(self, connections, load=True): |
| 291 | # load: whether or not to trigger the onLoad for the connection. This |
| 292 | # is useful for not doing a full load during layout validation. |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 293 | self.connections = connections |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 294 | self.connections.registerScheduler(self, load) |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 295 | |
| 296 | def stopConnections(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 297 | self.connections.stop() |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 298 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 299 | def setLauncher(self, launcher): |
| 300 | self.launcher = launcher |
| 301 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 302 | def setMerger(self, merger): |
| 303 | self.merger = merger |
| 304 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 305 | def setNodepool(self, nodepool): |
| 306 | self.nodepool = nodepool |
| 307 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 308 | def setZooKeeper(self, zk): |
| 309 | self.zk = zk |
| 310 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 311 | def addEvent(self, event): |
| 312 | self.log.debug("Adding trigger event: %s" % event) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 313 | try: |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 314 | if self.statsd: |
| 315 | self.statsd.incr('gerrit.event.%s' % event.type) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 316 | except: |
| 317 | self.log.exception("Exception reporting event stats") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 318 | self.trigger_event_queue.put(event) |
| 319 | self.wake_event.set() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 320 | self.log.debug("Done adding trigger event: %s" % event) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 321 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 322 | def onBuildStarted(self, build): |
| 323 | self.log.debug("Adding start event for build: %s" % build) |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 324 | build.start_time = time.time() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 325 | event = BuildStartedEvent(build) |
| 326 | self.result_event_queue.put(event) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 327 | self.wake_event.set() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 328 | self.log.debug("Done adding start event for build: %s" % build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 329 | |
James E. Blair | f035866 | 2015-07-20 15:19:12 -0700 | [diff] [blame] | 330 | def onBuildCompleted(self, build, result): |
| 331 | self.log.debug("Adding complete event for build: %s result: %s" % ( |
| 332 | build, result)) |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 333 | build.end_time = time.time() |
James E. Blair | f035866 | 2015-07-20 15:19:12 -0700 | [diff] [blame] | 334 | # Note, as soon as the result is set, other threads may act |
| 335 | # upon this, even though the event hasn't been fully |
| 336 | # processed. Ensure that any other data from the event (eg, |
| 337 | # timing) is recorded before setting the result. |
| 338 | build.result = result |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 339 | try: |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 340 | if self.statsd and build.pipeline: |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 341 | jobname = build.job.name.replace('.', '_') |
Timothy Chavez | b233208 | 2015-08-07 20:08:04 -0500 | [diff] [blame] | 342 | key = 'zuul.pipeline.%s.all_jobs' % build.pipeline.name |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 343 | self.statsd.incr(key) |
Timothy Chavez | b233208 | 2015-08-07 20:08:04 -0500 | [diff] [blame] | 344 | for label in build.node_labels: |
| 345 | # Jenkins includes the node name in its list of labels, so |
| 346 | # we filter it out here, since that is not statistically |
| 347 | # interesting. |
| 348 | if label == build.node_name: |
| 349 | continue |
| 350 | dt = int((build.start_time - build.launch_time) * 1000) |
James E. Blair | 50aacbc | 2015-11-17 14:09:59 -0800 | [diff] [blame] | 351 | key = 'zuul.pipeline.%s.label.%s.wait_time' % ( |
| 352 | build.pipeline.name, label) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 353 | self.statsd.timing(key, dt) |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 354 | key = 'zuul.pipeline.%s.job.%s.%s' % (build.pipeline.name, |
| 355 | jobname, build.result) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 356 | if build.result in ['SUCCESS', 'FAILURE'] and build.start_time: |
| 357 | dt = int((build.end_time - build.start_time) * 1000) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 358 | self.statsd.timing(key, dt) |
| 359 | self.statsd.incr(key) |
James E. Blair | 50aacbc | 2015-11-17 14:09:59 -0800 | [diff] [blame] | 360 | |
| 361 | key = 'zuul.pipeline.%s.job.%s.wait_time' % ( |
| 362 | build.pipeline.name, jobname) |
| 363 | dt = int((build.start_time - build.launch_time) * 1000) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 364 | self.statsd.timing(key, dt) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 365 | except: |
| 366 | self.log.exception("Exception reporting runtime stats") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 367 | event = BuildCompletedEvent(build) |
| 368 | self.result_event_queue.put(event) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 369 | self.wake_event.set() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 370 | self.log.debug("Done adding complete event for build: %s" % build) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 371 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 372 | def onMergeCompleted(self, build_set, zuul_url, merged, updated, |
| 373 | commit, files): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 374 | self.log.debug("Adding merge complete event for build set: %s" % |
| 375 | build_set) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 376 | event = MergeCompletedEvent(build_set, zuul_url, merged, |
| 377 | updated, commit, files) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 378 | self.result_event_queue.put(event) |
| 379 | self.wake_event.set() |
| 380 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 381 | def onNodesProvisioned(self, req): |
| 382 | self.log.debug("Adding nodes provisioned event for build set: %s" % |
| 383 | req.build_set) |
| 384 | event = NodesProvisionedEvent(req) |
| 385 | self.result_event_queue.put(event) |
| 386 | self.wake_event.set() |
| 387 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 388 | def reconfigure(self, config): |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 389 | self.log.debug("Prepare to reconfigure") |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 390 | event = ReconfigureEvent(config) |
| 391 | self.management_event_queue.put(event) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 392 | self.wake_event.set() |
| 393 | self.log.debug("Waiting for reconfiguration") |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 394 | event.wait() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 395 | self.log.debug("Reconfiguration complete") |
Sergey Lukjanov | 5d0438d | 2013-12-24 03:36:39 +0400 | [diff] [blame] | 396 | self.last_reconfigured = int(time.time()) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 397 | |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 398 | def promote(self, tenant_name, pipeline_name, change_ids): |
| 399 | event = PromoteEvent(tenant_name, pipeline_name, change_ids) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 400 | self.management_event_queue.put(event) |
| 401 | self.wake_event.set() |
| 402 | self.log.debug("Waiting for promotion") |
| 403 | event.wait() |
| 404 | self.log.debug("Promotion complete") |
| 405 | |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 406 | def enqueue(self, trigger_event): |
| 407 | event = EnqueueEvent(trigger_event) |
| 408 | self.management_event_queue.put(event) |
| 409 | self.wake_event.set() |
| 410 | self.log.debug("Waiting for enqueue") |
| 411 | event.wait() |
| 412 | self.log.debug("Enqueue complete") |
| 413 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 414 | def exit(self): |
| 415 | self.log.debug("Prepare to exit") |
| 416 | self._pause = True |
| 417 | self._exit = True |
| 418 | self.wake_event.set() |
| 419 | self.log.debug("Waiting for exit") |
| 420 | |
| 421 | def _get_queue_pickle_file(self): |
James E. Blair | 5a95c86 | 2012-07-09 15:11:17 -0700 | [diff] [blame] | 422 | if self.config.has_option('zuul', 'state_dir'): |
| 423 | state_dir = os.path.expanduser(self.config.get('zuul', |
| 424 | 'state_dir')) |
| 425 | else: |
| 426 | state_dir = '/var/lib/zuul' |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 427 | return os.path.join(state_dir, 'queue.pickle') |
| 428 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 429 | def _get_time_database_dir(self): |
| 430 | if self.config.has_option('zuul', 'state_dir'): |
| 431 | state_dir = os.path.expanduser(self.config.get('zuul', |
| 432 | 'state_dir')) |
| 433 | else: |
| 434 | state_dir = '/var/lib/zuul' |
| 435 | d = os.path.join(state_dir, 'times') |
| 436 | if not os.path.exists(d): |
| 437 | os.mkdir(d) |
| 438 | return d |
| 439 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 440 | def _save_queue(self): |
| 441 | pickle_file = self._get_queue_pickle_file() |
| 442 | events = [] |
| 443 | while not self.trigger_event_queue.empty(): |
| 444 | events.append(self.trigger_event_queue.get()) |
| 445 | self.log.debug("Queue length is %s" % len(events)) |
| 446 | if events: |
| 447 | self.log.debug("Saving queue") |
| 448 | pickle.dump(events, open(pickle_file, 'wb')) |
| 449 | |
| 450 | def _load_queue(self): |
| 451 | pickle_file = self._get_queue_pickle_file() |
| 452 | if os.path.exists(pickle_file): |
| 453 | self.log.debug("Loading queue") |
| 454 | events = pickle.load(open(pickle_file, 'rb')) |
| 455 | self.log.debug("Queue length is %s" % len(events)) |
| 456 | for event in events: |
| 457 | self.trigger_event_queue.put(event) |
| 458 | else: |
| 459 | self.log.debug("No queue file found") |
| 460 | |
| 461 | def _delete_queue(self): |
| 462 | pickle_file = self._get_queue_pickle_file() |
| 463 | if os.path.exists(pickle_file): |
| 464 | self.log.debug("Deleting saved queue") |
| 465 | os.unlink(pickle_file) |
| 466 | |
| 467 | def resume(self): |
| 468 | try: |
| 469 | self._load_queue() |
| 470 | except: |
| 471 | self.log.exception("Unable to load queue") |
| 472 | try: |
| 473 | self._delete_queue() |
| 474 | except: |
| 475 | self.log.exception("Unable to delete saved queue") |
| 476 | self.log.debug("Resuming queue processing") |
| 477 | self.wake_event.set() |
| 478 | |
| 479 | def _doPauseEvent(self): |
| 480 | if self._exit: |
| 481 | self.log.debug("Exiting") |
| 482 | self._save_queue() |
| 483 | os._exit(0) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 484 | |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 485 | def _doReconfigureEvent(self, event): |
| 486 | # This is called in the scheduler loop after another thread submits |
| 487 | # a request |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 488 | self.layout_lock.acquire() |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 489 | self.config = event.config |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 490 | try: |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 491 | self.log.debug("Performing reconfiguration") |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 492 | loader = configloader.ConfigLoader() |
| 493 | abide = loader.loadConfig( |
| 494 | self.config.get('zuul', 'tenant_config'), |
| 495 | self, self.merger, self.connections) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 496 | for tenant in abide.tenants.values(): |
| 497 | self._reconfigureTenant(tenant) |
| 498 | self.abide = abide |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 499 | finally: |
| 500 | self.layout_lock.release() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 501 | |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 502 | def _reenqueueTenant(self, old_tenant, tenant): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 503 | for name, new_pipeline in tenant.layout.pipelines.items(): |
| 504 | old_pipeline = old_tenant.layout.pipelines.get(name) |
| 505 | if not old_pipeline: |
| 506 | self.log.warning("No old pipeline matching %s found " |
| 507 | "when reconfiguring" % name) |
| 508 | continue |
| 509 | self.log.debug("Re-enqueueing changes for pipeline %s" % name) |
| 510 | items_to_remove = [] |
| 511 | builds_to_cancel = [] |
| 512 | last_head = None |
| 513 | for shared_queue in old_pipeline.queues: |
| 514 | for item in shared_queue.queue: |
| 515 | if not item.item_ahead: |
| 516 | last_head = item |
| 517 | item.item_ahead = None |
| 518 | item.items_behind = [] |
| 519 | item.pipeline = None |
| 520 | item.queue = None |
| 521 | project_name = item.change.project.name |
| 522 | item.change.project = new_pipeline.source.getProject( |
| 523 | project_name) |
James E. Blair | 3b5ff3b | 2016-07-21 10:08:24 -0700 | [diff] [blame] | 524 | if new_pipeline.manager.reEnqueueItem(item, |
| 525 | last_head): |
| 526 | new_jobs = item.getJobs() |
| 527 | for build in item.current_build_set.getBuilds(): |
Paul Belanger | e22baea | 2016-11-03 16:59:27 -0400 | [diff] [blame] | 528 | jobtree = item.job_tree.getJobTreeForJob(build.job) |
| 529 | if jobtree and jobtree.job in new_jobs: |
| 530 | build.job = jobtree.job |
James E. Blair | 3b5ff3b | 2016-07-21 10:08:24 -0700 | [diff] [blame] | 531 | else: |
| 532 | item.removeBuild(build) |
| 533 | builds_to_cancel.append(build) |
| 534 | else: |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 535 | items_to_remove.append(item) |
| 536 | for item in items_to_remove: |
| 537 | for build in item.current_build_set.getBuilds(): |
| 538 | builds_to_cancel.append(build) |
| 539 | for build in builds_to_cancel: |
| 540 | self.log.warning( |
| 541 | "Canceling build %s during reconfiguration" % (build,)) |
| 542 | try: |
| 543 | self.launcher.cancel(build) |
| 544 | except Exception: |
| 545 | self.log.exception( |
| 546 | "Exception while canceling build %s " |
| 547 | "for change %s" % (build, item.change)) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 548 | |
| 549 | def _reconfigureTenant(self, tenant): |
| 550 | # This is called from _doReconfigureEvent while holding the |
| 551 | # layout lock |
| 552 | old_tenant = self.abide.tenants.get(tenant.name) |
| 553 | if old_tenant: |
| 554 | self._reenqueueTenant(old_tenant, tenant) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 555 | # TODOv3(jeblair): update for tenants |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 556 | # self.maintainConnectionCache() |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 557 | self.connections.reconfigureDrivers(tenant) |
| 558 | # TODOv3(jeblair): remove postconfig calls? |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 559 | for pipeline in tenant.layout.pipelines.values(): |
| 560 | pipeline.source.postConfig() |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 561 | for trigger in pipeline.triggers: |
| 562 | trigger.postConfig(pipeline) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 563 | for reporter in pipeline.actions: |
| 564 | reporter.postConfig() |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 565 | if self.statsd: |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 566 | try: |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 567 | for pipeline in tenant.layout.pipelines.values(): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 568 | items = len(pipeline.getAllItems()) |
| 569 | # stats.gauges.zuul.pipeline.NAME.current_changes |
| 570 | key = 'zuul.pipeline.%s' % pipeline.name |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 571 | self.statsd.gauge(key + '.current_changes', items) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 572 | except Exception: |
| 573 | self.log.exception("Exception reporting initial " |
| 574 | "pipeline stats:") |
| 575 | |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 576 | def _doPromoteEvent(self, event): |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 577 | tenant = self.abide.tenants.get(event.tenant_name) |
| 578 | pipeline = tenant.layout.pipelines[event.pipeline_name] |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 579 | change_ids = [c.split(',') for c in event.change_ids] |
| 580 | items_to_enqueue = [] |
| 581 | change_queue = None |
| 582 | for shared_queue in pipeline.queues: |
| 583 | if change_queue: |
| 584 | break |
| 585 | for item in shared_queue.queue: |
| 586 | if (item.change.number == change_ids[0][0] and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 587 | item.change.patchset == change_ids[0][1]): |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 588 | change_queue = shared_queue |
| 589 | break |
| 590 | if not change_queue: |
| 591 | raise Exception("Unable to find shared change queue for %s" % |
| 592 | event.change_ids[0]) |
| 593 | for number, patchset in change_ids: |
| 594 | found = False |
| 595 | for item in change_queue.queue: |
| 596 | if (item.change.number == number and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 597 | item.change.patchset == patchset): |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 598 | found = True |
| 599 | items_to_enqueue.append(item) |
| 600 | break |
| 601 | if not found: |
| 602 | raise Exception("Unable to find %s,%s in queue %s" % |
| 603 | (number, patchset, change_queue)) |
| 604 | for item in change_queue.queue[:]: |
| 605 | if item not in items_to_enqueue: |
| 606 | items_to_enqueue.append(item) |
| 607 | pipeline.manager.cancelJobs(item) |
| 608 | pipeline.manager.dequeueItem(item) |
| 609 | for item in items_to_enqueue: |
Sean Dague | f39b9ca | 2014-01-10 21:34:35 -0500 | [diff] [blame] | 610 | pipeline.manager.addChange( |
| 611 | item.change, |
| 612 | enqueue_time=item.enqueue_time, |
James E. Blair | f9ab884 | 2014-07-10 13:12:07 -0700 | [diff] [blame] | 613 | quiet=True, |
| 614 | ignore_requirements=True) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 615 | |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 616 | def _doEnqueueEvent(self, event): |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 617 | tenant = self.abide.tenants.get(event.tenant_name) |
| 618 | project = tenant.layout.project_configs.get(event.project_name) |
| 619 | pipeline = tenant.layout.pipelines[event.forced_pipeline] |
James E. Blair | c0dedf8 | 2014-08-06 09:37:52 -0700 | [diff] [blame] | 620 | change = pipeline.source.getChange(event, project) |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 621 | self.log.debug("Event %s for change %s was directly assigned " |
| 622 | "to pipeline %s" % (event, change, self)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 623 | self.log.info("Adding %s %s to %s" % |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 624 | (project, change, pipeline)) |
| 625 | pipeline.manager.addChange(change, ignore_requirements=True) |
| 626 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 627 | def _areAllBuildsComplete(self): |
| 628 | self.log.debug("Checking if all builds are complete") |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 629 | if self.merger.areMergesOutstanding(): |
| 630 | self.log.debug("Waiting on merger") |
| 631 | return False |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 632 | waiting = False |
Paul Belanger | debd7a7 | 2016-11-11 19:56:15 -0500 | [diff] [blame] | 633 | for tenant in self.abide.tenants.values(): |
| 634 | for pipeline in tenant.layout.pipelines.values(): |
| 635 | for item in pipeline.getAllItems(): |
| 636 | for build in item.current_build_set.getBuilds(): |
| 637 | if build.result is None: |
| 638 | self.log.debug("%s waiting on %s" % |
| 639 | (pipeline.manager, build)) |
| 640 | waiting = True |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 641 | if not waiting: |
| 642 | self.log.debug("All builds are complete") |
| 643 | return True |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 644 | return False |
| 645 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 646 | def run(self): |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 647 | if self.statsd: |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 648 | self.log.debug("Statsd enabled") |
| 649 | else: |
| 650 | self.log.debug("Statsd disabled because python statsd " |
| 651 | "package not found") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 652 | while True: |
| 653 | self.log.debug("Run handler sleeping") |
| 654 | self.wake_event.wait() |
| 655 | self.wake_event.clear() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 656 | if self._stopped: |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 657 | self.log.debug("Run handler stopping") |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 658 | return |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 659 | self.log.debug("Run handler awake") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 660 | self.run_handler_lock.acquire() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 661 | try: |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 662 | while not self.management_event_queue.empty(): |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 663 | self.process_management_queue() |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 664 | |
James E. Blair | 263fba9 | 2013-02-27 13:07:19 -0800 | [diff] [blame] | 665 | # Give result events priority -- they let us stop builds, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 666 | # whereas trigger events cause us to launch builds. |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 667 | while not self.result_event_queue.empty(): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 668 | self.process_result_queue() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 669 | |
| 670 | if not self._pause: |
| 671 | while not self.trigger_event_queue.empty(): |
James E. Blair | 263fba9 | 2013-02-27 13:07:19 -0800 | [diff] [blame] | 672 | self.process_event_queue() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 673 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 674 | if self._pause and self._areAllBuildsComplete(): |
| 675 | self._doPauseEvent() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 676 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 677 | for tenant in self.abide.tenants.values(): |
| 678 | for pipeline in tenant.layout.pipelines.values(): |
| 679 | while pipeline.manager.processQueue(): |
| 680 | pass |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 681 | |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 682 | except Exception: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 683 | self.log.exception("Exception in run handler:") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 684 | # There may still be more events to process |
| 685 | self.wake_event.set() |
| 686 | finally: |
| 687 | self.run_handler_lock.release() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 688 | |
Joshua Hesketh | 4bd7da3 | 2016-02-17 20:58:47 +1100 | [diff] [blame] | 689 | def maintainConnectionCache(self): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 690 | # TODOv3(jeblair): update for tenants |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 691 | relevant = set() |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 692 | for tenant in self.abide.tenants.values(): |
| 693 | for pipeline in tenant.layout.pipelines.values(): |
Joshua Hesketh | dc7820c | 2016-03-11 13:14:28 +1100 | [diff] [blame] | 694 | self.log.debug("Gather relevant cache items for: %s" % |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 695 | pipeline) |
Joshua Hesketh | dc7820c | 2016-03-11 13:14:28 +1100 | [diff] [blame] | 696 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 697 | for item in pipeline.getAllItems(): |
| 698 | relevant.add(item.change) |
| 699 | relevant.update(item.change.getRelatedChanges()) |
Joshua Hesketh | 4bd7da3 | 2016-02-17 20:58:47 +1100 | [diff] [blame] | 700 | for connection in self.connections.values(): |
| 701 | connection.maintainCache(relevant) |
| 702 | self.log.debug( |
| 703 | "End maintain connection cache for: %s" % connection) |
| 704 | self.log.debug("Connection cache size: %s" % len(relevant)) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 705 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 706 | def process_event_queue(self): |
| 707 | self.log.debug("Fetching trigger event") |
| 708 | event = self.trigger_event_queue.get() |
| 709 | self.log.debug("Processing trigger event %s" % event) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 710 | try: |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 711 | for tenant in self.abide.tenants.values(): |
| 712 | for pipeline in tenant.layout.pipelines.values(): |
| 713 | # Get the change even if the project is unknown to |
| 714 | # us for the use of updating the cache if there is |
| 715 | # another change depending on this foreign one. |
| 716 | try: |
| 717 | change = pipeline.source.getChange(event) |
| 718 | except exceptions.ChangeNotFound as e: |
| 719 | self.log.debug("Unable to get change %s from " |
| 720 | "source %s (most likely looking " |
| 721 | "for a change from another " |
| 722 | "connection trigger)", |
| 723 | e.change, pipeline.source) |
| 724 | continue |
| 725 | if event.type == 'patchset-created': |
| 726 | pipeline.manager.removeOldVersionsOfChange(change) |
| 727 | elif event.type == 'change-abandoned': |
| 728 | pipeline.manager.removeAbandonedChange(change) |
| 729 | if pipeline.manager.eventMatches(event, change): |
| 730 | self.log.info("Adding %s %s to %s" % |
| 731 | (change.project, change, pipeline)) |
| 732 | pipeline.manager.addChange(change) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 733 | finally: |
James E. Blair | ff79197 | 2013-01-09 11:45:43 -0800 | [diff] [blame] | 734 | self.trigger_event_queue.task_done() |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 735 | |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 736 | def process_management_queue(self): |
| 737 | self.log.debug("Fetching management event") |
| 738 | event = self.management_event_queue.get() |
| 739 | self.log.debug("Processing management event %s" % event) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 740 | try: |
| 741 | if isinstance(event, ReconfigureEvent): |
| 742 | self._doReconfigureEvent(event) |
| 743 | elif isinstance(event, PromoteEvent): |
| 744 | self._doPromoteEvent(event) |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 745 | elif isinstance(event, EnqueueEvent): |
| 746 | self._doEnqueueEvent(event.trigger_event) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 747 | else: |
| 748 | self.log.error("Unable to handle event %s" % event) |
| 749 | event.done() |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 750 | except Exception: |
| 751 | event.exception(sys.exc_info()) |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 752 | self.management_event_queue.task_done() |
| 753 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 754 | def process_result_queue(self): |
| 755 | self.log.debug("Fetching result event") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 756 | event = self.result_event_queue.get() |
| 757 | self.log.debug("Processing result event %s" % event) |
| 758 | try: |
| 759 | if isinstance(event, BuildStartedEvent): |
| 760 | self._doBuildStartedEvent(event) |
| 761 | elif isinstance(event, BuildCompletedEvent): |
| 762 | self._doBuildCompletedEvent(event) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 763 | elif isinstance(event, MergeCompletedEvent): |
| 764 | self._doMergeCompletedEvent(event) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 765 | elif isinstance(event, NodesProvisionedEvent): |
| 766 | self._doNodesProvisionedEvent(event) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 767 | else: |
| 768 | self.log.error("Unable to handle event %s" % event) |
| 769 | finally: |
| 770 | self.result_event_queue.task_done() |
| 771 | |
| 772 | def _doBuildStartedEvent(self, event): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 773 | build = event.build |
| 774 | if build.build_set is not build.build_set.item.current_build_set: |
| 775 | self.log.warning("Build %s is not in the current build set" % |
| 776 | (build,)) |
| 777 | return |
| 778 | pipeline = build.build_set.item.pipeline |
| 779 | if not pipeline: |
| 780 | self.log.warning("Build %s is not associated with a pipeline" % |
| 781 | (build,)) |
| 782 | return |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 783 | try: |
| 784 | build.estimated_time = float(self.time_database.getEstimatedTime( |
| 785 | build.job.name)) |
| 786 | except Exception: |
| 787 | self.log.exception("Exception estimating build time:") |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 788 | pipeline.manager.onBuildStarted(event.build) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 789 | |
| 790 | def _doBuildCompletedEvent(self, event): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 791 | build = event.build |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 792 | |
| 793 | # Regardless of any other conditions which might cause us not |
| 794 | # to pass this on to the pipeline manager, make sure we return |
| 795 | # the nodes to nodepool. |
| 796 | try: |
| 797 | nodeset = build.build_set.getJobNodeSet(build.job.name) |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 798 | self.nodepool.returnNodeSet(nodeset) |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 799 | except Exception: |
| 800 | self.log.exception("Unable to return nodeset %s" % (nodeset,)) |
| 801 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 802 | if build.build_set is not build.build_set.item.current_build_set: |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 803 | self.log.debug("Build %s is not in the current build set" % |
| 804 | (build,)) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 805 | return |
| 806 | pipeline = build.build_set.item.pipeline |
| 807 | if not pipeline: |
| 808 | self.log.warning("Build %s is not associated with a pipeline" % |
| 809 | (build,)) |
| 810 | return |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 811 | if build.end_time and build.start_time and build.result: |
| 812 | duration = build.end_time - build.start_time |
Paul Belanger | 87e4ab0 | 2016-06-08 14:17:20 -0400 | [diff] [blame] | 813 | try: |
| 814 | self.time_database.update( |
| 815 | build.job.name, duration, build.result) |
| 816 | except Exception: |
| 817 | self.log.exception("Exception recording build time:") |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 818 | pipeline.manager.onBuildCompleted(event.build) |
| 819 | |
| 820 | def _doMergeCompletedEvent(self, event): |
| 821 | build_set = event.build_set |
| 822 | if build_set is not build_set.item.current_build_set: |
| 823 | self.log.warning("Build set %s is not current" % (build_set,)) |
| 824 | return |
| 825 | pipeline = build_set.item.pipeline |
| 826 | if not pipeline: |
| 827 | self.log.warning("Build set %s is not associated with a pipeline" % |
| 828 | (build_set,)) |
| 829 | return |
| 830 | pipeline.manager.onMergeCompleted(event) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 831 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 832 | def _doNodesProvisionedEvent(self, event): |
| 833 | request = event.request |
| 834 | build_set = request.build_set |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 835 | |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 836 | self.nodepool.acceptNodes(request) |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 837 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 838 | if build_set is not build_set.item.current_build_set: |
| 839 | self.log.warning("Build set %s is not current" % (build_set,)) |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 840 | if request.fulfilled: |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 841 | self.nodepool.returnNodeSet(request.nodeset) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 842 | return |
| 843 | pipeline = build_set.item.pipeline |
| 844 | if not pipeline: |
| 845 | self.log.warning("Build set %s is not associated with a pipeline" % |
| 846 | (build_set,)) |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 847 | if request.fulfilled: |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 848 | self.nodepool.returnNodeSet(request.nodeset) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 849 | return |
| 850 | pipeline.manager.onNodesProvisioned(event) |
| 851 | |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 852 | def formatStatusJSON(self, tenant_name): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 853 | # TODOv3(jeblair): use tenants |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 854 | if self.config.has_option('zuul', 'url_pattern'): |
| 855 | url_pattern = self.config.get('zuul', 'url_pattern') |
| 856 | else: |
| 857 | url_pattern = None |
| 858 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 859 | data = {} |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 860 | |
| 861 | data['zuul_version'] = self.zuul_version |
| 862 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 863 | if self._pause: |
| 864 | ret = '<p><b>Queue only mode:</b> preparing to ' |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 865 | if self._exit: |
| 866 | ret += 'exit' |
| 867 | ret += ', queue length: %s' % self.trigger_event_queue.qsize() |
| 868 | ret += '</p>' |
| 869 | data['message'] = ret |
| 870 | |
James E. Blair | fb682cc | 2013-02-26 15:23:27 -0800 | [diff] [blame] | 871 | data['trigger_event_queue'] = {} |
| 872 | data['trigger_event_queue']['length'] = \ |
| 873 | self.trigger_event_queue.qsize() |
| 874 | data['result_event_queue'] = {} |
| 875 | data['result_event_queue']['length'] = \ |
| 876 | self.result_event_queue.qsize() |
| 877 | |
Sergey Lukjanov | 5d0438d | 2013-12-24 03:36:39 +0400 | [diff] [blame] | 878 | if self.last_reconfigured: |
| 879 | data['last_reconfigured'] = self.last_reconfigured * 1000 |
| 880 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 881 | pipelines = [] |
| 882 | data['pipelines'] = pipelines |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 883 | tenant = self.abide.tenants.get(tenant_name) |
| 884 | for pipeline in tenant.layout.pipelines.values(): |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 885 | pipelines.append(pipeline.formatStatusJSON(url_pattern)) |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 886 | return json.dumps(data) |