summaryrefslogtreecommitdiff
path: root/distbuild/worker_build_scheduler.py
blob: 6dc05b273a8daed016c912ffb53f89fc62cefb8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# distbuild/worker_build_scheduler.py -- schedule worker-builds on workers
#
# Copyright (C) 2012, 2014-2016  Codethink Limited
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


import collections
import httplib
import logging
import socket
import urllib
import urlparse

import distbuild


class WorkerBuildRequest(object):

    def __init__(self, artifact, initiator_id):
        self.artifact = artifact
        self.initiator_id = initiator_id

class WorkerCancelPending(object):
    
    def __init__(self, initiator_id):
        self.initiator_id = initiator_id    

class WorkerBuildStepStarted(object):

    def __init__(self, initiators, cache_key, worker_name):
        self.initiators = initiators
        self.artifact_cache_key = cache_key
        self.worker_name = worker_name

class WorkerBuildStepAlreadyStarted(object):

    def __init__(self, initiator_id, cache_key, worker_name):
        self.initiator_id = initiator_id
        self.artifact_cache_key = cache_key
        self.worker_name = worker_name

class WorkerBuildWaiting(object):

    def __init__(self, initiator_id, cache_key):
        self.initiator_id = initiator_id
        self.artifact_cache_key = cache_key

class WorkerBuildOutput(object):

    def __init__(self, msg, cache_key):
        self.msg = msg
        self.artifact_cache_key = cache_key

class WorkerBuildCaching(object):

    def __init__(self, initiators, cache_key):
        self.initiators = initiators
        self.artifact_cache_key = cache_key

class WorkerBuildFinished(object):

    def __init__(self, msg, cache_key, worker_name):
        self.msg = msg
        self.artifact_cache_key = cache_key
        self.worker_name = worker_name

class WorkerBuildFailed(object):

    def __init__(self, msg, cache_key):
        self.msg = msg
        self.artifact_cache_key = cache_key


class _NeedJob(object):

    def __init__(self, who):
        self.who = who
        

class _HaveAJob(object):

    def __init__(self, job):
        self.job = job

class _Disconnected(object):

    def __init__(self, who):
        self.who = who


class Job(object):

    def __init__(self, job_id, artifact, initiator_id):
        self.id = job_id
        self.artifact = artifact
        self.initiators = [initiator_id]
        self.who = None  # we don't know who's going to do this yet

        self._state = 'queued'

    def describe_state(self):
        if self.who is not None:
            return self._state + ', given to %s' % self.who
        else:
            return self._state

    def running(self):
        return self._state == 'running'

    def active(self):
        return self._state == 'running' or self._state == 'queued'

    def failed(self):
        return self._state == 'failed'

    def set_state(self, state):
        assert state in ['queued', 'running', 'complete', 'failed']
        logging.debug('Setting job state for job %s with id %s to %s',
                      self.artifact.basename(), self.id, state)
        self._state = state


class JobQueue(object):
    '''Tracks worker build jobs that are queued, or in progress.'''

    def __init__(self, owner):
        self._owner = owner
        self._jobs = {}

    def get_active_job_for_artifact(self, artifact_basename):
        jobs =  [job for job in self.active_jobs()
                 if job.artifact.basename() == artifact_basename]
        if len(jobs) > 1:
            logging.warn('More than one running job for %s',
                         artifact_basename)
        if not jobs:
            return None
        return jobs[0]

    def get_job_for_id(self, id):
        return self._jobs.get(id, None)

    def add(self, job):
        artifact_basename = job.artifact.basename()

        if self.has_job_for_artifact(artifact_basename):
            logging.info(
                "Duplicate job for %s added to %s job queue.",
                artifact_basename, self._owner)
        self._jobs[job.id] = job

    def remove(self, job):
        if job.id in self._jobs:
            del self._jobs[job.id]
        else:
            logging.warning("Tried to remove a job that doesn't exist "
                            "(%s)", job.artifact.basename())

    def has_job_for_artifact(self, artifact_basename):
        for job in self:
            if job.artifact.basename() == artifact_basename:
                return True

    def __iter__(self):
        return self._jobs.itervalues()

    def remove_jobs(self, jobs):
        for job in jobs:
            self.remove(job)

    def get_next_job(self):
        # for now just return the first thing we find that's not being built
        waiting = [job for job in self if job.who == None]

        return waiting.pop() if len(waiting) > 0 else None

    def running_jobs(self):
        return [job for job in self if job.running()]

    def active_jobs(self):
        return [job for job in self if job.active()]

    def __repr__(self):
        items = []
        for job in self._jobs.itervalues():
            items.append(
                '%s (%s)' % (job.artifact.basename(), job.describe_state()))
        return str(items)


class _BuildFinished(object):

    def __init__(self, job):
        self.job = job


class _BuildFailed(object):

    pass


class _BuildCancelled(object):

    pass

        
class _Cached(object):

    pass


class _JobStarted(object):

    def __init__(self, job):
        self.job = job


class _JobFinished(object):

    def __init__(self, job):
        self.job = job


class _JobFailed(object):

    def __init__(self, job):
        self.job = job
    
class WorkerBuildQueuer(distbuild.StateMachine):

    '''Maintain queue of outstanding worker-build requests.
    
    This state machine captures WorkerBuildRequest events, and puts them
    into a queue. It also catches _NeedJob events, from a
    WorkerConnection, and responds to them with _HaveAJob events,
    when it has an outstanding request.
    
    '''
    
    def __init__(self):
        distbuild.StateMachine.__init__(self, 'idle')

    def setup(self):
        distbuild.crash_point()

        logging.debug('WBQ: Setting up %s' % self)
        self._available_workers = []
        self._jobs = JobQueue(owner='controller')
        self._idgen = distbuild.IdentifierGenerator('Job')

        spec = [
            # state, source, event_class, new_state, callback
            ('idle', WorkerBuildQueuer, WorkerBuildRequest, 'idle',
                self._handle_request),
            ('idle', WorkerBuildQueuer, WorkerCancelPending, 'idle',
                self._handle_cancel),

            ('idle', WorkerConnection, _NeedJob, 'idle', self._handle_worker),
            ('idle', WorkerConnection, _JobStarted, 'idle',
                self._set_job_started),
            ('idle', WorkerConnection, _JobFinished, 'idle',
                self._set_job_finished),
            ('idle', WorkerConnection, _JobFailed, 'idle',
                self._set_job_failed),

            ('idle', WorkerConnection, _Disconnected, 'idle',
                self._handle_worker_disconnected),
        ]
        self.add_transitions(spec)

    def _set_job_started(self, event_source, event):
        event.job.set_state('running')

    def _set_job_finished(self, event_source, event):
        job = event.job
        job.set_state('complete')
        self._jobs.remove(job)

    def _set_job_failed(self, event_source, event):
        job = event.job
        job.set_state('failed')
        self._jobs.remove(job)

    def _handle_request(self, event_source, event):
        distbuild.crash_point()

        logging.debug('Handling build request for %s' % event.initiator_id)
        logging.debug('Current jobs: %s' % self._jobs)
        logging.debug('Workers available: %d' % len(self._available_workers))

        # Have we already made a job for this thing?
        # If so, add our initiator id to the existing job
        # If not, create a job

        job = self._jobs.get_active_job_for_artifact(
            event.artifact.basename())
        if job is not None:
            job.initiators.append(event.initiator_id)

            # Completed jobs are not tracked, so we can't tell here if the
            # job was already built. It shouldn't happen, because the
            # BuildController has already checked for cached builds when
            # annotating the build graph.
            if job.running():
                logging.debug('Worker build step already started: %s' %
                    event.artifact.basename())
                progress = WorkerBuildStepAlreadyStarted(event.initiator_id,
                    event.artifact.cache_key, job.who.name())
            else:
                logging.debug('Job created but not building yet '
                    '(waiting for a worker to become available): %s' %
                    event.artifact.basename())
                progress = WorkerBuildWaiting(event.initiator_id,
                    event.artifact.cache_key)

            self.mainloop.queue_event(WorkerConnection, progress)
        else:
            logging.debug('WBQ: Creating job for: %s' % event.artifact.name)
            job = Job(self._idgen.next(), event.artifact, event.initiator_id)
            self._jobs.add(job)

            if self._available_workers:
                self._give_job(job)
            else:
                progress = WorkerBuildWaiting(event.initiator_id,
                    event.artifact.cache_key)
                self.mainloop.queue_event(WorkerConnection, progress)

    def _handle_cancel(self, event_source, event):

        def cancel_this(job):
            if event.initiator_id not in job.initiators:
                return False    # not for us

            name = job.artifact.basename()
            job_id = job.id

            logging.debug('Checking whether to remove job %s with job id %s',
                          name, job_id)

            if len(job.initiators) == 1:
                if job.running() or job.failed():
                    logging.debug('NOT removing running job %s with job id %s '
                                  '(WorkerConnection will cancel job)',
                                  name, job_id)
                else:
                    logging.debug('Removing job %s with job id %s',
                                  name, job_id)
                    return True
            else:
                # Don't cancel, but still remove this initiator from
                # the list of initiators
                logging.debug('NOT removing job %s with job id %s '
                              'other initiators want it: %s', name, job_id,
                              [i for i in job.initiators
                                if i != event.initiator_id])

                job.initiators.remove(event.initiator_id)

            return False

        self._jobs.remove_jobs(
            [job for job in self._jobs if cancel_this(job)])

    def _handle_worker(self, event_source, event):
        distbuild.crash_point()

        logging.debug('WBQ: Adding worker to queue: %s', event.who.name())
        self._available_workers.append(event)

        logging.debug('Current jobs: %s', self._jobs)
        logging.debug('Workers available: %d', len(self._available_workers))

        job = self._jobs.get_next_job()

        if job:
            self._give_job(job)
            
    def _give_job(self, job):
        worker = self._available_workers.pop(0)
        job.who = worker.who

        logging.debug(
            'WBQ: Giving %s to %s' %
                (job.artifact.name, worker.who.name()))

        self.mainloop.queue_event(worker.who, _HaveAJob(job))

    def _handle_worker_disconnected(self, event_source, event):
        self._remove_worker(self, event.who)

    def _remove_worker(self, worker):
        logging.debug('WBQ: Removing worker %s from queue', worker.name())

        # There should only be one InitiatorConnection instance per worker in
        # the _available_workers list. But anything can happen in space! So we
        # take care to remove all GiveJob messages in the list that came from
        # the disconnected worker, not the first.
        self._available_workers = filter(
            lambda worker_msg: worker_msg.who != worker,
            self._available_workers)


class WorkerConnection(distbuild.StateMachine):

    '''Communicate with a single worker.'''
    
    _request_ids = distbuild.IdentifierGenerator('WorkerConnection')
    _initiator_request_map = collections.defaultdict(set)

    def __init__(self, cm, conn, writeable_cache_server, 
                 worker_cache_server_port, morph_instance):
        distbuild.StateMachine.__init__(self, 'idle')
        self._cm = cm
        self._conn = conn
        self._writeable_cache_server = writeable_cache_server
        self._worker_cache_server_port = worker_cache_server_port
        self._morph_instance = morph_instance
        self._debug_exec_output = False

        addr, port = self._conn.getpeername()
        name = socket.getfqdn(addr)
        self._worker_name = '%s:%s' % (name, port)

        self._jobs = JobQueue(owner=self.name())

    def name(self):
        return self._worker_name

    def __str__(self):
        return self.name()

    def setup(self):
        distbuild.crash_point()

        logging.debug('WC: Setting up instance %s' % repr(self))
    
        self._jm = distbuild.JsonMachine(self._conn)
        self.mainloop.add_state_machine(self._jm)
        
        spec = [
            # state, source, event_class, new_state, callback
            ('idle', self._jm, distbuild.JsonEof, None,  self._disconnected),
            ('idle', self, _HaveAJob, 'building', self._start_build),
            
            ('building', distbuild.BuildController,
                distbuild.BuildCancel, 'building',
                self._maybe_cancel),

            ('building', self._jm, distbuild.JsonEof, None,
                self._disconnected),
            ('building', self._jm, distbuild.JsonNewMessage, 'building',
                self._handle_json_message),
            ('building', self, _BuildFailed, 'idle', self._request_job),
            ('building', self, _BuildCancelled, 'idle', self._request_job),
            ('building', self, _BuildFinished, 'caching',
                self._request_caching),

            ('caching', self._jm, distbuild.JsonEof, None, self._disconnected),
            ('caching', distbuild.HelperRouter, distbuild.HelperResult,
                'caching', self._maybe_handle_helper_result),
            ('caching', self, _Cached, 'idle', self._request_job),
            ('caching', self, _BuildFailed, 'idle', self._request_job),
        ]
        self.add_transitions(spec)
        
        self._request_job(None, None)

    def _maybe_cancel(self, event_source, build_cancel):
        logging.debug('WC: BuildController %r requested a cancel',
                      event_source)

        initiator_id = build_cancel.id
        for job in self._jobs.active_jobs():
            self._remove_initiator_from_job(job, initiator_id)

    def _remove_initiator_from_job(self, job, initiator_id):
        '''Remove the given initiator from 'job', and cancel it if needed.

        If the given initiator is not interested in 'job', nothing happens.

        '''

        if initiator_id in job.initiators:
            if len(job.initiators) == 1:
                self._cancel_job(job)
            else:
                logging.debug(
                    'WC: Not cancelling running job %s, other initiators want '
                    'it done: %s', job.artifact.basename(),
                    [i for i in job.initiators if i != initiator_id])
                job.initiators.remove(initiator_id)

    def _cancel_job(self, job):
        logging.debug(
            'WC: Cancelling job %s, currently building on %s',
            job.artifact.basename(), self.name())

        msg = distbuild.message('exec-cancel', id=job.id)
        self._jm.send(msg)
        # NOTE: We need to set job's state to 'failed' as soon as possible.
        # The exec-response message may take a while to arrive, and if we
        # wait for that then it is possible that another build-request for
        # the same artifact could be made, which would result in the build
        # not being started since the cancelled job would still be 'running'.
        # The new build will then fail when the exec-response for the old
        # build finally arrives.
        job.set_state('failed')
        self.mainloop.queue_event(self, _BuildCancelled())

    def _disconnected(self, event_source, event):
        distbuild.crash_point()

        logging.debug('WC: Disconnected from worker %s' % self.name())
        self.mainloop.queue_event(WorkerConnection, _Disconnected(self))

        self.mainloop.queue_event(self._cm, distbuild.Reconnect())

    def _sanity_check_new_job(self, job):
        # There's nothing that we can really do if the controller goes nuts
        # (there's no 'reject job' message), but we can at least log warnings.

        if self._jobs.has_job_for_artifact(job.artifact.basename()):
            logging.warn('Worker %s already has job %s', self.name(),
                         job.id)

        running_jobs = self._jobs.running_jobs()
        if len(running_jobs) != 0:
            logging.warn('This worker already has running jobs: %s',
                         running_jobs)

    def _start_build(self, event_source, event):
        distbuild.crash_point()

        job = event.job
        self._sanity_check_new_job(job)

        self._jobs.add(job)
        job.set_state('running')

        logging.debug('WC: starting build: %s for %s' %
                      (job.artifact.name, job.initiators))

        argv = [
            self._morph_instance,
            'worker-build',
            '--build-log-on-stdout',
            job.artifact.name,
        ]

        msg = distbuild.message('exec-request',
            id=job.id,
            argv=argv,
            stdin_contents=distbuild.encode_artifact_reference(job.artifact),
        )
        self._jm.send(msg)

        # The WorkerBuildQueuer object will set the job state to 'running' when
        # it receives the _JobStarted message.
        self.mainloop.queue_event(WorkerConnection, _JobStarted(job))

        started = WorkerBuildStepStarted(job.initiators,
            job.artifact.cache_key, self.name())
        self.mainloop.queue_event(WorkerConnection, started)

    def _handle_json_message(self, event_source, event):
        '''Handle JSON messages from the worker.'''

        distbuild.crash_point()

        logging.debug(
            'WC: from worker %s: %r' % (self._worker_name, event.msg))

        handlers = {
            'exec-output': self._handle_exec_output,
            'exec-response': self._handle_exec_response,
        }

        handler = handlers[event.msg['type']]
        job = self._jobs.get_job_for_id(event.msg['id'])

        if job:
            handler(event.msg, job)
        else:
            logging.warn('Received %s for unknown job %s',
                         event.msg['type'], event.msg['id'])

    def _handle_exec_output(self, msg, job):
        '''Handle output from a job that the worker is or was running.'''

        new = dict(msg)
        new['ids'] = job.initiators

        if self._debug_exec_output:
            logging.debug('WC: emitting: %s', repr(new))
        self.mainloop.queue_event(
            WorkerConnection,
            WorkerBuildOutput(new, job.artifact.cache_key))

    def _handle_exec_response(self, msg, job):
        '''Handle completion of a job that the worker is or was running.'''

        logging.debug('WC: finished building: %s' % job.artifact.name)
        logging.debug('initiators that need to know: %s' % job.initiators)

        new = dict(msg)
        new['ids'] = job.initiators

        if new['exit'] != 0:
            # Build failed.
            new_event = WorkerBuildFailed(new, job.artifact.cache_key)
            self.mainloop.queue_event(WorkerConnection, new_event)
            self.mainloop.queue_event(WorkerConnection, _JobFailed(job))
            self.mainloop.queue_event(self, _BuildFailed())
        else:
            # Build succeeded. We have more work to do: caching the result.
            self.mainloop.queue_event(self, _BuildFinished(job))
            job._exec_response = new

    def _request_job(self, event_source, event):
        distbuild.crash_point()
        self.mainloop.queue_event(WorkerConnection, _NeedJob(self))

    def _request_caching(self, event_source, event):
        # This code should be moved into the morphlib.remoteartifactcache
        # module. It would be good to share it with morphlib.buildcommand,
        # which also wants to fetch artifacts from a remote cache.
        distbuild.crash_point()

        logging.debug('Requesting shared artifact cache to get artifacts')

        job = event.job
        kind = job.artifact.kind

        if kind == 'chunk':
            artifact_names = job.artifact.source_artifact_names

            suffixes = ['%s.%s' % (kind, name) for name in artifact_names]
            suffixes.append('build-log')
        else:
            filename = '%s.%s' % (kind, job.artifact.name)
            suffixes = [filename]

            if kind == 'stratum':
                suffixes.append(filename + '.meta')
        
        suffixes = [urllib.quote(x) for x in suffixes]
        suffixes = ','.join(suffixes)

        worker_host = self._conn.getpeername()[0]
        
        url = urlparse.urljoin(
            self._writeable_cache_server, 
            '/1.0/fetch?host=%s:%d&cacheid=%s&artifacts=%s' %
                (urllib.quote(worker_host),
                 self._worker_cache_server_port,
                 urllib.quote(job.artifact.cache_key),
                 suffixes))

        msg = distbuild.message(
            'http-request', id=self._request_ids.next(), url=url,
            method='GET', body=None, headers=None)
        job._cache_request_id = msg['id']
        req = distbuild.HelperRequest(msg)
        self.mainloop.queue_event(distbuild.HelperRouter, req)
        
        progress = WorkerBuildCaching(job.initiators,
            job.artifact.cache_key)
        self.mainloop.queue_event(WorkerConnection, progress)

    def _maybe_handle_helper_result(self, event_source, event):
        # This function is called for every HelperResult message sent by the
        # controller's distbuild-helper process (for every completed or failed
        # http-request).
        for job in self._jobs:
            if event.msg['id'] == getattr(job, '_cache_request_id', None):
                self._handle_helper_result_for_job(job, event)

    def _handle_helper_result_for_job(self, job, event):
        distbuild.crash_point()

        logging.debug('caching: event.msg: %s' % repr(event.msg))
        if event.msg['status'] == httplib.OK:
            logging.debug('Shared artifact cache population done')

            finished_event = WorkerBuildFinished(
                job._exec_response, job.artifact.cache_key, job.who.name())
            self.mainloop.queue_event(WorkerConnection, finished_event)

            self.mainloop.queue_event(self, _Cached())
        else:
            logging.error(
                'Failed to populate artifact cache: %s %s' %
                    (event.msg['status'], event.msg['body']))

            # We will attempt to remove this job twice
            # unless we mark it as failed before the BuildController
            # processes the WorkerBuildFailed event.
            #
            # The BuildController will not try to cancel jobs that have
            # been marked as failed.
            self.mainloop.queue_event(WorkerConnection, _JobFailed(job))

            failed_event = WorkerBuildFailed(
                job._exec_response, job.artifact.cache_key)
            self.mainloop.queue_event(WorkerConnection, failed_event)

            self.mainloop.queue_event(self, _BuildFailed())

        # Caching is the last step of a job, so we're now done with it.
        self.mainloop.queue_event(WorkerConnection, _JobFinished(job))