aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2023-09-11 15:58:29 +0100
committerChristopher Baines <mail@cbaines.net>2023-09-12 13:11:00 +0100
commite5b92d207a1aa899cb4b340459d4e8d1e7877ee1 (patch)
treeefdcfcaae625260cc35930e09de3c3fdebac949b
parent94888457b2d22782f2dbb3d406cc76d37c1bc170 (diff)
downloadnar-herder-e5b92d207a1aa899cb4b340459d4e8d1e7877ee1.tar
nar-herder-e5b92d207a1aa899cb4b340459d4e8d1e7877ee1.tar.gz
Improve the metric handling further
This should avoid the metric being adjusted before it's been set initially.
-rw-r--r--nar-herder/storage.scm110
1 files changed, 57 insertions, 53 deletions
diff --git a/nar-herder/storage.scm b/nar-herder/storage.scm
index b78d4ed..6a6d630 100644
--- a/nar-herder/storage.scm
+++ b/nar-herder/storage.scm
@@ -59,13 +59,6 @@
(define* (remove-nar-files-by-hash database storage-root metrics-registry
hash
#:key (error-unless-files-to-remove? #t))
- (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 ((narinfo-files (database-select-narinfo-files database hash)))
(when (and (null? narinfo-files)
error-unless-files-to-remove?)
@@ -82,9 +75,14 @@
(remove-nar-from-storage storage-root
(assq-ref file 'url)))
- (metric-decrement nar-files-metric
- #:label-values
- `((stored . ,(if exists? "true" "false"))))))
+ (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
+ (metric-decrement
+ metric
+ #:label-values `((stored . ,(if exists? "true" "false"))))))))
narinfo-files)))
(define (get-storage-size storage-root)
@@ -201,42 +199,49 @@
(define* (update-nar-files-metric metrics-registry
nar-file-counts
#:key fetched-count removed-count)
- (define nar-files-metric
- (or (metrics-registry-fetch-metric metrics-registry
- "nar_files_total")
- (make-gauge-metric metrics-registry
- "nar_files_total"
- #:labels '(stored))))
-
- ;; Set the values if the counts are known
- (and=>
- (assq-ref nar-file-counts 'stored)
- (lambda (stored-count)
- (metric-set nar-files-metric
- stored-count
- #:label-values '((stored . "true")))))
- (and=>
- (assq-ref nar-file-counts 'not-stored)
- (lambda (not-stored-count)
- (metric-set nar-files-metric
- not-stored-count
- #:label-values '((stored . "false")))))
-
- ;; Then adjust by the fetched or removed counts
- (when fetched-count
- (metric-increment nar-files-metric
- #:by fetched-count
- #:label-values '((stored . "true")))
- (metric-decrement nar-files-metric
- #:by fetched-count
- #:label-values '((stored . "false"))))
- (when removed-count
- (metric-decrement nar-files-metric
- #:by removed-count
- #:label-values '((stored . "true")))
- (metric-increment nar-files-metric
- #:by removed-count
- #:label-values '((stored . "false")))))
+
+ ;; Avoid incrementing or decrementing the metric if it hasn't been
+ ;; set yet
+ (when (or (metrics-registry-fetch-metric metrics-registry
+ "nar_files_total")
+ (= (length nar-file-counts) 2))
+
+ (let ((nar-files-metric
+ (or (metrics-registry-fetch-metric metrics-registry
+ "nar_files_total")
+ (make-gauge-metric metrics-registry
+ "nar_files_total"
+ #:labels '(stored)))))
+
+ ;; Set the values if the counts are known
+ (and=>
+ (assq-ref nar-file-counts 'stored)
+ (lambda (stored-count)
+ (metric-set nar-files-metric
+ stored-count
+ #:label-values '((stored . "true")))))
+ (and=>
+ (assq-ref nar-file-counts 'not-stored)
+ (lambda (not-stored-count)
+ (metric-set nar-files-metric
+ not-stored-count
+ #:label-values '((stored . "false")))))
+
+ ;; Then adjust by the fetched or removed counts
+ (when fetched-count
+ (metric-increment nar-files-metric
+ #:by fetched-count
+ #:label-values '((stored . "true")))
+ (metric-decrement nar-files-metric
+ #:by fetched-count
+ #:label-values '((stored . "false"))))
+ (when removed-count
+ (metric-decrement nar-files-metric
+ #:by removed-count
+ #:label-values '((stored . "true")))
+ (metric-increment nar-files-metric
+ #:by removed-count
+ #:label-values '((stored . "false")))))))
(define (check-storage database storage-root metrics-registry)
(define files-count
@@ -546,7 +551,6 @@ When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates."
(define (download-nars initial-storage-size)
;; If there's free space, then consider downloading missing nars
(if (< initial-storage-size storage-limit)
-
(let ((result
nar-file-counts
(fold-nar-files
@@ -595,8 +599,8 @@ When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates."
(update-nar-files-metric metrics-registry
nar-file-counts
#:fetched-count fetched-count)
- (> fetched-count 0))))
- #f))
+ fetched-count)))
+ 0))
(define (fast-download-nars)
(define parallelism 3)
@@ -654,7 +658,8 @@ When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates."
(update-nar-files-metric metrics-registry
nar-file-counts
- #:fetched-count fetched-count)))))
+ #:fetched-count fetched-count)
+ fetched-count))))
(define (run-mirror-pass)
(log-msg 'DEBUG "running mirror pass")
@@ -662,12 +667,11 @@ When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates."
(get-storage-size storage-root))))
(metric-set storage-size-metric
initial-storage-size)
- (let ((any-change?
+ (let ((fetched-count
(if no-storage-limit?
(fast-download-nars)
(download-nars initial-storage-size))))
- (log-msg 'DEBUG "finished mirror pass (any change? " any-change? ")")
- any-change?)))
+ (log-msg 'DEBUG "finished mirror pass (fetched " fetched-count " nars)"))))
(let ((channel (make-channel)))
(spawn-fiber