aboutsummaryrefslogtreecommitdiff
path: root/guix-build-coordinator/agent.scm
blob: 0520da2461031f340206d85c5c67a18e027c0b22 (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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
;;; 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 agent)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-43)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:use-module (ice-9 futures)
  #:use-module (ice-9 atomic)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 textual-ports)
  #:use-module (rnrs bytevectors)
  #:use-module (web http)
  #:use-module (oop goops)
  #:use-module (logging logger)
  #:use-module (logging port-log)
  #:use-module (prometheus)
  #:use-module (lzlib)
  #:use-module (guix store)
  #:use-module (guix progress)
  #:use-module (guix derivations)
  #:use-module (guix base32)
  #:use-module (guix serialization)
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix-build-coordinator utils)
  #:use-module (guix-build-coordinator agent-messaging)
  #:use-module (guix-build-coordinator agent-messaging abstract)
  #:export (run-agent))

(define-record-type <upload-progress>
  (make-upload-progress file bytes-sent bytes-hashed
                        total-bytes)
  upload-progress?
  (file          upload-progress-file
                 set-upload-progress-file!)
  (bytes-sent    upload-progress-bytes-sent
                 set-upload-progress-bytes-sent!)
  (bytes-hashed  upload-progress-bytes-hashed
                 set-upload-progress-bytes-hashed!)
  (total-bytes   upload-progress-total-bytes
                 set-upload-progress-total-bytes!))

(define temporary-directory
  (or (getenv "TMPDIR") "/tmp"))

(define (run-agent uuid
                   coordinator-interface
                   systems
                   max-parallel-builds
                   max-allocated-builds
                   max-parallel-uploads
                   derivation-substitute-urls
                   non-derivation-substitute-urls
                   metrics-file
                   max-1min-load-average)
  (define lgr (make <logger>))
  (define port-log (make <port-log>
                     #:port (current-output-port)
                     #:formatter
                     ;; In guile-lib v0.2.8 onwards, the formatter is
                     ;; called with more arguments
                     (lambda args      ; lvl, time, str
                       (format #f "~a (~5a): ~a~%"
                               (strftime "%F %H:%M:%S" (localtime
                                                        (second args)))
                               (first args)
                               (third args)))))

  (define metrics-enabled?
    (and (not (string-null? metrics-file))
         (let ((directory (dirname metrics-file)))
           (or (file-exists? directory)
               (begin
                 (simple-format (current-error-port)
                                "skipping writing metrics as ~A does not exist\n"
                                directory)
                 #f)))
         (with-exception-handler
             (lambda (exn)
               (simple-format
                (current-error-port)
                "skipping writing metrics, encountered exception ~A\n"
                exn)
               #f)
           (lambda ()
             (let ((test-file (string-append metrics-file "-tmp")))
               (call-with-output-file test-file
                 (lambda (port)
                   (display "test" port)))
               (delete-file test-file)
               #t))
           #:unwind? #t)))

  (define metrics-registry
    (make-metrics-registry
     #:namespace "guixbuildcoordinator_agent"))

  (define (write-metrics)
    (when metrics-enabled?
      (write-textfile metrics-registry
                      metrics-file)))

  (define (make-uploads-updater list-jobs)
    (lambda ()
      (let ((upload-progress-records
             (filter-map
              (match-lambda
                ((build upload-progress thunk)
                 (if (upload-progress-file upload-progress)
                     upload-progress
                     #f)))
              (list-jobs))))
        (log-msg lgr 'INFO (length upload-progress-records)
                 " uploads in progress")

        (for-each
         (lambda (upload-progress)
           (when upload-progress
             (log-msg lgr 'INFO
                      (upload-progress-file upload-progress)
                      ": "
                      (let ((total-bytes
                             (upload-progress-total-bytes upload-progress))
                            (bytes-sent
                             (upload-progress-bytes-sent upload-progress))
                            (bytes-hashed
                             (upload-progress-bytes-hashed upload-progress)))
                        (if (and (= bytes-sent total-bytes)
                                 (> bytes-hashed 0))
                            (format
                             #f
                             "uploaded, ~2,2f/~2,2fMB hashed"
                             (/ bytes-hashed 1000000)
                             (/ total-bytes 1000000))
                            (format
                             #f
                             "~2,2f/~2,2fMB sent"
                             (/ bytes-sent 1000000)
                             (/ total-bytes 1000000)))))))
         upload-progress-records))))

  (define (process-job build perform-post-build-actions uploads-updater)
    (let ((build-id (assoc-ref build "uuid"))
          (derivation-name (or (assoc-ref build "derivation_name")
                               (assoc-ref build "derivation-name")))
          (submit-outputs? (match (assoc "submit_outputs" build)
                             ((_ . val) val)
                             (#f #t)))) ; default to submitting outputs

      (define (get-compressed-outputs store)
        (let ((outputs
               (derivation-outputs
                (read-derivation-from-file* derivation-name))))
          (for-each
           (match-lambda
             ((_ . output)
              (add-temp-root store (derivation-output-path output))))
           outputs)

          (map
           (match-lambda
             ((output-name . output)
              (let* ((file (derivation-output-path output))
                     (directory (or (getenv "TMPDIR") "/tmp"))
                     (template  (string-append
                                 directory
                                 "/guix-build-coordinator-file.XXXXXX"))
                     (out       (mkstemp! template)))
                (log-msg lgr 'INFO
                         build-id
                         ": compressing " file " -> " template
                         " prior to sending")
                (call-with-lzip-output-port out
                  (lambda (port)
                    (write-file file port))
                  #:level 9)
                (close-port out)

                (log-msg lgr 'INFO build-id ": finished compressing " file)

                (cons output-name template))))
           outputs)))

      (define (get-output-details store outputs)
        (map
         (match-lambda
           ((output-name . output)
            (let ((path-info
                   (query-path-info
                    store
                    (derivation-output-path output))))
              `((name       . ,output-name)
                (hash       . ,(bytevector->nix-base32-string
                                (path-info-hash path-info)))
                (size       . ,(path-info-nar-size path-info))
                (references . ,(list->vector
                                (map basename
                                     (path-info-references
                                      path-info))))))))
         outputs))

      (log-msg lgr 'INFO
               build-id
               ": setting up to build: "
               derivation-name)
      (with-store/non-blocking store
        (let ((pre-build-status
               (call-with-duration-metric
                metrics-registry
                "pre_build_duration_seconds"
                (lambda ()
                  (pre-build-process lgr
                                     store
                                     build-id
                                     derivation-substitute-urls
                                     non-derivation-substitute-urls
                                     derivation-name))
                #:buckets (list 1 2.5 5 10 25 50 100 200 500 1000 (inf)))))
          (write-metrics)
          (if (eq? (assq-ref pre-build-status 'result) 'success)
              (begin
                (log-msg lgr 'INFO
                         build-id
                         ": setup successful, building: "
                         derivation-name)
                (report-build-start coordinator-interface
                                    build-id
                                    #:log (build-log-procedure lgr build-id))
                (let* ((derivation (read-derivation-from-file* derivation-name))
                       (result (perform-build lgr store build-id derivation))
                       ;; TODO Check this handles timezones right
                       (end-time (localtime (time-second (current-time)) "UTC"))
                       (output-details
                        (if result
                            (get-output-details
                             store
                             (derivation-outputs derivation))
                            #f))
                       (compressed-outputs
                        (and result submit-outputs?
                             (get-compressed-outputs store))))

                  (perform-post-build-actions
                   (list
                    build
                    (make-upload-progress #f 0 0 0)
                    (lambda (upload-progress)
                      (agent-submit-log-file lgr
                                             coordinator-interface
                                             build-id derivation-name)

                      (if result
                          (post-build-success lgr
                                              coordinator-interface
                                              build-id
                                              derivation
                                              end-time
                                              submit-outputs?
                                              output-details
                                              compressed-outputs
                                              upload-progress
                                              uploads-updater)
                          (post-build-failure lgr
                                              coordinator-interface
                                              build-id
                                              end-time))
                      (log-msg lgr 'INFO
                               build-id
                               ": finished processing: "
                               derivation-name)))
                   #:priority
                   (list (assoc-ref build "derived_priority")
                         (if (and result submit-outputs?)
                             (fold
                              (lambda (output result)
                                (let ((file (cdr output)))
                                  (+ (stat:size (stat file))
                                     result)))
                              0
                              compressed-outputs)
                             0)))))

              (begin
                (log-msg lgr 'INFO
                         build-id
                         ": setup failure: "
                         (assq-ref pre-build-status 'failure_reason))
                (report-setup-failure coordinator-interface
                                      build-id
                                      pre-build-status
                                      #:log (build-log-procedure lgr
                                                                 build-id))))))))

  (define (current-max-builds)
    (let ((current-load (get-load-average #:period 1)))
      (if (>= current-load max-1min-load-average)
          1
          max-parallel-builds)))

  (add-handler! lgr port-log)
  (open-log! lgr)

  (log-msg lgr 'INFO "starting agent " uuid)

  (let ((left-over-temporary-files
         (scandir temporary-directory
                  (lambda (name)
                    (string-prefix? "guix-build-coordinator-file" name)))))
    (unless (null? left-over-temporary-files)
      (log-msg lgr 'INFO "deleting " (length left-over-temporary-files)
               " left over temporary files")
      (for-each (lambda (name)
                  (let ((full-filename
                         (string-append temporary-directory "/" name)))
                    (log-msg lgr 'INFO "deleting " full-filename)
                    (delete-file full-filename)))
                left-over-temporary-files)))

  (log-msg lgr 'INFO "connecting to coordinator "
           (slot-ref coordinator-interface 'coordinator-uri))
  (let* ((perform-post-build-actions
          count-post-build-jobs
          count-post-build-threads
          list-post-build-jobs
          (create-work-queue max-parallel-uploads
                             (lambda (build upload-progress proc)
                               (proc upload-progress))
                             #:name "upload"

                             ;; The priority here is a list where the
                             ;; first element is the build priority,
                             ;; and the second is the number of bytes
                             ;; to upload
                             #:priority<?
                             (lambda (a b)
                               (let ((a-priority (first a))
                                     (b-priority (first b)))
                                 (if (= a-priority b-priority)
                                     (> (second a)
                                        (second b))

                                     ;; Prioritise uploading smaller
                                     ;; files first
                                     (< a-priority
                                        b-priority))))))

         (uploads-updater
          (make-uploads-updater list-post-build-jobs))

         (process-job-with-queue
          count-jobs
          count-threads
          list-jobs
          (create-work-queue current-max-builds
                             (lambda (build)
                               (process-job build
                                            perform-post-build-actions
                                            uploads-updater))
                             #:thread-start-delay
                             (make-time
                              time-duration
                              0
                              (max 5
                                   (- 135
                                      (* 120
                                         (/ max-parallel-builds 64)))))
                             #:thread-stop-delay
                             (make-time
                              time-duration
                              0
                              20)
                             #:name "job")))
    (define (display-info)
      (display
       (simple-format
        #f "current threads: ~A current jobs: ~A\n~A\n"
        (count-threads)
        (+ (count-jobs) (count-post-build-jobs))
        (string-append
         (string-join
          (map (match-lambda
                 ((build-details)
                  (simple-format
                   #f " - ~A (derived priority: ~A)
     ~A"
                   (assoc-ref build-details "uuid")
                   (assoc-ref build-details "derived_priority")
                   (or
                    (assoc-ref build-details "derivation_name")
                    (assoc-ref build-details "derivation-name")))))
               (list-jobs))
          "\n")
         "\n"
         (string-join
          (map (match-lambda
                 ((build-details upload-progress _)
                  (simple-format
                   #f " - ~A (derived priority: ~A)
     ~A~A"
                   (assoc-ref build-details "uuid")
                   (assoc-ref build-details "derived_priority")
                   (or
                    (assoc-ref build-details "derivation_name")
                    (assoc-ref build-details "derivation-name"))
                   (if (upload-progress-file upload-progress)
                       (simple-format
                        #f "
     uploading ~A (~A/~A)"
                        (upload-progress-file upload-progress)
                        (upload-progress-bytes-sent upload-progress)
                        (upload-progress-total-bytes upload-progress))
                       ""))))
               (list-post-build-jobs))
          "\n")))
       (current-error-port)))

    (let ((details (submit-status coordinator-interface
                                  'idle
                                  #:1min-load-average
                                  (get-load-average #:period 1)
                                  #:system-uptime (get-uptime)
                                  #:initial-status-update? #t
                                  #:log (build-log-procedure lgr)
                                  #:retry-times 60)))
      (for-each
       (lambda (job-args)
         (process-job-with-queue job-args))
       (vector->list (assoc-ref details "builds")))

      (unless (running-on-the-hurd?)
        (call-with-new-thread
         (lambda ()
           (set-thread-name "signal info")

           (sigaction SIGUSR1
             (lambda _
               (display-info)))

           (while #t (sleep 100000))))

        (call-with-new-thread
         (lambda ()
           (set-thread-name "console info")

           (let loop ((line (get-line (current-input-port))))
             (unless (eof-object? line)
               (display-info)
               (loop (get-line (current-input-port))))))))

      (call-with-new-thread
       (lambda ()
         (set-thread-name "submit status")

         (while #t
           (with-exception-handler
               (lambda (exn)
                 (log-msg lgr 'WARN "exception submitting status: " exn))
             (lambda ()
               (submit-status coordinator-interface
                              (if (= (+ (count-jobs)
                                        (count-post-build-jobs))
                                     0)
                                  'idle
                                  'active)
                              #:1min-load-average
                              (get-load-average #:period 1)
                              #:system-uptime (get-uptime)
                              #:log (build-log-procedure lgr)))
             #:unwind? #t)

           (sleep 30))))

      (while #t
        (let ((current-threads (count-threads))
              (job-count (count-jobs)))
          (if (and (or (not max-allocated-builds)
                       (< (+ job-count (count-post-build-jobs))
                          max-allocated-builds))
                   (or (< job-count current-threads)
                       (= job-count 0)))
              (let* ((queued-build-ids
                      (map (lambda (job-args)
                             (assoc-ref (car job-args) "uuid"))
                           (append
                            (list-jobs)
                            (list-post-build-jobs))))
                     (fetched-builds
                      (with-exception-handler
                          (lambda (exn)
                            (log-msg lgr 'WARN "error fetching builds: " exn)

                            '())
                        (lambda ()
                          (fetch-builds-for-agent
                           coordinator-interface
                           systems
                           (+ (max current-threads 1)
                              (count-post-build-jobs))
                           #:log (build-log-procedure lgr)))
                        #:unwind? #t))
                     (new-builds
                      (remove (lambda (build)
                                (member (assoc-ref build "uuid")
                                        queued-build-ids))
                              fetched-builds)))

                (log-msg lgr 'INFO
                         "running " current-threads
                         " threads, currently allocated "
                         (length fetched-builds) " builds")
                (log-msg lgr 'INFO
                         "starting " (length new-builds)
                         " new "
                         (if (= (length new-builds) 1) "build" "builds"))
                (for-each
                 (lambda (job-args)
                   (process-job-with-queue job-args))
                 new-builds)

                (when (null? new-builds)
                  (sleep 5)))
              (sleep 3)))))))

(define* (build-log-procedure lgr #:optional build-id)
  (lambda (level . components)
    (apply log-msg
           lgr
           level
           (if build-id
               (cons*
                build-id
                ": "
                components)
               components))))

(define (agent-submit-log-file lgr coordinator-interface
                               build-id derivation-name)
  (let ((log-file
         ;; TODO Not sure if retrying here is useful?
         (retry-on-error
          (lambda ()
            (let ((file (derivation-log-file derivation-name)))
              (or file
                  (raise-exception
                   (make-exception-with-message
                    (simple-format #f "log file missing for ~A (~A)"
                                   derivation-name build-id))))))
          #:times 3
          #:delay 3)))
    (retry-on-error
     (lambda ()
       (log-msg lgr 'INFO
                build-id ": uploading log file "
                log-file)
       (submit-log-file
        coordinator-interface
        build-id
        log-file
        #:log (build-log-procedure lgr build-id)))
     #:times 12
     #:delay 10)))

(define (pre-build-process lgr
                           store
                           build-id
                           derivation-substitute-urls
                           non-derivation-substitute-urls
                           derivation-name)
  (define (find-missing-inputs derivation inputs)
    (log-msg lgr 'DEBUG
             build-id ": checking the availability of build inputs")
    (let* ((output-paths
            (append-map derivation-input-output-paths inputs))
           (missing-paths
            (remove (lambda (path)
                      (if (valid-path? store path)
                          (begin
                            (add-temp-root store path)
                            #t)
                          #f))
                    output-paths))
           (path-substitutes
            (begin
              (with-store/non-blocking substitute-store
                (apply set-build-options
                       substitute-store
                       `(,@(if non-derivation-substitute-urls
                               (list #:substitute-urls
                                     non-derivation-substitute-urls)
                               '())
                         #:max-silent-time 120
                         #:timeout ,(* 10 60)))

                (unless non-derivation-substitute-urls
                  (log-msg lgr 'WARN
                           "non-derivation-substitute-urls unset,
unable to query substitute servers without caching"))

                (map (lambda (file)
                       (log-msg lgr 'DEBUG
                                build-id ": looking for missing input " file)
                       (let ((substitute-urls-with-substitute
                              (if (list? non-derivation-substitute-urls)
                                  (retry-on-error
                                   (lambda ()
                                     (with-port-timeouts
                                      (lambda ()
                                        (has-substiutes-no-cache?
                                         non-derivation-substitute-urls
                                         file))
                                      #:timeout (* 60 1000)))
                                   #:times 20
                                   #:delay (random 15))
                                  #f)))
                         (and
                          (if (eq? substitute-urls-with-substitute #f)
                              #t        ; keep going
                              (not (null? substitute-urls-with-substitute)))
                          (let ((log-port (open-output-string)))
                            ;; TODO Do something with the logged output
                            (parameterize ((current-build-output-port log-port))
                              (if (has-substitutes? substitute-store file)
                                  #t
                                  (begin
                                    (log-msg
                                     lgr 'WARN
                                     "a substitute should be available for " file ",
but the guix-daemon claims it's unavailable"
                                     (if substitute-urls-with-substitute
                                         (string-append
                                          ":\n"
                                          (string-join substitute-urls-with-substitute "\n"))
                                         ""))
                                    #f)))))))
                     missing-paths)))))

      (if (null? missing-paths)
          '()
          (if (member #f path-substitutes)
              (fold (lambda (file substitute-available? result)
                      (if substitute-available?
                          result
                          (cons file result)))
                    '()
                    missing-paths
                    path-substitutes)
              (begin
                (log-msg lgr 'DEBUG
                         build-id ": fetching " (length missing-paths)
                         " missing paths")
                (retry-on-error
                 (lambda ()
                   (with-store/non-blocking fetch-substitute-store
                     ;; Download the substitutes
                     (apply set-build-options
                            fetch-substitute-store
                            `(,@(if non-derivation-substitute-urls
                                    (list #:substitute-urls
                                          non-derivation-substitute-urls)
                                    '())
                              #:max-silent-time 120
                              #:timeout ,(* 60 60)))

                     (let ((log-port (open-output-string)))
                       (with-throw-handler #t
                         (lambda ()
                           (with-port-timeouts
                            (lambda ()
                              (parameterize
                                  ((current-build-output-port log-port))
                                (build-things fetch-substitute-store
                                              missing-paths)))
                            #:timeout (* 60 10 1000)))
                         (lambda (key . args)
                           (log-msg lgr 'ERROR
                                    "exception when fetching missing paths "
                                    key ": " args)
                           (display (get-output-string log-port))
                           (display (newline))
                           (close-output-port log-port)))))
                   (for-each (lambda (missing-path)
                               (add-temp-root store missing-path))
                             missing-paths))
                 #:times 12
                 #:delay (random 15))

                ;; Double check everything is actually present.
                (let ((missing-files (remove (lambda (path)
                                               (valid-path? store path))
                                             output-paths)))
                  (if (null? missing-files)
                      '()
                      (begin
                        (log-msg lgr 'WARN
                                 "failed to fetch substitutes for "
                                 missing-files)

                        (let ((unavailable-outputs
                               (delete-duplicates
                                (append-map
                                 (lambda (missing-output)
                                   (find-missing-substitutes-for-output
                                    store
                                    non-derivation-substitute-urls
                                    missing-output))
                                 missing-files))))

                          (log-msg lgr 'WARN
                                   "the following outputs are missing: "
                                   (string-join
                                    (map (lambda (output)
                                           (string-append
                                            " - " output))
                                         unavailable-outputs)))

                          (if (null? unavailable-outputs) ; TODO This probably
                                                          ; shouldn't happen
                              missing-files
                              unavailable-outputs))))))))))

  (define (delete-outputs store derivation)
    (let* ((outputs (derivation-outputs derivation))
           (output-file-names
            (map derivation-output-path (map cdr outputs))))
      (if (any file-exists? output-file-names)
          (let ((log-port (open-output-string)))
            (catch
              #t
              (lambda ()
                (log-msg lgr 'DEBUG build-id ": deleting "
                         (if (= (length output-file-names) 1)
                             "output"
                             "outputs"))
                ;; There can be issues deleting links when collecting garbage
                ;; from multiple threads
                (monitor
                 ;; TODO Do something with the logged output
                 (parameterize ((current-build-output-port log-port))
                   (delete-paths store output-file-names)))
                #t)
              (lambda (key args)
                (display (get-output-string log-port))
                (log-msg lgr 'ERROR
                         "delete-outputs: "
                         key args)
                #f)))
          #t)))

  (let ((derivation
         (if (valid-path? store derivation-name)
             (begin
               (add-temp-root store derivation-name)
               (read-derivation-from-file* derivation-name))
             (and
              (with-exception-handler
                  (lambda (exn)
                    (log-msg lgr 'ERROR
                             "exception when reading/fetching derivation: "
                             exn)
                    (log-msg lgr 'ERROR
                             "derivation-substitute-urls: "
                             derivation-substitute-urls)
                    #f)
                (lambda ()
                  (log-msg lgr 'DEBUG
                           build-id ": substituting derivation")
                  (retry-on-error
                   (lambda ()
                     (with-store/non-blocking fetch-substitute-store
                       ;; substitute-derivation uses set-build-options, so use
                       ;; a temporary store connection
                       (substitute-derivation fetch-substitute-store
                                              derivation-name
                                              #:substitute-urls
                                              derivation-substitute-urls))
                     (add-temp-root store derivation-name))
                   #:times 20
                   #:delay (random 15))
                  #t)
                #:unwind? #t)
              (read-derivation-from-file* derivation-name)))))
    (if derivation
        (begin
          (log-msg lgr 'DEBUG
                   build-id ": derivation read from file")
          (match (delete-outputs store derivation)
            (#t
             (let ((missing-inputs
                    (find-missing-inputs derivation (derivation-inputs derivation))))
               (if (null? missing-inputs)
                   '((result . success))
                   (begin
                     (log-msg lgr 'DEBUG
                              build-id ": missing inputs: "
                              missing-inputs)

                     `((result         . failure)
                       (failure_reason . missing_inputs)
                       (missing_inputs . ,(list->vector missing-inputs)))))))
            (failure
             '((result         . failure)
               (failure_reason . could_not_delete_outputs)))))
        '((result         . failure)
          (failure_reason . error_fetching_derivation)))))

(define (perform-build lgr store build-id derivation)
  (set-build-options store
                     #:use-substitutes? #f)

  (parameterize ((current-build-output-port (%make-void-port "w")))
    (with-exception-handler
        (lambda (exn)
          (if (store-protocol-error? exn)
              (cond
               ((= (store-protocol-error-status exn) 1)
                (log-msg lgr 'INFO build-id ": build failed"))
               ((= (store-protocol-error-status exn) 100)
                (log-msg lgr 'INFO build-id ": build failed (permanent failure)"))
               ((= (store-protocol-error-status exn) 101)
                (log-msg lgr 'INFO build-id ": build failed due to a timeout"))
               (else
                (log-msg lgr 'ERROR build-id ": unknown store protocol error: "
                         (store-protocol-error-message exn) "(status: "
                         (store-protocol-error-status exn) ")")
                (raise-exception exn)))
              (begin
                (log-msg lgr 'ERROR build-id ": unknown exception " exn)
                (raise-exception exn)))
          #f)
      (lambda ()
        (with-throw-handler #t
          (lambda ()
            (build-things store (list (derivation-file-name derivation)))
            (for-each (lambda (output)
                        (add-temp-root store output))
                      (map derivation-output-path
                           (map cdr (derivation-outputs derivation)))))
          (lambda (key . args)
            (unless (and (eq? key '%exception)
                         (store-protocol-error? (car args))
                         (let ((status (store-protocol-error-status
                                        (car args))))
                           (or (= status 1)
                               (= status 100)
                               (= status 101))))
              (simple-format (current-error-port)
                             "exception when performing build: ~A ~A\n"
                             key args)
              (backtrace))))
        #t)
      #:unwind? #t)))

(define (post-build-failure lgr
                            coordinator-interface
                            build-id end-time)
  (log-msg lgr 'INFO build-id ": build failed")
  (with-exception-handler
      (lambda (exn)
        (unless (agent-error-from-coordinator? exn)
          (raise-exception exn))

        (let ((details
               (agent-error-from-coordinator-details exn)))
          (if (string? details)
              (cond
               ((string=? details "build_already_processed")
                (log-msg lgr 'WARN
                         build-id ": coordinator indicates this build is already marked as processed")
                #t)
               (else
                (raise-exception
                 (make-exception
                  (make-exception-with-message
                   "unrecognised error from coordinator")
                  (make-exception-with-irritants
                   details)))))
              (raise-exception
               (make-exception
                (make-exception-with-message
                 "unrecognised error from coordinator")
                (make-exception-with-irritants
                 details))))))
    (lambda ()
      (submit-build-result coordinator-interface
                           build-id
                           `((result   . failure)
                             (end_time . ,(strftime "%F %T" end-time)))
                           #:log (build-log-procedure lgr build-id)))
    #:unwind? #t))

(define (post-build-success lgr coordinator-interface
                            build-id derivation end-time
                            submit-outputs?
                            output-details
                            compressed-outputs
                            upload-progress
                            uploads-updater)
  (define outputs
    (derivation-outputs derivation))

  (define (maybe-delete-compressed-outputs)
    (with-exception-handler
        (lambda (exn)
          (log-msg lgr 'ERROR
                   build-id ": exception deleting compressed outputs: "
                   exn))
      (lambda ()
        (when submit-outputs?
          (for-each
           (lambda (file)
             (log-msg lgr 'INFO
                      build-id ": deleting " file)
             (delete-file file))
           (map cdr compressed-outputs))))
      #:unwind? #t))

  (define (attempt-submit-build-result)
    (with-exception-handler
      (lambda (exn)
        (unless (agent-error-from-coordinator? exn)
          (raise-exception exn))

        (let ((details
               (agent-error-from-coordinator-details exn)))
          (if (string? details)
              (cond
               ((string=? details "build_already_processed")
                (log-msg lgr 'WARN
                         build-id ": coordinator indicates this build is already marked as processed")
                (maybe-delete-compressed-outputs)
                #t)
               ((string=? details "cannot_store_result_for_canceled_build")
                (log-msg lgr 'WARN
                         build-id ": coordinator indicates this build is now canceled")
                (maybe-delete-compressed-outputs)
                #t)
               ((string=? details "missing_build_log_file")
                ;; Retry submitting the log file
                (agent-submit-log-file lgr
                                       coordinator-interface
                                       build-id
                                       (derivation-file-name derivation))

                (attempt-submit-build-result))
               (else
                (maybe-delete-compressed-outputs)

                (raise-exception
                 (make-exception
                  (make-exception-with-message
                   "unrecognised error from coordinator")
                  (make-exception-with-irritants
                   details)))))
              (or
               (and=> (assoc-ref details "missing_output")
                      (lambda (missing-output-name)
                        (let ((missing-output
                               (any (match-lambda
                                      ((name . output)
                                       (if (string=? name missing-output-name)
                                           output
                                           #f)))
                                    outputs)))
                          (unless missing-output
                            (raise-exception
                             (make-exception
                              (make-exception-with-message
                               "unknown missing output")
                              (make-exception-with-irritants
                               missing-output-name))))

                          (log-msg lgr 'DEBUG
                                   build-id ": missing output: " missing-output)
                          (log-msg lgr 'DEBUG
                                   build-id ": pausing before attempting to send")

                          (sleep 30)

                          (log-msg lgr 'DEBUG
                                   build-id ": reattempting submitting: " missing-output)
                          (let ((compressed-file
                                 (assoc-ref compressed-outputs
                                            missing-output-name)))
                            (submit-one-output missing-output-name
                                               missing-output
                                               (stat:size (stat compressed-file))
                                               compressed-file)))

                        (attempt-submit-build-result)))
               (raise-exception
                (make-exception
                 (make-exception-with-message
                  "unrecognised error from coordinator")
                 (make-exception-with-irritants
                  details)))))))
      (lambda ()
        (submit-build-result
         coordinator-interface
         build-id
         `((result   . success)
           (end_time . ,(strftime "%F %T" end-time))
           (outputs  . ,(list->vector output-details)))
         #:log (build-log-procedure lgr build-id))

        (maybe-delete-compressed-outputs))
      #:unwind? #t))

  (define (submit-one-output output-name output bytes compressed-file)
    (define bytes-already-sent 0)

    (define reporter
      (make-progress-reporter
       (lambda ()
         (set-upload-progress-bytes-sent! upload-progress
                                          bytes-already-sent))
       (lambda (bytes)
         (set-upload-progress-bytes-sent! upload-progress
                                          (+ bytes-already-sent bytes)))
       (lambda ()
         (set-upload-progress-bytes-sent! upload-progress bytes))))

    (define last-progress-update-bytes-sent 0)
    (define last-progress-update-bytes-hashed 0)
    (define last-progress-update-time (time-second (current-time)))

    (define (report-bytes-hashed bytes-now-hashed)
      (set-upload-progress-bytes-hashed! upload-progress
                                         bytes-now-hashed)
      (when (or (> bytes-now-hashed
                   (+ last-progress-update-bytes-hashed 10000000))
                (and (> (time-second (current-time))
                        (+ last-progress-update-time 15))))
        (set! last-progress-update-bytes-hashed
              bytes-now-hashed)
        (set! last-progress-update-time
              (time-second (current-time)))

        (uploads-updater)))

    (set-upload-progress-file! upload-progress (derivation-output-path output))
    (set-upload-progress-total-bytes! upload-progress bytes)

    (log-msg lgr 'INFO
             build-id ": submitting output "
             (derivation-output-path output))
    (submit-output coordinator-interface
                   build-id output-name
                   compressed-file
                   #:log (build-log-procedure lgr build-id)
                   #:reporter-set-bytes-already-sent
                   (lambda (bytes)
                     (set! bytes-already-sent bytes))
                   #:reporter reporter
                   #:report-bytes-hashed report-bytes-hashed)

    (set-upload-progress-file! upload-progress #f)

    (log-msg lgr 'INFO
             build-id ": finished submitting output "
             (derivation-output-path output)))

  (if submit-outputs?
      (begin
        (log-msg lgr 'INFO build-id ": build successful, submitting outputs")
        (for-each (match-lambda
                    ((output-name . output)
                     (let ((compressed-file
                            (assoc-ref compressed-outputs output-name)))
                       (submit-one-output output-name
                                          output
                                          (stat:size (stat compressed-file))
                                          compressed-file))))
                  outputs)
        (log-msg lgr 'INFO build-id
                 ": finished submitting outputs, reporting build success"))
      (begin
        (log-msg lgr 'INFO build-id
                 ": build successful, skipping submitting outputs")
        (log-msg lgr 'INFO build-id
                 ": reporting build success")))

  (attempt-submit-build-result))