Rework inserting derivations
To add more parallelism.
This commit is contained in:
parent
1e0407e9b6
commit
c650fc6e7a
1 changed files with 206 additions and 177 deletions
|
|
@ -929,25 +929,25 @@
|
|||
|
||||
(define (update-derivation-ids-hash-table! conn
|
||||
derivation-ids-hash-table
|
||||
file-names)
|
||||
(define file-names-count (vector-length file-names))
|
||||
derivations)
|
||||
(define derivations-count (length derivations))
|
||||
|
||||
(simple-format #t "debug: update-derivation-ids-hash-table!: ~A file-names\n"
|
||||
file-names-count)
|
||||
derivations-count)
|
||||
(let ((missing-file-names
|
||||
(vector-fold
|
||||
(lambda (_ result file-name)
|
||||
(if (and file-name
|
||||
(hash-ref derivation-ids-hash-table
|
||||
file-name))
|
||||
(fold
|
||||
(lambda (drv result)
|
||||
(if (hash-ref derivation-ids-hash-table
|
||||
(derivation-file-name drv))
|
||||
result
|
||||
(cons file-name result)))
|
||||
(cons (derivation-file-name drv)
|
||||
result)))
|
||||
'()
|
||||
file-names)))
|
||||
derivations)))
|
||||
|
||||
(simple-format
|
||||
#t "debug: update-derivation-ids-hash-table!: lookup ~A file-names, ~A not cached\n"
|
||||
file-names-count (length missing-file-names))
|
||||
derivations-count (length missing-file-names))
|
||||
|
||||
(unless (null? missing-file-names)
|
||||
(for-each
|
||||
|
|
@ -964,33 +964,17 @@
|
|||
(define (insert-missing-derivations postgresql-connection-pool
|
||||
utility-thread-channel
|
||||
derivation-ids-hash-table
|
||||
derivations)
|
||||
unfiltered-derivations)
|
||||
|
||||
(define (ensure-input-derivations-exist input-derivation-file-names)
|
||||
(unless (null? input-derivation-file-names)
|
||||
;; Ensure all the input derivations exist
|
||||
(for-each
|
||||
(lambda (chunk)
|
||||
(simple-format
|
||||
#t "debug: ensure-input-derivations-exist: processing ~A derivations\n"
|
||||
(length input-derivation-file-names))
|
||||
(length chunk))
|
||||
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(update-derivation-ids-hash-table! conn
|
||||
derivation-ids-hash-table
|
||||
(list->vector
|
||||
input-derivation-file-names)))
|
||||
(simple-format
|
||||
#t
|
||||
"debug: ensure-input-derivations-exist: checking for missing input derivations\n")
|
||||
(let ((missing-derivations-filenames
|
||||
(remove (lambda (derivation-file-name)
|
||||
(hash-ref derivation-ids-hash-table
|
||||
derivation-file-name))
|
||||
input-derivation-file-names)))
|
||||
|
||||
(unless (null? missing-derivations-filenames)
|
||||
(simple-format
|
||||
#f
|
||||
"debug: ensure-input-derivations-exist: inserting missing input derivations\n")
|
||||
;; Ensure all the input derivations exist
|
||||
(insert-missing-derivations
|
||||
postgresql-connection-pool
|
||||
utility-thread-channel
|
||||
|
|
@ -998,8 +982,8 @@
|
|||
(call-with-worker-thread
|
||||
utility-thread-channel
|
||||
(lambda ()
|
||||
(map read-derivation-from-file
|
||||
missing-derivations-filenames))))))))
|
||||
(map read-derivation-from-file chunk)))))
|
||||
(chunk! input-derivation-file-names 1000))))
|
||||
|
||||
(define (insert-into-derivations conn drvs)
|
||||
(string-append
|
||||
|
|
@ -1030,17 +1014,36 @@
|
|||
(with-time-logging
|
||||
(simple-format
|
||||
#f "insert-missing-derivations: inserting ~A derivations"
|
||||
(length derivations))
|
||||
(let* ((chunks (chunk derivations 500))
|
||||
(derivation-ids
|
||||
(length unfiltered-derivations))
|
||||
(let ((derivations
|
||||
derivation-ids
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(update-derivation-ids-hash-table! conn
|
||||
derivation-ids-hash-table
|
||||
unfiltered-derivations)
|
||||
|
||||
(let ((derivations
|
||||
;; Do this while holding the PostgreSQL connection to
|
||||
;; avoid conflicts with other fibers
|
||||
(filter-map (lambda (derivation)
|
||||
(if (hash-ref derivation-ids-hash-table
|
||||
(derivation-file-name
|
||||
derivation))
|
||||
#f
|
||||
derivation))
|
||||
unfiltered-derivations)))
|
||||
(if (null? derivations)
|
||||
(values '() '())
|
||||
(let ((derivation-ids
|
||||
(append-map!
|
||||
(lambda (chunk)
|
||||
(map (lambda (result)
|
||||
(string->number (car result)))
|
||||
(exec-query conn (insert-into-derivations conn chunk))))
|
||||
chunks))))
|
||||
(chunk derivations 500))))
|
||||
|
||||
;; Do this while holding the connection so that other
|
||||
;; fibers don't also try inserting the same derivations
|
||||
(with-time-logging
|
||||
"insert-missing-derivations: updating hash table"
|
||||
(for-each (lambda (derivation derivation-id)
|
||||
|
|
@ -1050,9 +1053,14 @@
|
|||
derivations
|
||||
derivation-ids))
|
||||
|
||||
(values derivations
|
||||
derivation-ids)))))))
|
||||
|
||||
(unless (null? derivations)
|
||||
(parallel-via-fibers
|
||||
(with-time-logging
|
||||
"insert-missing-derivations: inserting sources"
|
||||
(for-each
|
||||
(fibers-for-each
|
||||
(lambda (derivation-id derivation)
|
||||
(let ((sources (derivation-sources derivation)))
|
||||
(unless (null? sources)
|
||||
|
|
@ -1063,14 +1071,22 @@
|
|||
sources))))
|
||||
(par-map&
|
||||
(lambda (id source-file)
|
||||
(match
|
||||
(when
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(match
|
||||
(exec-query
|
||||
conn
|
||||
"
|
||||
SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
||||
(list (number->string id))))
|
||||
(list (number->string id)))
|
||||
(()
|
||||
;; Insert a placeholder to avoid other fibers
|
||||
;; working on this source file
|
||||
(insert-placeholder-derivation-source-file-nar
|
||||
conn
|
||||
id)
|
||||
#t)
|
||||
(_ #f)))
|
||||
(let ((nar-bytevector
|
||||
(call-with-worker-thread
|
||||
utility-thread-channel
|
||||
|
|
@ -1084,7 +1100,9 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(make-missing-store-item-error
|
||||
source-file)))
|
||||
(write-file source-file port)
|
||||
(get-bytevector)))))))
|
||||
(let ((res (get-bytevector)))
|
||||
(close-port port) ; maybe reduces memory?
|
||||
res)))))))
|
||||
(letpar&
|
||||
((compressed-nar-bytevector
|
||||
(call-with-worker-thread
|
||||
|
|
@ -1098,7 +1116,9 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(lambda (port)
|
||||
(put-bytevector port nar-bytevector))
|
||||
#:level 9)
|
||||
(get-bytevector))))))
|
||||
(let ((res (get-bytevector)))
|
||||
(close-port port) ; maybe reduces memory?
|
||||
res))))))
|
||||
(hash
|
||||
(call-with-worker-thread
|
||||
utility-thread-channel
|
||||
|
|
@ -1106,15 +1126,13 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(bytevector->nix-base32-string
|
||||
(sha256 nar-bytevector)))))
|
||||
(uncompressed-size (bytevector-length nar-bytevector)))
|
||||
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(insert-derivation-source-file-nar
|
||||
(update-derivation-source-file-nar
|
||||
conn
|
||||
id
|
||||
hash
|
||||
compressed-nar-bytevector
|
||||
uncompressed-size)))))
|
||||
(_ #f)))
|
||||
uncompressed-size))))))
|
||||
sources-ids
|
||||
sources)))))
|
||||
derivation-ids
|
||||
|
|
@ -1135,7 +1153,7 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(ensure-input-derivations-exist (deduplicate-strings
|
||||
(map derivation-input-path
|
||||
(append-map derivation-inputs
|
||||
derivations)))))
|
||||
derivations))))))
|
||||
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(with-time-logging
|
||||
|
|
@ -1144,7 +1162,7 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(length derivations))
|
||||
(insert-derivation-inputs conn
|
||||
derivation-ids
|
||||
derivations))))))
|
||||
derivations)))))))
|
||||
|
||||
(define (derivation-file-names->derivation-ids postgresql-connection-pool
|
||||
utility-thread-channel
|
||||
|
|
@ -1160,11 +1178,6 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
#t "debug: derivation-file-names->derivation-ids: processing ~A derivations\n"
|
||||
derivations-count)
|
||||
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(update-derivation-ids-hash-table! conn
|
||||
derivation-ids-hash-table
|
||||
derivation-file-names))
|
||||
|
||||
(let* ((missing-derivation-filenames
|
||||
(deduplicate-strings
|
||||
(vector-fold
|
||||
|
|
@ -1192,24 +1205,16 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(chunk! missing-derivation-filenames 1000))))
|
||||
|
||||
(for-each
|
||||
(lambda (missing-derivation-filenames-chunk)
|
||||
(lambda (missing-derivation-chunk-promise)
|
||||
(let ((missing-derivations-chunk
|
||||
;; Do the filter again, since processing the last chunk
|
||||
;; might have inserted some of the derivations in this
|
||||
;; chunk
|
||||
(remove! (lambda (derivation)
|
||||
(hash-ref derivation-ids-hash-table
|
||||
(derivation-file-name
|
||||
derivation)))
|
||||
(fibers-force
|
||||
missing-derivation-filenames-chunk))))
|
||||
|
||||
missing-derivation-chunk-promise)))
|
||||
(unless (null? missing-derivations-chunk)
|
||||
(insert-missing-derivations postgresql-connection-pool
|
||||
utility-thread-channel
|
||||
derivation-ids-hash-table
|
||||
missing-derivations-chunk))))
|
||||
missing-derivations-chunked-promises))
|
||||
missing-derivations-chunked-promises)
|
||||
|
||||
(let ((all-ids
|
||||
(vector-map
|
||||
|
|
@ -1217,11 +1222,35 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
|
|||
(if derivation-file-name
|
||||
(or (hash-ref derivation-ids-hash-table
|
||||
derivation-file-name)
|
||||
(error "missing derivation id"))
|
||||
;; If a derivation ID can't be found, update the
|
||||
;; hash table then check again
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(for-each
|
||||
(lambda (missing-derivations-chunked-promise)
|
||||
(update-derivation-ids-hash-table!
|
||||
conn
|
||||
derivation-ids-hash-table
|
||||
(fibers-force missing-derivations-chunked-promise)))
|
||||
missing-derivations-chunked-promises)
|
||||
(or (hash-ref derivation-ids-hash-table
|
||||
derivation-file-name)
|
||||
(error
|
||||
(simple-format #f "missing derivation id (~A)"
|
||||
derivation-file-name)))))
|
||||
#f))
|
||||
derivation-file-names)))
|
||||
|
||||
all-ids))))
|
||||
(with-resource-from-pool postgresql-connection-pool conn
|
||||
(simple-format
|
||||
(current-error-port)
|
||||
"guix-data-service: clearing the derivation-ids-hash-table\n")
|
||||
(hash-clear! derivation-ids-hash-table))
|
||||
|
||||
;; Just in case this helps clear memory
|
||||
(for-each fibers-promise-reset
|
||||
missing-derivations-chunked-promises)
|
||||
|
||||
all-ids)))))
|
||||
|
||||
(prevent-inlining-for-tests derivation-file-names->derivation-ids)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue