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
|
;;; Guix Build Coordinator
;;;
;;; Copyright © 2020 Christopher Baines <mail@cbaines.net>
;;;
;;; This file is part of the guix-build-coordinator.
;;;
;;; The Guix Build Coordinator 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; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; The Guix Build Coordinator 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 the guix-data-service. If not, see
;;; <http://www.gnu.org/licenses/>.
(define-module (guix-build-coordinator build-allocator)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-43)
#:use-module (ice-9 match)
#:use-module (prometheus)
#:use-module (guix memoization)
#:use-module (guix-build-coordinator utils)
#:use-module (guix-build-coordinator datastore)
#:use-module (guix-build-coordinator coordinator)
#:export (basic-build-allocation-strategy
derivation-ordered-build-allocation-strategy))
(define* (basic-build-allocation-strategy datastore
#:key
(planned-builds-for-agent-limit 512)
metrics-registry)
(define (log . args)
(when #f
(simple-format #t "allocator: ~A\n"
(string-join (map (lambda (arg)
(simple-format #f "~A" arg))
args)
" "))))
(define allocator-considered-builds-metric
(when metrics-registry
(let ((name "allocator_considered_builds"))
(or (metrics-registry-fetch-metric metrics-registry
name)
(make-gauge-metric metrics-registry
name)))))
(define cached/list-builds-for-output
(mlambda (output)
(datastore-list-builds-for-output datastore output)))
(let* ((agents (datastore-list-agents datastore))
(agent-tags (map (lambda (agent-details)
(let ((agent-id (assq-ref agent-details 'uuid)))
(cons agent-id
(datastore-fetch-agent-tags datastore
agent-id))))
agents))
(requested-systems-by-agent
(map (lambda (agent-details)
(let ((agent-id (assq-ref agent-details 'uuid)))
(cons agent-id
(datastore-agent-requested-systems
datastore
agent-id))))
agents))
(builds
(datastore-list-unprocessed-builds datastore))
(builds-count
(length builds))
(setup-failures-hash
(datastore-fetch-setup-failures datastore))
(derived-build-priorities-hash
;; Mapping from build_id to priority, initialised at the individual
;; priorities for the builds
(let ((table (make-hash-table builds-count)))
(for-each (lambda (build)
(hash-set! table
(assq-ref build 'uuid)
(assq-ref build 'priority)))
builds)
table))
;; build_id -> (list build_id ...) indicating that for the build_id
;; used as the key, the build_ids given as the value should happen
;; first.
(build-ordering-hints-hash
(make-hash-table))
(builds-ready-to-go-hash
(make-hash-table)))
(define systems-for-builds
;; TODO Should be one query
(let ((table (make-hash-table builds-count)))
(for-each (lambda (build)
(let ((build-id (assq-ref build 'uuid)))
(hash-set! table
build-id
(datastore-find-build-derivation-system
datastore
build-id))))
builds)
table))
(define tags-for-build
(let ((build-tags (make-hash-table)))
(lambda (build-id)
(let ((tags (hash-ref build-tags build-id)))
(if (eq? #f tags)
(let ((tags (datastore-fetch-build-tags datastore
build-id)))
(hash-set! build-tags
build-id
tags)
tags)
tags)))))
(define (filter-builds-for-agent agent-id)
(define requested-systems
(assoc-ref requested-systems-by-agent
agent-id))
(define (relevant-setup-failure? setup-failure)
(log "setup failure:" setup-failure)
(let ((failure-reason
(assq-ref setup-failure 'failure-reason)))
(cond
((string=? failure-reason "missing_inputs")
;; missing_inputs setup failures that should be resolved have been
;; filtered out by this point, so this is a relevant setup failure
#t)
((string=? failure-reason "could_not_delete_outputs")
;; This problem might go away, but just don't try the same agent
;; again for now.
(string=? (assq-ref setup-failure 'agent-id)
agent-id))
((string=? failure-reason "error_fetching_derivation")
;; This problem might go away, but just don't try the same agent
;; again for now.
(string=? (assq-ref setup-failure 'agent-id)
agent-id))
(else
(error "Unknown setup failure " failure-reason)))))
(define (agent-tags-match-build-tags build-id)
(let ((agent-tags (assoc-ref agent-tags agent-id)))
(or (null? agent-tags)
(let ((build-tags (tags-for-build build-id)))
(every (match-lambda
((agent-key . agent-value)
(any
(match-lambda
((_ . build-value)
(string=? agent-value build-value)))
(vector-fold
(lambda (_ result tag)
(if (string=? (car tag)
agent-key)
(cons tag result)
result))
'()
build-tags))))
(vector->list agent-tags))))))
(lambda (build)
(log "build:" (assq-ref build 'uuid))
(and
(or (null? requested-systems)
(let ((build-system (hash-ref systems-for-builds
(assq-ref build 'uuid))))
(member build-system requested-systems)))
(agent-tags-match-build-tags (assq-ref build 'uuid))
(let* ((build-id (assq-ref build 'uuid))
(setup-failures-for-build
(or (hash-ref setup-failures-hash build-id)
'()))
(relevant-setup-failures
(filter relevant-setup-failure?
setup-failures-for-build)))
(log "relevant setup failures:" relevant-setup-failures)
(if (null? relevant-setup-failures)
#t
#f)))))
(define (build-sorting-function-for-agent agent-id)
(lambda (a b)
(let ((a-priority (hash-ref derived-build-priorities-hash
(assq-ref a 'uuid)))
(b-priority (hash-ref derived-build-priorities-hash
(assq-ref b 'uuid))))
(< b-priority a-priority))))
(define (limit-planned-builds builds)
(if planned-builds-for-agent-limit
(if (> (length builds) planned-builds-for-agent-limit)
(take builds planned-builds-for-agent-limit)
builds)
builds))
(define (limit-processed-sublists lists)
(if planned-builds-for-agent-limit
(let loop ((remaining-lists lists)
(kept-lists '())
(count 0))
(if (or (>= count planned-builds-for-agent-limit)
(null? remaining-lists))
(reverse kept-lists)
(loop (cdr remaining-lists)
(cons (first remaining-lists)
kept-lists)
(+ count
(length (first remaining-lists))))))
lists))
(define (break-builds-in-to-priority-sublists all-builds)
(define (build-priority build)
(hash-ref derived-build-priorities-hash
(assq-ref build 'uuid)))
(let loop ((result '())
(builds all-builds)
(current-priority-builds '())
(current-priority (build-priority (first all-builds))))
(if (null? builds)
(reverse (cons (reverse current-priority-builds) result))
(let ((build (car builds)))
(if (= (build-priority build)
current-priority)
(loop result
(cdr builds)
(cons build current-priority-builds)
current-priority)
(loop (cons (reverse current-priority-builds) result)
(cdr builds)
(list build)
(build-priority build)))))))
(define (sort-priority-sublist builds-list)
(define (build-id build)
(assq-ref build 'uuid))
(define (builds-that-should-happen-first build-id)
(hash-ref build-ordering-hints-hash
build-id
'()))
(define seen-builds-hash
(make-hash-table))
(define deferred-builds-last-count 0)
(define (all-inputs-built? derivation)
(let ((inputs (datastore-find-derivation-inputs
datastore
derivation)))
(every (lambda (input-details)
(let ((output (assq-ref input-details 'output)))
(any (lambda (output-build)
(string=? (or (assq-ref output-build 'result)
"unknown")
"success"))
(cached/list-builds-for-output output))))
inputs)))
(define (push-deferred-builds-to-the-back builds-list)
(let loop ((result '())
(builds builds-list)
(builds-to-defer '()))
(if (null? builds)
(if (null? builds-to-defer)
result
(if (= (length builds-to-defer)
deferred-builds-last-count)
;; There can be loops in the graph of missing inputs, so
;; give up if the ordering doesn't seem to end
(append result builds-to-defer)
(begin (set! deferred-builds-last-count
(length builds-to-defer))
(loop result
builds-to-defer
'()))))
(let ((build (car builds)))
(if (every (lambda (required-build-id)
(hash-ref seen-builds-hash required-build-id))
(builds-that-should-happen-first (build-id build)))
(begin
(hash-set! seen-builds-hash (build-id build) #t)
(loop (cons build result)
(cdr builds)
builds-to-defer))
(loop result
(cdr builds)
(cons build builds-to-defer)))))))
(let-values (((ready-builds other-builds)
(partition (lambda (build)
(or (hash-ref builds-ready-to-go-hash
(assq-ref build 'uuid))
(all-inputs-built?
(assq-ref build 'derivation-name))))
builds-list)))
(append ready-builds
(push-deferred-builds-to-the-back other-builds))))
(define (treat-build-as-required build-id required-build-id priority)
(let ((required-build-derived-priority
(hash-ref derived-build-priorities-hash
required-build-id)))
;; Add an entry to the build-ordering-hints-hash to indicate that
;; required-build-id should happen prior to build-id
(hash-set!
build-ordering-hints-hash
build-id
(cons required-build-id
(hash-ref build-ordering-hints-hash
build-id
'())))
(when (> priority required-build-derived-priority)
;; Bump the priority of the build
(hash-set! derived-build-priorities-hash
required-build-id
priority))))
(when metrics-registry
(metric-set allocator-considered-builds-metric
(length builds)))
;; Go through the setup failures and look specifically at the
;; missing_inputs ones. Eliminate any missing_inputs failures if all the
;; missing inputs appear to have been built successfully, and update the
;; derived-build-priorities-hash to reflect the priorities of builds based
;; on the builds that would be "unblocked" if they were completed.
(for-each
(lambda (setup-failure-build-id)
(let ((setup-failures
(filter
(lambda (setup-failure)
(string=? "missing_inputs"
(assq-ref setup-failure 'failure-reason)))
(hash-ref setup-failures-hash
setup-failure-build-id)))
(setup-failure-build-derived-priority
(hash-ref derived-build-priorities-hash
setup-failure-build-id)))
(for-each
(lambda (setup-failure)
(let ((outputs-should-be-available
(map
(lambda (output)
(let ((builds (cached/list-builds-for-output output)))
(if (any (lambda (output-build)
(string=? (or (assq-ref output-build 'result)
"unknown")
"success"))
builds)
#t
(begin
(for-each
(lambda (build)
(when (and (not (assq-ref build 'processed))
;; The build might not be included
;; in this allocation, so skip it
;; if it isn't
(hash-ref derived-build-priorities-hash
(assq-ref build 'uuid)))
(treat-build-as-required
setup-failure-build-id
(assq-ref build 'uuid)
setup-failure-build-derived-priority)))
builds)
#f))))
(datastore-list-setup-failure-missing-inputs
datastore
(assq-ref setup-failure 'id)))))
(when (every (lambda (x) (eq? x #t))
outputs-should-be-available)
(hash-set! builds-ready-to-go-hash
setup-failure-build-id
#t)
;; At least one build for each missing input has been
;; successful, so delete the setup failure from the list of
;; setup failures in the hash
(hash-set! setup-failures-hash
setup-failure-build-id
(delete setup-failure
(hash-ref setup-failures-hash
setup-failure-build-id))))))
setup-failures)))
(hash-map->list
(lambda (key value) key)
setup-failures-hash))
(let ((result
(append-map
(lambda (agent-id)
(log "considering builds for" agent-id)
(let ((builds-sorted-by-derived-priority
(sort-list (filter (filter-builds-for-agent agent-id)
builds)
(build-sorting-function-for-agent agent-id))))
(if (null? builds-sorted-by-derived-priority)
'()
(let ((final-ordered-builds
(concatenate
(map sort-priority-sublist
(limit-processed-sublists
(break-builds-in-to-priority-sublists
builds-sorted-by-derived-priority))))))
(let ((builds-for-agent
(limit-planned-builds final-ordered-builds)))
(map (lambda (build-id ordering)
(list build-id
agent-id
ordering))
(map (lambda (build)
(assq-ref build 'uuid))
builds-for-agent)
(iota (length builds-for-agent))))))))
(map (lambda (agent)
(assq-ref agent 'uuid))
agents))))
(log "finished")
result)))
(define* (derivation-ordered-build-allocation-strategy
datastore
#:key
(planned-builds-for-agent-limit 512)
(builds-created-after "datetime('now', '-60 days')")
metrics-registry)
(define (log . args)
(when #f
(simple-format #t "allocator: ~A\n"
(string-join (map (lambda (arg)
(simple-format #f "~A" arg))
args)
" "))))
(define allocator-considered-builds-metric
(when metrics-registry
(let ((name "allocator_considered_builds"))
(or (metrics-registry-fetch-metric metrics-registry
name)
(make-gauge-metric metrics-registry
name)))))
(let* ((agents (datastore-list-agents datastore))
(agent-tags (map (lambda (agent-details)
(let ((agent-id (assq-ref agent-details 'uuid)))
(cons agent-id
(datastore-fetch-agent-tags datastore
agent-id))))
agents))
(requested-systems-by-agent
(map (lambda (agent-details)
(let ((agent-id (assq-ref agent-details 'uuid)))
(cons agent-id
(datastore-agent-requested-systems
datastore
agent-id))))
agents))
(setup-failures-hash
(datastore-fetch-setup-failures datastore)))
(define (limit-planned-builds build-ids)
(if planned-builds-for-agent-limit
(if (> (length build-ids) planned-builds-for-agent-limit)
(take build-ids planned-builds-for-agent-limit)
build-ids)
build-ids))
(let-values
(((derived-build-priorities-hash
build-ids-for-unprocessed-builds-with-built-inputs)
(with-time-logging
"fetch-build-ids-and-propagated-priorities-for-unprocessed-builds"
(datastore-fetch-build-ids-and-propagated-priorities-for-unprocessed-builds
datastore
builds-created-after))))
(define systems-for-builds
(map (lambda (build-id)
(cons build-id
(datastore-find-build-derivation-system
datastore
build-id)))
build-ids-for-unprocessed-builds-with-built-inputs))
(define tags-for-build
(let ((build-tags (make-hash-table)))
(lambda (build-id)
(let ((tags (hash-ref build-tags build-id)))
(if (eq? #f tags)
(let ((tags (datastore-fetch-build-tags datastore
build-id)))
(hash-set! build-tags
build-id
tags)
tags)
tags)))))
(define (filter-builds-for-agent agent-id)
(define requested-systems
(assoc-ref requested-systems-by-agent
agent-id))
(define (relevant-setup-failure? setup-failure)
(log "setup failure:" setup-failure)
(let ((failure-reason
(assq-ref setup-failure 'failure-reason)))
(cond
((string=? failure-reason "missing_inputs")
#f)
((string=? failure-reason "could_not_delete_outputs")
;; This problem might go away, but just don't try the same agent
;; again for now.
(string=? (assq-ref setup-failure 'agent-id)
agent-id))
((string=? failure-reason "error_fetching_derivation")
;; This problem might go away, but just don't try the same agent
;; again for now.
(string=? (assq-ref setup-failure 'agent-id)
agent-id))
(else
(error "Unknown setup failure " failure-reason)))))
(define (agent-tags-match-build-tags build-id)
(let ((agent-tags (assoc-ref agent-tags agent-id)))
(or (null? agent-tags)
(let ((build-tags (tags-for-build build-id)))
(every (match-lambda
((agent-key . agent-value)
(any
(match-lambda
((_ . build-value)
(string=? agent-value build-value)))
(vector-fold
(lambda (_ result tag)
(if (string=? (car tag)
agent-key)
(cons tag result)
result))
'()
build-tags))))
(vector->list agent-tags))))))
(lambda (build-id)
(log "build:" build-id)
(and
(or (null? requested-systems)
(let ((build-system (assoc-ref systems-for-builds build-id)))
(member build-system requested-systems)))
(agent-tags-match-build-tags build-id)
(let* ((setup-failures-for-build
(or (hash-ref setup-failures-hash build-id)
'()))
(relevant-setup-failures
(filter relevant-setup-failure?
setup-failures-for-build)))
(log "relevant setup failures:" relevant-setup-failures)
(if (null? relevant-setup-failures)
#t
#f)))))
(define (build-sorting-function-for-agent agent-id)
(lambda (a b)
(let ((a-priority (hash-ref derived-build-priorities-hash a))
(b-priority (hash-ref derived-build-priorities-hash b)))
(< b-priority a-priority))))
(when metrics-registry
(metric-set allocator-considered-builds-metric
(length build-ids-for-unprocessed-builds-with-built-inputs)))
(let ((result
(append-map
(lambda (agent-id)
(log "considering builds for" agent-id)
(let ((build-ids-sorted-by-derived-priority
(sort
(filter (filter-builds-for-agent agent-id)
build-ids-for-unprocessed-builds-with-built-inputs)
(build-sorting-function-for-agent agent-id))))
(if (null? build-ids-sorted-by-derived-priority)
'()
(let ((builds-for-agent
(limit-planned-builds
build-ids-sorted-by-derived-priority)))
(map (lambda (build-id ordering)
(list build-id
agent-id
ordering))
builds-for-agent
(iota (length builds-for-agent)))))))
(map (lambda (agent)
(assq-ref agent 'uuid))
agents))))
(log "finished")
result))))
|