aboutsummaryrefslogtreecommitdiff
path: root/nar-herder/mirror.scm
blob: a784165171220e9890331db5b087df580cead1ed (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
;;; 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 mirror)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-43)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 exceptions)
  #:use-module (rnrs bytevectors)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (prometheus)
  #:use-module (logging logger)
  #:use-module (json)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (guix narinfo)
  #:use-module ((guix store) #:select (store-path-hash-part))
  #:use-module (nar-herder utils)
  #:use-module (nar-herder database)
  #:use-module (nar-herder storage)
  #:export (start-fetch-changes-fiber))

(define (start-fetch-changes-fiber database metrics-registry
                                   storage-root mirror
                                   cached-compression-management-channel)
  (define (request-recent-changes)
    (define latest-recent-change
      (database-select-latest-recent-change-datetime database))

    (define processed-recent-changes
      ;; Strip datetimes, as these could differ from the mirrors
      ;; datetimes (since a mirror will often record different change
      ;; datetimes, since it's delayed in making changes
      (map strip-change-datetime
           (database-select-recent-changes database latest-recent-change)))

    (define (strip-change-datetime change)
      `((change . ,(assq-ref change 'change))
        (data   . ,(assq-ref change 'data))))

    (define uri
      (string->uri
       (string-append mirror "/recent-changes"
                      (if latest-recent-change
                          (string-append "?since=" (uri-encode
                                                    latest-recent-change))
                          ""))))

    (call-with-values
        (lambda ()
          (retry-on-error
           (lambda ()
             (log-msg 'INFO "querying for recent changes since "
                      latest-recent-change)
             (with-port-timeouts
              (lambda ()
                (let ((port
                       socket
                       (open-socket-for-uri* uri)))
                  (http-get uri
                            #:port port
                            #:streaming? #t)))
              #:timeout 30))
           #:times 3
           #:delay 15))
      (lambda (response body)
        (if (= (response-code response) 200)
            (let* ((json-body (json->scm body))
                   (recent-changes
                    (assoc-ref json-body "recent_changes")))

              (log-msg 'INFO "got " (vector-length recent-changes) " changes")

              ;; Switch to symbol keys and standardise the key order
              (vector-map!
               (lambda (_ change-details)
                 `((datetime . ,(assoc-ref change-details "datetime"))
                   (change   . ,(assoc-ref change-details "change"))
                   (data     . ,(assoc-ref change-details "data"))))
               recent-changes)

              (vector-for-each
               (lambda (_ change-details)
                 ;; Guard against processing changes that have already
                 ;; been processed
                 (unless (member (strip-change-datetime change-details)
                                 processed-recent-changes)
                   (let ((change (assq-ref change-details 'change)))
                     (cond
                      ((string=? change "addition")
                       (let ((narinfo
                              (call-with-input-string
                                  (assq-ref change-details 'data)
                                (lambda (port)
                                  (read-narinfo port
                                                "https://narherderdummyvalue")))))
                         (log-msg 'INFO "processing addition change for "
                                  (uri-path (first (narinfo-uris narinfo)))
                                  " (" (assq-ref change-details 'datetime) ")")
                         (database-insert-narinfo database
                                                  narinfo
                                                  #:change-datetime
                                                  (assq-ref change-details
                                                            'datetime))

                         (and=> (metrics-registry-fetch-metric metrics-registry
                                                               "nar_files_total")
                                (lambda (metric)
                                  ;; Just update this metric if it
                                  ;; exists, since if it does, it
                                  ;; should be set to a value
                                  (let ((new-files-count
                                         (length (narinfo-uris narinfo))))
                                    (metric-increment
                                     metric
                                     #:by new-files-count
                                     ;; TODO This should be
                                     ;; checked, rather than
                                     ;; assumed to be false
                                     #:label-values '((stored . "false"))))))))
                      ((string=? change "removal")
                       (let ((store-path (assq-ref change-details 'data)))
                         (log-msg 'INFO "processing removal change for "
                                  store-path
                                  " (" (assq-ref change-details 'datetime) ")")

                         (let* ((hash (store-path-hash-part store-path))
                                (narinfo-details
                                 (database-select-narinfo-by-hash
                                  database
                                  hash)))

                         (when storage-root
                           (remove-nar-files-by-hash
                            database
                            storage-root
                            metrics-registry
                            hash))

                         (let ((cached-narinfo-files
                                (database-select-cached-narinfo-files-by-narinfo-id
                                 database
                                 (assq-ref narinfo-details 'id))))
                           (for-each
                            (lambda (cached-narinfo-file-details)
                              ;; TODO Delete the file as well

                              (let ((reply (make-channel)))
                                (put-message
                                 cached-compression-management-channel
                                 (list 'cached-narinfo-removed
                                       (assq-ref narinfo-details 'id)
                                       (assq-ref cached-narinfo-files 'compression)
                                       (assq-ref cached-narinfo-files 'size)
                                       reply))
                                (get-message reply)))
                            cached-narinfo-files))

                         (database-remove-narinfo database
                                                  store-path
                                                  #:change-datetime
                                                  (assq-ref change-details
                                                            'datetime)))))
                      (else
                       (error "unimplemented"))))))
               recent-changes))
            (raise-exception
             (make-exception-with-message
              (simple-format #f "unknown response: ~A code: ~A"
                             (uri->string uri)
                             (response-code response))))))))

  (spawn-fiber
   (lambda ()
     (while #t
       (with-exception-handler
           (lambda (exn)
             (log-msg 'ERROR "fetching changes failed " exn))
         request-recent-changes
         #:unwind? #t)

       (log-msg 'DEBUG "finished requesting recent changes, sleeping")
       (sleep 60)))))