From ae60643c15a2eab2cf53230aa7c5fbc8af3430d1 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 3 Jul 2012 18:54:46 -0400 Subject: [PATCH] download-from-binary-cache: do negative NAR info caching I.e. if a NAR info file does *not* exist, we record it in the cache DB so that we don't retry it later. --- scripts/download-from-binary-cache.pl.in | 34 ++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/scripts/download-from-binary-cache.pl.in b/scripts/download-from-binary-cache.pl.in index 26437b0649..ba8d44fe24 100644 --- a/scripts/download-from-binary-cache.pl.in +++ b/scripts/download-from-binary-cache.pl.in @@ -9,7 +9,7 @@ use DBI; my @binaryCacheUrls = map { s/\/+$//; $_ } split(/ /, ($ENV{"NIX_BINARY_CACHES"} || "")); -my ($dbh, $insertNAR, $queryNAR); +my ($dbh, $insertNAR, $queryNAR, $insertNegativeNAR, $queryNegativeNAR); my %cacheIds; @@ -52,24 +52,48 @@ EOF ); EOF + $dbh->do(<prepare( "insert or replace into NARs(cache, storePath, url, compression, fileHash, fileSize, narHash, " . "narSize, refs, deriver, system, timestamp) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die; $queryNAR = $dbh->prepare("select * from NARs where cache = ? and storePath = ?") or die; + + $insertNegativeNAR = $dbh->prepare( + "insert or replace into NegativeNARs(cache, storePath, timestamp) values (?, ?, ?)") or die; + + $queryNegativeNAR = $dbh->prepare("select 1 from NegativeNARs where cache = ? and storePath = ?") or die; } sub getInfoFrom { - my ($storePath, $pathHash, $binaryCacheUrl, $cacheId) = @_; + my ($storePath, $pathHash, $binaryCacheUrl) = @_; + + my $cacheId = getCacheId($binaryCacheUrl); + + # Bail out if there is a negative cache entry. + $queryNegativeNAR->execute($cacheId, basename($storePath)); + return undef if @{$queryNegativeNAR->fetchall_arrayref()} != 0; my $infoUrl = "$binaryCacheUrl/$pathHash.narinfo"; print STDERR "checking $infoUrl...\n"; my $s = `$Nix::Config::curl --fail --silent --location $infoUrl`; if ($? != 0) { my $status = $? >> 8; - print STDERR "could not download ‘$infoUrl’ (curl returned status ", $? >> 8, ")\n" - if $status != 22 && $status != 37; + if ($status != 22 && $status != 37) { + print STDERR "could not download ‘$infoUrl’ (curl returned status ", $? >> 8, ")\n"; + } else { + $insertNegativeNAR->execute($cacheId, basename($storePath), time()); + } return undef; } @@ -97,7 +121,7 @@ sub getInfoFrom { # Cache the result. $insertNAR->execute( - getCacheId($binaryCacheUrl), basename($storePath), $url, $compression, $fileHash, $fileSize, + $cacheId, basename($storePath), $url, $compression, $fileHash, $fileSize, $narHash, $narSize, join(" ", @refs), $deriver, $system, time()); return