2020-10-12 17:08:52 +00:00
|
|
|
#include "machines.hh"
|
|
|
|
#include "worker.hh"
|
|
|
|
#include "substitution-goal.hh"
|
2020-11-09 14:40:10 +00:00
|
|
|
#include "drv-output-substitution-goal.hh"
|
2021-02-26 15:20:33 +00:00
|
|
|
#include "local-derivation-goal.hh"
|
2020-10-12 17:08:52 +00:00
|
|
|
#include "hook-instance.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2020-10-11 16:28:49 +00:00
|
|
|
#include <poll.h>
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2020-10-11 16:28:49 +00:00
|
|
|
namespace nix {
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
Worker::Worker(Store & store, Store & evalStore)
|
2017-08-16 14:38:23 +00:00
|
|
|
: act(*logger, actRealise)
|
|
|
|
, actDerivations(*logger, actBuilds)
|
|
|
|
, actSubstitutions(*logger, actCopyPaths)
|
2017-08-14 18:14:55 +00:00
|
|
|
, store(store)
|
2021-07-19 13:43:08 +00:00
|
|
|
, evalStore(evalStore)
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2012-07-27 13:59:18 +00:00
|
|
|
/* Debugging: prevent recursive workers. */
|
2009-03-31 21:14:07 +00:00
|
|
|
nrLocalBuilds = 0;
|
2016-12-06 20:58:04 +00:00
|
|
|
lastWokenUp = steady_time_point::min();
|
2010-12-13 16:53:23 +00:00
|
|
|
permanentFailure = false;
|
2014-08-17 17:09:03 +00:00
|
|
|
timedOut = false;
|
2019-07-01 22:12:12 +00:00
|
|
|
hashMismatch = false;
|
|
|
|
checkMismatch = false;
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Worker::~Worker()
|
|
|
|
{
|
2004-06-25 15:36:09 +00: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 18:14:55 +00:00
|
|
|
|
2017-08-14 20:12:36 +00:00
|
|
|
assert(expectedSubstitutions == 0);
|
2017-08-14 18:14:55 +00:00
|
|
|
assert(expectedDownloadSize == 0);
|
|
|
|
assert(expectedNarSize == 0);
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-22 20:44:47 +00:00
|
|
|
std::shared_ptr<DerivationGoal> Worker::makeDerivationGoalCommon(
|
|
|
|
const StorePath & drvPath,
|
|
|
|
const StringSet & wantedOutputs,
|
|
|
|
std::function<std::shared_ptr<DerivationGoal>()> mkDrvGoal)
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2020-10-11 17:07:14 +00:00
|
|
|
std::weak_ptr<DerivationGoal> & goal_weak = derivationGoals[drvPath];
|
|
|
|
std::shared_ptr<DerivationGoal> goal = goal_weak.lock();
|
|
|
|
if (!goal) {
|
2020-08-22 20:44:47 +00:00
|
|
|
goal = mkDrvGoal();
|
2020-10-11 17:07:14 +00:00
|
|
|
goal_weak = goal;
|
2012-10-02 18:08:59 +00:00
|
|
|
wakeUp(goal);
|
2020-08-22 20:44:47 +00:00
|
|
|
} else {
|
|
|
|
goal->addWantedOutputs(wantedOutputs);
|
|
|
|
}
|
2004-06-25 15:36:09 +00:00
|
|
|
return goal;
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2003-08-01 15:41:47 +00:00
|
|
|
|
2020-08-22 20:44:47 +00:00
|
|
|
std::shared_ptr<DerivationGoal> Worker::makeDerivationGoal(const StorePath & drvPath,
|
|
|
|
const StringSet & wantedOutputs, BuildMode buildMode)
|
|
|
|
{
|
2021-02-26 15:20:33 +00: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 20:44:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-16 20:20:18 +00:00
|
|
|
std::shared_ptr<DerivationGoal> Worker::makeBasicDerivationGoal(const StorePath & drvPath,
|
2020-08-22 20:44:47 +00:00
|
|
|
const BasicDerivation & drv, const StringSet & 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 15:57:40 +00:00
|
|
|
{
|
2021-02-26 15:20:33 +00: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 20:44:47 +00: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 15:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-09 12:47:06 +00:00
|
|
|
std::shared_ptr<PathSubstitutionGoal> Worker::makePathSubstitutionGoal(const StorePath & path, RepairFlag repair, std::optional<ContentAddress> ca)
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2020-11-09 12:47:06 +00:00
|
|
|
std::weak_ptr<PathSubstitutionGoal> & goal_weak = substitutionGoals[path];
|
2020-10-11 17:07:14 +00:00
|
|
|
auto goal = goal_weak.lock(); // FIXME
|
2012-10-02 18:08:59 +00:00
|
|
|
if (!goal) {
|
2020-11-09 12:47:06 +00:00
|
|
|
goal = std::make_shared<PathSubstitutionGoal>(path, *this, repair, ca);
|
2020-08-22 20:44:47 +00:00
|
|
|
goal_weak = goal;
|
2012-10-02 18:08:59 +00:00
|
|
|
wakeUp(goal);
|
|
|
|
}
|
|
|
|
return goal;
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 14:40:10 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename G>
|
|
|
|
static void removeGoal(std::shared_ptr<G> goal, std::map<K, std::weak_ptr<G>> & goalMap)
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2005-02-18 09:50:20 +00:00
|
|
|
/* !!! inefficient */
|
2020-10-17 21:47:52 +00:00
|
|
|
for (auto i = goalMap.begin();
|
2005-02-18 09:50:20 +00:00
|
|
|
i != goalMap.end(); )
|
|
|
|
if (i->second.lock() == goal) {
|
2020-10-17 21:47:52 +00:00
|
|
|
auto j = i; ++j;
|
2005-02-18 09:50:20 +00:00
|
|
|
goalMap.erase(i);
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
else ++i;
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2004-05-11 18:05:44 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
void Worker::removeGoal(GoalPtr goal)
|
|
|
|
{
|
2020-10-11 17:07:14 +00:00
|
|
|
if (auto drvGoal = std::dynamic_pointer_cast<DerivationGoal>(goal))
|
|
|
|
nix::removeGoal(drvGoal, derivationGoals);
|
2020-11-09 12:47:06 +00:00
|
|
|
else if (auto subGoal = std::dynamic_pointer_cast<PathSubstitutionGoal>(goal))
|
2020-10-11 17:07:14 +00:00
|
|
|
nix::removeGoal(subGoal, substitutionGoals);
|
2020-11-09 14:40:10 +00:00
|
|
|
else if (auto subGoal = std::dynamic_pointer_cast<DrvOutputSubstitutionGoal>(goal))
|
|
|
|
nix::removeGoal(subGoal, drvOutputSubstitutionGoals);
|
2020-10-11 17:07:14 +00:00
|
|
|
else
|
|
|
|
assert(false);
|
2021-04-07 10:21:31 +00:00
|
|
|
|
2005-02-23 11:19:27 +00: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 17:25:35 +00:00
|
|
|
if (goal->exitCode == Goal::ecFailed && !settings.keepGoing)
|
2005-02-23 11:19:27 +00:00
|
|
|
topGoals.clear();
|
|
|
|
}
|
2007-08-28 11:36:17 +00:00
|
|
|
|
|
|
|
/* Wake up goals waiting for any goal to finish. */
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : waitingForAnyGoal) {
|
|
|
|
GoalPtr goal = i.lock();
|
2007-08-28 11:36:17 +00:00
|
|
|
if (goal) wakeUp(goal);
|
|
|
|
}
|
|
|
|
|
|
|
|
waitingForAnyGoal.clear();
|
2004-05-11 18:05:44 +00:00
|
|
|
}
|
2003-11-21 16:05:19 +00:00
|
|
|
|
2004-05-11 18:05:44 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
void Worker::wakeUp(GoalPtr goal)
|
2004-05-11 18:05:44 +00:00
|
|
|
{
|
2004-06-25 15:36:09 +00:00
|
|
|
goal->trace("woken up");
|
2014-03-29 23:49:23 +00:00
|
|
|
addToWeakGoals(awake, goal);
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2004-05-11 18:05:44 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2009-03-31 21:14:07 +00:00
|
|
|
unsigned Worker::getNrLocalBuilds()
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2009-03-31 21:14:07 +00:00
|
|
|
return nrLocalBuilds;
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-21 15:28:23 +00:00
|
|
|
void Worker::childStarted(GoalPtr goal, const std::set<int> & fds,
|
2016-04-29 11:57:08 +00:00
|
|
|
bool inBuildSlot, bool respectTimeouts)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2004-06-19 21:45:04 +00:00
|
|
|
Child child;
|
|
|
|
child.goal = goal;
|
2016-08-30 13:45:39 +00:00
|
|
|
child.goal2 = goal.get();
|
2005-10-17 15:33:24 +00:00
|
|
|
child.fds = fds;
|
2016-12-06 20:58:04 +00:00
|
|
|
child.timeStarted = child.lastOutput = steady_time_point::clock::now();
|
2004-06-19 21:45:04 +00:00
|
|
|
child.inBuildSlot = inBuildSlot;
|
2013-04-23 16:04:59 +00:00
|
|
|
child.respectTimeouts = respectTimeouts;
|
2016-04-29 11:57:08 +00:00
|
|
|
children.emplace_back(child);
|
2009-03-31 21:14:07 +00:00
|
|
|
if (inBuildSlot) nrLocalBuilds++;
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2016-08-30 13:45:39 +00:00
|
|
|
void Worker::childTerminated(Goal * goal, bool wakeSleepers)
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2016-04-29 11:57:08 +00:00
|
|
|
auto i = std::find_if(children.begin(), children.end(),
|
2016-08-30 13:45:39 +00:00
|
|
|
[&](const Child & child) { return child.goal2 == goal; });
|
2016-09-08 15:29:50 +00:00
|
|
|
if (i == children.end()) return;
|
2004-06-19 21:45:04 +00:00
|
|
|
|
2016-04-29 11:57:08 +00:00
|
|
|
if (i->inBuildSlot) {
|
2009-03-31 21:14:07 +00:00
|
|
|
assert(nrLocalBuilds > 0);
|
|
|
|
nrLocalBuilds--;
|
2004-06-19 21:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 11:57:08 +00:00
|
|
|
children.erase(i);
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2004-07-01 16:24:35 +00:00
|
|
|
if (wakeSleepers) {
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2004-07-01 16:24:35 +00:00
|
|
|
/* Wake up goals waiting for a build slot. */
|
2016-04-29 11:57:08 +00:00
|
|
|
for (auto & j : wantingToBuild) {
|
|
|
|
GoalPtr goal = j.lock();
|
2004-07-01 16:24:35 +00:00
|
|
|
if (goal) wakeUp(goal);
|
|
|
|
}
|
|
|
|
|
|
|
|
wantingToBuild.clear();
|
2004-06-25 15:36:09 +00:00
|
|
|
}
|
2003-10-16 16:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-08 17:26:21 +00:00
|
|
|
void Worker::waitForBuildSlot(GoalPtr goal)
|
2003-10-16 16:29:57 +00:00
|
|
|
{
|
2004-06-18 18:09:32 +00:00
|
|
|
debug("wait for build slot");
|
2012-07-30 23:55:41 +00:00
|
|
|
if (getNrLocalBuilds() < settings.maxBuildJobs)
|
2004-06-18 18:09:32 +00:00
|
|
|
wakeUp(goal); /* we can do it right away */
|
|
|
|
else
|
2014-03-29 23:49:23 +00:00
|
|
|
addToWeakGoals(wantingToBuild, goal);
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2004-02-13 10:45:09 +00:00
|
|
|
|
2003-10-16 16:29:57 +00:00
|
|
|
|
2007-08-28 11:36:17 +00:00
|
|
|
void Worker::waitForAnyGoal(GoalPtr goal)
|
|
|
|
{
|
|
|
|
debug("wait for any goal");
|
2014-03-29 23:49:23 +00:00
|
|
|
addToWeakGoals(waitingForAnyGoal, goal);
|
2007-08-28 11:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 01:05:54 +00:00
|
|
|
void Worker::waitForAWhile(GoalPtr goal)
|
|
|
|
{
|
|
|
|
debug("wait for a while");
|
2014-03-29 23:49:23 +00:00
|
|
|
addToWeakGoals(waitingForAWhile, goal);
|
2009-03-23 01:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-23 11:19:27 +00:00
|
|
|
void Worker::run(const Goals & _topGoals)
|
2004-06-18 18:09:32 +00:00
|
|
|
{
|
2021-04-05 13:48:18 +00:00
|
|
|
std::vector<nix::DerivedPath> topPaths;
|
2020-11-18 13:36:15 +00:00
|
|
|
|
|
|
|
for (auto & i : _topGoals) {
|
|
|
|
topGoals.insert(i);
|
|
|
|
if (auto goal = dynamic_cast<DerivationGoal *>(i.get())) {
|
2021-04-05 13:48:18 +00:00
|
|
|
topPaths.push_back(DerivedPath::Built{goal->drvPath, goal->wantedOutputs});
|
2020-11-09 12:47:06 +00:00
|
|
|
} else if (auto goal = dynamic_cast<PathSubstitutionGoal *>(i.get())) {
|
2021-04-05 13:48:18 +00:00
|
|
|
topPaths.push_back(DerivedPath::Opaque{goal->storePath});
|
2020-11-18 13:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:34:04 +00:00
|
|
|
/* Call queryMissing() to efficiently query substitutes. */
|
2020-11-18 13:36:15 +00:00
|
|
|
StorePathSet willBuild, willSubstitute, unknown;
|
|
|
|
uint64_t downloadSize, narSize;
|
|
|
|
store.queryMissing(topPaths, willBuild, willSubstitute, unknown, downloadSize, narSize);
|
|
|
|
|
2017-05-16 14:09:57 +00:00
|
|
|
debug("entered goal loop");
|
2004-06-18 18:09:32 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2020-12-20 17:36:52 +00:00
|
|
|
// TODO GC interface?
|
|
|
|
if (auto localStore = dynamic_cast<LocalStore *>(&store))
|
|
|
|
localStore->autoGC(false);
|
2017-09-05 18:43:42 +00:00
|
|
|
|
2014-11-24 15:48:04 +00:00
|
|
|
/* Call every wake goal (in the ordering established by
|
|
|
|
CompareGoalPtrs). */
|
2005-02-23 11:19:27 +00:00
|
|
|
while (!awake.empty() && !topGoals.empty()) {
|
2014-11-24 15:48:04 +00:00
|
|
|
Goals awake2;
|
|
|
|
for (auto & i : awake) {
|
|
|
|
GoalPtr goal = i.lock();
|
|
|
|
if (goal) awake2.insert(goal);
|
|
|
|
}
|
2004-06-18 18:09:32 +00:00
|
|
|
awake.clear();
|
2014-11-24 15:48:04 +00:00
|
|
|
for (auto & goal : awake2) {
|
2004-06-18 18:09:32 +00:00
|
|
|
checkInterrupt();
|
2014-11-24 15:48:04 +00:00
|
|
|
goal->work();
|
|
|
|
if (topGoals.empty()) break; // stuff may have been cancelled
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2003-10-16 16:29:57 +00:00
|
|
|
}
|
|
|
|
|
2004-06-25 15:36:09 +00:00
|
|
|
if (topGoals.empty()) break;
|
2003-10-16 16:29:57 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
/* Wait for input. */
|
2009-03-23 01:05:54 +00:00
|
|
|
if (!children.empty() || !waitingForAWhile.empty())
|
2007-08-12 00:29:28 +00:00
|
|
|
waitForInput();
|
2009-03-29 18:06:00 +00:00
|
|
|
else {
|
2020-05-13 21:56:39 +00:00
|
|
|
if (awake.empty() && 0 == settings.maxBuildJobs)
|
2020-08-05 16:58:00 +00:00
|
|
|
{
|
|
|
|
if (getMachines().empty())
|
|
|
|
throw Error("unable to start any build; either increase '--max-jobs' "
|
|
|
|
"or enable remote builds."
|
2021-12-06 15:42:57 +00:00
|
|
|
"\nhttps://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html");
|
2020-08-05 16:58:00 +00:00
|
|
|
else
|
|
|
|
throw Error("unable to start any build; remote machines may not have "
|
|
|
|
"all required system features."
|
2021-12-06 15:42:57 +00:00
|
|
|
"\nhttps://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html");
|
2020-08-05 16:58:00 +00:00
|
|
|
|
|
|
|
}
|
2007-08-12 00:29:28 +00:00
|
|
|
assert(!awake.empty());
|
2009-03-29 18:06:00 +00:00
|
|
|
}
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2003-10-16 16:29:57 +00:00
|
|
|
|
2004-06-25 15:36:09 +00: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-30 23:55:41 +00:00
|
|
|
assert(!settings.keepGoing || awake.empty());
|
|
|
|
assert(!settings.keepGoing || wantingToBuild.empty());
|
|
|
|
assert(!settings.keepGoing || children.empty());
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
void Worker::waitForInput()
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2004-06-18 18:09:32 +00:00
|
|
|
printMsg(lvlVomit, "waiting for children");
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2005-10-17 15:33:24 +00: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 18:09:32 +00:00
|
|
|
|
2009-03-23 01:05:54 +00:00
|
|
|
bool useTimeout = false;
|
2020-04-21 00:32:50 +00:00
|
|
|
long timeout = 0;
|
2016-12-06 20:58:04 +00:00
|
|
|
auto before = steady_time_point::clock::now();
|
2011-06-30 15:19:13 +00:00
|
|
|
|
2013-04-23 16:04:59 +00: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 20:58:04 +00:00
|
|
|
auto nearest = steady_time_point::max(); // nearest deadline
|
2017-09-05 18:43:42 +00: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 17:24:28 +00:00
|
|
|
for (auto & i : children) {
|
2016-04-29 11:57:08 +00:00
|
|
|
if (!i.respectTimeouts) continue;
|
2017-04-14 12:42:20 +00:00
|
|
|
if (0 != settings.maxSilentTime)
|
2016-12-06 20:58:04 +00:00
|
|
|
nearest = std::min(nearest, i.lastOutput + std::chrono::seconds(settings.maxSilentTime));
|
2017-04-14 12:42:20 +00:00
|
|
|
if (0 != settings.buildTimeout)
|
2016-12-06 20:58:04 +00:00
|
|
|
nearest = std::min(nearest, i.timeStarted + std::chrono::seconds(settings.buildTimeout));
|
2013-04-23 16:04:59 +00:00
|
|
|
}
|
2016-12-06 20:58:04 +00:00
|
|
|
if (nearest != steady_time_point::max()) {
|
2020-04-21 00:32:50 +00:00
|
|
|
timeout = std::max(1L, (long) std::chrono::duration_cast<std::chrono::seconds>(nearest - before).count());
|
2012-07-27 14:56:33 +00:00
|
|
|
useTimeout = true;
|
2006-12-08 15:44:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-23 01:05:54 +00: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 20:58:04 +00:00
|
|
|
if (lastWokenUp == steady_time_point::min() || lastWokenUp > before) lastWokenUp = before;
|
2020-04-21 00:32:50 +00:00
|
|
|
timeout = std::max(1L,
|
2016-12-08 19:36:14 +00:00
|
|
|
(long) std::chrono::duration_cast<std::chrono::seconds>(
|
2016-12-06 20:58:04 +00:00
|
|
|
lastWokenUp + std::chrono::seconds(settings.pollInterval) - before).count());
|
|
|
|
} else lastWokenUp = steady_time_point::min();
|
|
|
|
|
|
|
|
if (useTimeout)
|
2020-04-21 00:32:50 +00:00
|
|
|
vomit("sleeping %d seconds", timeout);
|
2009-03-23 01:05:54 +00:00
|
|
|
|
2004-06-18 18:09:32 +00: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 00:32:50 +00:00
|
|
|
std::vector<struct pollfd> pollStatus;
|
2022-05-04 09:31:39 +00:00
|
|
|
std::map<int, size_t> fdToPollStatus;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : children) {
|
2016-04-29 11:57:08 +00:00
|
|
|
for (auto & j : i.fds) {
|
2020-04-21 00:32:50 +00:00
|
|
|
pollStatus.push_back((struct pollfd) { .fd = j, .events = POLLIN });
|
|
|
|
fdToPollStatus[j] = pollStatus.size() - 1;
|
2005-10-17 15:33:24 +00:00
|
|
|
}
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2020-04-21 00:32:50 +00:00
|
|
|
if (poll(pollStatus.data(), pollStatus.size(),
|
|
|
|
useTimeout ? timeout * 1000 : -1) == -1) {
|
2004-06-18 18:09:32 +00:00
|
|
|
if (errno == EINTR) return;
|
|
|
|
throw SysError("waiting for input");
|
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
|
2016-12-06 20:58:04 +00:00
|
|
|
auto after = steady_time_point::clock::now();
|
2006-12-08 15:44:00 +00:00
|
|
|
|
2017-01-19 15:58:39 +00:00
|
|
|
/* Process all available file descriptors. FIXME: this is
|
|
|
|
O(children * fds). */
|
2016-04-29 11:57:08 +00:00
|
|
|
decltype(children)::iterator i;
|
|
|
|
for (auto j = children.begin(); j != children.end(); j = i) {
|
|
|
|
i = std::next(j);
|
2006-12-08 18:41:48 +00:00
|
|
|
|
2004-06-18 18:09:32 +00:00
|
|
|
checkInterrupt();
|
2016-04-29 11:57:08 +00:00
|
|
|
|
|
|
|
GoalPtr goal = j->goal.lock();
|
2004-06-25 15:36:09 +00:00
|
|
|
assert(goal);
|
2006-12-08 18:41:48 +00:00
|
|
|
|
2022-02-21 15:28:23 +00:00
|
|
|
std::set<int> fds2(j->fds);
|
2018-03-02 00:58:41 +00:00
|
|
|
std::vector<unsigned char> buffer(4096);
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & k : fds2) {
|
2022-05-04 05:44:32 +00:00
|
|
|
const auto fdPollStatusId = get(fdToPollStatus, k);
|
|
|
|
assert(fdPollStatusId);
|
|
|
|
assert(*fdPollStatusId < pollStatus.size());
|
|
|
|
if (pollStatus.at(*fdPollStatusId).revents) {
|
2020-07-24 21:02:51 +00:00
|
|
|
ssize_t rd = ::read(k, buffer.data(), buffer.size());
|
2019-05-17 20:29:15 +00: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-11 21:52:15 +00:00
|
|
|
debug("%1%: got EOF", goal->getName());
|
2015-07-17 17:24:28 +00:00
|
|
|
goal->handleEOF(k);
|
2016-04-29 11:57:08 +00:00
|
|
|
j->fds.erase(k);
|
2019-05-17 20:29:15 +00:00
|
|
|
} else if (rd == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
throw SysError("%s: read failed", goal->getName());
|
2005-10-17 15:33:24 +00:00
|
|
|
} else {
|
2020-05-11 21:52:15 +00:00
|
|
|
printMsg(lvlVomit, "%1%: read %2% bytes",
|
|
|
|
goal->getName(), rd);
|
2022-02-25 15:00:00 +00:00
|
|
|
std::string data((char *) buffer.data(), rd);
|
2016-04-29 11:57:08 +00:00
|
|
|
j->lastOutput = after;
|
2015-07-17 17:24:28 +00:00
|
|
|
goal->handleChildOutput(k, data);
|
2005-10-17 15:33:24 +00:00
|
|
|
}
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-08 15:44:00 +00:00
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
if (goal->exitCode == Goal::ecBusy &&
|
2017-04-14 12:42:20 +00:00
|
|
|
0 != settings.maxSilentTime &&
|
2016-04-29 11:57:08 +00:00
|
|
|
j->respectTimeouts &&
|
2016-12-06 20:58:04 +00:00
|
|
|
after - j->lastOutput >= std::chrono::seconds(settings.maxSilentTime))
|
2006-12-08 17:26:21 +00:00
|
|
|
{
|
2020-06-15 17:25:35 +00:00
|
|
|
goal->timedOut(Error(
|
2020-06-02 14:22:24 +00:00
|
|
|
"%1% timed out after %2% seconds of silence",
|
2020-06-15 17:25:35 +00:00
|
|
|
goal->getName(), settings.maxSilentTime));
|
2006-12-08 17:26:21 +00:00
|
|
|
}
|
2011-06-30 15:19:13 +00:00
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
else if (goal->exitCode == Goal::ecBusy &&
|
2017-04-14 12:42:20 +00:00
|
|
|
0 != settings.buildTimeout &&
|
2016-04-29 11:57:08 +00:00
|
|
|
j->respectTimeouts &&
|
2016-12-06 20:58:04 +00:00
|
|
|
after - j->timeStarted >= std::chrono::seconds(settings.buildTimeout))
|
2011-06-30 15:19:13 +00:00
|
|
|
{
|
2020-06-15 17:25:35 +00:00
|
|
|
goal->timedOut(Error(
|
2020-06-02 14:22:24 +00:00
|
|
|
"%1% timed out after %2% seconds",
|
2020-06-15 17:25:35 +00:00
|
|
|
goal->getName(), settings.buildTimeout));
|
2011-06-30 15:19:13 +00:00
|
|
|
}
|
2004-06-18 18:09:32 +00:00
|
|
|
}
|
2009-03-23 01:05:54 +00:00
|
|
|
|
2016-12-06 20:58:04 +00:00
|
|
|
if (!waitingForAWhile.empty() && lastWokenUp + std::chrono::seconds(settings.pollInterval) <= after) {
|
2009-03-23 01:05:54 +00:00
|
|
|
lastWokenUp = after;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : waitingForAWhile) {
|
|
|
|
GoalPtr goal = i.lock();
|
2009-03-23 01:05:54 +00:00
|
|
|
if (goal) wakeUp(goal);
|
|
|
|
}
|
|
|
|
waitingForAWhile.clear();
|
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-13 16:53:23 +00:00
|
|
|
unsigned int Worker::exitStatus()
|
|
|
|
{
|
2019-06-15 13:28:32 +00:00
|
|
|
/*
|
|
|
|
* 1100100
|
|
|
|
* ^^^^
|
|
|
|
* |||`- timeout
|
|
|
|
* ||`-- output hash mismatch
|
|
|
|
* |`--- build failure
|
|
|
|
* `---- not deterministic
|
|
|
|
*/
|
2019-05-11 21:14:19 +00:00
|
|
|
unsigned int mask = 0;
|
2019-06-15 13:28:32 +00:00
|
|
|
bool buildFailure = permanentFailure || timedOut || hashMismatch;
|
|
|
|
if (buildFailure)
|
|
|
|
mask |= 0x04; // 100
|
2019-05-11 21:14:19 +00:00
|
|
|
if (timedOut)
|
2019-06-15 13:28:32 +00:00
|
|
|
mask |= 0x01; // 101
|
2019-05-11 21:14:19 +00:00
|
|
|
if (hashMismatch)
|
2019-06-15 13:28:32 +00:00
|
|
|
mask |= 0x02; // 102
|
|
|
|
if (checkMismatch) {
|
|
|
|
mask |= 0x08; // 104
|
|
|
|
}
|
2019-05-11 21:14:19 +00:00
|
|
|
|
2019-06-15 13:28:32 +00:00
|
|
|
if (mask)
|
|
|
|
mask |= 0x60;
|
|
|
|
return mask ? mask : 1;
|
2010-12-13 16:53:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 15:33:24 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
bool Worker::pathContentsGood(const StorePath & path)
|
2016-04-08 16:07:13 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
auto i = pathContentsGoodCache.find(path);
|
2016-04-08 16:07:13 +00:00
|
|
|
if (i != pathContentsGoodCache.end()) return i->second;
|
2019-12-05 18:11:09 +00:00
|
|
|
printInfo("checking path '%s'...", store.printStorePath(path));
|
2016-04-19 16:50:15 +00:00
|
|
|
auto info = store.queryPathInfo(path);
|
2016-04-08 16:07:13 +00:00
|
|
|
bool res;
|
2019-12-05 18:11:09 +00:00
|
|
|
if (!pathExists(store.printStorePath(path)))
|
2016-04-08 16:07:13 +00:00
|
|
|
res = false;
|
|
|
|
else {
|
2020-08-05 18:42:48 +00:00
|
|
|
HashResult current = hashPath(info->narHash.type, store.printStorePath(path));
|
2016-04-08 16:07:13 +00:00
|
|
|
Hash nullHash(htSHA256);
|
2016-04-19 16:50:15 +00:00
|
|
|
res = info->narHash == nullHash || info->narHash == current.first;
|
2016-04-08 16:07:13 +00:00
|
|
|
}
|
2020-06-16 20:20:18 +00:00
|
|
|
pathContentsGoodCache.insert_or_assign(path, res);
|
2020-05-13 21:56:39 +00:00
|
|
|
if (!res)
|
2021-01-20 23:27:36 +00:00
|
|
|
printError("path '%s' is corrupted or missing!", store.printStorePath(path));
|
2016-04-08 16:07:13 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-16 20:20:18 +00:00
|
|
|
void Worker::markContentsGood(const StorePath & path)
|
2016-04-08 16:07:13 +00:00
|
|
|
{
|
2020-06-16 20:20:18 +00:00
|
|
|
pathContentsGoodCache.insert_or_assign(path, true);
|
2016-04-08 16:07:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:47:22 +00:00
|
|
|
|
2020-11-09 12:47:06 +00:00
|
|
|
GoalPtr upcast_goal(std::shared_ptr<PathSubstitutionGoal> subGoal) {
|
2020-10-17 21:45:31 +00:00
|
|
|
return subGoal;
|
2020-10-12 20:47:22 +00:00
|
|
|
}
|
2020-11-09 14:40:10 +00:00
|
|
|
GoalPtr upcast_goal(std::shared_ptr<DrvOutputSubstitutionGoal> subGoal) {
|
|
|
|
return subGoal;
|
|
|
|
}
|
2020-10-12 20:47:22 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|