aboutsummaryrefslogtreecommitdiff
path: root/nar-herder/cached-compression.scm
blob: 6148c7e2c5047cdf3315f2e963eb850dac677880 (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
;;; Nar Herder
;;;
;;; Copyright © 2022, 2023 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 (nar-herder cached-compression)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 atomic)
  #:use-module (ice-9 threads)
  #:use-module (logging logger)
  #:use-module (prometheus)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (guix store)
  #:use-module ((guix utils) #:select (compressed-file?
                                       call-with-decompressed-port))
  #:use-module ((guix build utils)
                #:select (dump-port mkdir-p))
  #:use-module (nar-herder utils)
  #:use-module (nar-herder database)
  #:export (make-maybe-trigger-creation-of-compressed-nars))

;; Nar caching overview
;;
;; On start
;;  - Compute the size of each cached compression directory
;;  - Remove database entries if they're missing from the directory
;;  - Remove files if they're missing from the database
;;  - Remove (random TODO) files if the size exceeds the max
;;
;; On nar usage
;;  - Bump count
;;  - If count is sufficient
;;    - Trigger generation of the cached nar
;;      - Skip if the work queue already includes this job
;;      - At the start of the job, check for a database entry and exit
;;        early if one exists
;;    - Update the size of the cached compression directory
;;      - If the size exceeds the max, remove (random TODO) files

(define (perform-cached-compression-startup database
                                            enabled-cached-compressions)
  (let* ((database-entries-missing-files
          ;; List tracking entries in the database for cached files,
          ;; where the file is missing from the disk.
          ;;
          ;; These database entries will be removed at the end of the
          ;; startup procecss.
          '())

         ;; alist of compression to hash table of files
         ;;
         ;; Entries from the hash tables will be removed when the
         ;; database entry is processed below, so these hash tables
         ;; will be left reflecting files in the directories, but with
         ;; no entry in the database.
         ;;
         ;; These files will be deleted at the end of the startup
         ;; process.
         (files-by-compression
          (map
           (match-lambda
             ((compression . details)
              (let ((result (make-hash-table)))
                (for-each
                 (lambda (file)
                   (hash-set! result file #t))
                 (scandir (assq-ref details 'directory)
                          (negate (cut member <> '("." "..")))))
                (cons compression
                      result))))
           enabled-cached-compressions))

         (cached-bytes-by-compression
          (database-fold-cached-narinfo-files
           database
           (lambda (details result)
             (let ((compression
                    (assq-ref details 'compression))
                   (filename
                    (store-path-base
                     (assq-ref details 'store-path))))

               (let ((files-hash
                      (assq-ref files-by-compression compression)))
                 (if (hash-ref files-hash filename)
                     (begin
                       (hash-remove! files-hash filename)

                       `((,compression . ,(+ (assq-ref details 'size)
                                             (or (assq-ref result compression)
                                                 0)))
                         ,@(alist-delete compression result)))

                     ;; Database entry, but file missing
                     (begin
                       (set! database-entries-missing-files
                             (cons details
                                   database-entries-missing-files))
                       result)))))
           (map
            (lambda (compression)
              (cons compression 0))
            (map car enabled-cached-compressions)))))

    ;; Delete cached files with no database entries
    (for-each
     (match-lambda
       ((compression . hash-table)
        (let ((count (hash-count (const #t)
                                 hash-table)))
          (unless (= 0 count)
            (let ((directory
                   (assq-ref
                    (assq-ref enabled-cached-compressions compression)
                    'directory)))
              (simple-format #t "deleting ~A cached files from ~A\n"
                             count
                             directory)
              (hash-for-each
               (lambda (filename _)
                 (delete-file (string-append directory "/" filename)))
               hash-table))))))
     files-by-compression)

    ;; Delete database entries where the file is missing
    (let ((count (length database-entries-missing-files)))
      (unless (= 0 count)
        (simple-format
         #t
         "deleting ~A cached_narinfo_files entries due to missing files\n"
         count)

        (for-each
         (lambda (details)
           (database-remove-cached-narinfo-file database
                                                (assq-ref details 'narinfo-id)
                                                (symbol->string
                                                 (assq-ref details 'compression))))
         database-entries-missing-files)))

    cached-bytes-by-compression))

(define (maybe-remove-cached-files-for-compression database
                                                   enabled-cached-compressions
                                                   compression
                                                   cached-bytes-by-compression-box
                                                   nar-cache-bytes-metric)
  (let* ((compression-details
          (assq-ref enabled-cached-compressions
                    compression))
         (max-size
          (assq-ref compression-details
                    'directory-max-size)))
    (when max-size
      (let ((current-size
             (assq-ref
              (atomic-box-ref cached-bytes-by-compression-box)
              compression)))
        (when (> current-size max-size)
          (let ((bytes-to-remove
                 (- current-size max-size)))
            (log-msg 'DEBUG
                     "looking to remove " bytes-to-remove " bytes of "
                     compression " compressed nars")

            (match
                (database-fold-cached-narinfo-files
                 database
                 (lambda (details result)
                   (match result
                     (#(finished? bytes files-to-remove)
                      (if finished?
                          result
                          (if (eq? (assq-ref details 'compression)
                                   compression)
                              (let ((new-bytes
                                     (+ bytes
                                        (assq-ref details 'size))))
                                ;; finished if enough bytes are going
                                ;; to be removed
                                (vector (>= new-bytes bytes-to-remove)
                                        new-bytes
                                        (cons details files-to-remove)))
                              result)))))
                 #(#f 0 ()))
              (#(#t bytes-for-removal files-to-remove-details)
               (log-msg 'DEBUG "removing " (length files-to-remove-details)
                        " " compression " compressed nars from the cache")

               ;; Use an explicit transaction as it handles the
               ;; database being busy,
               (database-call-with-transaction
                database
                (lambda _
                  (for-each
                   (lambda (details)
                     ;; Remove all the database entries first, as
                     ;; that'll stop these files appearing in narinfos
                     (database-remove-cached-narinfo-file
                      database
                      (assq-ref details 'narinfo-id)
                      (symbol->string compression)))
                   files-to-remove-details)))

               (let ((directory
                      (assq-ref compression-details 'directory)))
                 (for-each
                  (lambda (details)
                    (let ((filename
                           (string-append
                            directory "/"
                            (basename (assq-ref details 'store-path)))))
                      (log-msg 'DEBUG "deleting " filename)
                      (delete-file filename)))
                  files-to-remove-details)

                 (let* ((cached-bytes-by-compression
                         (atomic-box-ref cached-bytes-by-compression-box))
                        (new-cached-bytes
                         (- (assq-ref cached-bytes-by-compression
                                      compression)
                            bytes-for-removal)))
                   (atomic-box-set!
                    cached-bytes-by-compression-box
                    (alist-cons
                     compression
                     new-cached-bytes
                     (alist-delete
                      compression
                      cached-bytes-by-compression)))

                   (metric-set
                    nar-cache-bytes-metric
                    new-cached-bytes
                    #:label-values `((compression . ,compression)))

                   (log-msg 'DEBUG "finished removing " bytes-for-removal
                            " bytes of " compression
                            " cached nars from " directory
                            " (new size " new-cached-bytes ")")))))))))))

(define* (make-maybe-trigger-creation-of-compressed-nars
          database
          metrics-registry
          nar-source
          enabled-cached-compressions
          cached-compression-min-uses
          #:key (cached-compression-workers 2) scheduler)

  (define nar-cache-bytes-metric
    (make-gauge-metric metrics-registry
                       "nar_cache_size_bytes"
                       #:labels '(compression)))

  (let ((consider-nar-request-channel
         (make-worker-thread-set
          (lambda ()
            (let ((cached-bytes-by-compression-box
                   (make-atomic-box #f)))
              (atomic-box-set!
               cached-bytes-by-compression-box
               (perform-cached-compression-startup database
                                                   enabled-cached-compressions))

              (for-each
               (match-lambda
                 ((compression . bytes)
                  (metric-set
                   nar-cache-bytes-metric
                   bytes
                   #:label-values `((compression . ,compression)))))
               (atomic-box-ref cached-bytes-by-compression-box))

              ;; Remove cached files if the max size is exceeded
              (for-each
               (match-lambda
                 ((compression . _)
                  (maybe-remove-cached-files-for-compression
                   database
                   enabled-cached-compressions
                   compression
                   cached-bytes-by-compression-box
                   nar-cache-bytes-metric)))
               (atomic-box-ref cached-bytes-by-compression-box))

              (list cached-bytes-by-compression-box)))
          #:name "comp nar req"
          ;; Just make one thread, as this thread won't do much work
          ;; and relies on a hash table that shouldn't be accessed by
          ;; multiple threads
          #:parallelism 1)))

    (define with-usage-hash-table
      (let ((nar-cached-compression-usage-hash-table
             (make-hash-table 65536)))
        (lambda (proc)
          (monitor
           (proc nar-cached-compression-usage-hash-table)))))

    (define (proc narinfo-id cached-bytes-by-compression-box)
      (let* ((existing-cached-files
              ;; This is important both to avoid trying to create
              ;; files twice, but also in the case where additional
              ;; compressions are enabled and more files need to be
              ;; generated
              (database-select-cached-narinfo-files-by-narinfo-id
               database
               narinfo-id))
             (existing-compressions
              (map (lambda (details)
                     (assq-ref details 'compression))
                   existing-cached-files))
             (missing-compressions
              (lset-difference eq?
                               (map car enabled-cached-compressions)
                               existing-compressions))
             (compress-file?
              (let ((url
                     (assq-ref
                      (first
                       (database-select-narinfo-files-by-narinfo-id
                        database
                        narinfo-id))
                      'url)))

                ;; TODO: Maybe this should be configurable?
                (not (compressed-file? url)))))

        (when (and compress-file?
                   (not (null? missing-compressions)))
          (let ((new-count
                 (with-usage-hash-table
                  (lambda (usage)
                    (let ((val (+ 1
                                  (or (hash-ref usage narinfo-id)
                                      0))))
                      (hash-set! usage
                                 narinfo-id
                                 val)
                      val)))))

            (when (> new-count
                     cached-compression-min-uses)
              (for-each
               (lambda (missing-compression)
                 (let ((new-bytes
                        (make-compressed-nar
                         database
                         nar-source
                         enabled-cached-compressions
                         narinfo-id
                         missing-compression
                         #:level (assq-ref
                                  (assq-ref enabled-cached-compressions
                                            missing-compression)
                                  'level))))

                   ;; Do this here, after creating the new cached nar,
                   ;; just in case there's a lack of space
                   (monitor
                    (let* ((cached-bytes-by-compression
                            (atomic-box-ref
                             cached-bytes-by-compression-box))
                           (updated-bytes-for-compression
                            (+ (assq-ref cached-bytes-by-compression
                                         missing-compression)
                               new-bytes)))

                      (metric-set
                       nar-cache-bytes-metric
                       updated-bytes-for-compression
                       #:label-values `((compression . ,missing-compression)))

                      (atomic-box-set!
                       cached-bytes-by-compression-box
                       (alist-cons
                        missing-compression
                        updated-bytes-for-compression
                        (alist-delete
                         missing-compression
                         cached-bytes-by-compression)))

                      (maybe-remove-cached-files-for-compression
                       database
                       enabled-cached-compressions
                       missing-compression
                       cached-bytes-by-compression-box
                       nar-cache-bytes-metric)))))
               missing-compressions)

              (with-usage-hash-table
               (lambda (usage)
                 (hash-remove! usage narinfo-id))))))))

    (let ((process-job
           count-jobs
           count-threads
           list-jobs
           (create-work-queue cached-compression-workers
                              proc
                              #:name "cached compression")))

      (lambda (narinfo-id)
        (spawn-fiber
         (lambda ()
           (call-with-worker-thread
            consider-nar-request-channel
            (lambda (cached-bytes-by-compression-box)
              (let ((in-progress-narinfo-ids
                     (map car (list-jobs))))

                (unless (member narinfo-id in-progress-narinfo-ids)
                  (process-job narinfo-id cached-bytes-by-compression-box)))
              #t)))
         scheduler
         #:parallel? #t)))))

(define* (make-compressed-nar database
                              nar-source
                              enabled-cached-compressions
                              narinfo-id
                              target-compression
                              #:key level)
  (define cached-compression-details
    (assq-ref enabled-cached-compressions target-compression))

  (define narinfo-files
    (database-select-narinfo-files-by-narinfo-id database
                                                 narinfo-id))

  (log-msg 'DEBUG "making " target-compression " for "
           (uri-decode
            (basename
             (assq-ref (first narinfo-files) 'url))))

  (let* ((source-narinfo-file
          ;; There's no specific logic to this, it should be possible
          ;; to use any file
          (first narinfo-files))
         (source-filename
          (cond
           ((string-prefix? "http" nar-source)
            (let* ((output-port (mkstemp "/tmp/nar-herder-source-nar-XXXXXX"))
                   (filename
                    (port-filename output-port))
                   (uri
                    (string->uri
                     (string-append nar-source
                                    (assq-ref source-narinfo-file 'url)))))

              (log-msg 'DEBUG "downloading " (uri->string uri))
              (with-exception-handler
                  (lambda (exn)
                    (close-port output-port)
                    (delete-file filename)
                    (raise-exception exn))
                (lambda ()
                  (with-fibers-port-timeouts
                   (lambda ()
                     (call-with-values
                         (lambda ()
                           (let ((port
                                  socket
                                  (open-socket-for-uri* uri)))
                             (http-get uri
                                       #:port port
                                       #:decode-body? #f
                                       #:streaming? #t)))
                       (lambda (response body)
                         (unless (= (response-code response)
                                    200)
                           (error "unknown response code"
                                  (response-code response)))

                         (dump-port body output-port)))
                     (close-port output-port))
                   #:timeout 30))
                #:unwind? #t)

              filename))
           ((string-prefix? "/" nar-source)
            (string-append
             ;; If it's a filename, then it's the canonical path to
             ;; the storage directory
             nar-source
             (uri-decode (assq-ref source-narinfo-file 'url))))
           (else
            (error "unknown nar source")))))

    (let* ((dest-directory
            (assq-ref cached-compression-details
                      'directory))
           (dest-filename
            (string-append
             dest-directory
             "/"
             (uri-decode
              (basename
               (assq-ref source-narinfo-file 'url)))))
           (tmp-dest-filename
            (string-append dest-filename ".tmp")))

      (when (file-exists? tmp-dest-filename)
        (delete-file tmp-dest-filename))
      (when (file-exists? dest-filename)
        (delete-file dest-filename))

      (mkdir-p dest-directory)

      (call-with-input-file
          source-filename
        (lambda (source-port)
          (call-with-decompressed-port
              (string->symbol
               (assq-ref source-narinfo-file
                         'compression))
              source-port
            (lambda (decompressed-source-port)
              (let ((call-with-compressed-output-port*
                     (match target-compression
                       ('gzip
                        (@ (zlib) call-with-gzip-output-port))
                       ('lzip
                        (@ (lzlib) call-with-lzip-output-port))
                       ('zstd
                        (@ (zstd) call-with-zstd-output-port))
                       ('none
                        (lambda (port proc)
                          (proc port))))))
                (apply
                 call-with-compressed-output-port*
                 (open-output-file tmp-dest-filename)
                 (lambda (compressed-port)
                   (dump-port decompressed-source-port
                              compressed-port))
                 (if level
                     `(#:level ,level)
                     '())))))))
      (rename-file
       tmp-dest-filename
       dest-filename)

      (when (string-prefix? "http" nar-source)
        (log-msg 'DEBUG "deleting temporary file " source-filename)
        (delete-file source-filename))

      (let ((bytes
             (stat:size (stat dest-filename))))

        (database-call-with-transaction
         database
         (lambda _
           (database-insert-cached-narinfo-file
            database
            narinfo-id
            bytes
            (symbol->string target-compression))))

        (log-msg 'DEBUG "created " dest-filename)

        bytes))))