Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jun 24, 2022
1 parent bde94e3 commit cac0bdf
Show file tree
Hide file tree
Showing 300 changed files with 21,211 additions and 19,472 deletions.
140 changes: 84 additions & 56 deletions src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
using namespace nix;
using std::cin;

static void handleAlarm(int sig) {
}
static void handleAlarm(int sig) {}

std::string escapeUri(std::string uri)
{
Expand All @@ -37,16 +36,20 @@ static std::string currentLoad;

static AutoCloseFD openSlotLock(const Machine & m, uint64_t slot)
{
return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
return openLockFile(
fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
}

static bool allSupportedLocally(Store & store, const std::set<std::string>& requiredFeatures) {
static bool allSupportedLocally(Store & store,
const std::set<std::string> & requiredFeatures)
{
for (auto & feature : requiredFeatures)
if (!store.systemFeatures.get().count(feature)) return false;
if (!store.systemFeatures.get().count(feature))
return false;
return true;
}

static int main_build_remote(int argc, char * * argv)
static int main_build_remote(int argc, char ** argv)
{
{
logger = makeJSONLogger(*logger);
Expand All @@ -72,7 +75,8 @@ static int main_build_remote(int argc, char * * argv)
settings.set(name, value);
}

settings.maxBuildJobs.set("1"); // hack to make tests with local?root= work
settings.maxBuildJobs.set(
"1"); // hack to make tests with local?root= work

initPlugins();

Expand All @@ -82,7 +86,7 @@ static int main_build_remote(int argc, char * * argv)
that gets cleared on reboot, but it wouldn't work on macOS. */
auto currentLoadName = "/current-load";
if (auto localStore = store.dynamic_pointer_cast<LocalFSStore>())
currentLoad = std::string { localStore->stateDir } + currentLoadName;
currentLoad = std::string{localStore->stateDir} + currentLoadName;
else
currentLoad = settings.nixStateDir + currentLoadName;

Expand All @@ -104,42 +108,46 @@ static int main_build_remote(int argc, char * * argv)

try {
auto s = readString(source);
if (s != "try") return 0;
} catch (EndOfFile &) { return 0; }
if (s != "try")
return 0;
} catch (EndOfFile &) {
return 0;
}

auto amWilling = readInt(source);
auto neededSystem = readString(source);
drvPath = store->parseStorePath(readString(source));
auto requiredFeatures = readStrings<std::set<std::string>>(source);

auto canBuildLocally = amWilling
&& ( neededSystem == settings.thisSystem
|| settings.extraPlatforms.get().count(neededSystem) > 0)
&& allSupportedLocally(*store, requiredFeatures);
auto canBuildLocally =
amWilling &&
(neededSystem == settings.thisSystem ||
settings.extraPlatforms.get().count(neededSystem) > 0) &&
allSupportedLocally(*store, requiredFeatures);

/* Error ignored here, will be caught later */
mkdir(currentLoad.c_str(), 0777);

while (true) {
bestSlotLock = -1;
AutoCloseFD lock = openLockFile(currentLoad + "/main-lock", true);
AutoCloseFD lock =
openLockFile(currentLoad + "/main-lock", true);
lockFile(lock.get(), ltWrite, true);

bool rightType = false;

Machine * bestMachine = nullptr;
uint64_t bestLoad = 0;
for (auto & m : machines) {
debug("considering building on remote machine '%s'", m.storeUri);
debug("considering building on remote machine '%s'",
m.storeUri);

if (m.enabled
&& (neededSystem == "builtin"
|| std::find(m.systemTypes.begin(),
m.systemTypes.end(),
neededSystem) != m.systemTypes.end()) &&
if (m.enabled &&
(neededSystem == "builtin" ||
std::find(m.systemTypes.begin(), m.systemTypes.end(),
neededSystem) != m.systemTypes.end()) &&
m.allSupported(requiredFeatures) &&
m.mandatoryMet(requiredFeatures))
{
m.mandatoryMet(requiredFeatures)) {
rightType = true;
AutoCloseFD free;
uint64_t load = 0;
Expand All @@ -159,12 +167,15 @@ static int main_build_remote(int argc, char * * argv)
bool best = false;
if (!bestSlotLock) {
best = true;
} else if (load / m.speedFactor < bestLoad / bestMachine->speedFactor) {
} else if (load / m.speedFactor <
bestLoad / bestMachine->speedFactor) {
best = true;
} else if (load / m.speedFactor == bestLoad / bestMachine->speedFactor) {
} else if (load / m.speedFactor ==
bestLoad / bestMachine->speedFactor) {
if (m.speedFactor > bestMachine->speedFactor) {
best = true;
} else if (m.speedFactor == bestMachine->speedFactor) {
} else if (m.speedFactor ==
bestMachine->speedFactor) {
if (load < bestLoad) {
best = true;
}
Expand All @@ -181,14 +192,15 @@ static int main_build_remote(int argc, char * * argv)
if (!bestSlotLock) {
if (rightType && !canBuildLocally)
std::cerr << "# postpone\n";
else
{
else {
// build the hint template.
std::string errorText =
"Failed to find a machine for remote build!\n"
"derivation: %s\nrequired (system, features): (%s, %s)";
"derivation: %s\nrequired (system, features): (%s, "
"%s)";
errorText += "\n%s available machines:";
errorText += "\n(systems, maxjobs, supportedFeatures, mandatoryFeatures)";
errorText += "\n(systems, maxjobs, supportedFeatures, "
"mandatoryFeatures)";

for (unsigned int i = 0; i < machines.size(); ++i)
errorText += "\n(%s, %s, %s, %s)";
Expand All @@ -201,18 +213,20 @@ static int main_build_remote(int argc, char * * argv)
drvstr = "<unknown>";

auto error = hintformat(errorText);
error
% drvstr
% neededSystem
% concatStringsSep<StringSet>(", ", requiredFeatures)
% machines.size();
error % drvstr % neededSystem %
concatStringsSep<StringSet>(", ",
requiredFeatures) %
machines.size();

for (auto & m : machines)
error
% concatStringsSep<std::vector<std::string>>(", ", m.systemTypes)
% m.maxJobs
% concatStringsSep<StringSet>(", ", m.supportedFeatures)
% concatStringsSep<StringSet>(", ", m.mandatoryFeatures);
error %
concatStringsSep<std::vector<std::string>>(
", ", m.systemTypes) %
m.maxJobs %
concatStringsSep<StringSet>(
", ", m.supportedFeatures) %
concatStringsSep<StringSet>(
", ", m.mandatoryFeatures);

printMsg(canBuildLocally ? lvlChatty : lvlWarn, error);

Expand All @@ -231,7 +245,9 @@ static int main_build_remote(int argc, char * * argv)

try {

Activity act(*logger, lvlTalkative, actUnknown, fmt("connecting to '%s'", bestMachine->storeUri));
Activity act(
*logger, lvlTalkative, actUnknown,
fmt("connecting to '%s'", bestMachine->storeUri));

sshStore = bestMachine->openStore();
sshStore->connect();
Expand All @@ -240,8 +256,8 @@ static int main_build_remote(int argc, char * * argv)
} catch (std::exception & e) {
auto msg = chomp(drainFD(5, false));
printError("cannot build on '%s': %s%s",
bestMachine->storeUri, e.what(),
msg.empty() ? "" : ": " + msg);
bestMachine->storeUri, e.what(),
msg.empty() ? "" : ": " + msg);
bestMachine->enabled = false;
continue;
}
Expand All @@ -250,32 +266,38 @@ static int main_build_remote(int argc, char * * argv)
}
}

connected:
connected:
close(5);

std::cerr << "# accept\n" << storeUri << "\n";

auto inputs = readStrings<PathSet>(source);
auto wantedOutputs = readStrings<StringSet>(source);

AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
AutoCloseFD uploadLock = openLockFile(
currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);

{
Activity act(*logger, lvlTalkative, actUnknown, fmt("waiting for the upload lock to '%s'", storeUri));
Activity act(*logger, lvlTalkative, actUnknown,
fmt("waiting for the upload lock to '%s'", storeUri));

auto old = signal(SIGALRM, handleAlarm);
alarm(15 * 60);
if (!lockFile(uploadLock.get(), ltWrite, true))
printError("somebody is hogging the upload lock for '%s', continuing...");
printError("somebody is hogging the upload lock for '%s', "
"continuing...");
alarm(0);
signal(SIGALRM, old);
}

auto substitute = settings.buildersUseSubstitutes ? Substitute : NoSubstitute;
auto substitute =
settings.buildersUseSubstitutes ? Substitute : NoSubstitute;

{
Activity act(*logger, lvlTalkative, actUnknown, fmt("copying dependencies to '%s'", storeUri));
copyPaths(*store, *sshStore, store->parseStorePathSet(inputs), NoRepair, NoCheckSigs, substitute);
Activity act(*logger, lvlTalkative, actUnknown,
fmt("copying dependencies to '%s'", storeUri));
copyPaths(*store, *sshStore, store->parseStorePathSet(inputs),
NoRepair, NoCheckSigs, substitute);
}

uploadLock = -1;
Expand All @@ -296,14 +318,17 @@ static int main_build_remote(int argc, char * * argv)
auto result = sshStore->buildDerivation(*drvPath, drv);

if (!result.success())
throw Error("build of '%s' on '%s' failed: %s", store->printStorePath(*drvPath), storeUri, result.errorMsg);
throw Error("build of '%s' on '%s' failed: %s",
store->printStorePath(*drvPath), storeUri,
result.errorMsg);

std::set<Realisation> missingRealisations;
StorePathSet missingPaths;
if (settings.isExperimentalFeatureEnabled(Xp::CaDerivations) && !drv.type().hasKnownOutputPaths()) {
if (settings.isExperimentalFeatureEnabled(Xp::CaDerivations) &&
!drv.type().hasKnownOutputPaths()) {
for (auto & outputName : wantedOutputs) {
auto thisOutputHash = outputHashes.at(outputName);
auto thisOutputId = DrvOutput{ thisOutputHash, outputName };
auto thisOutputId = DrvOutput{thisOutputHash, outputName};
if (!store->queryRealisation(thisOutputId)) {
debug("missing output %s", outputName);
assert(result.builtOutputs.count(thisOutputId));
Expand All @@ -322,11 +347,14 @@ static int main_build_remote(int argc, char * * argv)
}

if (!missingPaths.empty()) {
Activity act(*logger, lvlTalkative, actUnknown, fmt("copying outputs from '%s'", storeUri));
Activity act(*logger, lvlTalkative, actUnknown,
fmt("copying outputs from '%s'", storeUri));
if (auto localStore = store.dynamic_pointer_cast<LocalStore>())
for (auto & path : missingPaths)
localStore->locksHeld.insert(store->printStorePath(path)); /* FIXME: ugly */
copyPaths(*sshStore, *store, missingPaths, NoRepair, NoCheckSigs, NoSubstitute);
localStore->locksHeld.insert(
store->printStorePath(path)); /* FIXME: ugly */
copyPaths(*sshStore, *store, missingPaths, NoRepair, NoCheckSigs,
NoSubstitute);
}
// XXX: Should be done as part of `copyPaths`
for (auto & realisation : missingRealisations) {
Expand Down
Loading

0 comments on commit cac0bdf

Please sign in to comment.