aboutsummaryrefslogtreecommitdiff
path: root/nar-herder/storage.scm
blob: 565037cc94aa41b063fdac61efc4ca96a469f7a6 (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
;;; Nar Herder
;;;
;;; Copyright © 2021 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 storage)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)
  #:use-module (rnrs bytevectors)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (logging logger)
  #:use-module (logging port-log)
  #:use-module (prometheus)
  #:use-module (json)
  #:use-module ((guix build utils) #:select (dump-port mkdir-p))
  #:use-module ((guix store) #:select (store-path-hash-part))
  #:use-module (nar-herder utils)
  #:use-module (nar-herder database)
  #:export (store-item-in-local-storage?

            start-nar-removal-thread
            start-mirroring-thread))

(define (store-item-in-local-storage? database storage-root hash)
  (let ((narinfo-files (database-select-narinfo-files database hash)))
    (when (null? narinfo-files)
      (error "no narinfo files"))
    (every (lambda (file)
             (file-exists?
              (string-append storage-root
                             (uri-decode
                              (assq-ref file 'url)))))
           narinfo-files)))

(define (get-storage-size storage-root)
  (define enter? (const #t))
  (define (leaf name stat result)
    (+ result
       (or (and=> (stat:blocks stat)
                  (lambda (blocks)
                    (* blocks 512)))
           (stat:size stat))))

  (define (down name stat result) result)
  (define (up name stat result) result)
  (define (skip name stat result) result)

  (define (error name stat errno result)
    (format (current-error-port) "warning: ~a: ~a~%"
            name (strerror errno))
    result)

  (file-system-fold enter? leaf down up skip error
                           0 ; Start counting at 0
                           storage-root))

(define (remove-nar-from-storage storage-root nar-file)
  (let* ((filename
          (string-append storage-root "/" nar-file)))
    (log-msg 'INFO "removing nar " nar-file)
    (delete-file filename))
  #t)

(define (index-storage database storage-root)
  (define (get-files-hash)
    (define storage-root-length
      (string-length storage-root))

    (define enter? (const #t))
    (define (leaf name stat result)
      (hash-set! result
                 (string-drop name storage-root-length)
                 #t)
      result)

    (define (down name stat result) result)
    (define (up name stat result) result)
    (define (skip name stat result) result)

    (define (error name stat errno result)
      (format (current-error-port) "warning: ~a: ~a~%"
              name (strerror errno))
      result)

    (file-system-fold enter? leaf down up skip error
                      (make-hash-table (expt 2 19))
                      storage-root))

  (let* ((files-hash
          (get-files-hash))
         (narinfo-files
          (database-map-all-narinfo-files
           database
           (lambda (file)
             (let* ((url (uri-decode
                          (assq-ref file 'url)))
                    (stored? (hash-ref files-hash url)))
               (when stored?
                 ;; Delete the hash entry, so
                 ;; that the hash at the end will
                 ;; just contain the unrecognised
                 ;; files
                 (hash-remove! files-hash url))

               `(,@file
                 (stored? . ,stored?)))))))
    `((narinfo-files      . ,narinfo-files)
      (unrecognised-files . ,(hash-map->list (lambda (key _) key)
                                             files-hash)))))

(define* (get-nar-files database storage-root metrics-registry
                        #:key stored?)
  (define nar-files-metric
    (or (metrics-registry-fetch-metric metrics-registry
                                       "nar_files_total")
        (make-gauge-metric metrics-registry
                           "nar_files_total"
                           #:labels '(stored))))

  (let* ((index (index-storage database storage-root))
         (selected-files
          (filter
           (lambda (file)
             (eq? (assq-ref file 'stored?) stored?))
           (assq-ref index 'narinfo-files))))

    (let ((selected-files-count
           (length selected-files))
          (all-files-count
           (length (assq-ref index 'narinfo-files))))

      (metric-set nar-files-metric
                  selected-files-count
                  #:label-values `((stored . ,(if stored? "true" "false"))))
      (metric-set nar-files-metric
                  (- all-files-count selected-files-count)
                  #:label-values `((stored . ,(if stored? "false" "true")))))

    selected-files))

(define (start-nar-removal-thread database
                                  storage-root storage-limit
                                  metrics-registry
                                  nar-removal-criteria)

  (define (check-removal-criteria nar criteria)
    (define narinfo
      (database-select-narinfo-for-file database (assq-ref nar 'url)))

    (match criteria
      (('and and-criteria ...)
       (every check-removal-criteria criteria))
      (('stored-on url)
       (let ((uri (string->uri
                   (string-append url
                                  "/"
                                  (store-path-hash-part
                                   (assq-ref narinfo 'store-path))
                                  ".narinfo/info"))))
         (call-with-values
             (lambda ()
               (retry-on-error
                (lambda ()
                  (http-get uri #:decode-body? #f))
                #:times 3
                #:delay 5))
           (lambda (response body)
             (and (= (response-code response)
                     200)

                  (let ((json-body (json-string->scm
                                    (utf8->string body))))
                    (eq? (assoc-ref json-body "stored")
                         #t)))))))))

  (define (nar-can-be-removed? nar)
    (any (lambda (criteria)
           (check-removal-criteria nar criteria))
         nar-removal-criteria))

  (define (run-removal-pass)
    (log-msg 'INFO "looking for nars to remove")
    (let ((initial-storage-size
           (with-time-logging "getting storage size"
             (get-storage-size storage-root))))
      (log-msg 'DEBUG "initial storage size " initial-storage-size)

      (let loop ((storage-size
                  initial-storage-size)
                 (stored-nar-files
                  (with-time-logging "getting stored nar files"
                    (get-nar-files database storage-root metrics-registry
                                   #:stored? #t))))
        ;; Look through items in local storage, check if the removal
        ;; criteria have been met, and if so, delete it

        (unless (null? stored-nar-files)
          (let ((nar-to-consider (car stored-nar-files)))
            (if (nar-can-be-removed? nar-to-consider)
                (begin
                  (remove-nar-from-storage
                   storage-root
                   (uri-decode
                    (assq-ref nar-to-consider 'url)))

                  (let ((storage-size-estimate
                         (- storage-size
                            (assq-ref nar-to-consider 'size))))
                    (when (> storage-size-estimate storage-limit)
                      (loop storage-size-estimate
                            (cdr stored-nar-files)))))
                (loop storage-size
                      (cdr stored-nar-files)))))))
    (log-msg 'INFO "finished looking for nars to remove"))

  (when (null? nar-removal-criteria)
    (error "must be some removal criteria"))

  (call-with-new-thread
   (lambda ()
     (while #t
       (run-removal-pass)

       (sleep 300)))))

(define (start-mirroring-thread database mirror storage-limit storage-root
                                metrics-registry)

  (define storage-size-metric
    (make-gauge-metric metrics-registry
                       "storage_size_bytes"))

  (define (fetch-file file)
    (let* ((string-url
            (string-append mirror file))
           (uri
            (string->uri (string-append mirror file)))
           (destination-file-name
            (string-append storage-root
                           (uri-decode file)))
           (tmp-file-name
            (string-append destination-file-name "-tmp")))
      (log-msg 'INFO "fetching " string-url)

      (mkdir-p (dirname destination-file-name))

      (when (file-exists? tmp-file-name)
        (delete-file tmp-file-name))

      (call-with-values
          (lambda ()
            (http-get uri
                      #:decode-body? #f
                      #:streaming? #t))
        (lambda (response body)
          (unless (= (response-code response)
                     200)
            (error "unknown response code"))

          (call-with-output-file tmp-file-name
            (lambda (output-port)
              (dump-port body output-port)))
          (rename-file tmp-file-name
                       destination-file-name)))))

  (define (run-mirror-pass)
    (define no-storage-limit?
      (not (integer? storage-limit)))

    (log-msg 'DEBUG "running mirror pass")
    (let ((initial-storage-size (with-time-logging "getting storage size"
                                  (get-storage-size storage-root))))
      (metric-set storage-size-metric
                  initial-storage-size)
      ;; If there's free space, then consider downloading missing nars
      (when (or no-storage-limit?
                (< initial-storage-size storage-limit))
        (let loop ((storage-size initial-storage-size)
                   (missing-nar-files (get-nar-files
                                       database storage-root metrics-registry
                                       #:stored? #f)))
          (unless (null? missing-nar-files)
            (let ((file (car missing-nar-files)))
              (log-msg 'DEBUG "considering "
                       (assq-ref file 'url))
              (let ((file-bytes (assq-ref file 'size)))
                (if (or no-storage-limit?
                        (< (+ storage-size file-bytes)
                           storage-limit))
                    (begin
                      (retry-on-error
                       (lambda ()
                         (fetch-file (assq-ref file 'url)))
                       #:times 3
                       #:delay 5)
                      (loop (+ storage-size file-bytes)
                            (cdr missing-nar-files)))
                    ;; This file won't fit, so try the next one
                    (loop storage-size
                          (cdr missing-nar-files))))))))))

  (call-with-new-thread
   (lambda ()
     (while #t
       (run-mirror-pass)
       (log-msg 'DEBUG "finished mirror pass")

       (sleep 300)))))