libstore: un-callback-ify Store::queryRealisationUncached

Change-Id: I4a328f46eaac3bb8b19ddc091306de83348be9cf
This commit is contained in:
eldritch horrors 2024-04-27 23:44:12 +02:00
parent 2f4a1dd6e0
commit 17965bf11c
11 changed files with 60 additions and 94 deletions

View file

@ -442,21 +442,16 @@ StorePath BinaryCacheStore::addTextToStore(
})->path; })->path;
} }
void BinaryCacheStore::queryRealisationUncached(const DrvOutput & id, std::shared_ptr<const Realisation> BinaryCacheStore::queryRealisationUncached(const DrvOutput & id)
Callback<std::shared_ptr<const Realisation>> callback) noexcept
{ {
auto outputInfoFilePath = realisationsPrefix + "/" + id.to_string() + ".doi"; auto outputInfoFilePath = realisationsPrefix + "/" + id.to_string() + ".doi";
try { auto data = getFile(outputInfoFilePath);
auto data = getFile(outputInfoFilePath); if (!data) return {};
if (!data) return callback({});
auto realisation = Realisation::fromJSON( auto realisation = Realisation::fromJSON(
nlohmann::json::parse(*data), outputInfoFilePath); nlohmann::json::parse(*data), outputInfoFilePath);
return callback(std::make_shared<const Realisation>(realisation)); return std::make_shared<const Realisation>(realisation);
} catch (...) {
callback.rethrow();
}
} }
void BinaryCacheStore::registerDrvOutput(const Realisation& info) { void BinaryCacheStore::registerDrvOutput(const Realisation& info) {

View file

@ -134,8 +134,7 @@ public:
void registerDrvOutput(const Realisation & info) override; void registerDrvOutput(const Realisation & info) override;
void queryRealisationUncached(const DrvOutput &, std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput &) override;
Callback<std::shared_ptr<const Realisation>> callback) noexcept override;
void narFromPath(const StorePath & path, Sink & sink) override; void narFromPath(const StorePath & path, Sink & sink) override;

View file

@ -1353,14 +1353,13 @@ struct RestrictedStore : public virtual RestrictedStoreConfig, public virtual In
// corresponds to an allowed derivation // corresponds to an allowed derivation
{ throw Error("registerDrvOutput"); } { throw Error("registerDrvOutput"); }
void queryRealisationUncached(const DrvOutput & id, std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput & id) override
Callback<std::shared_ptr<const Realisation>> callback) noexcept override
// XXX: This should probably be allowed if the realisation corresponds to // XXX: This should probably be allowed if the realisation corresponds to
// an allowed derivation // an allowed derivation
{ {
if (!goal.isAllowed(id)) if (!goal.isAllowed(id))
callback(nullptr); return nullptr;
next->queryRealisation(id, std::move(callback)); return next->queryRealisation(id);
} }
void buildPaths(const std::vector<DerivedPath> & paths, BuildMode buildMode, std::shared_ptr<Store> evalStore) override void buildPaths(const std::vector<DerivedPath> & paths, BuildMode buildMode, std::shared_ptr<Store> evalStore) override

View file

@ -67,9 +67,8 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
void narFromPath(const StorePath & path, Sink & sink) override void narFromPath(const StorePath & path, Sink & sink) override
{ unsupported("narFromPath"); } { unsupported("narFromPath"); }
void queryRealisationUncached(const DrvOutput &, std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput &) override
Callback<std::shared_ptr<const Realisation>> callback) noexcept override { return nullptr; }
{ callback(nullptr); }
virtual ref<FSAccessor> getFSAccessor() override virtual ref<FSAccessor> getFSAccessor() override
{ unsupported("getFSAccessor"); } { unsupported("getFSAccessor"); }

View file

@ -407,8 +407,7 @@ public:
return std::nullopt; return std::nullopt;
} }
void queryRealisationUncached(const DrvOutput &, std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput &) override
Callback<std::shared_ptr<const Realisation>> callback) noexcept override
// TODO: Implement // TODO: Implement
{ unsupported("queryRealisation"); } { unsupported("queryRealisation"); }
}; };

View file

@ -1856,24 +1856,17 @@ std::optional<const Realisation> LocalStore::queryRealisation_(
return { res }; return { res };
} }
void LocalStore::queryRealisationUncached(const DrvOutput & id, std::shared_ptr<const Realisation> LocalStore::queryRealisationUncached(const DrvOutput & id)
Callback<std::shared_ptr<const Realisation>> callback) noexcept
{ {
try { auto maybeRealisation
auto maybeRealisation = retrySQLite<std::optional<const Realisation>>([&]() {
= retrySQLite<std::optional<const Realisation>>([&]() { auto state(_state.lock());
auto state(_state.lock()); return queryRealisation_(*state, id);
return queryRealisation_(*state, id); });
}); if (maybeRealisation)
if (maybeRealisation) return std::make_shared<const Realisation>(maybeRealisation.value());
callback( else
std::make_shared<const Realisation>(maybeRealisation.value())); return nullptr;
else
callback(nullptr);
} catch (...) {
callback.rethrow();
}
} }
ContentAddress LocalStore::hashCAPath( ContentAddress LocalStore::hashCAPath(

View file

@ -294,8 +294,7 @@ public:
std::optional<const Realisation> queryRealisation_(State & state, const DrvOutput & id); std::optional<const Realisation> queryRealisation_(State & state, const DrvOutput & id);
std::optional<std::pair<int64_t, Realisation>> queryRealisationCore_(State & state, const DrvOutput & id); std::optional<std::pair<int64_t, Realisation>> queryRealisationCore_(State & state, const DrvOutput & id);
void queryRealisationUncached(const DrvOutput&, std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput&) override;
Callback<std::shared_ptr<const Realisation>> callback) noexcept override;
std::optional<std::string> getVersion() override; std::optional<std::string> getVersion() override;

View file

@ -612,39 +612,32 @@ void RemoteStore::registerDrvOutput(const Realisation & info)
conn.processStderr(); conn.processStderr();
} }
void RemoteStore::queryRealisationUncached(const DrvOutput & id, std::shared_ptr<const Realisation> RemoteStore::queryRealisationUncached(const DrvOutput & id)
Callback<std::shared_ptr<const Realisation>> callback) noexcept
{ {
try { auto conn(getConnection());
auto conn(getConnection());
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 27) { if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 27) {
warn("the daemon is too old to support content-addressed derivations, please upgrade it to 2.4"); warn("the daemon is too old to support content-addressed derivations, please upgrade it to 2.4");
return callback(nullptr); return nullptr;
} }
conn->to << WorkerProto::Op::QueryRealisation; conn->to << WorkerProto::Op::QueryRealisation;
conn->to << id.to_string(); conn->to << id.to_string();
conn.processStderr(); conn.processStderr();
auto real = [&]() -> std::shared_ptr<const Realisation> { if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 31) {
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 31) { auto outPaths = WorkerProto::Serialise<std::set<StorePath>>::read(
auto outPaths = WorkerProto::Serialise<std::set<StorePath>>::read( *this, *conn);
*this, *conn); if (outPaths.empty())
if (outPaths.empty()) return nullptr;
return nullptr; return std::make_shared<const Realisation>(Realisation { .id = id, .outPath = *outPaths.begin() });
return std::make_shared<const Realisation>(Realisation { .id = id, .outPath = *outPaths.begin() }); } else {
} else { auto realisations = WorkerProto::Serialise<std::set<Realisation>>::read(
auto realisations = WorkerProto::Serialise<std::set<Realisation>>::read( *this, *conn);
*this, *conn); if (realisations.empty())
if (realisations.empty()) return nullptr;
return nullptr; return std::make_shared<const Realisation>(*realisations.begin());
return std::make_shared<const Realisation>(*realisations.begin()); }
}
}();
callback(std::shared_ptr<const Realisation>(real));
} catch (...) { return callback.rethrow(); }
} }
void RemoteStore::copyDrvsFromEvalStore( void RemoteStore::copyDrvsFromEvalStore(

View file

@ -108,8 +108,7 @@ public:
void registerDrvOutput(const Realisation & info) override; void registerDrvOutput(const Realisation & info) override;
void queryRealisationUncached(const DrvOutput &, std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput &) override;
Callback<std::shared_ptr<const Realisation>> callback) noexcept override;
void buildPaths(const std::vector<DerivedPath> & paths, BuildMode buildMode, std::shared_ptr<Store> evalStore) override; void buildPaths(const std::vector<DerivedPath> & paths, BuildMode buildMode, std::shared_ptr<Store> evalStore) override;

View file

@ -743,29 +743,21 @@ void Store::queryRealisation(const DrvOutput & id,
return callback.rethrow(); return callback.rethrow();
} }
auto callbackPtr try {
= std::make_shared<decltype(callback)>(std::move(callback)); auto info = queryRealisationUncached(id);
queryRealisationUncached( if (diskCache) {
id, if (info)
{ [this, id, callbackPtr]( diskCache->upsertRealisation(getUri(), *info);
std::future<std::shared_ptr<const Realisation>> fut) { else
try { diskCache->upsertAbsentRealisation(getUri(), id);
auto info = fut.get(); }
if (diskCache) { callback(std::shared_ptr<const Realisation>(info));
if (info)
diskCache->upsertRealisation(getUri(), *info);
else
diskCache->upsertAbsentRealisation(getUri(), id);
}
(*callbackPtr)(std::shared_ptr<const Realisation>(info)); } catch (...) {
callback.rethrow();
} catch (...) { }
callbackPtr->rethrow();
}
} });
} }
std::shared_ptr<const Realisation> Store::queryRealisation(const DrvOutput & id) std::shared_ptr<const Realisation> Store::queryRealisation(const DrvOutput & id)

View file

@ -402,8 +402,7 @@ public:
protected: protected:
virtual std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) = 0; virtual std::shared_ptr<const ValidPathInfo> queryPathInfoUncached(const StorePath & path) = 0;
virtual void queryRealisationUncached(const DrvOutput &, virtual std::shared_ptr<const Realisation> queryRealisationUncached(const DrvOutput &) = 0;
Callback<std::shared_ptr<const Realisation>> callback) noexcept = 0;
public: public: