aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/substitutes.scm
blob: c7a45dc880d28de75bae4cd4eb313686e77cf5b6 (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
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019, 2020 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; 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
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-data-service substitutes)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (guix substitutes)
  #:use-module (guix narinfo)
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix-data-service utils)
  #:use-module (guix-data-service database)
  #:use-module (guix-data-service model build)
  #:use-module (guix-data-service model build-server)
  #:use-module (guix-data-service model git-branch)
  #:use-module (guix-data-service model git-repository)
  #:use-module (guix-data-service model nar)
  #:export (query-build-server-substitutes
            start-substitute-query-threads

            request-query-of-build-server-substitutes))

(define verbose-output?
  (make-parameter #f))

(define* (query-build-server-substitutes conn build-server-ids revision-commits
                                         outputs
                                         #:key verbose?)
  (parameterize
      ((verbose-output? verbose?))
    (let ((build-servers (select-build-servers conn)))
      (for-each
       (match-lambda
         ((id url lookup-all-derivations? lookup-builds?)
          (when (or (or (not build-servers)
                        (not build-server-ids))
                    (member id build-server-ids))
            (when lookup-all-derivations?
              (simple-format #t "\nQuerying ~A\n" url)
              (with-throw-handler #t
                (lambda ()
                  (fetch-narinfo-files conn id url revision-commits
                                       #:specific-outputs
                                       outputs))
                (lambda (key . args)
                  (simple-format
                   (current-error-port)
                   "exception in query-build-server: ~A ~A\n"
                   key args)
                  (backtrace)))))))
       build-servers))))

(define %narinfo-max-size
  (- (expt 2 (- (* 8 8) ;; 8 bytes
                1))
     1))

(define* (fetch-narinfo-files conn build-server-id build-server-url
                              revision-commits
                              #:key specific-outputs)
  (let loop ((last-id #f)
             (requests 0)
             (success-responses 0))
    (let ((outputs-chunk
           (or specific-outputs
               (select-outputs-without-known-nar-entries
                conn
                build-server-id
                revision-commits
                #:build-success-after
                (if (null? revision-commits)
                    (time-utc->date
                     (subtract-duration (current-time)
                                        (make-time time-duration 0 (* 60 5)))
                     0)                        ; tz-offset
                    #f)
                #:after-id last-id))))

      (unless (null? outputs-chunk)
        (let* ((narinfos
                (lookup-narinfos (string-trim-right build-server-url #\/)
                                 (map car outputs-chunk)))
               (narinfos-count
                (length narinfos))
               (total-requested
                (+ requests (length outputs-chunk)))
               (total-narinfos
                (+ success-responses narinfos-count)))

          (simple-format #t "Fetched ~A narinfos from ~A (total requested: ~A, total narinfos: ~A)\n"
                         (length narinfos)
                         build-server-url
                         total-requested
                         total-narinfos)

          (let ((filtered-narinfos
                 (filter-map
                  (lambda (narinfo)
                    (if (> (narinfo-size narinfo)
                           %narinfo-max-size)
                        (begin
                          (simple-format (current-error-port)
                                         "narinfo ~A has excessive size ~A\n"
                                         (narinfo-path narinfo)
                                         (narinfo-size narinfo))
                          #f)
                        narinfo))
                  narinfos)))

            (unless (null? filtered-narinfos)
              (with-postgresql-transaction
               conn
               (lambda (conn)
                 (record-narinfo-details-and-return-ids
                  conn
                  build-server-id
                  filtered-narinfos)))))

          (unless specific-outputs
            (loop (false-if-exception (second (last outputs-chunk)))
                  total-requested
                  total-narinfos)))))))

(define %substitute-query-channel #f)

(define (request-query-of-build-server-substitutes build-server-id
                                                   build-ids)
  (spawn-fiber
   (lambda ()
     (and=> %substitute-query-channel
            (lambda (channel)
              (put-message channel (cons build-server-id build-ids)))))))

(define (start-substitute-query-threads)
  (define channel
    (make-channel))

  (set! %substitute-query-channel channel)

  (call-with-new-thread
   (lambda ()
     (catch 'system-error
       (lambda ()
         (set-thread-name "request substitute query"))
       (const #t))

     (while #t
       (with-exception-handler
           (lambda (exn)
             (simple-format
              (current-error-port)
              "exception in request substitute query thread: ~A\n"
              exn))
         (lambda ()
           (with-throw-handler #t
             (lambda ()
               (with-postgresql-connection
                "request-substitute-query-thread"
                (lambda (conn)
                  (while #t
                    (match (get-message channel)
                      ((build-server-id . build-ids)

                       (let ((outputs
                              (delete-duplicates!
                               (append-map!
                                (lambda (build-id)
                                  (select-build-outputs conn build-id))
                                build-ids))))

                         (simple-format
                          (current-output-port)
                          "querying for ~A outputs from build server ~A\n"
                          (length outputs)
                          build-server-id)

                         (query-build-server-substitutes
                          conn
                          (list build-server-id)
                          #f
                          outputs))))))))
             (lambda _
               (backtrace))))
         #:unwind? #t))))

  (call-with-new-thread
   (lambda ()
     (catch 'system-error
       (lambda ()
         (set-thread-name "bulk substitute query"))
       (const #t))

     (while #t
       (with-exception-handler
           (lambda (exn)
             (simple-format (current-error-port)
                            "exception when querying substitutes: ~A\n"
                            exn))
         (lambda ()
           (with-postgresql-connection
            "substitute-query-thread"
            (lambda (conn)
              (for-each
               (match-lambda
                 ((git-repository-id rest ...)
                  (when (git-repository-query-substitutes? conn git-repository-id)
                    (for-each
                     (match-lambda
                       ((branch-name rest ...)
                        (and=> (latest-processed-commit-for-branch
                                conn
                                (number->string git-repository-id)
                                branch-name)
                               (lambda (commit)
                                 (query-build-server-substitutes
                                  conn
                                  #f ;; All build servers
                                  (list commit)
                                  #f)))))
                     (all-branches-with-most-recent-commit
                      conn
                      git-repository-id)))))
               (all-git-repositories conn))))

           (simple-format #t "finished checking substitutes, now sleeping\n")
           (sleep (* 60 30)))
         #:unwind? #t)))))