From 3bca42eb0049772d9079f29f25186575f8e5a4ae Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sun, 9 Feb 2025 17:30:04 +0100 Subject: [PATCH] libstore: asyncify Store::optimiseStore Change-Id: I443fe4bddb0bec519ee36d55060e082ae66802cc --- lix/legacy/nix-store.cc | 2 +- lix/libstore/daemon.cc | 2 +- lix/libstore/local-store.hh | 4 ++-- lix/libstore/optimise-store.cc | 17 ++++++++++++----- lix/libstore/remote-store.cc | 7 +++++-- lix/libstore/remote-store.hh | 2 +- lix/libstore/store-api.hh | 2 +- lix/nix/optimise-store.cc | 2 +- 8 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lix/legacy/nix-store.cc b/lix/legacy/nix-store.cc index abfd66cd1..1c885f1a6 100644 --- a/lix/legacy/nix-store.cc +++ b/lix/legacy/nix-store.cc @@ -811,7 +811,7 @@ static void opOptimise(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) if (!opArgs.empty() || !opFlags.empty()) throw UsageError("no arguments expected"); - store->optimiseStore(); + aio.blockOn(store->optimiseStore()); } /* Serve the nix store in a way usable by a restricted ssh user. */ diff --git a/lix/libstore/daemon.cc b/lix/libstore/daemon.cc index 392654050..c56e0e884 100644 --- a/lix/libstore/daemon.cc +++ b/lix/libstore/daemon.cc @@ -843,7 +843,7 @@ static void performOp(AsyncIoRoot & aio, TunnelLogger * logger, ref store case WorkerProto::Op::OptimiseStore: logger->startWork(); - store->optimiseStore(); + aio.blockOn(store->optimiseStore()); logger->stopWork(); to << 1; break; diff --git a/lix/libstore/local-store.hh b/lix/libstore/local-store.hh index e65b60e0c..3ce451fbb 100644 --- a/lix/libstore/local-store.hh +++ b/lix/libstore/local-store.hh @@ -263,9 +263,9 @@ public: * Optimise the disk space usage of the Nix store by hard-linking * files with the same contents. */ - void optimiseStore(OptimiseStats & stats); + kj::Promise> optimiseStore(OptimiseStats & stats); - void optimiseStore() override; + kj::Promise> optimiseStore() override; /** * Optimise a single store path. Optionally, test the encountered diff --git a/lix/libstore/optimise-store.cc b/lix/libstore/optimise-store.cc index 15a793096..541114f52 100644 --- a/lix/libstore/optimise-store.cc +++ b/lix/libstore/optimise-store.cc @@ -1,5 +1,6 @@ #include "lix/libstore/local-store.hh" #include "lix/libstore/globals.hh" +#include "lix/libutil/result.hh" #include "lix/libutil/signals.hh" #include "lix/libutil/strings.hh" @@ -256,8 +257,8 @@ void LocalStore::optimisePath_(Activity * act, OptimiseStats & stats, } -void LocalStore::optimiseStore(OptimiseStats & stats) -{ +kj::Promise> LocalStore::optimiseStore(OptimiseStats & stats) +try { Activity act(*logger, actOptimiseStore); auto paths = queryAllValidPaths(); @@ -283,17 +284,23 @@ void LocalStore::optimiseStore(OptimiseStats & stats) done++; act.progress(done, paths.size()); } + co_return result::success(); +} catch (...) { + co_return result::current_exception(); } -void LocalStore::optimiseStore() -{ +kj::Promise> LocalStore::optimiseStore() +try { OptimiseStats stats; - optimiseStore(stats); + TRY_AWAIT(optimiseStore(stats)); printInfo("%s freed by hard-linking %d files", showBytes(stats.bytesFreed), stats.filesLinked); + co_return result::success(); +} catch (...) { + co_return result::current_exception(); } void LocalStore::optimisePath(const Path & path, RepairFlag repair) diff --git a/lix/libstore/remote-store.cc b/lix/libstore/remote-store.cc index 87420d22f..7cbff236f 100644 --- a/lix/libstore/remote-store.cc +++ b/lix/libstore/remote-store.cc @@ -808,12 +808,15 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results) } -void RemoteStore::optimiseStore() -{ +kj::Promise> RemoteStore::optimiseStore() +try { auto conn(getConnection()); conn->to << WorkerProto::Op::OptimiseStore; conn.processStderr(); readInt(conn->from); + co_return result::success(); +} catch (...) { + co_return result::current_exception(); } diff --git a/lix/libstore/remote-store.hh b/lix/libstore/remote-store.hh index ffccd697b..0c26f3fc9 100644 --- a/lix/libstore/remote-store.hh +++ b/lix/libstore/remote-store.hh @@ -135,7 +135,7 @@ public: void collectGarbage(const GCOptions & options, GCResults & results) override; - void optimiseStore() override; + kj::Promise> optimiseStore() override; kj::Promise> verifyStore(bool checkContents, RepairFlag repair) override; diff --git a/lix/libstore/store-api.hh b/lix/libstore/store-api.hh index df5777311..b059f0c78 100644 --- a/lix/libstore/store-api.hh +++ b/lix/libstore/store-api.hh @@ -703,7 +703,7 @@ public: * Optimise the disk space usage of the Nix store by hard-linking files * with the same contents. */ - virtual void optimiseStore() { }; + virtual kj::Promise> optimiseStore() { return {result::success()}; } /** * Check the integrity of the Nix store. diff --git a/lix/nix/optimise-store.cc b/lix/nix/optimise-store.cc index 53fd510be..f2b236e6c 100644 --- a/lix/nix/optimise-store.cc +++ b/lix/nix/optimise-store.cc @@ -23,7 +23,7 @@ struct CmdOptimiseStore : StoreCommand void run(ref store) override { - store->optimiseStore(); + aio().blockOn(store->optimiseStore()); } };