Skip to content

Commit 17fcc8a

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 2656419 commit 17fcc8a

File tree

6 files changed

+56
-50
lines changed

6 files changed

+56
-50
lines changed

src/libstore/daemon.cc

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

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

540528
case WorkerProto::Op::BuildPaths: {
541-
auto drvs = readDerivedPaths(*store, clientVersion, rconn);
529+
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
542530
BuildMode mode = bmNormal;
543531
if (GET_PROTOCOL_MINOR(clientVersion) >= 15) {
544532
mode = (BuildMode) readInt(from);
@@ -563,7 +551,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
563551
}
564552

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

@@ -929,7 +917,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
929917
}
930918

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

src/libstore/remote-store.cc

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -655,33 +655,6 @@ void RemoteStore::queryRealisationUncached(const DrvOutput & id,
655655
} catch (...) { return callback.rethrow(); }
656656
}
657657

658-
static void writeDerivedPaths(RemoteStore & store, RemoteStore::Connection & conn, const std::vector<DerivedPath> & reqs)
659-
{
660-
if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 30) {
661-
WorkerProto::write(store, conn, reqs);
662-
} else {
663-
Strings ss;
664-
for (auto & p : reqs) {
665-
auto sOrDrvPath = StorePathWithOutputs::tryFromDerivedPath(p);
666-
std::visit(overloaded {
667-
[&](const StorePathWithOutputs & s) {
668-
ss.push_back(s.to_string(store));
669-
},
670-
[&](const StorePath & drvPath) {
671-
throw Error("trying to request '%s', but daemon protocol %d.%d is too old (< 1.29) to request a derivation file",
672-
store.printStorePath(drvPath),
673-
GET_PROTOCOL_MAJOR(conn.daemonVersion),
674-
GET_PROTOCOL_MINOR(conn.daemonVersion));
675-
},
676-
[&](std::monostate) {
677-
throw Error("wanted to build a derivation that is itself a build product, but the legacy 'ssh://' protocol doesn't support that. Try using 'ssh-ng://'");
678-
},
679-
}, sOrDrvPath);
680-
}
681-
conn.to << ss;
682-
}
683-
}
684-
685658
void RemoteStore::copyDrvsFromEvalStore(
686659
const std::vector<DerivedPath> & paths,
687660
std::shared_ptr<Store> evalStore)
@@ -711,7 +684,7 @@ void RemoteStore::buildPaths(const std::vector<DerivedPath> & drvPaths, BuildMod
711684
auto conn(getConnection());
712685
conn->to << WorkerProto::Op::BuildPaths;
713686
assert(GET_PROTOCOL_MINOR(conn->daemonVersion) >= 13);
714-
writeDerivedPaths(*this, *conn, drvPaths);
687+
WorkerProto::write(*this, *conn, drvPaths);
715688
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 15)
716689
conn->to << buildMode;
717690
else
@@ -735,7 +708,7 @@ std::vector<KeyedBuildResult> RemoteStore::buildPathsWithResults(
735708

736709
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 34) {
737710
conn->to << WorkerProto::Op::BuildPathsWithResults;
738-
writeDerivedPaths(*this, *conn, paths);
711+
WorkerProto::write(*this, *conn, paths);
739712
conn->to << buildMode;
740713
conn.processStderr();
741714
return WorkerProto::Serialise<std::vector<KeyedBuildResult>>::read(*this, *conn);
@@ -916,7 +889,7 @@ void RemoteStore::queryMissing(const std::vector<DerivedPath> & targets,
916889
// to prevent a deadlock.
917890
goto fallback;
918891
conn->to << WorkerProto::Op::QueryMissing;
919-
writeDerivedPaths(*this, *conn, targets);
892+
WorkerProto::write(*this, *conn, targets);
920893
conn.processStderr();
921894
willBuild = WorkerProto::Serialise<StorePathSet>::read(*this, *conn);
922895
willSubstitute = WorkerProto::Serialise<StorePathSet>::read(*this, *conn);

src/libstore/tests/worker-protocol.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,32 @@ VERSIONED_CHARACTERIZATION_TEST(
6969

7070
VERSIONED_CHARACTERIZATION_TEST(
7171
WorkerProtoTest,
72-
derivedPath,
73-
"derived-path",
74-
defaultVersion,
72+
derivedPath_1_29,
73+
"derived-path-1.29",
74+
1 << 8 | 29,
75+
(std::tuple<DerivedPath, DerivedPath, DerivedPath> {
76+
DerivedPath::Opaque {
77+
.path = StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo" },
78+
},
79+
DerivedPath::Built {
80+
.drvPath = makeConstantStorePathRef(StorePath {
81+
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-bar.drv",
82+
}),
83+
.outputs = OutputsSpec::All { },
84+
},
85+
DerivedPath::Built {
86+
.drvPath = makeConstantStorePathRef(StorePath {
87+
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-bar.drv",
88+
}),
89+
.outputs = OutputsSpec::Names { "x", "y" },
90+
},
91+
}))
92+
93+
VERSIONED_CHARACTERIZATION_TEST(
94+
WorkerProtoTest,
95+
derivedPath_1_30,
96+
"derived-path-1.30",
97+
1 << 8 | 30,
7598
(std::tuple<DerivedPath, DerivedPath, DerivedPath, DerivedPath> {
7699
DerivedPath::Opaque {
77100
.path = StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo" },

src/libstore/worker-protocol.cc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,34 @@ 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+
[&](std::monostate) {
78+
throw Error("wanted to build a derivation that is itself a build product, but protocols do not support that. Try upgrading the Nix on the other end of this connection");
79+
},
80+
}, sOrDrvPath);
81+
}
6082
}
6183

6284

184 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)