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
|
;;; 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-19)
#: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 (fibers)
#:use-module (fibers timers)
#:use-module (fibers channels)
#:use-module (fibers operations)
#: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 (start-cached-compression-management-fiber
start-cached-compression-removal-fiber
start-cached-compression-schedule-removal-fiber))
;; 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
;;
;; 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 bumping the atime of the file
;; - If the file doesn't exist, check if there's free space
;; - If there's free space, create the file
;;
;; Nar removal fiber
;; - Periodically remove nars that have been scheduled for removal
;; (when the scheduled time passes)
;;
;; Nar schedule removal fiber
;; - Periodically check for nars that haven't been accessed in some
;; time and schedule them for removal
(define (perform-cached-compression-startup database
enabled-cached-compressions
nar-cache-files)
(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)
(metric-increment nar-cache-files
#:label-values
`((compression . ,compression)))
`((,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))
;; This fiber manages metadata around cached compressions, and
;; delegates tasks to the thread pool to generate newly compressed
;; nars
(define* (start-cached-compression-management-fiber
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)))
(define nar-cache-files
(make-gauge-metric metrics-registry
"nar_cache_files"
#:labels '(compression)))
(define channel
(make-channel))
(let ((process-job
count-jobs
count-threads
list-jobs
(create-work-queue cached-compression-workers
(lambda (thunk)
(thunk))
#:name "cached compression")))
(define (consider-narinfo cached-bytes-by-compression
usage-hash-table
narinfo-id)
(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))
(narinfo-files
(database-select-narinfo-files-by-narinfo-id
database
narinfo-id))
(compress-file?
(let ((url (assq-ref (first narinfo-files) 'url)))
;; TODO: Maybe this should be configurable?
(not (compressed-file? url)))))
(when (and compress-file?
(not (null? missing-compressions)))
(let ((new-count
(let ((val (+ 1
(or (hash-ref usage-hash-table narinfo-id)
0))))
(hash-set! usage-hash-table
narinfo-id
val)
val)))
(when (and (> new-count
cached-compression-min-uses))
(let* ((narinfo-details
(database-select-narinfo database narinfo-id))
(nar-size
(assq-ref narinfo-details 'nar-size))
(compressions-with-space
(filter
(lambda (compression)
(let ((directory-max-size
(assq-ref
(assq-ref enabled-cached-compressions
compression)
'directory-max-size))
(current-directory-size
(assq-ref cached-bytes-by-compression
compression)))
(if directory-max-size
(< (+ current-directory-size
;; Assume the commpressed nar could be
;; as big as the uncompressed nar
nar-size)
directory-max-size)
#t)))
missing-compressions)))
(for-each
(lambda (compression)
(spawn-fiber
(lambda ()
(process-job
(lambda ()
(let ((new-bytes
(make-compressed-nar
narinfo-files
nar-source
enabled-cached-compressions
compression
#:level (assq-ref
(assq-ref enabled-cached-compressions
compression)
'level))))
(put-message channel
(list 'cached-narinfo-added
narinfo-id
compression
new-bytes
#f))))))))
compressions-with-space)))))))
(spawn-fiber
(lambda ()
(let ((initial-cached-bytes-by-compression
(perform-cached-compression-startup
database
enabled-cached-compressions
nar-cache-files))
(nar-cached-compression-usage-hash-table
(make-hash-table 65536)))
(for-each
(match-lambda
((compression . bytes)
(metric-set
nar-cache-bytes-metric
bytes
#:label-values `((compression . ,compression)))))
initial-cached-bytes-by-compression)
(let loop ((cached-bytes-by-compression
initial-cached-bytes-by-compression))
(match (get-message channel)
(('narinfo-id . narinfo-id)
(consider-narinfo cached-bytes-by-compression
nar-cached-compression-usage-hash-table
narinfo-id)
(loop cached-bytes-by-compression))
(((and (or 'cached-narinfo-added 'cached-narinfo-removed)
action)
narinfo-id compression size reply)
(let ((updated-bytes
((if (eq? action 'cached-narinfo-added)
+
-)
(or (assq-ref cached-bytes-by-compression
compression)
0)
size)))
(metric-set
nar-cache-bytes-metric
updated-bytes
#:label-values `((compression . ,compression)))
((if (eq? action 'cached-narinfo-added)
metric-increment
metric-decrement)
nar-cache-files
#:label-values `((compression . ,compression)))
;; Use an explicit transaction as it handles the
;; database being busy,
(database-call-with-transaction
database
(lambda _
(if (eq? action 'cached-narinfo-added)
(database-insert-cached-narinfo-file
database
narinfo-id
size
compression)
(let ((cached-narinfo-details
(database-select-cached-narinfo-file-by-narinfo-id-and-compression
database
narinfo-id
compression)))
;; It might not have been scheduled for
;; removal, but remove any schedule that
;; exists
(database-delete-scheduled-cached-narinfo-removal
database
(assq-ref cached-narinfo-details 'id))
;; Remove all the database entries first, as
;; that'll stop these files appearing in narinfos
(database-remove-cached-narinfo-file
database
narinfo-id
(symbol->string compression))))))
(hash-remove! nar-cached-compression-usage-hash-table
narinfo-id)
(when reply
(put-message reply #t))
(loop (alist-cons
cached-bytes-by-compression
updated-bytes
(alist-delete compression
cached-bytes-by-compression)))))))))
scheduler)
channel))
;; Periodically check for nars that haven't been accessed in some time
;; and schedule them for removal
(define (start-cached-compression-schedule-removal-fiber
database
cached-compression-management-channel
enabled-cached-compressions
cached-compression-removal-fiber-wakeup-channel)
(define (files-to-schedule-for-removal compression-details)
(let* ((directory (assq-ref compression-details 'directory))
(unused-removal-duration
(assq-ref compression-details 'unused-removal-duration))
(atime-threshold
(time-second
(subtract-duration (current-time)
unused-removal-duration))))
(scandir
directory
(lambda (filename)
(and
(< (stat:atime (stat (string-append directory "/" filename)))
atime-threshold)
(not (member filename '("." ".."))))))))
(define (schedule-removal compression compression-details)
(let* ((files (files-to-schedule-for-removal compression-details))
(all-cached-narinfo-file-details
(map
(lambda (file)
(database-select-cached-narinfo-file-by-hash
database
(string-take file 32) ; hash part
compression))
files))
(existing-scheduled-removals
(map
(lambda (cached-narinfo-file-details)
(database-select-scheduled-cached-narinfo-removal
database
(assq-ref cached-narinfo-file-details 'id)))
all-cached-narinfo-file-details)))
(for-each
(lambda (file cached-narinfo-file-details existing-scheduled-removal)
(unless existing-scheduled-removal
(let ((removal-time
;; The earliest this can be removed is the current
;; time, plus the TTL
(add-duration
(current-time)
(make-time time-duration
0
(assq-ref compression-details 'ttl)))))
(database-insert-scheduled-cached-narinfo-removal
database
(assq-ref cached-narinfo-file-details 'id)
removal-time))))
files
all-cached-narinfo-file-details
existing-scheduled-removals)
(when (any not existing-scheduled-removals)
;; Wake the cached compression removal fiber in case one of
;; the new scheduled removals is before it's scheduled to wake
;; up
(put-message
cached-compression-removal-fiber-wakeup-channel
#t))))
(spawn-fiber
(lambda ()
(let ((sleep-duration
(apply min
(map (lambda (compression-details)
(/ (time-second
(assq-ref compression-details
'unused-removal-duration))
4))
enabled-cached-compressions))))
(while #t
(log-msg 'DEBUG "cached-compression-schedule-removal-fiber starting pass")
(for-each
(match-lambda
((compression . details)
(with-exception-handler
(lambda (exn)
(log-msg 'ERROR "cached-compression-schedule-removal-fiber: "
"exception: " exn))
(lambda ()
(schedule-removal compression details))
#:unwind? #t)))
enabled-cached-compressions)
(log-msg 'DEBUG "cached-compression-schedule-removal-fiber sleeping for "
sleep-duration)
(sleep sleep-duration))))))
;; Process the scheduled removals for cached nars
(define (start-cached-compression-removal-fiber
database
cached-compression-management-channel
enabled-cached-compressions)
(define wakeup-channel
(make-channel))
(define (make-pass)
(log-msg 'DEBUG "cached-compression-removal-fiber starting pass")
(let ((scheduled-cached-narinfo-removal
(database-select-oldest-scheduled-cached-narinfo-removal
database)))
(log-msg 'DEBUG "scheduled removal: " scheduled-cached-narinfo-removal)
(if scheduled-cached-narinfo-removal
(if (time<=? (assq-ref scheduled-cached-narinfo-removal
'scheduled-removal-time)
(current-time))
(let ((id (assq-ref scheduled-cached-narinfo-removal 'id))
(narinfo-id (assq-ref scheduled-cached-narinfo-removal
'narinfo-id))
(compression (assq-ref scheduled-cached-narinfo-removal
'compression))
(size (assq-ref scheduled-cached-narinfo-removal
'size))
(store-path (assq-ref scheduled-cached-narinfo-removal
'store-path)))
(let ((reply (make-channel)))
(put-message
cached-compression-management-channel
(list 'cached-narinfo-removed
narinfo-id
compression
size
reply))
;; Wait for the management fiber to delete the
;; database entry before removing the file.
(get-message reply))
(let ((directory
(assq-ref (assq-ref enabled-cached-compressions
compression)
'directory)))
(let ((filename
(string-append
directory "/"
(basename store-path))))
(log-msg 'DEBUG "deleting " filename)
(delete-file filename))))
(let ((duration
(time-difference
(assq-ref scheduled-cached-narinfo-removal
'scheduled-removal-time)
(current-time))))
(perform-operation
(choice-operation
(sleep-operation (max 1 (+ 1 (time-second duration))))
(get-operation wakeup-channel)))))
;; Sleep until woken
(get-message wakeup-channel))))
(spawn-fiber
(lambda ()
(while #t
(with-exception-handler
(lambda (exn)
(log-msg 'ERROR "exception in cached-compression-removal-fiber: "
exn))
(lambda ()
(with-throw-handler #t
make-pass
(lambda _
(backtrace))))
#:unwind? #t))))
wakeup-channel)
(define* (make-compressed-nar narinfo-files
nar-source
enabled-cached-compressions
target-compression
#:key level)
(define cached-compression-details
(assq-ref enabled-cached-compressions target-compression))
(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-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))))
(log-msg 'DEBUG "created " dest-filename)
bytes))))
|