From b6dadd5cf01ab20d8ba4f9352eb83e76113f267a Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Fri, 21 Feb 2025 00:35:11 +0100 Subject: [PATCH] libstore: asyncify Store::queryValidPaths Change-Id: I3a3303b288321cd812f4df87c80a676f997e57b9 --- lix/legacy/nix-env.cc | 2 +- lix/legacy/nix-store.cc | 2 +- lix/libstore/daemon.cc | 2 +- lix/libstore/legacy-ssh-store.cc | 8 +++++--- lix/libstore/local-store.cc | 11 +++++++---- lix/libstore/local-store.hh | 2 +- lix/libstore/remote-store.cc | 9 ++++++--- lix/libstore/remote-store.hh | 2 +- lix/libstore/store-api.cc | 13 ++++++++----- lix/libstore/store-api.hh | 2 +- 10 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lix/legacy/nix-env.cc b/lix/legacy/nix-env.cc index 77dba64c6..fea427c0e 100644 --- a/lix/legacy/nix-env.cc +++ b/lix/legacy/nix-env.cc @@ -1102,7 +1102,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) printMsg(lvlTalkative, "skipping derivation named '%s' which gives an assertion failure", i.queryName(*state)); i.setFailed(); } - validPaths = store.queryValidPaths(paths); + validPaths = globals.aio.blockOn(store.queryValidPaths(paths)); substitutablePaths = globals.aio.blockOn(store.querySubstitutablePaths(paths)); } diff --git a/lix/legacy/nix-store.cc b/lix/legacy/nix-store.cc index 5364d58f1..f3e5c1b25 100644 --- a/lix/legacy/nix-store.cc +++ b/lix/legacy/nix-store.cc @@ -897,7 +897,7 @@ static void opServe(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) aio.blockOn(store->substitutePaths(paths)); } - auto valid = store->queryValidPaths(paths); + auto valid = aio.blockOn(store->queryValidPaths(paths)); out << ServeProto::write(*store, wconn, valid); break; } diff --git a/lix/libstore/daemon.cc b/lix/libstore/daemon.cc index 3a0fff193..4d3ec5159 100644 --- a/lix/libstore/daemon.cc +++ b/lix/libstore/daemon.cc @@ -292,7 +292,7 @@ static void performOp(AsyncIoRoot & aio, TunnelLogger * logger, ref store if (substitute) { aio.blockOn(store->substitutePaths(paths)); } - auto res = store->queryValidPaths(paths, substitute); + auto res = aio.blockOn(store->queryValidPaths(paths, substitute)); logger->stopWork(); to << WorkerProto::write(*store, wconn, res); break; diff --git a/lix/libstore/legacy-ssh-store.cc b/lix/libstore/legacy-ssh-store.cc index 7f0ef6b9a..a03a48213 100644 --- a/lix/libstore/legacy-ssh-store.cc +++ b/lix/libstore/legacy-ssh-store.cc @@ -410,9 +410,9 @@ public: co_return result::current_exception(); } - StorePathSet queryValidPaths(const StorePathSet & paths, + kj::Promise> queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute = NoSubstitute) override - { + try { auto conn(connections->get()); conn->to @@ -422,7 +422,9 @@ public: conn->to << ServeProto::write(*this, *conn, paths); conn->to.flush(); - return ServeProto::Serialise::read(*this, *conn); + co_return ServeProto::Serialise::read(*this, *conn); + } catch (...) { + co_return result::current_exception(); } void connect() override diff --git a/lix/libstore/local-store.cc b/lix/libstore/local-store.cc index ce2276160..ce683b4bf 100644 --- a/lix/libstore/local-store.cc +++ b/lix/libstore/local-store.cc @@ -977,12 +977,15 @@ bool LocalStore::isValidPathUncached(const StorePath & path) } -StorePathSet LocalStore::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) -{ +kj::Promise> +LocalStore::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) +try { StorePathSet res; for (auto & i : paths) if (isValidPath(i)) res.insert(i); - return res; + co_return res; +} catch (...) { + co_return result::current_exception(); } @@ -1085,7 +1088,7 @@ try { if (sub->config().storeDir != config_.storeDir) continue; if (!sub->config().wantMassQuery) continue; - auto valid = sub->queryValidPaths(remaining); + auto valid = TRY_AWAIT(sub->queryValidPaths(remaining)); StorePathSet remaining2; for (auto & path : remaining) diff --git a/lix/libstore/local-store.hh b/lix/libstore/local-store.hh index 68fa67209..8497888b7 100644 --- a/lix/libstore/local-store.hh +++ b/lix/libstore/local-store.hh @@ -187,7 +187,7 @@ public: bool isValidPathUncached(const StorePath & path) override; - StorePathSet queryValidPaths(const StorePathSet & paths, + kj::Promise> queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute = NoSubstitute) override; StorePathSet queryAllValidPaths() override; diff --git a/lix/libstore/remote-store.cc b/lix/libstore/remote-store.cc index 5ca81bc10..1533545d5 100644 --- a/lix/libstore/remote-store.cc +++ b/lix/libstore/remote-store.cc @@ -207,8 +207,9 @@ bool RemoteStore::isValidPathUncached(const StorePath & path) } -StorePathSet RemoteStore::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) -{ +kj ::Promise> +RemoteStore::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) +try { auto conn(getConnection()); conn->to << WorkerProto::Op::QueryValidPaths; conn->to << WorkerProto::write(*this, *conn, paths); @@ -216,7 +217,9 @@ StorePathSet RemoteStore::queryValidPaths(const StorePathSet & paths, Substitute conn->to << maybeSubstitute; } 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 b2a84748c..b168ecc4e 100644 --- a/lix/libstore/remote-store.hh +++ b/lix/libstore/remote-store.hh @@ -51,7 +51,7 @@ public: bool isValidPathUncached(const StorePath & path) override; - StorePathSet queryValidPaths(const StorePathSet & paths, + kj::Promise> queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute = NoSubstitute) override; StorePathSet queryAllValidPaths() override; diff --git a/lix/libstore/store-api.cc b/lix/libstore/store-api.cc index 2e651bcd8..8f2eb231f 100644 --- a/lix/libstore/store-api.cc +++ b/lix/libstore/store-api.cc @@ -844,8 +844,9 @@ try { } -StorePathSet Store::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) -{ +kj::Promise> +Store::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) +try { struct State { size_t left; @@ -890,16 +891,18 @@ StorePathSet Store::queryValidPaths(const StorePathSet & paths, SubstituteFlag m for (auto & path : paths) pool.enqueue(std::bind(doQuery, path)); - pool.process(); + TRY_AWAIT(pool.processAsync()); while (true) { auto state(state_.lock()); if (!state->left) { if (state->exc) std::rethrow_exception(state->exc); - return std::move(state->valid); + co_return std::move(state->valid); } state.wait(wakeup); } +} catch (...) { + co_return result::current_exception(); } @@ -1220,7 +1223,7 @@ kj::Promise>> copyPaths( CheckSigsFlag checkSigs, SubstituteFlag substitute) try { - auto valid = dstStore.queryValidPaths(storePaths, substitute); + auto valid = TRY_AWAIT(dstStore.queryValidPaths(storePaths, substitute)); StorePathSet missing; for (auto & path : storePaths) diff --git a/lix/libstore/store-api.hh b/lix/libstore/store-api.hh index ce6897259..ae8ae1aa0 100644 --- a/lix/libstore/store-api.hh +++ b/lix/libstore/store-api.hh @@ -361,7 +361,7 @@ public: * Query which of the given paths is valid. Optionally, try to * substitute missing paths. */ - virtual StorePathSet queryValidPaths(const StorePathSet & paths, + virtual kj::Promise> queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute = NoSubstitute); /**