libstore: de-callback-ify Store::queryPathInfoUncached

Change-Id: I23a156aaff5328f67ca16ccd85c0ea1711b21e35
This commit is contained in:
eldritch horrors 2024-04-27 21:24:36 +02:00
parent c77bd88259
commit 2f4a1dd6e0
11 changed files with 106 additions and 168 deletions

View file

@ -349,8 +349,7 @@ void BinaryCacheStore::narFromPath(const StorePath & storePath, Sink & sink)
stats.narReadBytes += narSize.length; stats.narReadBytes += narSize.length;
} }
void BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath, std::shared_ptr<const ValidPathInfo> BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath)
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept
{ {
auto uri = getUri(); auto uri = getUri();
auto storePathS = printStorePath(storePath); auto storePathS = printStorePath(storePath);
@ -360,17 +359,13 @@ void BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath,
auto narInfoFile = narInfoFileFor(storePath); auto narInfoFile = narInfoFileFor(storePath);
try { auto data = getFile(narInfoFile);
auto data = getFile(narInfoFile);
if (!data) return callback({}); if (!data) return {};
stats.narInfoRead++; stats.narInfoRead++;
callback(std::make_shared<NarInfo>(*this, *data, narInfoFile)); return std::make_shared<NarInfo>(*this, *data, narInfoFile);
} catch (...) {
callback.rethrow();
}
} }
StorePath BinaryCacheStore::addToStore( StorePath BinaryCacheStore::addToStore(

View file

@ -107,8 +107,7 @@ public:
bool isValidPathUncached(const StorePath & path) override; bool isValidPathUncached(const StorePath & path) override;
void queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) override;
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override;
std::optional<StorePath> queryPathFromHashPart(const std::string & hashPart) override; std::optional<StorePath> queryPathFromHashPart(const std::string & hashPart) override;

View file

@ -1260,8 +1260,7 @@ struct RestrictedStore : public virtual RestrictedStoreConfig, public virtual In
return paths; return paths;
} }
void queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) override
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override
{ {
if (goal.isAllowed(path)) { if (goal.isAllowed(path)) {
try { try {
@ -1271,12 +1270,12 @@ struct RestrictedStore : public virtual RestrictedStoreConfig, public virtual In
info->registrationTime = 0; info->registrationTime = 0;
info->ultimate = false; info->ultimate = false;
info->sigs.clear(); info->sigs.clear();
callback(info); return info;
} catch (InvalidPath &) { } catch (InvalidPath &) {
callback(nullptr); return nullptr;
} }
} else } else
callback(nullptr); return nullptr;
}; };
void queryReferrers(const StorePath & path, StorePathSet & referrers) override void queryReferrers(const StorePath & path, StorePathSet & referrers) override

View file

@ -33,10 +33,9 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
return *uriSchemes().begin(); return *uriSchemes().begin();
} }
void queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) override
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override
{ {
callback(nullptr); return nullptr;
} }
/** /**

View file

@ -143,36 +143,33 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
return *uriSchemes().begin() + "://" + host; return *uriSchemes().begin() + "://" + host;
} }
void queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) override
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override
{ {
try { auto conn(connections->get());
auto conn(connections->get());
/* No longer support missing NAR hash */ /* No longer support missing NAR hash */
assert(GET_PROTOCOL_MINOR(conn->remoteVersion) >= 4); assert(GET_PROTOCOL_MINOR(conn->remoteVersion) >= 4);
debug("querying remote host '%s' for info on '%s'", host, printStorePath(path)); debug("querying remote host '%s' for info on '%s'", host, printStorePath(path));
conn->to << ServeProto::Command::QueryPathInfos << PathSet{printStorePath(path)}; conn->to << ServeProto::Command::QueryPathInfos << PathSet{printStorePath(path)};
conn->to.flush(); conn->to.flush();
auto p = readString(conn->from); auto p = readString(conn->from);
if (p.empty()) return callback(nullptr); if (p.empty()) return nullptr;
auto path2 = parseStorePath(p); auto path2 = parseStorePath(p);
assert(path == path2); assert(path == path2);
auto info = std::make_shared<ValidPathInfo>( auto info = std::make_shared<ValidPathInfo>(
path, path,
ServeProto::Serialise<UnkeyedValidPathInfo>::read(*this, *conn)); ServeProto::Serialise<UnkeyedValidPathInfo>::read(*this, *conn));
if (info->narHash == Hash::dummy) if (info->narHash == Hash::dummy)
throw Error("NAR hash is now mandatory"); throw Error("NAR hash is now mandatory");
auto s = readString(conn->from); auto s = readString(conn->from);
assert(s == ""); assert(s == "");
callback(std::move(info)); return info;
} catch (...) { callback.rethrow(); }
} }
void addToStore(const ValidPathInfo & info, Source & source, void addToStore(const ValidPathInfo & info, Source & source,

View file

@ -880,16 +880,12 @@ uint64_t LocalStore::addValidPath(State & state,
} }
void LocalStore::queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> LocalStore::queryPathInfoUncached(const StorePath & path)
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept
{ {
try { return retrySQLite<std::shared_ptr<const ValidPathInfo>>([&]() {
callback(retrySQLite<std::shared_ptr<const ValidPathInfo>>([&]() { auto state(_state.lock());
auto state(_state.lock()); return queryPathInfoInternal(*state, path);
return queryPathInfoInternal(*state, path); });
}));
} catch (...) { callback.rethrow(); }
} }

View file

@ -168,8 +168,7 @@ public:
StorePathSet queryAllValidPaths() override; StorePathSet queryAllValidPaths() override;
void queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) override;
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override;
void queryReferrers(const StorePath & path, StorePathSet & referrers) override; void queryReferrers(const StorePath & path, StorePathSet & referrers) override;

View file

@ -304,32 +304,25 @@ void RemoteStore::querySubstitutablePathInfos(const StorePathCAMap & pathsMap, S
} }
void RemoteStore::queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> RemoteStore::queryPathInfoUncached(const StorePath & path)
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept
{ {
auto conn(getConnection());
conn->to << WorkerProto::Op::QueryPathInfo << printStorePath(path);
try { try {
std::shared_ptr<const ValidPathInfo> info; conn.processStderr();
{ } catch (Error & e) {
auto conn(getConnection()); // Ugly backwards compatibility hack.
conn->to << WorkerProto::Op::QueryPathInfo << printStorePath(path); if (e.msg().find("is not valid") != std::string::npos)
try { throw InvalidPath(std::move(e.info()));
conn.processStderr(); throw;
} catch (Error & e) { }
// Ugly backwards compatibility hack. if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 17) {
if (e.msg().find("is not valid") != std::string::npos) bool valid; conn->from >> valid;
throw InvalidPath(std::move(e.info())); if (!valid) throw InvalidPath("path '%s' is not valid", printStorePath(path));
throw; }
} return std::make_shared<ValidPathInfo>(
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 17) { StorePath{path},
bool valid; conn->from >> valid; WorkerProto::Serialise<UnkeyedValidPathInfo>::read(*this, *conn));
if (!valid) throw InvalidPath("path '%s' is not valid", printStorePath(path));
}
info = std::make_shared<ValidPathInfo>(
StorePath{path},
WorkerProto::Serialise<UnkeyedValidPathInfo>::read(*this, *conn));
}
callback(std::move(info));
} catch (...) { callback.rethrow(); }
} }

View file

@ -53,8 +53,7 @@ public:
StorePathSet queryAllValidPaths() override; StorePathSet queryAllValidPaths() override;
void queryPathInfoUncached(const StorePath & path, std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) override;
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override;
void queryReferrers(const StorePath & path, StorePathSet & referrers) override; void queryReferrers(const StorePath & path, StorePathSet & referrers) override;

View file

@ -660,23 +660,6 @@ bool Store::isValidPathUncached(const StorePath & path)
} }
ref<const ValidPathInfo> Store::queryPathInfo(const StorePath & storePath)
{
std::promise<ref<const ValidPathInfo>> promise;
queryPathInfo(storePath,
{[&](std::future<ref<const ValidPathInfo>> result) {
try {
promise.set_value(result.get());
} catch (...) {
promise.set_exception(std::current_exception());
}
}});
return promise.get_future().get();
}
static bool goodStorePath(const StorePath & expected, const StorePath & actual) static bool goodStorePath(const StorePath & expected, const StorePath & actual)
{ {
return return
@ -685,64 +668,52 @@ static bool goodStorePath(const StorePath & expected, const StorePath & actual)
} }
void Store::queryPathInfo(const StorePath & storePath, ref<const ValidPathInfo> Store::queryPathInfo(const StorePath & storePath)
Callback<ref<const ValidPathInfo>> callback) noexcept
{ {
auto hashPart = std::string(storePath.hashPart()); auto hashPart = std::string(storePath.hashPart());
try { {
{ auto res = state.lock()->pathInfoCache.get(std::string(storePath.to_string()));
auto res = state.lock()->pathInfoCache.get(std::string(storePath.to_string())); if (res && res->isKnownNow()) {
if (res && res->isKnownNow()) { stats.narInfoReadAverted++;
stats.narInfoReadAverted++; if (!res->didExist())
if (!res->didExist()) throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
throw InvalidPath("path '%s' is not valid", printStorePath(storePath)); return ref<const ValidPathInfo>(res->value);
return callback(ref<const ValidPathInfo>(res->value));
}
} }
}
if (diskCache) { if (diskCache) {
auto res = diskCache->lookupNarInfo(getUri(), hashPart); auto res = diskCache->lookupNarInfo(getUri(), hashPart);
if (res.first != NarInfoDiskCache::oUnknown) { if (res.first != NarInfoDiskCache::oUnknown) {
stats.narInfoReadAverted++; stats.narInfoReadAverted++;
{ {
auto state_(state.lock()); auto state_(state.lock());
state_->pathInfoCache.upsert(std::string(storePath.to_string()), state_->pathInfoCache.upsert(std::string(storePath.to_string()),
res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second }); res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second });
if (res.first == NarInfoDiskCache::oInvalid || if (res.first == NarInfoDiskCache::oInvalid ||
!goodStorePath(storePath, res.second->path)) !goodStorePath(storePath, res.second->path))
throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
}
return callback(ref<const ValidPathInfo>(res.second));
}
}
} catch (...) { return callback.rethrow(); }
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));
queryPathInfoUncached(storePath,
{[this, storePath, hashPart, callbackPtr](std::future<std::shared_ptr<const ValidPathInfo>> fut) {
try {
auto info = fut.get();
if (diskCache)
diskCache->upsertNarInfo(getUri(), hashPart, info);
{
auto state_(state.lock());
state_->pathInfoCache.upsert(std::string(storePath.to_string()), PathInfoCacheValue { .value = info });
}
if (!info || !goodStorePath(storePath, info->path)) {
stats.narInfoMissing++;
throw InvalidPath("path '%s' is not valid", printStorePath(storePath)); throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
} }
return ref<const ValidPathInfo>(res.second);
}
}
(*callbackPtr)(ref<const ValidPathInfo>(info)); auto info = queryPathInfoUncached(storePath);
} catch (...) { callbackPtr->rethrow(); }
}}); if (diskCache)
diskCache->upsertNarInfo(getUri(), hashPart, info);
{
auto state_(state.lock());
state_->pathInfoCache.upsert(std::string(storePath.to_string()), PathInfoCacheValue { .value = info });
}
if (!info || !goodStorePath(storePath, info->path)) {
stats.narInfoMissing++;
throw InvalidPath("path '%s' is not valid", printStorePath(storePath));
}
return ref<const ValidPathInfo>(info);
} }
void Store::queryRealisation(const DrvOutput & id, void Store::queryRealisation(const DrvOutput & id,
@ -852,19 +823,17 @@ StorePathSet Store::queryValidPaths(const StorePathSet & paths, SubstituteFlag m
auto doQuery = [&](const StorePath & path) { auto doQuery = [&](const StorePath & path) {
checkInterrupt(); checkInterrupt();
queryPathInfo(path, {[path, &state_, &wakeup](std::future<ref<const ValidPathInfo>> fut) { auto state(state_.lock());
auto state(state_.lock()); try {
try { auto info = queryPathInfo(path);
auto info = fut.get(); state->valid.insert(path);
state->valid.insert(path); } catch (InvalidPath &) {
} catch (InvalidPath &) { } catch (...) {
} catch (...) { state->exc = std::current_exception();
state->exc = std::current_exception(); }
} assert(state->left);
assert(state->left); if (!--state->left)
if (!--state->left) wakeup.notify_one();
wakeup.notify_one();
}});
}; };
for (auto & path : paths) for (auto & path : paths)

View file

@ -366,12 +366,6 @@ public:
*/ */
ref<const ValidPathInfo> queryPathInfo(const StorePath & path); ref<const ValidPathInfo> queryPathInfo(const StorePath & path);
/**
* Asynchronous version of queryPathInfo().
*/
void queryPathInfo(const StorePath & path,
Callback<ref<const ValidPathInfo>> callback) noexcept;
/** /**
* Query the information about a realisation. * Query the information about a realisation.
*/ */
@ -407,8 +401,7 @@ public:
protected: protected:
virtual void queryPathInfoUncached(const StorePath & path, virtual std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) = 0;
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept = 0;
virtual void queryRealisationUncached(const DrvOutput &, virtual void queryRealisationUncached(const DrvOutput &,
Callback<std::shared_ptr<const Realisation>> callback) noexcept = 0; Callback<std::shared_ptr<const Realisation>> callback) noexcept = 0;