Skip to content

Commit d5028d4

Browse files
committed
Systematize the worker protocol derived path serialiser
It was some ad-hoc functions to account for versions, while the already factored-out serializer just supported the latest version. Now, we can fold that version-specific logic into the factored out one, and so we do.
1 parent 4ce5012 commit d5028d4

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

src/libstore/daemon.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,6 @@ struct ClientSettings
260260
}
261261
};
262262

263-
static std::vector<DerivedPath> readDerivedPaths(Store & store, unsigned int clientVersion, WorkerProto::ReadConn conn)
264-
{
265-
std::vector<DerivedPath> reqs;
266-
if (GET_PROTOCOL_MINOR(clientVersion) >= 30) {
267-
reqs = WorkerProto::Serialise<std::vector<DerivedPath>>::read(store, conn);
268-
} else {
269-
for (auto & s : readStrings<Strings>(conn.from))
270-
reqs.push_back(parsePathWithOutputs(store, s).toDerivedPath());
271-
}
272-
return reqs;
273-
}
274-
275263
static void performOp(TunnelLogger * logger, ref<Store> store,
276264
TrustedFlag trusted, RecursiveFlag recursive, unsigned int clientVersion,
277265
Source & from, BufferedSink & to, WorkerProto::Op op)
@@ -537,7 +525,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
537525
}
538526

539527
case WorkerProto::Op::BuildPaths: {
540-
auto drvs = readDerivedPaths(*store, clientVersion, rconn);
528+
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
541529
BuildMode mode = bmNormal;
542530
if (GET_PROTOCOL_MINOR(clientVersion) >= 15) {
543531
mode = (BuildMode) readInt(from);
@@ -562,7 +550,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
562550
}
563551

564552
case WorkerProto::Op::BuildPathsWithResults: {
565-
auto drvs = readDerivedPaths(*store, clientVersion, rconn);
553+
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
566554
BuildMode mode = bmNormal;
567555
mode = (BuildMode) readInt(from);
568556

@@ -930,7 +918,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
930918
}
931919

932920
case WorkerProto::Op::QueryMissing: {
933-
auto targets = readDerivedPaths(*store, clientVersion, rconn);
921+
auto targets = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
934922
logger->startWork();
935923
StorePathSet willBuild, willSubstitute, unknown;
936924
uint64_t downloadSize, narSize;

src/libstore/remote-store.cc

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -658,30 +658,6 @@ void RemoteStore::queryRealisationUncached(const DrvOutput & id,
658658
} catch (...) { return callback.rethrow(); }
659659
}
660660

661-
static void writeDerivedPaths(RemoteStore & store, RemoteStore::Connection & conn, const std::vector<DerivedPath> & reqs)
662-
{
663-
if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 30) {
664-
WorkerProto::write(store, conn, reqs);
665-
} else {
666-
Strings ss;
667-
for (auto & p : reqs) {
668-
auto sOrDrvPath = StorePathWithOutputs::tryFromDerivedPath(p);
669-
std::visit(overloaded {
670-
[&](const StorePathWithOutputs & s) {
671-
ss.push_back(s.to_string(store));
672-
},
673-
[&](const StorePath & drvPath) {
674-
throw Error("trying to request '%s', but daemon protocol %d.%d is too old (< 1.29) to request a derivation file",
675-
store.printStorePath(drvPath),
676-
GET_PROTOCOL_MAJOR(conn.daemonVersion),
677-
GET_PROTOCOL_MINOR(conn.daemonVersion));
678-
},
679-
}, sOrDrvPath);
680-
}
681-
conn.to << ss;
682-
}
683-
}
684-
685661
void RemoteStore::copyDrvsFromEvalStore(
686662
const std::vector<DerivedPath> & paths,
687663
std::shared_ptr<Store> evalStore)
@@ -704,7 +680,7 @@ void RemoteStore::buildPaths(const std::vector<DerivedPath> & drvPaths, BuildMod
704680
auto conn(getConnection());
705681
conn->to << WorkerProto::Op::BuildPaths;
706682
assert(GET_PROTOCOL_MINOR(conn->daemonVersion) >= 13);
707-
writeDerivedPaths(*this, *conn, drvPaths);
683+
WorkerProto::write(*this, *conn, drvPaths);
708684
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 15)
709685
conn->to << buildMode;
710686
else
@@ -728,7 +704,7 @@ std::vector<KeyedBuildResult> RemoteStore::buildPathsWithResults(
728704

729705
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 34) {
730706
conn->to << WorkerProto::Op::BuildPathsWithResults;
731-
writeDerivedPaths(*this, *conn, paths);
707+
WorkerProto::write(*this, *conn, paths);
732708
conn->to << buildMode;
733709
conn.processStderr();
734710
return WorkerProto::Serialise<std::vector<KeyedBuildResult>>::read(*this, *conn);
@@ -917,7 +893,7 @@ void RemoteStore::queryMissing(const std::vector<DerivedPath> & targets,
917893
// to prevent a deadlock.
918894
goto fallback;
919895
conn->to << WorkerProto::Op::QueryMissing;
920-
writeDerivedPaths(*this, *conn, targets);
896+
WorkerProto::write(*this, *conn, targets);
921897
conn.processStderr();
922898
willBuild = WorkerProto::Serialise<StorePathSet>::read(*this, *conn);
923899
willSubstitute = WorkerProto::Serialise<StorePathSet>::read(*this, *conn);

src/libstore/worker-protocol.cc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,31 @@ void WorkerProto::Serialise<std::optional<TrustedFlag>>::write(const Store & sto
5151
DerivedPath WorkerProto::Serialise<DerivedPath>::read(const Store & store, WorkerProto::ReadConn conn)
5252
{
5353
auto s = readString(conn.from);
54-
return DerivedPath::parseLegacy(store, s);
54+
if (GET_PROTOCOL_MINOR(conn.version) >= 30) {
55+
return DerivedPath::parseLegacy(store, s);
56+
} else {
57+
return parsePathWithOutputs(store, s).toDerivedPath();
58+
}
5559
}
5660

5761
void WorkerProto::Serialise<DerivedPath>::write(const Store & store, WorkerProto::WriteConn conn, const DerivedPath & req)
5862
{
59-
conn.to << req.to_string_legacy(store);
63+
if (GET_PROTOCOL_MINOR(conn.version) >= 30) {
64+
conn.to << req.to_string_legacy(store);
65+
} else {
66+
auto sOrDrvPath = StorePathWithOutputs::tryFromDerivedPath(req);
67+
std::visit(overloaded {
68+
[&](const StorePathWithOutputs & s) {
69+
conn.to << s.to_string(store);
70+
},
71+
[&](const StorePath & drvPath) {
72+
throw Error("trying to request '%s', but daemon protocol %d.%d is too old (< 1.29) to request a derivation file",
73+
store.printStorePath(drvPath),
74+
GET_PROTOCOL_MAJOR(conn.version),
75+
GET_PROTOCOL_MINOR(conn.version));
76+
},
77+
}, sOrDrvPath);
78+
}
6079
}
6180

6281

0 commit comments

Comments
 (0)