2020-10-12 20:08:52 +03:00
|
|
|
#include "machines.hh"
|
|
|
|
#include "worker.hh"
|
|
|
|
#include "substitution-goal.hh"
|
2020-11-09 16:40:10 +02:00
|
|
|
#include "drv-output-substitution-goal.hh"
|
2021-02-26 17:20:33 +02:00
|
|
|
#include "local-derivation-goal.hh"
|
2020-10-12 20:08:52 +03:00
|
|
|
#include "hook-instance.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "signals.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2020-10-11 19:28:49 +03:00
|
|
|
#include <poll.h>
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2020-10-11 19:28:49 +03:00
|
|
|
namespace nix {
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2021-07-19 16:43:08 +03:00
|
|
|
Worker::Worker(Store & store, Store & evalStore)
|
2017-08-16 17:38:23 +03:00
|
|
|
: act(*logger, actRealise)
|
|
|
|
, actDerivations(*logger, actBuilds)
|
|
|
|
, actSubstitutions(*logger, actCopyPaths)
|
2017-08-14 21:14:55 +03:00
|
|
|
, store(store)
|
2021-07-19 16:43:08 +03:00
|
|
|
, evalStore(evalStore)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2012-07-27 16:59:18 +03:00
|
|
|
/* Debugging: prevent recursive workers. */
|
2009-04-01 00:14:07 +03:00
|
|
|
nrLocalBuilds = 0;
|
2023-04-30 11:56:24 +03:00
|
|
|
nrSubstitutions = 0;
|
2016-12-06 22:58:04 +02:00
|
|
|
lastWokenUp = steady_time_point::min();
|
2010-12-13 18:53:23 +02:00
|
|
|
permanentFailure = false;
|
2014-08-17 20:09:03 +03:00
|
|
|
timedOut = false;
|
2019-07-02 01:12:12 +03:00
|
|
|
hashMismatch = false;
|
|
|
|
checkMismatch = false;
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Worker::~Worker()
|
|
|
|
{
|
2004-06-25 18:36:09 +03:00
|
|
|
/* Explicitly get rid of all strong pointers now. After this all
|
|
|
|
goals that refer to this worker should be gone. (Otherwise we
|
|
|
|
are in trouble, since goals may call childTerminated() etc. in
|
|
|
|
their destructors). */
|
|
|
|
topGoals.clear();
|
2017-08-14 21:14:55 +03:00
|
|
|
|
2017-08-14 23:12:36 +03:00
|
|
|
assert(expectedSubstitutions == 0);
|
2017-08-14 21:14:55 +03:00
|
|
|
assert(expectedDownloadSize == 0);
|
|
|
|
assert(expectedNarSize == 0);
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-22 23:44:47 +03:00
|
|
|
std::shared_ptr<DerivationGoal> Worker::makeDerivationGoalCommon(
|
|
|
|
const StorePath & drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
const OutputsSpec & wantedOutputs,
|
2020-08-22 23:44:47 +03:00
|
|
|
std::function<std::shared_ptr<DerivationGoal>()> mkDrvGoal)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2020-10-11 20:07:14 +03:00
|
|
|
std::weak_ptr<DerivationGoal> & goal_weak = derivationGoals[drvPath];
|
|
|
|
std::shared_ptr<DerivationGoal> goal = goal_weak.lock();
|
|
|
|
if (!goal) {
|
2020-08-22 23:44:47 +03:00
|
|
|
goal = mkDrvGoal();
|
2020-10-11 20:07:14 +03:00
|
|
|
goal_weak = goal;
|
2012-10-02 21:08:59 +03:00
|
|
|
wakeUp(goal);
|
2020-08-22 23:44:47 +03:00
|
|
|
} else {
|
|
|
|
goal->addWantedOutputs(wantedOutputs);
|
|
|
|
}
|
2004-06-25 18:36:09 +03:00
|
|
|
return goal;
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2003-07-20 22:29:38 +03:00
|
|
|
|
2003-08-01 18:41:47 +03:00
|
|
|
|
2020-08-22 23:44:47 +03:00
|
|
|
std::shared_ptr<DerivationGoal> Worker::makeDerivationGoal(const StorePath & drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
const OutputsSpec & wantedOutputs, BuildMode buildMode)
|
2020-08-22 23:44:47 +03:00
|
|
|
{
|
2021-02-26 17:20:33 +02:00
|
|
|
return makeDerivationGoalCommon(drvPath, wantedOutputs, [&]() -> std::shared_ptr<DerivationGoal> {
|
|
|
|
return !dynamic_cast<LocalStore *>(&store)
|
|
|
|
? std::make_shared</* */DerivationGoal>(drvPath, wantedOutputs, *this, buildMode)
|
|
|
|
: std::make_shared<LocalDerivationGoal>(drvPath, wantedOutputs, *this, buildMode);
|
2020-08-22 23:44:47 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-16 23:20:18 +03:00
|
|
|
std::shared_ptr<DerivationGoal> Worker::makeBasicDerivationGoal(const StorePath & drvPath,
|
2023-01-12 01:57:18 +02:00
|
|
|
const BasicDerivation & drv, const OutputsSpec & wantedOutputs, BuildMode buildMode)
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
{
|
2021-02-26 17:20:33 +02:00
|
|
|
return makeDerivationGoalCommon(drvPath, wantedOutputs, [&]() -> std::shared_ptr<DerivationGoal> {
|
|
|
|
return !dynamic_cast<LocalStore *>(&store)
|
|
|
|
? std::make_shared</* */DerivationGoal>(drvPath, drv, wantedOutputs, *this, buildMode)
|
|
|
|
: std::make_shared<LocalDerivationGoal>(drvPath, drv, wantedOutputs, *this, buildMode);
|
2020-08-22 23:44:47 +03:00
|
|
|
});
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-09 14:47:06 +02:00
|
|
|
std::shared_ptr<PathSubstitutionGoal> Worker::makePathSubstitutionGoal(const StorePath & path, RepairFlag repair, std::optional<ContentAddress> ca)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2020-11-09 14:47:06 +02:00
|
|
|
std::weak_ptr<PathSubstitutionGoal> & goal_weak = substitutionGoals[path];
|
2020-10-11 20:07:14 +03:00
|
|
|
auto goal = goal_weak.lock(); // FIXME
|
2012-10-02 21:08:59 +03:00
|
|
|
if (!goal) {
|
2020-11-09 14:47:06 +02:00
|
|
|
goal = std::make_shared<PathSubstitutionGoal>(path, *this, repair, ca);
|
2020-08-22 23:44:47 +03:00
|
|
|
goal_weak = goal;
|
2012-10-02 21:08:59 +03:00
|
|
|
wakeUp(goal);
|
|
|
|
}
|
|
|
|
return goal;
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 03:11:09 +02:00
|
|
|
|
2020-11-09 16:40:10 +02:00
|
|
|
std::shared_ptr<DrvOutputSubstitutionGoal> Worker::makeDrvOutputSubstitutionGoal(const DrvOutput& id, RepairFlag repair, std::optional<ContentAddress> ca)
|
|
|
|
{
|
|
|
|
std::weak_ptr<DrvOutputSubstitutionGoal> & goal_weak = drvOutputSubstitutionGoals[id];
|
|
|
|
auto goal = goal_weak.lock(); // FIXME
|
|
|
|
if (!goal) {
|
|
|
|
goal = std::make_shared<DrvOutputSubstitutionGoal>(id, *this, repair, ca);
|
|
|
|
goal_weak = goal;
|
|
|
|
wakeUp(goal);
|
|
|
|
}
|
|
|
|
return goal;
|
|
|
|
}
|
|
|
|
|
2021-03-03 03:11:09 +02:00
|
|
|
|
|
|
|
GoalPtr Worker::makeGoal(const DerivedPath & req, BuildMode buildMode)
|
|
|
|
{
|
|
|
|
return std::visit(overloaded {
|
|
|
|
[&](const DerivedPath::Built & bfd) -> GoalPtr {
|
2023-10-02 06:29:45 +03:00
|
|
|
if (auto bop = std::get_if<DerivedPath::Opaque>(&*bfd.drvPath))
|
|
|
|
return makeDerivationGoal(bop->path, bfd.outputs, buildMode);
|
|
|
|
else
|
|
|
|
throw UnimplementedError("Building dynamic derivations in one shot is not yet implemented.");
|
2021-03-03 03:11:09 +02:00
|
|
|
},
|
|
|
|
[&](const DerivedPath::Opaque & bo) -> GoalPtr {
|
|
|
|
return makePathSubstitutionGoal(bo.path, buildMode == bmRepair ? Repair : NoRepair);
|
|
|
|
},
|
|
|
|
}, req.raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-09 16:40:10 +02:00
|
|
|
template<typename K, typename G>
|
|
|
|
static void removeGoal(std::shared_ptr<G> goal, std::map<K, std::weak_ptr<G>> & goalMap)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2005-02-18 11:50:20 +02:00
|
|
|
/* !!! inefficient */
|
2023-10-02 06:29:45 +03:00
|
|
|
for (auto i = goalMap.begin();
|
|
|
|
i != goalMap.end(); )
|
|
|
|
if (i->second.lock() == goal) {
|
|
|
|
auto j = i; ++j;
|
|
|
|
goalMap.erase(i);
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
else ++i;
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2004-05-11 21:05:44 +03:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
|
|
|
|
void Worker::removeGoal(GoalPtr goal)
|
|
|
|
{
|
2023-10-02 06:29:45 +03:00
|
|
|
if (auto drvGoal = std::dynamic_pointer_cast<DerivationGoal>(goal))
|
2020-10-11 20:07:14 +03:00
|
|
|
nix::removeGoal(drvGoal, derivationGoals);
|
2020-11-09 14:47:06 +02:00
|
|
|
else if (auto subGoal = std::dynamic_pointer_cast<PathSubstitutionGoal>(goal))
|
2020-10-11 20:07:14 +03:00
|
|
|
nix::removeGoal(subGoal, substitutionGoals);
|
2020-11-09 16:40:10 +02:00
|
|
|
else if (auto subGoal = std::dynamic_pointer_cast<DrvOutputSubstitutionGoal>(goal))
|
|
|
|
nix::removeGoal(subGoal, drvOutputSubstitutionGoals);
|
2020-10-11 20:07:14 +03:00
|
|
|
else
|
|
|
|
assert(false);
|
2021-04-07 13:21:31 +03:00
|
|
|
|
2005-02-23 13:19:27 +02:00
|
|
|
if (topGoals.find(goal) != topGoals.end()) {
|
|
|
|
topGoals.erase(goal);
|
|
|
|
/* If a top-level goal failed, then kill all other goals
|
|
|
|
(unless keepGoing was set). */
|
2020-06-15 20:25:35 +03:00
|
|
|
if (goal->exitCode == Goal::ecFailed && !settings.keepGoing)
|
2005-02-23 13:19:27 +02:00
|
|
|
topGoals.clear();
|
|
|
|
}
|
2007-08-28 14:36:17 +03:00
|
|
|
|
|
|
|
/* Wake up goals waiting for any goal to finish. */
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : waitingForAnyGoal) {
|
|
|
|
GoalPtr goal = i.lock();
|
2007-08-28 14:36:17 +03:00
|
|
|
if (goal) wakeUp(goal);
|
|
|
|
}
|
|
|
|
|
|
|
|
waitingForAnyGoal.clear();
|
2004-05-11 21:05:44 +03:00
|
|
|
}
|
2003-11-21 18:05:19 +02:00
|
|
|
|
2004-05-11 21:05:44 +03:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
void Worker::wakeUp(GoalPtr goal)
|
2004-05-11 21:05:44 +03:00
|
|
|
{
|
2004-06-25 18:36:09 +03:00
|
|
|
goal->trace("woken up");
|
2014-03-30 01:49:23 +02:00
|
|
|
addToWeakGoals(awake, goal);
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2004-05-11 21:05:44 +03:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2009-04-01 00:14:07 +03:00
|
|
|
unsigned Worker::getNrLocalBuilds()
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2009-04-01 00:14:07 +03:00
|
|
|
return nrLocalBuilds;
|
2003-07-20 22:29:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-30 11:56:24 +03:00
|
|
|
unsigned Worker::getNrSubstitutions()
|
|
|
|
{
|
|
|
|
return nrSubstitutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-21 17:28:23 +02:00
|
|
|
void Worker::childStarted(GoalPtr goal, const std::set<int> & fds,
|
2016-04-29 14:57:08 +03:00
|
|
|
bool inBuildSlot, bool respectTimeouts)
|
2003-07-20 22:29:38 +03:00
|
|
|
{
|
2004-06-20 00:45:04 +03:00
|
|
|
Child child;
|
|
|
|
child.goal = goal;
|
2016-08-30 16:45:39 +03:00
|
|
|
child.goal2 = goal.get();
|
2005-10-17 18:33:24 +03:00
|
|
|
child.fds = fds;
|
2016-12-06 22:58:04 +02:00
|
|
|
child.timeStarted = child.lastOutput = steady_time_point::clock::now();
|
2004-06-20 00:45:04 +03:00
|
|
|
child.inBuildSlot = inBuildSlot;
|
2013-04-23 19:04:59 +03:00
|
|
|
child.respectTimeouts = respectTimeouts;
|
2016-04-29 14:57:08 +03:00
|
|
|
children.emplace_back(child);
|
2023-04-30 11:56:24 +03:00
|
|
|
if (inBuildSlot) {
|
2023-11-20 14:18:25 +02:00
|
|
|
switch (goal->jobCategory()) {
|
|
|
|
case JobCategory::Substitution:
|
|
|
|
nrSubstitutions++;
|
|
|
|
break;
|
|
|
|
case JobCategory::Build:
|
|
|
|
nrLocalBuilds++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2023-04-30 11:56:24 +03:00
|
|
|
}
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2003-07-20 22:29:38 +03:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2016-08-30 16:45:39 +03:00
|
|
|
void Worker::childTerminated(Goal * goal, bool wakeSleepers)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2016-04-29 14:57:08 +03:00
|
|
|
auto i = std::find_if(children.begin(), children.end(),
|
2016-08-30 16:45:39 +03:00
|
|
|
[&](const Child & child) { return child.goal2 == goal; });
|
2016-09-08 18:29:50 +03:00
|
|
|
if (i == children.end()) return;
|
2004-06-20 00:45:04 +03:00
|
|
|
|
2016-04-29 14:57:08 +03:00
|
|
|
if (i->inBuildSlot) {
|
2023-11-20 14:18:25 +02:00
|
|
|
switch (goal->jobCategory()) {
|
|
|
|
case JobCategory::Substitution:
|
2023-04-30 11:56:24 +03:00
|
|
|
assert(nrSubstitutions > 0);
|
|
|
|
nrSubstitutions--;
|
2023-11-20 14:18:25 +02:00
|
|
|
break;
|
|
|
|
case JobCategory::Build:
|
2023-04-30 11:56:24 +03:00
|
|
|
assert(nrLocalBuilds > 0);
|
|
|
|
nrLocalBuilds--;
|
2023-11-20 14:18:25 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
2023-04-30 11:56:24 +03:00
|
|
|
}
|
2004-06-20 00:45:04 +03:00
|
|
|
}
|
|
|
|
|
2016-04-29 14:57:08 +03:00
|
|
|
children.erase(i);
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2004-07-01 19:24:35 +03:00
|
|
|
if (wakeSleepers) {
|
2012-07-27 16:59:18 +03:00
|
|
|
|
2004-07-01 19:24:35 +03:00
|
|
|
/* Wake up goals waiting for a build slot. */
|
2016-04-29 14:57:08 +03:00
|
|
|
for (auto & j : wantingToBuild) {
|
|
|
|
GoalPtr goal = j.lock();
|
2004-07-01 19:24:35 +03:00
|
|
|
if (goal) wakeUp(goal);
|
|
|
|
}
|
|
|
|
|
|
|
|
wantingToBuild.clear();
|
2004-06-25 18:36:09 +03:00
|
|
|
}
|
2003-10-16 19:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-08 19:26:21 +02:00
|
|
|
void Worker::waitForBuildSlot(GoalPtr goal)
|
2003-10-16 19:29:57 +03:00
|
|
|
{
|
2024-01-18 16:29:54 +02:00
|
|
|
goal->trace("wait for build slot");
|
2023-05-08 21:45:46 +03:00
|
|
|
bool isSubstitutionGoal = goal->jobCategory() == JobCategory::Substitution;
|
2023-04-30 11:56:24 +03:00
|
|
|
if ((!isSubstitutionGoal && getNrLocalBuilds() < settings.maxBuildJobs) ||
|
|
|
|
(isSubstitutionGoal && getNrSubstitutions() < settings.maxSubstitutionJobs))
|
2004-06-18 21:09:32 +03:00
|
|
|
wakeUp(goal); /* we can do it right away */
|
|
|
|
else
|
2014-03-30 01:49:23 +02:00
|
|
|
addToWeakGoals(wantingToBuild, goal);
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2004-02-13 12:45:09 +02:00
|
|
|
|
2003-10-16 19:29:57 +03:00
|
|
|
|
2007-08-28 14:36:17 +03:00
|
|
|
void Worker::waitForAnyGoal(GoalPtr goal)
|
|
|
|
{
|
|
|
|
debug("wait for any goal");
|
2014-03-30 01:49:23 +02:00
|
|
|
addToWeakGoals(waitingForAnyGoal, goal);
|
2007-08-28 14:36:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 03:05:54 +02:00
|
|
|
void Worker::waitForAWhile(GoalPtr goal)
|
|
|
|
{
|
|
|
|
debug("wait for a while");
|
2014-03-30 01:49:23 +02:00
|
|
|
addToWeakGoals(waitingForAWhile, goal);
|
2009-03-23 03:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-23 13:19:27 +02:00
|
|
|
void Worker::run(const Goals & _topGoals)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2021-04-05 16:48:18 +03:00
|
|
|
std::vector<nix::DerivedPath> topPaths;
|
2020-11-18 15:36:15 +02:00
|
|
|
|
|
|
|
for (auto & i : _topGoals) {
|
|
|
|
topGoals.insert(i);
|
2023-10-02 06:29:45 +03:00
|
|
|
if (auto goal = dynamic_cast<DerivationGoal *>(i.get())) {
|
2023-08-16 19:29:23 +03:00
|
|
|
topPaths.push_back(DerivedPath::Built {
|
2023-10-02 06:29:45 +03:00
|
|
|
.drvPath = makeConstantStorePathRef(goal->drvPath),
|
2023-08-16 19:29:23 +03:00
|
|
|
.outputs = goal->wantedOutputs,
|
|
|
|
});
|
2020-11-09 14:47:06 +02:00
|
|
|
} else if (auto goal = dynamic_cast<PathSubstitutionGoal *>(i.get())) {
|
2021-04-05 16:48:18 +03:00
|
|
|
topPaths.push_back(DerivedPath::Opaque{goal->storePath});
|
2020-11-18 15:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 14:34:04 +03:00
|
|
|
/* Call queryMissing() to efficiently query substitutes. */
|
2020-11-18 15:36:15 +02:00
|
|
|
StorePathSet willBuild, willSubstitute, unknown;
|
|
|
|
uint64_t downloadSize, narSize;
|
|
|
|
store.queryMissing(topPaths, willBuild, willSubstitute, unknown, downloadSize, narSize);
|
|
|
|
|
2017-05-16 17:09:57 +03:00
|
|
|
debug("entered goal loop");
|
2004-06-18 21:09:32 +03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
2004-01-15 22:23:55 +02:00
|
|
|
checkInterrupt();
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2020-12-20 19:36:52 +02:00
|
|
|
// TODO GC interface?
|
|
|
|
if (auto localStore = dynamic_cast<LocalStore *>(&store))
|
|
|
|
localStore->autoGC(false);
|
2017-09-05 21:43:42 +03:00
|
|
|
|
2014-11-24 17:48:04 +02:00
|
|
|
/* Call every wake goal (in the ordering established by
|
|
|
|
CompareGoalPtrs). */
|
2005-02-23 13:19:27 +02:00
|
|
|
while (!awake.empty() && !topGoals.empty()) {
|
2014-11-24 17:48:04 +02:00
|
|
|
Goals awake2;
|
|
|
|
for (auto & i : awake) {
|
|
|
|
GoalPtr goal = i.lock();
|
|
|
|
if (goal) awake2.insert(goal);
|
|
|
|
}
|
2004-06-18 21:09:32 +03:00
|
|
|
awake.clear();
|
2014-11-24 17:48:04 +02:00
|
|
|
for (auto & goal : awake2) {
|
2004-06-18 21:09:32 +03:00
|
|
|
checkInterrupt();
|
2014-11-24 17:48:04 +02:00
|
|
|
goal->work();
|
|
|
|
if (topGoals.empty()) break; // stuff may have been cancelled
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2003-10-16 19:29:57 +03:00
|
|
|
}
|
|
|
|
|
2004-06-25 18:36:09 +03:00
|
|
|
if (topGoals.empty()) break;
|
2003-10-16 19:29:57 +03:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
/* Wait for input. */
|
2009-03-23 03:05:54 +02:00
|
|
|
if (!children.empty() || !waitingForAWhile.empty())
|
2007-08-12 03:29:28 +03:00
|
|
|
waitForInput();
|
2009-03-29 21:06:00 +03:00
|
|
|
else {
|
2022-10-22 16:25:35 +03:00
|
|
|
if (awake.empty() && 0U == settings.maxBuildJobs)
|
2020-08-05 19:58:00 +03:00
|
|
|
{
|
|
|
|
if (getMachines().empty())
|
|
|
|
throw Error("unable to start any build; either increase '--max-jobs' "
|
|
|
|
"or enable remote builds."
|
2021-12-06 17:42:57 +02:00
|
|
|
"\nhttps://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html");
|
2020-08-05 19:58:00 +03:00
|
|
|
else
|
|
|
|
throw Error("unable to start any build; remote machines may not have "
|
|
|
|
"all required system features."
|
2021-12-06 17:42:57 +02:00
|
|
|
"\nhttps://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html");
|
2020-08-05 19:58:00 +03:00
|
|
|
|
|
|
|
}
|
2007-08-12 03:29:28 +03:00
|
|
|
assert(!awake.empty());
|
2009-03-29 21:06:00 +03:00
|
|
|
}
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2003-10-16 19:29:57 +03:00
|
|
|
|
2004-06-25 18:36:09 +03:00
|
|
|
/* If --keep-going is not set, it's possible that the main goal
|
|
|
|
exited while some of its subgoals were still active. But if
|
|
|
|
--keep-going *is* set, then they must all be finished now. */
|
2012-07-31 02:55:41 +03:00
|
|
|
assert(!settings.keepGoing || awake.empty());
|
|
|
|
assert(!settings.keepGoing || wantingToBuild.empty());
|
|
|
|
assert(!settings.keepGoing || children.empty());
|
2003-07-20 22:29:38 +03:00
|
|
|
}
|
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
void Worker::waitForInput()
|
2003-07-20 22:29:38 +03:00
|
|
|
{
|
2004-06-18 21:09:32 +03:00
|
|
|
printMsg(lvlVomit, "waiting for children");
|
2003-07-20 22:29:38 +03:00
|
|
|
|
2005-10-17 18:33:24 +03:00
|
|
|
/* Process output from the file descriptors attached to the
|
|
|
|
children, namely log output and output path creation commands.
|
|
|
|
We also use this to detect child termination: if we get EOF on
|
|
|
|
the logger pipe of a build, we assume that the builder has
|
|
|
|
terminated. */
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2009-03-23 03:05:54 +02:00
|
|
|
bool useTimeout = false;
|
2020-04-21 03:32:50 +03:00
|
|
|
long timeout = 0;
|
2016-12-06 22:58:04 +02:00
|
|
|
auto before = steady_time_point::clock::now();
|
2011-06-30 18:19:13 +03:00
|
|
|
|
2013-04-23 19:04:59 +03:00
|
|
|
/* If we're monitoring for silence on stdout/stderr, or if there
|
|
|
|
is a build timeout, then wait for input until the first
|
|
|
|
deadline for any child. */
|
2016-12-06 22:58:04 +02:00
|
|
|
auto nearest = steady_time_point::max(); // nearest deadline
|
2017-09-05 21:43:42 +03:00
|
|
|
if (settings.minFree.get() != 0)
|
|
|
|
// Periodicallty wake up to see if we need to run the garbage collector.
|
|
|
|
nearest = before + std::chrono::seconds(10);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : children) {
|
2016-04-29 14:57:08 +03:00
|
|
|
if (!i.respectTimeouts) continue;
|
2017-04-14 15:42:20 +03:00
|
|
|
if (0 != settings.maxSilentTime)
|
2016-12-06 22:58:04 +02:00
|
|
|
nearest = std::min(nearest, i.lastOutput + std::chrono::seconds(settings.maxSilentTime));
|
2017-04-14 15:42:20 +03:00
|
|
|
if (0 != settings.buildTimeout)
|
2016-12-06 22:58:04 +02:00
|
|
|
nearest = std::min(nearest, i.timeStarted + std::chrono::seconds(settings.buildTimeout));
|
2013-04-23 19:04:59 +03:00
|
|
|
}
|
2016-12-06 22:58:04 +02:00
|
|
|
if (nearest != steady_time_point::max()) {
|
2020-04-21 03:32:50 +03:00
|
|
|
timeout = std::max(1L, (long) std::chrono::duration_cast<std::chrono::seconds>(nearest - before).count());
|
2012-07-27 17:56:33 +03:00
|
|
|
useTimeout = true;
|
2006-12-08 17:44:00 +02:00
|
|
|
}
|
|
|
|
|
2009-03-23 03:05:54 +02:00
|
|
|
/* If we are polling goals that are waiting for a lock, then wake
|
|
|
|
up after a few seconds at most. */
|
|
|
|
if (!waitingForAWhile.empty()) {
|
|
|
|
useTimeout = true;
|
2016-12-06 22:58:04 +02:00
|
|
|
if (lastWokenUp == steady_time_point::min() || lastWokenUp > before) lastWokenUp = before;
|
2020-04-21 03:32:50 +03:00
|
|
|
timeout = std::max(1L,
|
2016-12-08 21:36:14 +02:00
|
|
|
(long) std::chrono::duration_cast<std::chrono::seconds>(
|
2016-12-06 22:58:04 +02:00
|
|
|
lastWokenUp + std::chrono::seconds(settings.pollInterval) - before).count());
|
|
|
|
} else lastWokenUp = steady_time_point::min();
|
|
|
|
|
|
|
|
if (useTimeout)
|
2020-04-21 03:32:50 +03:00
|
|
|
vomit("sleeping %d seconds", timeout);
|
2009-03-23 03:05:54 +02:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
/* Use select() to wait for the input side of any logger pipe to
|
|
|
|
become `available'. Note that `available' (i.e., non-blocking)
|
|
|
|
includes EOF. */
|
2020-04-21 03:32:50 +03:00
|
|
|
std::vector<struct pollfd> pollStatus;
|
2022-05-04 12:31:39 +03:00
|
|
|
std::map<int, size_t> fdToPollStatus;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : children) {
|
2016-04-29 14:57:08 +03:00
|
|
|
for (auto & j : i.fds) {
|
2020-04-21 03:32:50 +03:00
|
|
|
pollStatus.push_back((struct pollfd) { .fd = j, .events = POLLIN });
|
|
|
|
fdToPollStatus[j] = pollStatus.size() - 1;
|
2005-10-17 18:33:24 +03:00
|
|
|
}
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2003-07-20 22:29:38 +03:00
|
|
|
|
2020-04-21 03:32:50 +03:00
|
|
|
if (poll(pollStatus.data(), pollStatus.size(),
|
|
|
|
useTimeout ? timeout * 1000 : -1) == -1) {
|
2004-06-18 21:09:32 +03:00
|
|
|
if (errno == EINTR) return;
|
|
|
|
throw SysError("waiting for input");
|
|
|
|
}
|
2003-07-20 22:29:38 +03:00
|
|
|
|
2016-12-06 22:58:04 +02:00
|
|
|
auto after = steady_time_point::clock::now();
|
2006-12-08 17:44:00 +02:00
|
|
|
|
2017-01-19 17:58:39 +02:00
|
|
|
/* Process all available file descriptors. FIXME: this is
|
|
|
|
O(children * fds). */
|
2016-04-29 14:57:08 +03:00
|
|
|
decltype(children)::iterator i;
|
|
|
|
for (auto j = children.begin(); j != children.end(); j = i) {
|
|
|
|
i = std::next(j);
|
2006-12-08 20:41:48 +02:00
|
|
|
|
2004-06-18 21:09:32 +03:00
|
|
|
checkInterrupt();
|
2016-04-29 14:57:08 +03:00
|
|
|
|
|
|
|
GoalPtr goal = j->goal.lock();
|
2004-06-25 18:36:09 +03:00
|
|
|
assert(goal);
|
2006-12-08 20:41:48 +02:00
|
|
|
|
2022-02-21 17:28:23 +02:00
|
|
|
std::set<int> fds2(j->fds);
|
2018-03-02 02:58:41 +02:00
|
|
|
std::vector<unsigned char> buffer(4096);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & k : fds2) {
|
2022-05-04 08:44:32 +03:00
|
|
|
const auto fdPollStatusId = get(fdToPollStatus, k);
|
|
|
|
assert(fdPollStatusId);
|
|
|
|
assert(*fdPollStatusId < pollStatus.size());
|
|
|
|
if (pollStatus.at(*fdPollStatusId).revents) {
|
2020-07-25 00:02:51 +03:00
|
|
|
ssize_t rd = ::read(k, buffer.data(), buffer.size());
|
2019-05-17 23:29:15 +03:00
|
|
|
// FIXME: is there a cleaner way to handle pt close
|
|
|
|
// than EIO? Is this even standard?
|
|
|
|
if (rd == 0 || (rd == -1 && errno == EIO)) {
|
2020-05-12 00:52:15 +03:00
|
|
|
debug("%1%: got EOF", goal->getName());
|
2015-07-17 20:24:28 +03:00
|
|
|
goal->handleEOF(k);
|
2016-04-29 14:57:08 +03:00
|
|
|
j->fds.erase(k);
|
2019-05-17 23:29:15 +03:00
|
|
|
} else if (rd == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
throw SysError("%s: read failed", goal->getName());
|
2005-10-17 18:33:24 +03:00
|
|
|
} else {
|
2020-05-12 00:52:15 +03:00
|
|
|
printMsg(lvlVomit, "%1%: read %2% bytes",
|
|
|
|
goal->getName(), rd);
|
2024-01-13 21:27:20 +02:00
|
|
|
std::string_view data((char *) buffer.data(), rd);
|
2016-04-29 14:57:08 +03:00
|
|
|
j->lastOutput = after;
|
2015-07-17 20:24:28 +03:00
|
|
|
goal->handleChildOutput(k, data);
|
2005-10-17 18:33:24 +03:00
|
|
|
}
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
|
|
|
}
|
2006-12-08 17:44:00 +02:00
|
|
|
|
2020-06-15 20:25:35 +03:00
|
|
|
if (goal->exitCode == Goal::ecBusy &&
|
2017-04-14 15:42:20 +03:00
|
|
|
0 != settings.maxSilentTime &&
|
2016-04-29 14:57:08 +03:00
|
|
|
j->respectTimeouts &&
|
2016-12-06 22:58:04 +02:00
|
|
|
after - j->lastOutput >= std::chrono::seconds(settings.maxSilentTime))
|
2006-12-08 19:26:21 +02:00
|
|
|
{
|
2020-06-15 20:25:35 +03:00
|
|
|
goal->timedOut(Error(
|
2020-06-02 17:22:24 +03:00
|
|
|
"%1% timed out after %2% seconds of silence",
|
2020-06-15 20:25:35 +03:00
|
|
|
goal->getName(), settings.maxSilentTime));
|
2006-12-08 19:26:21 +02:00
|
|
|
}
|
2011-06-30 18:19:13 +03:00
|
|
|
|
2020-06-15 20:25:35 +03:00
|
|
|
else if (goal->exitCode == Goal::ecBusy &&
|
2017-04-14 15:42:20 +03:00
|
|
|
0 != settings.buildTimeout &&
|
2016-04-29 14:57:08 +03:00
|
|
|
j->respectTimeouts &&
|
2016-12-06 22:58:04 +02:00
|
|
|
after - j->timeStarted >= std::chrono::seconds(settings.buildTimeout))
|
2011-06-30 18:19:13 +03:00
|
|
|
{
|
2020-06-15 20:25:35 +03:00
|
|
|
goal->timedOut(Error(
|
2020-06-02 17:22:24 +03:00
|
|
|
"%1% timed out after %2% seconds",
|
2020-06-15 20:25:35 +03:00
|
|
|
goal->getName(), settings.buildTimeout));
|
2011-06-30 18:19:13 +03:00
|
|
|
}
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
2009-03-23 03:05:54 +02:00
|
|
|
|
2016-12-06 22:58:04 +02:00
|
|
|
if (!waitingForAWhile.empty() && lastWokenUp + std::chrono::seconds(settings.pollInterval) <= after) {
|
2009-03-23 03:05:54 +02:00
|
|
|
lastWokenUp = after;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : waitingForAWhile) {
|
|
|
|
GoalPtr goal = i.lock();
|
2009-03-23 03:05:54 +02:00
|
|
|
if (goal) wakeUp(goal);
|
|
|
|
}
|
|
|
|
waitingForAWhile.clear();
|
|
|
|
}
|
2003-07-20 22:29:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-22 21:23:25 +03:00
|
|
|
unsigned int Worker::failingExitStatus()
|
2010-12-13 18:53:23 +02:00
|
|
|
{
|
2023-06-22 21:23:25 +03:00
|
|
|
// See API docs in header for explanation
|
2019-05-12 00:14:19 +03:00
|
|
|
unsigned int mask = 0;
|
2019-06-15 16:28:32 +03:00
|
|
|
bool buildFailure = permanentFailure || timedOut || hashMismatch;
|
|
|
|
if (buildFailure)
|
|
|
|
mask |= 0x04; // 100
|
2019-05-12 00:14:19 +03:00
|
|
|
if (timedOut)
|
2019-06-15 16:28:32 +03:00
|
|
|
mask |= 0x01; // 101
|
2019-05-12 00:14:19 +03:00
|
|
|
if (hashMismatch)
|
2019-06-15 16:28:32 +03:00
|
|
|
mask |= 0x02; // 102
|
|
|
|
if (checkMismatch) {
|
|
|
|
mask |= 0x08; // 104
|
|
|
|
}
|
2019-05-12 00:14:19 +03:00
|
|
|
|
2019-06-15 16:28:32 +03:00
|
|
|
if (mask)
|
|
|
|
mask |= 0x60;
|
|
|
|
return mask ? mask : 1;
|
2010-12-13 18:53:23 +02:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:33:24 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
bool Worker::pathContentsGood(const StorePath & path)
|
2016-04-08 19:07:13 +03:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
auto i = pathContentsGoodCache.find(path);
|
2016-04-08 19:07:13 +03:00
|
|
|
if (i != pathContentsGoodCache.end()) return i->second;
|
2019-12-05 20:11:09 +02:00
|
|
|
printInfo("checking path '%s'...", store.printStorePath(path));
|
2016-04-19 19:50:15 +03:00
|
|
|
auto info = store.queryPathInfo(path);
|
2016-04-08 19:07:13 +03:00
|
|
|
bool res;
|
2019-12-05 20:11:09 +02:00
|
|
|
if (!pathExists(store.printStorePath(path)))
|
2016-04-08 19:07:13 +03:00
|
|
|
res = false;
|
|
|
|
else {
|
2023-11-04 22:25:41 +02:00
|
|
|
HashResult current = hashPath(
|
|
|
|
*store.getFSAccessor(), CanonPath { store.printStorePath(path) },
|
|
|
|
FileIngestionMethod::Recursive, info->narHash.algo);
|
2023-11-28 15:20:27 +02:00
|
|
|
Hash nullHash(HashAlgorithm::SHA256);
|
2016-04-19 19:50:15 +03:00
|
|
|
res = info->narHash == nullHash || info->narHash == current.first;
|
2016-04-08 19:07:13 +03:00
|
|
|
}
|
2020-06-16 23:20:18 +03:00
|
|
|
pathContentsGoodCache.insert_or_assign(path, res);
|
2020-05-14 00:56:39 +03:00
|
|
|
if (!res)
|
2021-01-21 01:27:36 +02:00
|
|
|
printError("path '%s' is corrupted or missing!", store.printStorePath(path));
|
2016-04-08 19:07:13 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-16 23:20:18 +03:00
|
|
|
void Worker::markContentsGood(const StorePath & path)
|
2016-04-08 19:07:13 +03:00
|
|
|
{
|
2020-06-16 23:20:18 +03:00
|
|
|
pathContentsGoodCache.insert_or_assign(path, true);
|
2016-04-08 19:07:13 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 23:47:22 +03:00
|
|
|
|
2021-03-08 23:24:49 +02:00
|
|
|
GoalPtr upcast_goal(std::shared_ptr<PathSubstitutionGoal> subGoal)
|
|
|
|
{
|
|
|
|
return subGoal;
|
|
|
|
}
|
|
|
|
|
|
|
|
GoalPtr upcast_goal(std::shared_ptr<DrvOutputSubstitutionGoal> subGoal)
|
|
|
|
{
|
2020-10-18 00:45:31 +03:00
|
|
|
return subGoal;
|
2020-10-12 23:47:22 +03:00
|
|
|
}
|
2021-03-08 23:24:49 +02:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|