Skip to content

Commit f267188

Browse files
authored
Merge pull request #13223 from NixOS/remove-global-fetcher-cache
Remove global fetcher cache
2 parents e5e5c81 + efcb9e3 commit f267188

File tree

24 files changed

+94
-54
lines changed

24 files changed

+94
-54
lines changed

src/libcmd/common-eval-args.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ EvalSettings evalSettings {
3434
auto flakeRef = parseFlakeRef(fetchSettings, std::string { rest }, {}, true, false);
3535
debug("fetching flake search path element '%s''", rest);
3636
auto [accessor, lockedRef] = flakeRef.resolve(state.store).lazyFetch(state.store);
37-
auto storePath = nix::fetchToStore(*state.store, SourcePath(accessor), FetchMode::Copy, lockedRef.input.getName());
37+
auto storePath = nix::fetchToStore(
38+
state.fetchSettings,
39+
*state.store,
40+
SourcePath(accessor),
41+
FetchMode::Copy,
42+
lockedRef.input.getName());
3843
state.allowPath(storePath);
3944
return state.storePath(storePath);
4045
},
@@ -177,15 +182,24 @@ SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * bas
177182
state.store,
178183
state.fetchSettings,
179184
EvalSettings::resolvePseudoUrl(s));
180-
auto storePath = fetchToStore(*state.store, SourcePath(accessor), FetchMode::Copy);
185+
auto storePath = fetchToStore(
186+
state.fetchSettings,
187+
*state.store,
188+
SourcePath(accessor),
189+
FetchMode::Copy);
181190
return state.storePath(storePath);
182191
}
183192

184193
else if (hasPrefix(s, "flake:")) {
185194
experimentalFeatureSettings.require(Xp::Flakes);
186195
auto flakeRef = parseFlakeRef(fetchSettings, std::string(s.substr(6)), {}, true, false);
187196
auto [accessor, lockedRef] = flakeRef.resolve(state.store).lazyFetch(state.store);
188-
auto storePath = nix::fetchToStore(*state.store, SourcePath(accessor), FetchMode::Copy, lockedRef.input.getName());
197+
auto storePath = nix::fetchToStore(
198+
state.fetchSettings,
199+
*state.store,
200+
SourcePath(accessor),
201+
FetchMode::Copy,
202+
lockedRef.input.getName());
189203
state.allowPath(storePath);
190204
return state.storePath(storePath);
191205
}

src/libcmd/installable-value.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ref<InstallableValue> InstallableValue::require(ref<Installable> installable)
4545
std::optional<DerivedPathWithInfo> InstallableValue::trySinglePathToDerivedPaths(Value & v, const PosIdx pos, std::string_view errorCtx)
4646
{
4747
if (v.type() == nPath) {
48-
auto storePath = fetchToStore(*state->store, v.path(), FetchMode::Copy);
48+
auto storePath = fetchToStore(state->fetchSettings, *state->store, v.path(), FetchMode::Copy);
4949
return {{
5050
.path = DerivedPath::Opaque {
5151
.path = std::move(storePath),

src/libexpr/eval.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,7 @@ StorePath EvalState::copyPathToStore(NixStringContext & context, const SourcePat
24232423
? *dstPathCached
24242424
: [&]() {
24252425
auto dstPath = fetchToStore(
2426+
fetchSettings,
24262427
*store,
24272428
path.resolveSymlinks(SymlinkResolution::Ancestors),
24282429
settings.readOnlyMode ? FetchMode::DryRun : FetchMode::Copy,
@@ -3125,7 +3126,7 @@ std::optional<SourcePath> EvalState::resolveLookupPathPath(const LookupPath::Pat
31253126
store,
31263127
fetchSettings,
31273128
EvalSettings::resolvePseudoUrl(value));
3128-
auto storePath = fetchToStore(*store, SourcePath(accessor), FetchMode::Copy);
3129+
auto storePath = fetchToStore(fetchSettings, *store, SourcePath(accessor), FetchMode::Copy);
31293130
return finish(this->storePath(storePath));
31303131
} catch (Error & e) {
31313132
logWarning({

src/libexpr/primops.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,7 @@ static void addPath(
25452545

25462546
if (!expectedHash || !state.store->isValidPath(*expectedStorePath)) {
25472547
auto dstPath = fetchToStore(
2548+
state.fetchSettings,
25482549
*state.store,
25492550
path.resolveSymlinks(),
25502551
settings.readOnlyMode ? FetchMode::DryRun : FetchMode::Copy,

src/libexpr/primops/fetchTree.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,12 @@ static void fetch(EvalState & state, const PosIdx pos, Value * * args, Value & v
537537
auto storePath =
538538
unpack
539539
? fetchToStore(
540+
state.fetchSettings,
540541
*state.store,
541542
fetchers::downloadTarball(state.store, state.fetchSettings, *url),
542543
FetchMode::Copy,
543544
name)
544-
: fetchers::downloadFile(state.store, *url, name).storePath;
545+
: fetchers::downloadFile(state.store, state.fetchSettings, *url, name).storePath;
545546

546547
if (expectedHash) {
547548
auto hash = unpack

src/libfetchers/cache.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "nix/fetchers/cache.hh"
2+
#include "nix/fetchers/fetch-settings.hh"
23
#include "nix/util/users.hh"
34
#include "nix/store/sqlite.hh"
45
#include "nix/util/sync.hh"
@@ -162,10 +163,12 @@ struct CacheImpl : Cache
162163
}
163164
};
164165

165-
ref<Cache> getCache()
166+
ref<Cache> Settings::getCache() const
166167
{
167-
static auto cache = std::make_shared<CacheImpl>();
168-
return ref<Cache>(cache);
168+
auto cache(_cache.lock());
169+
if (!*cache)
170+
*cache = std::make_shared<CacheImpl>();
171+
return ref<Cache>(*cache);
169172
}
170173

171174
}

src/libfetchers/fetch-to-store.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include "nix/fetchers/fetch-to-store.hh"
22
#include "nix/fetchers/fetchers.hh"
3+
#include "nix/fetchers/fetch-settings.hh"
34

45
namespace nix {
56

67
fetchers::Cache::Key makeFetchToStoreCacheKey(
7-
const std::string &name,
8-
const std::string &fingerprint,
8+
const std::string & name,
9+
const std::string & fingerprint,
910
ContentAddressMethod method,
10-
const std::string &path)
11+
const std::string & path)
1112
{
1213
return fetchers::Cache::Key{"fetchToStore", {
1314
{"name", name},
@@ -19,6 +20,7 @@ fetchers::Cache::Key makeFetchToStoreCacheKey(
1920
}
2021

2122
StorePath fetchToStore(
23+
const fetchers::Settings & settings,
2224
Store & store,
2325
const SourcePath & path,
2426
FetchMode mode,
@@ -34,7 +36,7 @@ StorePath fetchToStore(
3436

3537
if (!filter && path.accessor->fingerprint) {
3638
cacheKey = makeFetchToStoreCacheKey(std::string{name}, *path.accessor->fingerprint, method, path.path.abs());
37-
if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store)) {
39+
if (auto res = settings.getCache()->lookupStorePath(*cacheKey, store)) {
3840
debug("store path cache hit for '%s'", path);
3941
return res->storePath;
4042
}
@@ -56,7 +58,7 @@ StorePath fetchToStore(
5658
debug(mode == FetchMode::DryRun ? "hashed '%s'" : "copied '%s' to '%s'", path, store.printStorePath(storePath));
5759

5860
if (cacheKey && mode == FetchMode::Copy)
59-
fetchers::getCache()->upsert(*cacheKey, store, {}, storePath);
61+
settings.getCache()->upsert(*cacheKey, store, {}, storePath);
6062

6163
return storePath;
6264
}

src/libfetchers/fetchers.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ std::pair<StorePath, Input> Input::fetchToStore(ref<Store> store) const
198198
try {
199199
auto [accessor, result] = getAccessorUnchecked(store);
200200

201-
auto storePath = nix::fetchToStore(*store, SourcePath(accessor), FetchMode::Copy, result.getName());
201+
auto storePath = nix::fetchToStore(*settings, *store, SourcePath(accessor), FetchMode::Copy, result.getName());
202202

203203
auto narHash = store->queryPathInfo(storePath)->narHash;
204204
result.attrs.insert_or_assign("narHash", narHash.to_string(HashFormat::SRI, true));

src/libfetchers/git-utils.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "nix/fetchers/git-utils.hh"
22
#include "nix/fetchers/git-lfs-fetch.hh"
33
#include "nix/fetchers/cache.hh"
4+
#include "nix/fetchers/fetch-settings.hh"
45
#include "nix/util/finally.hh"
56
#include "nix/util/processes.hh"
67
#include "nix/util/signals.hh"
@@ -610,18 +611,18 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
610611
throw Error("Commit signature verification on commit %s failed: %s", rev.gitRev(), output);
611612
}
612613

613-
Hash treeHashToNarHash(const Hash & treeHash) override
614+
Hash treeHashToNarHash(const fetchers::Settings & settings, const Hash & treeHash) override
614615
{
615616
auto accessor = getAccessor(treeHash, false, "");
616617

617618
fetchers::Cache::Key cacheKey{"treeHashToNarHash", {{"treeHash", treeHash.gitRev()}}};
618619

619-
if (auto res = fetchers::getCache()->lookup(cacheKey))
620+
if (auto res = settings.getCache()->lookup(cacheKey))
620621
return Hash::parseAny(fetchers::getStrAttr(*res, "narHash"), HashAlgorithm::SHA256);
621622

622623
auto narHash = accessor->hashPath(CanonPath::root);
623624

624-
fetchers::getCache()->upsert(cacheKey, fetchers::Attrs({{"narHash", narHash.to_string(HashFormat::SRI, true)}}));
625+
settings.getCache()->upsert(cacheKey, fetchers::Attrs({{"narHash", narHash.to_string(HashFormat::SRI, true)}}));
625626

626627
return narHash;
627628
}

src/libfetchers/git.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,11 @@ struct GitInputScheme : InputScheme
480480
return repoInfo;
481481
}
482482

483-
uint64_t getLastModified(const RepoInfo & repoInfo, const std::filesystem::path & repoDir, const Hash & rev) const
483+
uint64_t getLastModified(const Settings & settings, const RepoInfo & repoInfo, const std::filesystem::path & repoDir, const Hash & rev) const
484484
{
485485
Cache::Key key{"gitLastModified", {{"rev", rev.gitRev()}}};
486486

487-
auto cache = getCache();
487+
auto cache = settings.getCache();
488488

489489
if (auto res = cache->lookup(key))
490490
return getIntAttr(*res, "lastModified");
@@ -496,11 +496,11 @@ struct GitInputScheme : InputScheme
496496
return lastModified;
497497
}
498498

499-
uint64_t getRevCount(const RepoInfo & repoInfo, const std::filesystem::path & repoDir, const Hash & rev) const
499+
uint64_t getRevCount(const Settings & settings, const RepoInfo & repoInfo, const std::filesystem::path & repoDir, const Hash & rev) const
500500
{
501501
Cache::Key key{"gitRevCount", {{"rev", rev.gitRev()}}};
502502

503-
auto cache = getCache();
503+
auto cache = settings.getCache();
504504

505505
if (auto revCountAttrs = cache->lookup(key))
506506
return getIntAttr(*revCountAttrs, "revCount");
@@ -678,12 +678,12 @@ struct GitInputScheme : InputScheme
678678

679679
Attrs infoAttrs({
680680
{"rev", rev.gitRev()},
681-
{"lastModified", getLastModified(repoInfo, repoDir, rev)},
681+
{"lastModified", getLastModified(*input.settings, repoInfo, repoDir, rev)},
682682
});
683683

684684
if (!getShallowAttr(input))
685685
infoAttrs.insert_or_assign("revCount",
686-
getRevCount(repoInfo, repoDir, rev));
686+
getRevCount(*input.settings, repoInfo, repoDir, rev));
687687

688688
printTalkative("using revision %s of repo '%s'", rev.gitRev(), repoInfo.locationToArg());
689689

@@ -799,7 +799,7 @@ struct GitInputScheme : InputScheme
799799

800800
input.attrs.insert_or_assign("rev", rev.gitRev());
801801
input.attrs.insert_or_assign("revCount",
802-
rev == nullRev ? 0 : getRevCount(repoInfo, repoPath, rev));
802+
rev == nullRev ? 0 : getRevCount(*input.settings, repoInfo, repoPath, rev));
803803

804804
verifyCommit(input, repo);
805805
} else {
@@ -818,7 +818,7 @@ struct GitInputScheme : InputScheme
818818
input.attrs.insert_or_assign(
819819
"lastModified",
820820
repoInfo.workdirInfo.headRev
821-
? getLastModified(repoInfo, repoPath, *repoInfo.workdirInfo.headRev)
821+
? getLastModified(*input.settings, repoInfo, repoPath, *repoInfo.workdirInfo.headRev)
822822
: 0);
823823

824824
return {accessor, std::move(input)};

0 commit comments

Comments
 (0)