From patchwork Tue May 11 13:08:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 29247 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id CD8E827BC82; Tue, 11 May 2021 14:09:31 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 4A4D527BC81 for ; Tue, 11 May 2021 14:09:31 +0100 (BST) Received: from localhost ([::1]:59134 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgS8g-0004aA-FA for patchwork@mira.cbaines.net; Tue, 11 May 2021 09:09:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgS8F-0004QX-3v for guix-patches@gnu.org; Tue, 11 May 2021 09:09:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:52040) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lgS8E-0006C7-S5 for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lgS8E-0003vp-Mr for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#47897] [PATCH 1/2] publish: Add '--negative-ttl'. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 11 May 2021 13:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47897 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 47897@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 47897-submit@debbugs.gnu.org id=B47897.162073854015098 (code B ref 47897); Tue, 11 May 2021 13:09:02 +0000 Received: (at 47897) by debbugs.gnu.org; 11 May 2021 13:09:00 +0000 Received: from localhost ([127.0.0.1]:35352 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lgS8B-0003vK-Kz for submit@debbugs.gnu.org; Tue, 11 May 2021 09:09:00 -0400 Received: from eggs.gnu.org ([209.51.188.92]:42908) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lgS89-0003uy-FN for 47897@debbugs.gnu.org; Tue, 11 May 2021 09:08:58 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:48406) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgS83-000625-Ic; Tue, 11 May 2021 09:08:51 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=57486 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lgS83-0004e7-5o; Tue, 11 May 2021 09:08:51 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 11 May 2021 15:08:41 +0200 Message-Id: <20210511130842.32381-1-ludo@gnu.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <87wnsthpyn.fsf@cbaines.net> References: <87wnsthpyn.fsf@cbaines.net> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/publish.scm (show-help, %options): Add '--negative-ttl'. (render-narinfo, render-narinfo/cached, make-request-handler): Add #:negative-ttl and honor it. (run-publish-server): Add #:narinfo-negative-ttl and honor it. (guix-publish): Honor '--negative-ttl'. * tests/publish.scm ("negative TTL", "no negative TTL"): New tests. --- doc/guix.texi | 10 ++++++++++ guix/scripts/publish.scm | 30 ++++++++++++++++++++++-------- tests/publish.scm | 32 +++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 0947b9f028..a34b2fca1e 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -12727,6 +12727,16 @@ Additionally, when @option{--cache} is used, cached entries that have not been accessed for @var{ttl} and that no longer have a corresponding item in the store, may be deleted. +@item --negative-ttl=@var{ttl} +Similarly produce @code{Cache-Control} HTTP headers to advertise the +time-to-live (TTL) of @emph{negative} lookups---missing store items, for +which the HTTP 404 code is returned. By default, no negative TTL is +advertised. + +This parameter can help adjust server load and substitute latency by +instructing cooperating clients to be more or less patient when a store +item is missing. + @item --cache-bypass-threshold=@var{size} When used in conjunction with @option{--cache}, store items smaller than @var{size} are immediately available, even when they are not yet in diff --git a/guix/scripts/publish.scm b/guix/scripts/publish.scm index 39bb224cad..ef6fa5f074 100644 --- a/guix/scripts/publish.scm +++ b/guix/scripts/publish.scm @@ -1,7 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015 David Thompson ;;; Copyright © 2020 by Amar M. Singh -;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; Copyright © 2020 Maxim Cournoyer ;;; Copyright © 2021 Simon Tournier ;;; @@ -101,6 +101,8 @@ Publish ~a over HTTP.\n") %store-directory) --workers=N use N workers to bake items")) (display (G_ " --ttl=TTL announce narinfos can be cached for TTL seconds")) + (display (G_ " + --negative-ttl=TTL announce missing narinfos can be cached for TTL seconds")) (display (G_ " --nar-path=PATH use PATH as the prefix for nar URLs")) (display (G_ " @@ -224,6 +226,13 @@ usage." (leave (G_ "~a: invalid duration~%") arg)) (alist-cons 'narinfo-ttl (time-second duration) result)))) + (option '("negative-ttl") #t #f + (lambda (opt name arg result) + (let ((duration (string->duration arg))) + (unless duration + (leave (G_ "~a: invalid duration~%") arg)) + (alist-cons 'narinfo-negative-ttl (time-second duration) + result)))) (option '("nar-path") #t #f (lambda (opt name arg result) (alist-cons 'nar-path arg result))) @@ -390,14 +399,14 @@ References: ~a~%" (define* (render-narinfo store request hash #:key ttl (compressions (list %no-compression)) - (nar-path "nar")) + (nar-path "nar") negative-ttl) "Render metadata for the store path corresponding to HASH. If TTL is true, advertise it as the maximum validity period (in seconds) via the 'Cache-Control' header. This allows 'guix substitute' to cache it for an appropriate duration. NAR-PATH specifies the prefix for nar URLs." (let ((store-path (hash-part->path store hash))) (if (string-null? store-path) - (not-found request #:phrase "") + (not-found request #:phrase "" #:ttl negative-ttl) (values `((content-type . (application/x-nix-narinfo)) ,@(if ttl `((cache-control (max-age . ,ttl))) @@ -512,7 +521,7 @@ interpreted as the basename of a store item." (define* (render-narinfo/cached store request hash #:key ttl (compressions (list %no-compression)) - (nar-path "nar") + (nar-path "nar") negative-ttl cache pool) "Respond to the narinfo request for REQUEST. If the narinfo is available in CACHE, then send it; otherwise, return 404 and \"bake\" that nar and narinfo @@ -536,7 +545,7 @@ requested using POOL." #:compression (first compressions))))) (cond ((string-null? item) - (not-found request)) + (not-found request #:ttl negative-ttl)) ((file-exists? cached) ;; Narinfo is in cache, send it. (values `((content-type . (application/x-nix-narinfo)) @@ -584,7 +593,7 @@ requested using POOL." #:phrase "We're baking it" #:ttl 300))) ;should be available within 5m (else - (not-found request #:phrase ""))))) + (not-found request #:phrase "" #:ttl negative-ttl))))) (define (compress-nar cache item compression) "Save in directory CACHE the nar for ITEM compressed with COMPRESSION." @@ -974,7 +983,7 @@ methods, return the applicable compression." (define* (make-request-handler store #:key cache pool - narinfo-ttl + narinfo-ttl narinfo-negative-ttl (nar-path "nar") (compressions (list %no-compression))) (define compression-type? @@ -1006,10 +1015,12 @@ methods, return the applicable compression." #:cache cache #:pool pool #:ttl narinfo-ttl + #:negative-ttl narinfo-negative-ttl #:nar-path nar-path #:compressions compressions) (render-narinfo store request hash #:ttl narinfo-ttl + #:negative-ttl narinfo-negative-ttl #:nar-path nar-path #:compressions compressions))) ;; /nar/file/NAME/sha256/HASH @@ -1068,7 +1079,7 @@ methods, return the applicable compression." #:key advertise? port (compressions (list %no-compression)) - (nar-path "nar") narinfo-ttl + (nar-path "nar") narinfo-ttl narinfo-negative-ttl cache pool) (when advertise? (let ((name (service-name))) @@ -1084,6 +1095,7 @@ methods, return the applicable compression." #:pool pool #:nar-path nar-path #:narinfo-ttl narinfo-ttl + #:narinfo-negative-ttl narinfo-negative-ttl #:compressions compressions) concurrent-http-server `(#:socket ,socket))) @@ -1127,6 +1139,7 @@ methods, return the applicable compression." (user (assoc-ref opts 'user)) (port (assoc-ref opts 'port)) (ttl (assoc-ref opts 'narinfo-ttl)) + (negative-ttl (assoc-ref opts 'narinfo-negative-ttl)) (compressions (match (filter-map (match-lambda (('compression . compression) compression) @@ -1192,6 +1205,7 @@ consider using the '--user' option!~%"))) "publish worker")) #:nar-path nar-path #:compressions compressions + #:narinfo-negative-ttl negative-ttl #:narinfo-ttl ttl)))))) ;;; Local Variables: diff --git a/tests/publish.scm b/tests/publish.scm index 3e67c435ac..c3d086995a 100644 --- a/tests/publish.scm +++ b/tests/publish.scm @@ -1,7 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015 David Thompson ;;; Copyright © 2020 by Amar M. Singh -;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -700,6 +700,36 @@ References: ~%" (= (response-content-length response) (stat:size (stat log))) (first (response-content-type response)))))) +(test-equal "negative TTL" + `(404 42) + + (call-with-temporary-directory + (lambda (cache) + (let ((thread (with-separate-output-ports + (call-with-new-thread + (lambda () + (guix-publish "--port=6786" "-C0" + "--negative-ttl=42s")))))) + (wait-until-ready 6786) + + (let* ((base "http://localhost:6786/") + (url (string-append base (make-string 32 #\z) + ".narinfo")) + (response (http-get url))) + (list (response-code response) + (match (assq-ref (response-headers response) 'cache-control) + ((('max-age . ttl)) ttl) + (_ #f)))))))) + +(test-equal "no negative TTL" + `(404 #f) + (let* ((uri (publish-uri + (string-append "/" (make-string 32 #\z) + ".narinfo"))) + (response (http-get uri))) + (list (response-code response) + (assq-ref (response-headers response) 'cache-control)))) + (test-equal "/log/NAME not found" 404 (let ((uri (publish-uri "/log/does-not-exist"))) From patchwork Tue May 11 13:08:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 29246 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id A25A327BC82; Tue, 11 May 2021 14:09:10 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 5C4EA27BC81 for ; Tue, 11 May 2021 14:09:10 +0100 (BST) Received: from localhost ([::1]:58918 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgS8L-0004Qx-Ei for patchwork@mira.cbaines.net; Tue, 11 May 2021 09:09:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53284) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgS8E-0004QP-K4 for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:52039) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lgS8E-0006BP-CT for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lgS8E-0003vi-7H for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#47897] [PATCH 2/2] substitutes: Reduce negative TTLs. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 11 May 2021 13:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47897 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 47897@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 47897-submit@debbugs.gnu.org id=B47897.162073853915090 (code B ref 47897); Tue, 11 May 2021 13:09:02 +0000 Received: (at 47897) by debbugs.gnu.org; 11 May 2021 13:08:59 +0000 Received: from localhost ([127.0.0.1]:35350 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lgS8B-0003vI-AY for submit@debbugs.gnu.org; Tue, 11 May 2021 09:08:59 -0400 Received: from eggs.gnu.org ([209.51.188.92]:42912) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lgS89-0003uz-Fr for 47897@debbugs.gnu.org; Tue, 11 May 2021 09:08:58 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:48408) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgS84-00062z-Bl; Tue, 11 May 2021 09:08:52 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=57486 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lgS83-0004e7-Ra; Tue, 11 May 2021 09:08:52 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 11 May 2021 15:08:42 +0200 Message-Id: <20210511130842.32381-2-ludo@gnu.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210511130842.32381-1-ludo@gnu.org> References: <87wnsthpyn.fsf@cbaines.net> <20210511130842.32381-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/substitutes.scm (%narinfo-negative-ttl): Change to 15mn. (%narinfo-transient-error-ttl): Halve. --- guix/substitutes.scm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/guix/substitutes.scm b/guix/substitutes.scm index 08f8c24efd..c1b036e42c 100644 --- a/guix/substitutes.scm +++ b/guix/substitutes.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; Copyright © 2014 Nikita Karetnikov ;;; Copyright © 2018 Kyle Meyer ;;; Copyright © 2020 Christopher Baines @@ -72,11 +72,11 @@ (define %narinfo-negative-ttl ;; Likewise, but for negative lookups---i.e., cached lookup failures (404). - (* 1 3600)) + (* 15 60)) (define %narinfo-transient-error-ttl ;; Likewise, but for transient errors such as 504 ("Gateway timeout"). - (* 10 60)) + (* 5 60)) (define %narinfo-cache-directory ;; A local cache of narinfos, to avoid going to the network. Most of the