From 85cd558b6d52d38bd535815f5f5ee7cff0c77395 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Tue, 4 Mar 2025 23:08:34 +0100 Subject: [PATCH] libstore: asyncify Store::queryValidDerivers Change-Id: I9ac447cb57e0ba330eee216280eb3d7913c5782b --- lix/legacy/nix-store.cc | 5 +++-- lix/libcmd/installables.cc | 12 +++++++----- lix/libstore/daemon.cc | 2 +- lix/libstore/gc.cc | 2 +- lix/libstore/local-store.cc | 27 +++++++++++++++++---------- lix/libstore/local-store.hh | 2 +- lix/libstore/misc.cc | 2 +- lix/libstore/remote-store.cc | 8 +++++--- lix/libstore/remote-store.hh | 2 +- lix/libstore/store-api.hh | 5 ++++- 10 files changed, 41 insertions(+), 26 deletions(-) diff --git a/lix/legacy/nix-store.cc b/lix/legacy/nix-store.cc index 5479eec2b..3c8985ce5 100644 --- a/lix/legacy/nix-store.cc +++ b/lix/legacy/nix-store.cc @@ -398,8 +398,9 @@ static void opQuery(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) case qValidDerivers: { StorePathSet result; for (auto & i : opArgs) { - auto derivers = store->queryValidDerivers(store->followLinksToStorePath(i)); - for (const auto &i: derivers) { + auto derivers = + aio.blockOn(store->queryValidDerivers(store->followLinksToStorePath(i))); + for (const auto & i : derivers) { result.insert(i); } } diff --git a/lix/libcmd/installables.cc b/lix/libcmd/installables.cc index 6f595b3f0..ec9a1693c 100644 --- a/lix/libcmd/installables.cc +++ b/lix/libcmd/installables.cc @@ -386,16 +386,18 @@ DerivedPathWithInfo Installable::toDerivedPath(EvalState & state) return std::move(buildables[0]); } -static StorePath getDeriver( +static kj::Promise> getDeriver( ref store, const Installable & i, const StorePath & drvPath) -{ - auto derivers = store->queryValidDerivers(drvPath); +try { + auto derivers = TRY_AWAIT(store->queryValidDerivers(drvPath)); if (derivers.empty()) throw Error("'%s' does not have a known deriver", i.what()); // FIXME: use all derivers? - return *derivers.begin(); + co_return *derivers.begin(); +} catch (...) { + co_return result::current_exception(); } ref openEvalCache( @@ -785,7 +787,7 @@ StorePathSet Installable::toDerivations( bo.path.isDerivation() ? bo.path : useDeriver - ? getDeriver(store, *i, bo.path) + ? state.aio.blockOn(getDeriver(store, *i, bo.path)) : throw Error("argument '%s' did not evaluate to a derivation", i->what())); }, [&](const DerivedPath::Built & bfd) { diff --git a/lix/libstore/daemon.cc b/lix/libstore/daemon.cc index fd6e9b00d..db2aa3142 100644 --- a/lix/libstore/daemon.cc +++ b/lix/libstore/daemon.cc @@ -353,7 +353,7 @@ static void performOp(AsyncIoRoot & aio, TunnelLogger * logger, ref store break; } case WorkerProto::Op::QueryValidDerivers: { - paths = store->queryValidDerivers(path); + paths = aio.blockOn(store->queryValidDerivers(path)); break; } case WorkerProto::Op::QueryDerivationOutputs: { diff --git a/lix/libstore/gc.cc b/lix/libstore/gc.cc index 6850b6c24..5d5a10319 100644 --- a/lix/libstore/gc.cc +++ b/lix/libstore/gc.cc @@ -782,7 +782,7 @@ try { /* If keep-outputs is set, then visit the derivers. */ if (gcKeepOutputs) { - auto derivers = queryValidDerivers(*path); + auto derivers = TRY_AWAIT(queryValidDerivers(*path)); for (auto & i : derivers) enqueue(i); } diff --git a/lix/libstore/local-store.cc b/lix/libstore/local-store.cc index 03b648bc4..cb136e982 100644 --- a/lix/libstore/local-store.cc +++ b/lix/libstore/local-store.cc @@ -1060,19 +1060,26 @@ void LocalStore::queryReferrers(const StorePath & path, StorePathSet & referrers } -StorePathSet LocalStore::queryValidDerivers(const StorePath & path) -{ - return retrySQLite([&]() { - auto state = dbPool.get(); +kj::Promise> LocalStore::queryValidDerivers(const StorePath & path) +try { + // NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines) + co_return TRY_AWAIT(retrySQLite([&]() -> kj::Promise> { + try { + auto state = dbPool.get(); - auto useQueryValidDerivers(state->stmts->QueryValidDerivers.use()(printStorePath(path))); + auto useQueryValidDerivers(state->stmts->QueryValidDerivers.use()(printStorePath(path))); - StorePathSet derivers; - while (useQueryValidDerivers.next()) - derivers.insert(parseStorePath(useQueryValidDerivers.getStr(1))); + StorePathSet derivers; + while (useQueryValidDerivers.next()) + derivers.insert(parseStorePath(useQueryValidDerivers.getStr(1))); - return derivers; - }, always_progresses); + co_return derivers; + } catch (...) { + co_return result::current_exception(); + } + })); +} catch (...) { + co_return result::current_exception(); } diff --git a/lix/libstore/local-store.hh b/lix/libstore/local-store.hh index 9cf5d29c9..7c08369fa 100644 --- a/lix/libstore/local-store.hh +++ b/lix/libstore/local-store.hh @@ -200,7 +200,7 @@ public: void queryReferrers(const StorePath & path, StorePathSet & referrers) override; - StorePathSet queryValidDerivers(const StorePath & path) override; + kj::Promise> queryValidDerivers(const StorePath & path) override; kj::Promise>>> queryStaticPartialDerivationOutputMap(const StorePath & path) override; diff --git a/lix/libstore/misc.cc b/lix/libstore/misc.cc index 6850efe66..707d3f137 100644 --- a/lix/libstore/misc.cc +++ b/lix/libstore/misc.cc @@ -32,7 +32,7 @@ try { res.insert(ref); if (includeOutputs) - for (auto& i : queryValidDerivers(path)) + for (auto& i : TRY_AWAIT(queryValidDerivers(path))) res.insert(i); if (includeDerivers && path.isDerivation()) diff --git a/lix/libstore/remote-store.cc b/lix/libstore/remote-store.cc index 45151f072..1bcce6250 100644 --- a/lix/libstore/remote-store.cc +++ b/lix/libstore/remote-store.cc @@ -317,12 +317,14 @@ void RemoteStore::queryReferrers(const StorePath & path, } -StorePathSet RemoteStore::queryValidDerivers(const StorePath & path) -{ +kj::Promise> RemoteStore::queryValidDerivers(const StorePath & path) +try { auto conn(getConnection()); conn->to << WorkerProto::Op::QueryValidDerivers << printStorePath(path); conn.processStderr(); - return WorkerProto::Serialise::read(*this, *conn); + co_return WorkerProto::Serialise::read(*this, *conn); +} catch (...) { + co_return result::current_exception(); } diff --git a/lix/libstore/remote-store.hh b/lix/libstore/remote-store.hh index 913184d89..8d4c7c235 100644 --- a/lix/libstore/remote-store.hh +++ b/lix/libstore/remote-store.hh @@ -61,7 +61,7 @@ public: void queryReferrers(const StorePath & path, StorePathSet & referrers) override; - StorePathSet queryValidDerivers(const StorePath & path) override; + kj::Promise> queryValidDerivers(const StorePath & path) override; kj::Promise> queryDerivationOutputs(const StorePath & path) override; diff --git a/lix/libstore/store-api.hh b/lix/libstore/store-api.hh index e223cdac8..187c3ac96 100644 --- a/lix/libstore/store-api.hh +++ b/lix/libstore/store-api.hh @@ -440,7 +440,10 @@ public: * was actually used to produce `path`, which may not exist * anymore.) */ - virtual StorePathSet queryValidDerivers(const StorePath & path) { return {}; }; + virtual kj::Promise> queryValidDerivers(const StorePath & path) + { + return {StorePathSet{}}; + } /** * Query the outputs of the derivation denoted by `path`.