2006-11-30 19:54:43 +00:00
|
|
|
#include "serialise.hh"
|
|
|
|
#include "util.hh"
|
2006-11-30 18:35:50 +00:00
|
|
|
#include "remote-store.hh"
|
2006-11-30 20:13:59 +00:00
|
|
|
#include "worker-protocol.hh"
|
2006-11-30 20:45:20 +00:00
|
|
|
#include "archive.hh"
|
2013-08-07 11:51:55 +00:00
|
|
|
#include "affinity.hh"
|
2006-12-04 13:09:16 +00:00
|
|
|
#include "globals.hh"
|
2015-09-03 10:56:59 +00:00
|
|
|
#include "derivations.hh"
|
2016-02-23 14:00:59 +00:00
|
|
|
#include "pool.hh"
|
2018-04-16 09:14:39 +00:00
|
|
|
#include "finally.hh"
|
2020-07-02 15:04:31 +00:00
|
|
|
#include "logging.hh"
|
2006-11-30 18:35:50 +00:00
|
|
|
|
2006-12-03 02:08:13 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2006-12-03 02:36:44 +00:00
|
|
|
#include <sys/socket.h>
|
2006-12-04 14:21:39 +00:00
|
|
|
#include <sys/un.h>
|
2014-12-09 11:16:27 +00:00
|
|
|
#include <errno.h>
|
2006-12-03 02:08:13 +00:00
|
|
|
#include <fcntl.h>
|
2006-11-30 19:54:43 +00:00
|
|
|
#include <unistd.h>
|
2016-02-23 15:40:16 +00:00
|
|
|
|
2010-06-24 17:51:04 +00:00
|
|
|
#include <cstring>
|
2006-11-30 19:54:43 +00:00
|
|
|
|
2006-11-30 18:35:50 +00:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
template<> StorePathSet readStorePaths(const Store & store, Source & from)
|
2006-12-05 01:31:45 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet paths;
|
|
|
|
for (auto & i : readStrings<Strings>(from))
|
|
|
|
paths.insert(store.parseStorePath(i));
|
|
|
|
return paths;
|
2006-12-05 01:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void writeStorePaths(const Store & store, Sink & out, const StorePathSet & paths)
|
2006-12-05 01:31:45 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
out << paths.size();
|
|
|
|
for (auto & i : paths)
|
|
|
|
out << store.printStorePath(i);
|
2006-12-05 01:31:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:08:11 +00:00
|
|
|
StorePathCAMap readStorePathCAMap(const Store & store, Source & from)
|
2020-06-19 22:06:19 +00:00
|
|
|
{
|
|
|
|
StorePathCAMap paths;
|
|
|
|
auto count = readNum<size_t>(from);
|
2020-06-22 17:08:11 +00:00
|
|
|
while (count--)
|
|
|
|
paths.insert_or_assign(store.parseStorePath(readString(from)), parseContentAddressOpt(readString(from)));
|
2020-06-19 22:06:19 +00:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeStorePathCAMap(const Store & store, Sink & out, const StorePathCAMap & paths)
|
|
|
|
{
|
|
|
|
out << paths.size();
|
|
|
|
for (auto & i : paths) {
|
|
|
|
out << store.printStorePath(i.first);
|
2020-06-22 17:08:11 +00:00
|
|
|
out << renderContentAddress(i.second);
|
2020-06-19 22:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 09:20:52 +00:00
|
|
|
std::map<string, StorePath> readOutputPathMap(const Store & store, Source & from)
|
|
|
|
{
|
|
|
|
std::map<string, StorePath> pathMap;
|
|
|
|
auto rawInput = readStrings<Strings>(from);
|
2020-07-01 13:31:34 +00:00
|
|
|
if (rawInput.size() % 2)
|
|
|
|
throw Error("got an odd number of elements from the daemon when trying to read a output path map");
|
2020-06-10 09:20:52 +00:00
|
|
|
auto curInput = rawInput.begin();
|
|
|
|
while (curInput != rawInput.end()) {
|
2020-07-01 13:31:34 +00:00
|
|
|
auto thisKey = *curInput++;
|
|
|
|
auto thisValue = *curInput++;
|
2020-06-10 09:20:52 +00:00
|
|
|
pathMap.emplace(thisKey, store.parseStorePath(thisValue));
|
|
|
|
}
|
|
|
|
return pathMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeOutputPathMap(const Store & store, Sink & out, const std::map<string, StorePath> & pathMap)
|
|
|
|
{
|
|
|
|
out << 2*pathMap.size();
|
|
|
|
for (auto & i : pathMap) {
|
|
|
|
out << i.first;
|
|
|
|
out << store.printStorePath(i.second);
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 22:31:25 +00:00
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
/* TODO: Separate these store impls into different files, give them better names */
|
2017-03-03 18:05:50 +00:00
|
|
|
RemoteStore::RemoteStore(const Params & params)
|
2016-09-02 18:15:04 +00:00
|
|
|
: Store(params)
|
2016-06-01 12:49:12 +00:00
|
|
|
, connections(make_ref<Pool<Connection>>(
|
2017-04-13 13:55:38 +00:00
|
|
|
std::max(1, (int) maxConnections),
|
2017-03-03 18:35:34 +00:00
|
|
|
[this]() { return openConnectionWrapper(); },
|
2017-09-14 16:10:38 +00:00
|
|
|
[this](const ref<Connection> & r) {
|
|
|
|
return
|
|
|
|
r->to.good()
|
|
|
|
&& r->from.good()
|
|
|
|
&& std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
std::chrono::steady_clock::now() - r->startTime).count() < maxConnectionAge;
|
|
|
|
}
|
2016-02-24 10:39:56 +00:00
|
|
|
))
|
2006-12-04 13:28:14 +00:00
|
|
|
{
|
2008-12-11 14:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 18:35:34 +00:00
|
|
|
ref<RemoteStore::Connection> RemoteStore::openConnectionWrapper()
|
|
|
|
{
|
|
|
|
if (failed)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error("opening a connection to remote store '%s' previously failed", getUri());
|
2017-03-03 18:35:34 +00:00
|
|
|
try {
|
|
|
|
return openConnection();
|
|
|
|
} catch (...) {
|
|
|
|
failed = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 18:05:50 +00:00
|
|
|
UDSRemoteStore::UDSRemoteStore(const Params & params)
|
2016-09-02 18:15:04 +00:00
|
|
|
: Store(params)
|
|
|
|
, LocalFSStore(params)
|
2017-03-03 18:05:50 +00:00
|
|
|
, RemoteStore(params)
|
2016-09-02 18:15:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-15 02:20:22 +00:00
|
|
|
UDSRemoteStore::UDSRemoteStore(std::string socket_path, const Params & params)
|
|
|
|
: Store(params)
|
|
|
|
, LocalFSStore(params)
|
|
|
|
, RemoteStore(params)
|
|
|
|
, path(socket_path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
std::string UDSRemoteStore::getUri()
|
2016-04-29 14:26:16 +00:00
|
|
|
{
|
2018-01-15 02:20:22 +00:00
|
|
|
if (path) {
|
|
|
|
return std::string("unix://") + *path;
|
|
|
|
} else {
|
|
|
|
return "daemon";
|
|
|
|
}
|
2016-04-29 14:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
ref<RemoteStore::Connection> UDSRemoteStore::openConnection()
|
2008-12-11 14:30:25 +00:00
|
|
|
{
|
2016-02-23 14:00:59 +00:00
|
|
|
auto conn = make_ref<Connection>();
|
2008-12-11 14:30:25 +00:00
|
|
|
|
2016-01-31 09:19:14 +00:00
|
|
|
/* Connect to a daemon that does the privileged work for us. */
|
2016-06-09 14:15:58 +00:00
|
|
|
conn->fd = socket(PF_UNIX, SOCK_STREAM
|
|
|
|
#ifdef SOCK_CLOEXEC
|
|
|
|
| SOCK_CLOEXEC
|
|
|
|
#endif
|
|
|
|
, 0);
|
2016-07-11 19:44:44 +00:00
|
|
|
if (!conn->fd)
|
2006-12-04 14:21:39 +00:00
|
|
|
throw SysError("cannot create Unix domain socket");
|
2016-07-11 19:44:44 +00:00
|
|
|
closeOnExec(conn->fd.get());
|
2006-12-04 14:21:39 +00:00
|
|
|
|
2018-01-15 02:20:22 +00:00
|
|
|
string socketPath = path ? *path : settings.nixDaemonSocketFile;
|
2006-12-04 14:21:39 +00:00
|
|
|
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
addr.sun_family = AF_UNIX;
|
2016-03-23 16:16:16 +00:00
|
|
|
if (socketPath.size() + 1 >= sizeof(addr.sun_path))
|
2020-04-21 23:07:07 +00:00
|
|
|
throw Error("socket path '%1%' is too long", socketPath);
|
2016-03-23 16:16:16 +00:00
|
|
|
strcpy(addr.sun_path, socketPath.c_str());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2017-05-02 12:18:46 +00:00
|
|
|
if (::connect(conn->fd.get(), (struct sockaddr *) &addr, sizeof(addr)) == -1)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("cannot connect to daemon at '%1%'", socketPath);
|
2008-04-09 05:57:01 +00:00
|
|
|
|
2016-07-11 19:44:44 +00:00
|
|
|
conn->from.fd = conn->fd.get();
|
|
|
|
conn->to.fd = conn->fd.get();
|
2016-02-23 14:00:59 +00:00
|
|
|
|
2017-09-14 16:10:38 +00:00
|
|
|
conn->startTime = std::chrono::steady_clock::now();
|
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
initConnection(*conn);
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteStore::initConnection(Connection & conn)
|
|
|
|
{
|
2016-02-23 14:00:59 +00:00
|
|
|
/* Send the magic greeting, check for the reply. */
|
|
|
|
try {
|
2016-09-02 18:15:04 +00:00
|
|
|
conn.to << WORKER_MAGIC_1;
|
|
|
|
conn.to.flush();
|
|
|
|
unsigned int magic = readInt(conn.from);
|
2016-02-23 14:00:59 +00:00
|
|
|
if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch");
|
|
|
|
|
2017-03-01 12:52:54 +00:00
|
|
|
conn.from >> conn.daemonVersion;
|
2016-09-02 18:15:04 +00:00
|
|
|
if (GET_PROTOCOL_MAJOR(conn.daemonVersion) != GET_PROTOCOL_MAJOR(PROTOCOL_VERSION))
|
2016-02-23 14:00:59 +00:00
|
|
|
throw Error("Nix daemon protocol version not supported");
|
2016-09-02 18:15:04 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn.daemonVersion) < 10)
|
2016-07-27 13:03:20 +00:00
|
|
|
throw Error("the Nix daemon version is too old");
|
2016-09-02 18:15:04 +00:00
|
|
|
conn.to << PROTOCOL_VERSION;
|
2016-02-23 14:00:59 +00:00
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 14) {
|
2019-10-11 16:48:59 +00:00
|
|
|
int cpu = sameMachine() && settings.lockCPU ? lockToCurrentCPU() : -1;
|
2016-02-23 14:00:59 +00:00
|
|
|
if (cpu != -1)
|
2016-09-02 18:15:04 +00:00
|
|
|
conn.to << 1 << cpu;
|
2016-02-23 14:00:59 +00:00
|
|
|
else
|
2016-09-02 18:15:04 +00:00
|
|
|
conn.to << 0;
|
2016-02-23 14:00:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 11)
|
|
|
|
conn.to << false;
|
2016-02-23 14:00:59 +00:00
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
auto ex = conn.processStderr();
|
|
|
|
if (ex) std::rethrow_exception(ex);
|
2016-02-23 14:00:59 +00:00
|
|
|
}
|
|
|
|
catch (Error & e) {
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error("cannot open connection to remote store '%s': %s", getUri(), e.what());
|
2016-02-23 14:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setOptions(conn);
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
void RemoteStore::setOptions(Connection & conn)
|
2007-09-18 09:11:20 +00:00
|
|
|
{
|
2016-09-02 18:15:04 +00:00
|
|
|
conn.to << wopSetOptions
|
2015-07-19 23:16:16 +00:00
|
|
|
<< settings.keepFailed
|
|
|
|
<< settings.keepGoing
|
|
|
|
<< settings.tryFallback
|
|
|
|
<< verbosity
|
|
|
|
<< settings.maxBuildJobs
|
2016-07-27 13:03:20 +00:00
|
|
|
<< settings.maxSilentTime
|
2017-10-24 09:00:16 +00:00
|
|
|
<< true
|
2016-07-27 13:03:20 +00:00
|
|
|
<< (settings.verboseBuild ? lvlError : lvlVomit)
|
|
|
|
<< 0 // obsolete log type
|
|
|
|
<< 0 /* obsolete print build trace */
|
|
|
|
<< settings.buildCores
|
|
|
|
<< settings.useSubstitutes;
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2016-09-02 18:15:04 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 12) {
|
2018-03-27 16:41:31 +00:00
|
|
|
std::map<std::string, Config::SettingInfo> overrides;
|
|
|
|
globalConfig.getSettings(overrides, true);
|
2019-08-28 14:29:44 +00:00
|
|
|
overrides.erase(settings.keepFailed.name);
|
|
|
|
overrides.erase(settings.keepGoing.name);
|
|
|
|
overrides.erase(settings.tryFallback.name);
|
|
|
|
overrides.erase(settings.maxBuildJobs.name);
|
|
|
|
overrides.erase(settings.maxSilentTime.name);
|
|
|
|
overrides.erase(settings.buildCores.name);
|
|
|
|
overrides.erase(settings.useSubstitutes.name);
|
2020-07-02 15:04:31 +00:00
|
|
|
overrides.erase(loggerSettings.showTrace.name);
|
2016-09-02 18:15:04 +00:00
|
|
|
conn.to << overrides.size();
|
2015-07-19 23:16:16 +00:00
|
|
|
for (auto & i : overrides)
|
2018-03-27 16:41:31 +00:00
|
|
|
conn.to << i.first << i.second.value;
|
2012-07-31 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
auto ex = conn.processStderr();
|
|
|
|
if (ex) std::rethrow_exception(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* A wrapper around Pool<RemoteStore::Connection>::Handle that marks
|
|
|
|
the connection as bad (causing it to be closed) if a non-daemon
|
|
|
|
exception is thrown before the handle is closed. Such an exception
|
|
|
|
causes a deviation from the expected protocol and therefore a
|
|
|
|
desynchronization between the client and daemon. */
|
|
|
|
struct ConnectionHandle
|
|
|
|
{
|
|
|
|
Pool<RemoteStore::Connection>::Handle handle;
|
|
|
|
bool daemonException = false;
|
|
|
|
|
|
|
|
ConnectionHandle(Pool<RemoteStore::Connection>::Handle && handle)
|
|
|
|
: handle(std::move(handle))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ConnectionHandle(ConnectionHandle && h)
|
|
|
|
: handle(std::move(h.handle))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~ConnectionHandle()
|
|
|
|
{
|
2020-06-17 02:15:47 +00:00
|
|
|
if (!daemonException && std::uncaught_exceptions()) {
|
2018-10-16 21:36:15 +00:00
|
|
|
handle.markBad();
|
|
|
|
debug("closing daemon connection because of an exception");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteStore::Connection * operator -> () { return &*handle; }
|
|
|
|
|
|
|
|
void processStderr(Sink * sink = 0, Source * source = 0)
|
|
|
|
{
|
|
|
|
auto ex = handle->processStderr(sink, source);
|
|
|
|
if (ex) {
|
|
|
|
daemonException = true;
|
|
|
|
std::rethrow_exception(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionHandle RemoteStore::getConnection()
|
|
|
|
{
|
|
|
|
return ConnectionHandle(connections->get());
|
2007-09-18 09:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
bool RemoteStore::isValidPathUncached(const StorePath & path)
|
2006-11-30 18:35:50 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopIsValidPath << printStorePath(path);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2017-03-01 12:52:54 +00:00
|
|
|
return readInt(conn->from);
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet RemoteStore::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute)
|
2012-07-11 15:08:47 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 12) {
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet res;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : paths)
|
2020-06-16 20:20:18 +00:00
|
|
|
if (isValidPath(i)) res.insert(i);
|
2012-07-11 15:08:47 +00:00
|
|
|
return res;
|
|
|
|
} else {
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQueryValidPaths;
|
|
|
|
writeStorePaths(*this, conn->to, paths);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
return readStorePaths<StorePathSet>(*this, conn->from);
|
2012-07-11 15:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet RemoteStore::queryAllValidPaths()
|
2008-01-29 18:17:36 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopQueryAllValidPaths;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
return readStorePaths<StorePathSet>(*this, conn->from);
|
2008-01-29 18:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet RemoteStore::querySubstitutablePaths(const StorePathSet & paths)
|
2006-11-30 22:43:55 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 12) {
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet res;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : paths) {
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopHasSubstitutes << printStorePath(i);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2020-06-16 20:20:18 +00:00
|
|
|
if (readInt(conn->from)) res.insert(i);
|
2012-07-11 21:52:18 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
} else {
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQuerySubstitutablePaths;
|
|
|
|
writeStorePaths(*this, conn->to, paths);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
return readStorePaths<StorePathSet>(*this, conn->from);
|
2012-07-11 21:52:18 +00:00
|
|
|
}
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-17 19:03:05 +00:00
|
|
|
void RemoteStore::querySubstitutablePathInfos(const StorePathCAMap & pathsMap, SubstitutablePathInfos & infos)
|
download-from-binary-cache: parallelise fetching of NAR info files
Getting substitute information using the binary cache substituter has
non-trivial latency overhead. A package or NixOS system configuration
can have hundreds of dependencies, and in the worst case (when the
local info cache is empty) we have to do a separate HTTP request for
each of these. If the ping time to the server is t, getting N info
files will take tN seconds; e.g., with a ping time of 0.1s to
nixos.org, sequentially downloading 1000 info files (a typical NixOS
config) will take at least 100 seconds.
To fix this problem, the binary cache substituter can now perform
requests in parallel. This required changing the substituter
interface to support a function querySubstitutablePathInfos() that
queries multiple paths at the same time, and rewriting queryMissing()
to take advantage of parallelism. (Due to local caching,
parallelising queryMissing() is sufficient for most use cases, since
it's almost always called before building a derivation and thus fills
the local info cache.)
For example, parallelism speeds up querying all 1056 paths in a
particular NixOS system configuration from 116s to 2.6s. It works so
well because the eccentricity of the top-level derivation in the
dependency graph is only 9. So we only need 10 round-trips (when
using an unlimited number of parallel connections) to get everything.
Currently we do a maximum of 150 parallel connections to the server.
Thus it's important that the binary cache server (e.g. nixos.org) has
a high connection limit. Alternatively we could use HTTP pipelining,
but WWW::Curl doesn't support it and libcurl has a hard-coded limit of
5 requests per pipeline.
2012-07-06 23:08:20 +00:00
|
|
|
{
|
2020-06-17 19:03:05 +00:00
|
|
|
if (pathsMap.empty()) return;
|
2012-07-11 14:43:24 +00:00
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2016-02-23 14:00:59 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 12) {
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2020-06-17 19:03:05 +00:00
|
|
|
for (auto & i : pathsMap) {
|
2012-07-11 14:43:24 +00:00
|
|
|
SubstitutablePathInfo info;
|
2020-06-17 19:03:05 +00:00
|
|
|
conn->to << wopQuerySubstitutablePathInfo << printStorePath(i.first);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
unsigned int reply = readInt(conn->from);
|
2012-07-11 14:43:24 +00:00
|
|
|
if (reply == 0) continue;
|
2019-12-05 18:11:09 +00:00
|
|
|
auto deriver = readString(conn->from);
|
|
|
|
if (deriver != "")
|
|
|
|
info.deriver = parseStorePath(deriver);
|
|
|
|
info.references = readStorePaths<StorePathSet>(*this, conn->from);
|
2016-02-23 14:00:59 +00:00
|
|
|
info.downloadSize = readLongLong(conn->from);
|
2016-07-27 13:03:20 +00:00
|
|
|
info.narSize = readLongLong(conn->from);
|
2020-06-17 19:03:05 +00:00
|
|
|
infos.insert_or_assign(i.first, std::move(info));
|
2012-07-11 14:43:24 +00:00
|
|
|
}
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2012-07-11 14:43:24 +00:00
|
|
|
} else {
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQuerySubstitutablePathInfos;
|
2020-06-19 22:06:19 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 22) {
|
|
|
|
StorePathSet paths;
|
|
|
|
for (auto & path : pathsMap)
|
|
|
|
paths.insert(path.first);
|
|
|
|
writeStorePaths(*this, conn->to, paths);
|
|
|
|
} else
|
|
|
|
writeStorePathCAMap(*this, conn->to, pathsMap);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2017-03-01 12:52:54 +00:00
|
|
|
size_t count = readNum<size_t>(conn->from);
|
|
|
|
for (size_t n = 0; n < count; n++) {
|
2019-12-05 18:11:09 +00:00
|
|
|
SubstitutablePathInfo & info(infos[parseStorePath(readString(conn->from))]);
|
|
|
|
auto deriver = readString(conn->from);
|
|
|
|
if (deriver != "")
|
|
|
|
info.deriver = parseStorePath(deriver);
|
|
|
|
info.references = readStorePaths<StorePathSet>(*this, conn->from);
|
2016-02-23 14:00:59 +00:00
|
|
|
info.downloadSize = readLongLong(conn->from);
|
|
|
|
info.narSize = readLongLong(conn->from);
|
2012-07-11 14:43:24 +00:00
|
|
|
}
|
2012-07-30 21:13:25 +00:00
|
|
|
|
download-from-binary-cache: parallelise fetching of NAR info files
Getting substitute information using the binary cache substituter has
non-trivial latency overhead. A package or NixOS system configuration
can have hundreds of dependencies, and in the worst case (when the
local info cache is empty) we have to do a separate HTTP request for
each of these. If the ping time to the server is t, getting N info
files will take tN seconds; e.g., with a ping time of 0.1s to
nixos.org, sequentially downloading 1000 info files (a typical NixOS
config) will take at least 100 seconds.
To fix this problem, the binary cache substituter can now perform
requests in parallel. This required changing the substituter
interface to support a function querySubstitutablePathInfos() that
queries multiple paths at the same time, and rewriting queryMissing()
to take advantage of parallelism. (Due to local caching,
parallelising queryMissing() is sufficient for most use cases, since
it's almost always called before building a derivation and thus fills
the local info cache.)
For example, parallelism speeds up querying all 1056 paths in a
particular NixOS system configuration from 116s to 2.6s. It works so
well because the eccentricity of the top-level derivation in the
dependency graph is only 9. So we only need 10 round-trips (when
using an unlimited number of parallel connections) to get everything.
Currently we do a maximum of 150 parallel connections to the server.
Thus it's important that the binary cache server (e.g. nixos.org) has
a high connection limit. Alternatively we could use HTTP pipelining,
but WWW::Curl doesn't support it and libcurl has a hard-coded limit of
5 requests per pipeline.
2012-07-06 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::queryPathInfoUncached(const StorePath & path,
|
2018-09-25 16:54:16 +00:00
|
|
|
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept
|
2010-11-16 17:11:46 +00:00
|
|
|
{
|
2018-03-27 20:16:01 +00:00
|
|
|
try {
|
2018-04-09 19:26:16 +00:00
|
|
|
std::shared_ptr<ValidPathInfo> info;
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQueryPathInfo << printStorePath(path);
|
2018-04-09 19:26:16 +00:00
|
|
|
try {
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2018-04-09 19:26:16 +00:00
|
|
|
} catch (Error & e) {
|
|
|
|
// Ugly backwards compatibility hack.
|
|
|
|
if (e.msg().find("is not valid") != std::string::npos)
|
2020-04-30 00:57:05 +00:00
|
|
|
throw InvalidPath(e.info());
|
2018-04-09 19:26:16 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 17) {
|
|
|
|
bool valid; conn->from >> valid;
|
2019-12-05 18:11:09 +00:00
|
|
|
if (!valid) throw InvalidPath("path '%s' is not valid", printStorePath(path));
|
2018-04-09 19:26:16 +00:00
|
|
|
}
|
2020-06-16 20:20:18 +00:00
|
|
|
info = std::make_shared<ValidPathInfo>(StorePath(path));
|
2019-12-05 18:11:09 +00:00
|
|
|
auto deriver = readString(conn->from);
|
|
|
|
if (deriver != "") info->deriver = parseStorePath(deriver);
|
2020-07-01 22:34:18 +00:00
|
|
|
info->narHash = Hash::parseAny(readString(conn->from), htSHA256);
|
2019-12-05 18:11:09 +00:00
|
|
|
info->references = readStorePaths<StorePathSet>(*this, conn->from);
|
2018-04-09 19:26:16 +00:00
|
|
|
conn->from >> info->registrationTime >> info->narSize;
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 16) {
|
|
|
|
conn->from >> info->ultimate;
|
|
|
|
info->sigs = readStrings<StringSet>(conn->from);
|
2020-06-02 16:47:18 +00:00
|
|
|
info->ca = parseContentAddressOpt(readString(conn->from));
|
2018-04-09 19:26:16 +00:00
|
|
|
}
|
2016-09-16 16:54:14 +00:00
|
|
|
}
|
2018-03-27 20:16:01 +00:00
|
|
|
callback(std::move(info));
|
|
|
|
} catch (...) { callback.rethrow(); }
|
2010-11-16 17:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::queryReferrers(const StorePath & path,
|
|
|
|
StorePathSet & referrers)
|
2006-11-30 18:35:50 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQueryReferrers << printStorePath(path);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
for (auto & i : readStorePaths<StorePathSet>(*this, conn->from))
|
2020-06-16 20:20:18 +00:00
|
|
|
referrers.insert(i);
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet RemoteStore::queryValidDerivers(const StorePath & path)
|
2012-12-20 17:41:44 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQueryValidDerivers << printStorePath(path);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
return readStorePaths<StorePathSet>(*this, conn->from);
|
2012-12-20 17:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet RemoteStore::queryDerivationOutputs(const StorePath & path)
|
2010-02-22 12:44:36 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2020-06-10 09:20:52 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 0x16) {
|
|
|
|
return Store::queryDerivationOutputs(path);
|
|
|
|
}
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQueryDerivationOutputs << printStorePath(path);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
return readStorePaths<StorePathSet>(*this, conn->from);
|
2010-02-22 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-10 09:20:52 +00:00
|
|
|
OutputPathMap RemoteStore::queryDerivationOutputMap(const StorePath & path)
|
|
|
|
{
|
|
|
|
auto conn(getConnection());
|
|
|
|
conn->to << wopQueryDerivationOutputMap << printStorePath(path);
|
|
|
|
conn.processStderr();
|
|
|
|
return readOutputPathMap(*this, conn->from);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::optional<StorePath> RemoteStore::queryPathFromHashPart(const std::string & hashPart)
|
2012-07-17 22:55:39 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopQueryPathFromHashPart << hashPart;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
Path path = readString(conn->from);
|
2019-12-05 18:11:09 +00:00
|
|
|
if (path.empty()) return {};
|
|
|
|
return parseStorePath(path);
|
2012-07-17 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-21 22:42:21 +00:00
|
|
|
void RemoteStore::addToStore(const ValidPathInfo & info, Source & source,
|
2020-07-13 15:37:44 +00:00
|
|
|
RepairFlag repair, CheckSigsFlag checkSigs)
|
2016-05-04 11:36:54 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-11-09 17:45:06 +00:00
|
|
|
|
2016-11-09 17:57:22 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 18) {
|
|
|
|
conn->to << wopImportPaths;
|
|
|
|
|
2018-03-21 22:46:03 +00:00
|
|
|
auto source2 = sinkToSource([&](Sink & sink) {
|
|
|
|
sink << 1 // == path follows
|
|
|
|
;
|
|
|
|
copyNAR(source, sink);
|
|
|
|
sink
|
|
|
|
<< exportMagic
|
2019-12-05 18:11:09 +00:00
|
|
|
<< printStorePath(info.path);
|
|
|
|
writeStorePaths(*this, sink, info.references);
|
|
|
|
sink
|
|
|
|
<< (info.deriver ? printStorePath(*info.deriver) : "")
|
2018-03-21 22:46:03 +00:00
|
|
|
<< 0 // == no legacy signature
|
|
|
|
<< 0 // == no path follows
|
|
|
|
;
|
|
|
|
});
|
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr(0, source2.get());
|
2016-11-09 17:57:22 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto importedPaths = readStorePaths<StorePathSet>(*this, conn->from);
|
2016-11-09 17:57:22 +00:00
|
|
|
assert(importedPaths.size() <= 1);
|
|
|
|
}
|
2016-11-09 17:45:06 +00:00
|
|
|
|
2016-11-09 17:57:22 +00:00
|
|
|
else {
|
|
|
|
conn->to << wopAddToStoreNar
|
2019-12-05 18:11:09 +00:00
|
|
|
<< printStorePath(info.path)
|
|
|
|
<< (info.deriver ? printStorePath(*info.deriver) : "")
|
2020-08-05 18:42:48 +00:00
|
|
|
<< info.narHash.to_string(Base16, false);
|
2019-12-05 18:11:09 +00:00
|
|
|
writeStorePaths(*this, conn->to, info.references);
|
|
|
|
conn->to << info.registrationTime << info.narSize
|
2020-06-02 00:37:43 +00:00
|
|
|
<< info.ultimate << info.sigs << renderContentAddress(info.ca)
|
2017-06-28 16:11:01 +00:00
|
|
|
<< repair << !checkSigs;
|
2020-07-28 22:48:39 +00:00
|
|
|
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 23) {
|
|
|
|
|
|
|
|
std::exception_ptr ex;
|
|
|
|
|
|
|
|
struct FramedSink : BufferedSink
|
|
|
|
{
|
|
|
|
ConnectionHandle & conn;
|
|
|
|
std::exception_ptr & ex;
|
|
|
|
|
|
|
|
FramedSink(ConnectionHandle & conn, std::exception_ptr & ex) : conn(conn), ex(ex)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~FramedSink()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
conn->to << 0;
|
|
|
|
conn->to.flush();
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(const unsigned char * data, size_t len) override
|
|
|
|
{
|
|
|
|
/* Don't send more data if the remote has
|
|
|
|
encountered an error. */
|
|
|
|
if (ex) {
|
|
|
|
auto ex2 = ex;
|
|
|
|
ex = nullptr;
|
|
|
|
std::rethrow_exception(ex2);
|
|
|
|
}
|
|
|
|
conn->to << len;
|
|
|
|
conn->to(data, len);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Handle log messages / exceptions from the remote on a
|
|
|
|
separate thread. */
|
|
|
|
std::thread stderrThread([&]()
|
|
|
|
{
|
|
|
|
try {
|
2020-07-29 16:04:35 +00:00
|
|
|
conn.processStderr();
|
2020-07-28 22:48:39 +00:00
|
|
|
} catch (...) {
|
|
|
|
ex = std::current_exception();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Finally joinStderrThread([&]()
|
|
|
|
{
|
|
|
|
if (stderrThread.joinable()) {
|
|
|
|
stderrThread.join();
|
|
|
|
if (ex) {
|
|
|
|
try {
|
|
|
|
std::rethrow_exception(ex);
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
FramedSink sink(conn, ex);
|
|
|
|
copyNAR(source, sink);
|
|
|
|
sink.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
stderrThread.join();
|
|
|
|
if (ex)
|
|
|
|
std::rethrow_exception(ex);
|
|
|
|
|
|
|
|
} else if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 21) {
|
|
|
|
conn.processStderr(0, &source);
|
|
|
|
} else {
|
|
|
|
copyNAR(source, conn->to);
|
|
|
|
conn.processStderr(0, nullptr);
|
|
|
|
}
|
2016-11-09 17:57:22 +00:00
|
|
|
}
|
2016-05-04 11:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePath RemoteStore::addToStore(const string & name, const Path & _srcPath,
|
2020-03-29 05:04:55 +00:00
|
|
|
FileIngestionMethod method, HashType hashAlgo, PathFilter & filter, RepairFlag repair)
|
2006-11-30 18:35:50 +00:00
|
|
|
{
|
2012-10-03 19:09:18 +00:00
|
|
|
if (repair) throw Error("repairing is not supported when building through the Nix daemon");
|
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2006-12-01 20:51:18 +00:00
|
|
|
Path srcPath(absPath(_srcPath));
|
2011-12-01 13:48:48 +00:00
|
|
|
|
2020-03-29 05:04:55 +00:00
|
|
|
conn->to
|
|
|
|
<< wopAddToStore
|
|
|
|
<< name
|
|
|
|
<< ((hashAlgo == htSHA256 && method == FileIngestionMethod::Recursive) ? 0 : 1) /* backwards compatibility hack */
|
|
|
|
<< (method == FileIngestionMethod::Recursive ? 1 : 0)
|
|
|
|
<< printHashType(hashAlgo);
|
2014-06-10 11:45:50 +00:00
|
|
|
|
|
|
|
try {
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to.written = 0;
|
|
|
|
conn->to.warn = true;
|
2017-04-26 16:38:16 +00:00
|
|
|
connections->incCapacity();
|
2018-04-16 09:14:39 +00:00
|
|
|
{
|
|
|
|
Finally cleanup([&]() { connections->decCapacity(); });
|
|
|
|
dumpPath(srcPath, conn->to, filter);
|
|
|
|
}
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to.warn = false;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2014-06-10 11:45:50 +00:00
|
|
|
} catch (SysError & e) {
|
|
|
|
/* Daemon closed while we were sending the path. Probably OOM
|
|
|
|
or I/O error. */
|
|
|
|
if (e.errNo == EPIPE)
|
|
|
|
try {
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2014-06-10 11:45:50 +00:00
|
|
|
} catch (EndOfFile & e) { }
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
return parseStorePath(readString(conn->from));
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePath RemoteStore::addTextToStore(const string & name, const string & s,
|
|
|
|
const StorePathSet & references, RepairFlag repair)
|
2006-11-30 18:35:50 +00:00
|
|
|
{
|
2012-10-03 19:09:18 +00:00
|
|
|
if (repair) throw Error("repairing is not supported when building through the Nix daemon");
|
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopAddTextToStore << name << s;
|
|
|
|
writeStorePaths(*this, conn->to, references);
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
return parseStorePath(readString(conn->from));
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::buildPaths(const std::vector<StorePathWithOutputs> & drvPaths, BuildMode buildMode)
|
2006-11-30 18:35:50 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopBuildPaths;
|
2019-12-05 18:11:09 +00:00
|
|
|
assert(GET_PROTOCOL_MINOR(conn->daemonVersion) >= 13);
|
|
|
|
Strings ss;
|
|
|
|
for (auto & p : drvPaths)
|
|
|
|
ss.push_back(p.to_string(*this));
|
|
|
|
conn->to << ss;
|
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 15)
|
|
|
|
conn->to << buildMode;
|
|
|
|
else
|
|
|
|
/* Old daemons did not take a 'buildMode' parameter, so we
|
|
|
|
need to validate it here on the client side. */
|
|
|
|
if (buildMode != bmNormal)
|
|
|
|
throw Error("repairing or checking is not supported when building through the Nix daemon");
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
readInt(conn->from);
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
BuildResult RemoteStore::buildDerivation(const StorePath & drvPath, const BasicDerivation & drv,
|
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
|
|
|
BuildMode buildMode)
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopBuildDerivation << printStorePath(drvPath);
|
|
|
|
writeDerivation(conn->to, *this, drv);
|
|
|
|
conn->to << buildMode;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2015-09-03 10:56:59 +00:00
|
|
|
BuildResult res;
|
|
|
|
unsigned int status;
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->from >> status >> res.errorMsg;
|
2015-09-03 10:56:59 +00:00
|
|
|
res.status = (BuildResult::Status) status;
|
|
|
|
return res;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::ensurePath(const StorePath & path)
|
2006-11-30 18:35:50 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopEnsurePath << printStorePath(path);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
readInt(conn->from);
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::addTempRoot(const StorePath & path)
|
2006-12-02 16:41:36 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopAddTempRoot << printStorePath(path);
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
readInt(conn->from);
|
2006-12-02 16:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-04 23:29:16 +00:00
|
|
|
void RemoteStore::addIndirectRoot(const Path & path)
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopAddIndirectRoot << path;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
readInt(conn->from);
|
2006-12-04 23:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-02 16:41:36 +00:00
|
|
|
void RemoteStore::syncWithGC()
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopSyncWithGC;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
readInt(conn->from);
|
2006-12-02 16:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
Roots RemoteStore::findRoots(bool censor)
|
2006-12-05 01:31:45 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopFindRoots;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2017-03-01 12:52:54 +00:00
|
|
|
size_t count = readNum<size_t>(conn->from);
|
2006-12-05 01:31:45 +00:00
|
|
|
Roots result;
|
|
|
|
while (count--) {
|
2016-02-23 14:00:59 +00:00
|
|
|
Path link = readString(conn->from);
|
2019-12-05 18:11:09 +00:00
|
|
|
auto target = parseStorePath(readString(conn->from));
|
|
|
|
result[std::move(target)].emplace(link);
|
2006-12-05 01:31:45 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 09:34:17 +00:00
|
|
|
void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
|
2006-12-05 02:18:46 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2016-07-27 13:03:20 +00:00
|
|
|
conn->to
|
2019-12-05 18:11:09 +00:00
|
|
|
<< wopCollectGarbage << options.action;
|
|
|
|
writeStorePaths(*this, conn->to, options.pathsToDelete);
|
|
|
|
conn->to << options.ignoreLiveness
|
2016-07-27 13:03:20 +00:00
|
|
|
<< options.maxFreed
|
2009-11-20 17:12:38 +00:00
|
|
|
/* removed options */
|
2016-07-27 13:03:20 +00:00
|
|
|
<< 0 << 0 << 0;
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2016-02-23 14:00:59 +00:00
|
|
|
results.paths = readStrings<PathSet>(conn->from);
|
|
|
|
results.bytesFreed = readLongLong(conn->from);
|
|
|
|
readLongLong(conn->from); // obsolete
|
2016-04-19 16:50:15 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto state_(Store::state.lock());
|
|
|
|
state_->pathInfoCache.clear();
|
|
|
|
}
|
2006-12-05 02:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-01 20:21:42 +00:00
|
|
|
void RemoteStore::optimiseStore()
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopOptimiseStore;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-02-23 14:00:59 +00:00
|
|
|
readInt(conn->from);
|
2014-09-01 20:21:42 +00:00
|
|
|
}
|
2010-05-04 10:45:10 +00:00
|
|
|
|
2016-04-08 16:16:53 +00:00
|
|
|
|
2017-06-28 16:11:01 +00:00
|
|
|
bool RemoteStore::verifyStore(bool checkContents, RepairFlag repair)
|
2015-06-01 21:20:11 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2016-02-23 14:00:59 +00:00
|
|
|
conn->to << wopVerifyStore << checkContents << repair;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2017-03-01 12:52:54 +00:00
|
|
|
return readInt(conn->from);
|
2015-06-01 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 15:40:16 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::addSignatures(const StorePath & storePath, const StringSet & sigs)
|
2016-04-05 13:30:22 +00:00
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopAddSignatures << printStorePath(storePath) << sigs;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2016-04-05 13:30:22 +00:00
|
|
|
readInt(conn->from);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void RemoteStore::queryMissing(const std::vector<StorePathWithOutputs> & targets,
|
|
|
|
StorePathSet & willBuild, StorePathSet & willSubstitute, StorePathSet & unknown,
|
2020-07-30 11:10:49 +00:00
|
|
|
uint64_t & downloadSize, uint64_t & narSize)
|
2017-04-06 16:40:19 +00:00
|
|
|
{
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2017-04-06 16:40:19 +00:00
|
|
|
if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 19)
|
|
|
|
// Don't hold the connection handle in the fallback case
|
|
|
|
// to prevent a deadlock.
|
|
|
|
goto fallback;
|
2019-12-05 18:11:09 +00:00
|
|
|
conn->to << wopQueryMissing;
|
|
|
|
Strings ss;
|
|
|
|
for (auto & p : targets)
|
|
|
|
ss.push_back(p.to_string(*this));
|
|
|
|
conn->to << ss;
|
2018-10-16 21:36:15 +00:00
|
|
|
conn.processStderr();
|
2019-12-05 18:11:09 +00:00
|
|
|
willBuild = readStorePaths<StorePathSet>(*this, conn->from);
|
|
|
|
willSubstitute = readStorePaths<StorePathSet>(*this, conn->from);
|
|
|
|
unknown = readStorePaths<StorePathSet>(*this, conn->from);
|
2017-04-06 16:40:19 +00:00
|
|
|
conn->from >> downloadSize >> narSize;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fallback:
|
|
|
|
return Store::queryMissing(targets, willBuild, willSubstitute,
|
|
|
|
unknown, downloadSize, narSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-02 12:18:46 +00:00
|
|
|
void RemoteStore::connect()
|
|
|
|
{
|
2018-10-16 21:36:15 +00:00
|
|
|
auto conn(getConnection());
|
2017-05-02 12:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-30 21:28:47 +00:00
|
|
|
unsigned int RemoteStore::getProtocol()
|
|
|
|
{
|
|
|
|
auto conn(connections->get());
|
|
|
|
return conn->daemonVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 16:10:38 +00:00
|
|
|
void RemoteStore::flushBadConnections()
|
|
|
|
{
|
|
|
|
connections->flushBad();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-23 15:40:16 +00:00
|
|
|
RemoteStore::Connection::~Connection()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
to.flush();
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-28 16:49:42 +00:00
|
|
|
static Logger::Fields readFields(Source & from)
|
|
|
|
{
|
|
|
|
Logger::Fields fields;
|
|
|
|
size_t size = readInt(from);
|
|
|
|
for (size_t n = 0; n < size; n++) {
|
|
|
|
auto type = (decltype(Logger::Field::type)) readInt(from);
|
|
|
|
if (type == Logger::Field::tInt)
|
|
|
|
fields.push_back(readNum<uint64_t>(from));
|
|
|
|
else if (type == Logger::Field::tString)
|
|
|
|
fields.push_back(readString(from));
|
|
|
|
else
|
|
|
|
throw Error("got unsupported field type %x from Nix daemon", (int) type);
|
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-16 21:36:15 +00:00
|
|
|
std::exception_ptr RemoteStore::Connection::processStderr(Sink * sink, Source * source)
|
2006-12-03 02:08:13 +00:00
|
|
|
{
|
2011-12-14 23:30:06 +00:00
|
|
|
to.flush();
|
2017-08-28 16:49:42 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
auto msg = readNum<uint64_t>(from);
|
|
|
|
|
2007-02-21 17:34:02 +00:00
|
|
|
if (msg == STDERR_WRITE) {
|
|
|
|
string s = readString(from);
|
2007-02-21 16:34:00 +00:00
|
|
|
if (!sink) throw Error("no sink");
|
2016-05-04 13:46:25 +00:00
|
|
|
(*sink)(s);
|
2007-02-21 16:34:00 +00:00
|
|
|
}
|
2017-08-28 16:49:42 +00:00
|
|
|
|
2007-02-21 17:34:02 +00:00
|
|
|
else if (msg == STDERR_READ) {
|
|
|
|
if (!source) throw Error("no source");
|
2017-03-01 12:52:54 +00:00
|
|
|
size_t len = readNum<size_t>(from);
|
2017-01-16 21:24:29 +00:00
|
|
|
auto buf = std::make_unique<unsigned char[]>(len);
|
|
|
|
writeString(buf.get(), source->read(buf.get(), len), to);
|
2011-12-14 23:30:06 +00:00
|
|
|
to.flush();
|
2007-02-21 17:34:02 +00:00
|
|
|
}
|
2017-08-28 16:49:42 +00:00
|
|
|
|
|
|
|
else if (msg == STDERR_ERROR) {
|
|
|
|
string error = readString(from);
|
|
|
|
unsigned int status = readInt(from);
|
2018-10-16 21:36:15 +00:00
|
|
|
return std::make_exception_ptr(Error(status, error));
|
2017-08-28 16:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (msg == STDERR_NEXT)
|
2016-09-21 14:11:01 +00:00
|
|
|
printError(chomp(readString(from)));
|
2017-08-28 16:49:42 +00:00
|
|
|
|
|
|
|
else if (msg == STDERR_START_ACTIVITY) {
|
|
|
|
auto act = readNum<ActivityId>(from);
|
2017-08-28 17:13:24 +00:00
|
|
|
auto lvl = (Verbosity) readInt(from);
|
2017-08-28 16:49:42 +00:00
|
|
|
auto type = (ActivityType) readInt(from);
|
|
|
|
auto s = readString(from);
|
|
|
|
auto fields = readFields(from);
|
|
|
|
auto parent = readNum<ActivityId>(from);
|
2017-08-28 17:13:24 +00:00
|
|
|
logger->startActivity(act, lvl, type, s, fields, parent);
|
2017-08-28 16:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (msg == STDERR_STOP_ACTIVITY) {
|
|
|
|
auto act = readNum<ActivityId>(from);
|
|
|
|
logger->stopActivity(act);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (msg == STDERR_RESULT) {
|
|
|
|
auto act = readNum<ActivityId>(from);
|
|
|
|
auto type = (ResultType) readInt(from);
|
|
|
|
auto fields = readFields(from);
|
|
|
|
logger->result(act, type, fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (msg == STDERR_LAST)
|
|
|
|
break;
|
|
|
|
|
|
|
|
else
|
|
|
|
throw Error("got unknown message type %x from Nix daemon", msg);
|
2006-12-03 02:08:13 +00:00
|
|
|
}
|
2018-10-16 21:36:15 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2006-12-03 02:08:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 02:20:22 +00:00
|
|
|
static std::string uriScheme = "unix://";
|
|
|
|
|
|
|
|
static RegisterStoreImplementation regStore([](
|
|
|
|
const std::string & uri, const Store::Params & params)
|
|
|
|
-> std::shared_ptr<Store>
|
|
|
|
{
|
|
|
|
if (std::string(uri, 0, uriScheme.size()) != uriScheme) return 0;
|
|
|
|
return std::make_shared<UDSRemoteStore>(std::string(uri, uriScheme.size()), params);
|
|
|
|
});
|
2006-12-03 02:08:13 +00:00
|
|
|
|
2006-11-30 18:35:50 +00:00
|
|
|
}
|