From 3edc272341b22f4d088f30c407b55d91da9bff2c Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Mon, 30 Sep 2024 01:31:30 +0200 Subject: [PATCH] libstore: turn DrvOutputSubstitutionGoal::work into *one* promise Change-Id: I2d4dcedff0a278d2d8f3d264a9186dfb399275e2 --- .../build/drv-output-substitution-goal.cc | 50 +++++++------------ .../build/drv-output-substitution-goal.hh | 10 ++-- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/libstore/build/drv-output-substitution-goal.cc b/src/libstore/build/drv-output-substitution-goal.cc index 87a8b0c55..9ffa33d7b 100644 --- a/src/libstore/build/drv-output-substitution-goal.cc +++ b/src/libstore/build/drv-output-substitution-goal.cc @@ -19,36 +19,32 @@ DrvOutputSubstitutionGoal::DrvOutputSubstitutionGoal( : Goal(worker, isDependency) , id(id) { - state = &DrvOutputSubstitutionGoal::init; name = fmt("substitution of '%s'", id.to_string()); trace("created"); } -kj::Promise> DrvOutputSubstitutionGoal::init(bool inBuildSlot) noexcept +kj::Promise> DrvOutputSubstitutionGoal::work() noexcept try { trace("init"); /* If the derivation already exists, we’re done */ if (worker.store.queryRealisation(id)) { - return {Finished{ecSuccess, std::move(buildResult)}}; + co_return Finished{ecSuccess, std::move(buildResult)}; } subs = settings.useSubstitutes ? getDefaultSubstituters() : std::list>(); - return tryNext(inBuildSlot); + co_return co_await tryNext(); } catch (...) { - return {std::current_exception()}; + co_return result::failure(std::current_exception()); } -kj::Promise> DrvOutputSubstitutionGoal::tryNext(bool inBuildSlot) noexcept +kj::Promise> DrvOutputSubstitutionGoal::tryNext() noexcept try { trace("trying next substituter"); - if (!inBuildSlot) { - return worker.substitutions.acquire().then([this](auto token) { - slotToken = std::move(token); - return work(); - }); + if (!slotToken.valid()) { + slotToken = co_await worker.substitutions.acquire(); } maintainRunningSubstitutions = worker.runningSubstitutions.addTemporarily(1); @@ -65,7 +61,7 @@ try { /* Hack: don't indicate failure if there were no substituters. In that case the calling derivation should just do a build. */ - return {Finished{substituterFailed ? ecFailed : ecNoSubstituters, std::move(buildResult)}}; + co_return Finished{substituterFailed ? ecFailed : ecNoSubstituters, std::move(buildResult)}; } sub = subs.front(); @@ -85,13 +81,13 @@ try { return sub->queryRealisation(id); }); - state = &DrvOutputSubstitutionGoal::realisationFetched; - return pipe.promise.then([]() -> Result { return StillAlive{}; }); + co_await pipe.promise; + co_return co_await realisationFetched(); } catch (...) { - return {std::current_exception()}; + co_return result::failure(std::current_exception()); } -kj::Promise> DrvOutputSubstitutionGoal::realisationFetched(bool inBuildSlot) noexcept +kj::Promise> DrvOutputSubstitutionGoal::realisationFetched() noexcept try { maintainRunningSubstitutions.reset(); slotToken = {}; @@ -104,7 +100,7 @@ try { } if (!outputInfo) { - return tryNext(inBuildSlot); + co_return co_await tryNext(); } kj::Vector>> dependencies; @@ -121,7 +117,7 @@ try { worker.store.printStorePath(localOutputInfo->outPath), worker.store.printStorePath(depPath) ); - return tryNext(inBuildSlot); + co_return co_await tryNext(); } dependencies.add(worker.goalFactory().makeDrvOutputSubstitutionGoal(depId)); } @@ -129,17 +125,15 @@ try { dependencies.add(worker.goalFactory().makePathSubstitutionGoal(outputInfo->outPath)); - if (dependencies.empty()) { - return outPathValid(inBuildSlot); - } else { - state = &DrvOutputSubstitutionGoal::outPathValid; - return waitForGoals(dependencies.releaseAsArray()); + if (!dependencies.empty()) { + (co_await waitForGoals(dependencies.releaseAsArray())).value(); } + co_return co_await outPathValid(); } catch (...) { - return {std::current_exception()}; + co_return result::failure(std::current_exception()); } -kj::Promise> DrvOutputSubstitutionGoal::outPathValid(bool inBuildSlot) noexcept +kj::Promise> DrvOutputSubstitutionGoal::outPathValid() noexcept try { assert(outputInfo); trace("output path substituted"); @@ -166,10 +160,4 @@ try { return {std::current_exception()}; } -kj::Promise> DrvOutputSubstitutionGoal::work() noexcept -{ - return (this->*state)(slotToken.valid()); -} - - } diff --git a/src/libstore/build/drv-output-substitution-goal.hh b/src/libstore/build/drv-output-substitution-goal.hh index 543fa7ed4..86020705e 100644 --- a/src/libstore/build/drv-output-substitution-goal.hh +++ b/src/libstore/build/drv-output-substitution-goal.hh @@ -65,13 +65,9 @@ public: std::optional ca = std::nullopt ); - typedef kj::Promise> (DrvOutputSubstitutionGoal::*GoalState)(bool inBuildSlot) noexcept; - GoalState state; - - kj::Promise> init(bool inBuildSlot) noexcept; - kj::Promise> tryNext(bool inBuildSlot) noexcept; - kj::Promise> realisationFetched(bool inBuildSlot) noexcept; - kj::Promise> outPathValid(bool inBuildSlot) noexcept; + kj::Promise> tryNext() noexcept; + kj::Promise> realisationFetched() noexcept; + kj::Promise> outPathValid() noexcept; kj::Promise> finished() noexcept; kj::Promise> work() noexcept override;