Skip to content

Commit e07c16d

Browse files
committed
Factor out serialization for BuildResult
With the differences between the two protocols reconciled, I also factor out a `worker_proto` for `BuildResult` to be used in the latest versions of each protocol.
1 parent 13b17d1 commit e07c16d

File tree

9 files changed

+100
-56
lines changed

9 files changed

+100
-56
lines changed

src/libstore/daemon.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
614614

615615
auto res = store->buildDerivation(drvPath, drv, buildMode);
616616
logger->stopWork();
617-
to << res.status << res.errorMsg;
618-
if (GET_PROTOCOL_MINOR(clientVersion) >= 29) {
619-
to << res.timesBuilt << res.isNonDeterministic << res.startTime << res.stopTime;
620-
}
621-
if (GET_PROTOCOL_MINOR(clientVersion) >= 28) {
622-
worker_proto::write(*store, clientVersion, to, res.builtOutputs);
623-
}
617+
worker_proto::writeWithoutReq(*store, clientVersion, to, res);
624618
break;
625619
}
626620

src/libstore/gen-protocol.cc-inc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,32 @@ void write(const Store & store, unsigned int version, Sink & out, const std::opt
9090
{
9191
out << (caOpt ? renderContentAddress(*caOpt) : "");
9292
}
93+
94+
/* N suffix indicates this is the nth version of them in common. Wrapped downstream
95+
by protocol-specific functions which handle the previous versions too. */
96+
97+
BuildResult read0(const Store & store, unsigned int version, Source & from, Phantom<BuildResult> _, DerivedPath req)
98+
{
99+
BuildResult res { .path = req };
100+
res.status = (BuildResult::Status) readInt(from);
101+
from
102+
>> res.errorMsg
103+
>> res.timesBuilt
104+
>> res.isNonDeterministic
105+
>> res.startTime
106+
>> res.stopTime;
107+
res.builtOutputs = read(store, version, from, Phantom<DrvOutputs> {});
108+
return res;
109+
}
110+
111+
void write0(const Store & store, unsigned int version, Sink & out, const BuildResult & res)
112+
{
113+
out
114+
<< res.status
115+
<< res.errorMsg
116+
<< res.timesBuilt
117+
<< res.isNonDeterministic
118+
<< res.startTime
119+
<< res.stopTime;
120+
write(store, version, out, res.builtOutputs);
121+
}

src/libstore/legacy-ssh-store.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,8 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
279279

280280
conn->to.flush();
281281

282-
BuildResult status { .path = DerivedPath::Built { .drvPath = drvPath } };
283-
status.status = (BuildResult::Status) readInt(conn->from);
284-
conn->from >> status.errorMsg;
285-
286-
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 3)
287-
conn->from >> status.timesBuilt >> status.isNonDeterministic >> status.startTime >> status.stopTime;
288-
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 6) {
289-
status.builtOutputs = serve_proto::read(*this, conn->remoteVersion, conn->from, Phantom<DrvOutputs> {});
290-
}
291-
return status;
282+
return serve_proto::read(*this, conn->remoteVersion, conn->from, Phantom<BuildResult> {},
283+
DerivedPath::Built { .drvPath = drvPath });
292284
}
293285

294286
void buildPaths(const std::vector<DerivedPath> & drvPaths, BuildMode buildMode, std::shared_ptr<Store> evalStore) override

src/libstore/remote-store.cc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -778,17 +778,8 @@ BuildResult RemoteStore::buildDerivation(const StorePath & drvPath, const BasicD
778778
writeDerivation(conn->to, *this, drv);
779779
conn->to << buildMode;
780780
conn.processStderr();
781-
BuildResult res { .path = DerivedPath::Built { .drvPath = drvPath } };
782-
res.status = (BuildResult::Status) readInt(conn->from);
783-
conn->from >> res.errorMsg;
784-
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 29) {
785-
conn->from >> res.timesBuilt >> res.isNonDeterministic >> res.startTime >> res.stopTime;
786-
}
787-
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 28) {
788-
auto builtOutputs = worker_proto::read(*this, conn->daemonVersion, conn->from, Phantom<DrvOutputs> {});
789-
res.builtOutputs = builtOutputs;
790-
}
791-
return res;
781+
return worker_proto::read(*this, conn->daemonVersion, conn->from, Phantom<BuildResult>{},
782+
DerivedPath::Built { .drvPath = drvPath });
792783
}
793784

794785

src/libstore/serve-protocol.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "util.hh"
33
#include "path-with-outputs.hh"
44
#include "store-api.hh"
5+
#include "build-result.hh"
56
#include "serve-protocol.hh"
67
#include "serve-protocol-impl.hh"
78
#include "archive.hh"
@@ -17,6 +18,33 @@ namespace serve_proto {
1718

1819
/* protocol-specific definitions */
1920

21+
BuildResult read(const Store & store, unsigned int version, Source & from, Phantom<BuildResult> _, DerivedPath req)
22+
{
23+
if (GET_PROTOCOL_MINOR(version) < 6) {
24+
BuildResult status { .path = req };
25+
status.status = (BuildResult::Status) readInt(from);
26+
from >> status.errorMsg;
27+
28+
if (GET_PROTOCOL_MINOR(version) >= 3)
29+
from >> status.timesBuilt >> status.isNonDeterministic >> status.startTime >> status.stopTime;
30+
return status;
31+
} else
32+
return serve_proto::read0(store, version, from, Phantom<BuildResult> {}, req);
33+
}
34+
35+
void writeWithoutReq(const Store & store, unsigned int version, Sink & out, const BuildResult & status)
36+
{
37+
if (GET_PROTOCOL_MINOR(version < 6)) {
38+
out << status.status << status.errorMsg;
39+
40+
if (GET_PROTOCOL_MINOR(version) >= 3)
41+
out << status.timesBuilt << status.isNonDeterministic << status.startTime << status.stopTime;
42+
} else {
43+
serve_proto::write0(store, version, out, status);
44+
}
45+
}
46+
47+
2048
ValidPathInfo readValidPathInfo(const Store & store, unsigned int version, Source & source)
2149
{
2250
auto path = read(store, version, source, Phantom<StorePath>{});

src/libstore/serve-protocol.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct Source;
3333
struct DerivedPath;
3434
struct DrvOutput;
3535
struct Realisation;
36+
struct BuildResult;
3637
struct PathInfo;
3738

3839

@@ -66,6 +67,9 @@ MAKE_SERVE_PROTO(, std::optional<ContentAddress>);
6667

6768
/* These are a non-standard form for historical reasons. */
6869

70+
BuildResult read(const Store & store, unsigned int version, Source & from, Phantom<BuildResult> _, DerivedPath req);
71+
void writeWithoutReq(const Store & store, unsigned int version, Sink & out, const BuildResult & str);
72+
6973
ValidPathInfo readValidPathInfo(const Store & store, unsigned int version, Source & source);
7074
ValidPathInfo readValidPathInfo(const Store & store, unsigned int version, Source & source, StorePath && path);
7175

src/libstore/worker-protocol.cc

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,42 @@ namespace worker_proto {
1818

1919
/* protocol-specific definitions */
2020

21+
BuildResult read(const Store & store, unsigned int version, Source & from, Phantom<BuildResult> _, DerivedPath req)
22+
{
23+
if (GET_PROTOCOL_MINOR(version) < 29) {
24+
BuildResult res { .path = req };
25+
res.status = (BuildResult::Status) readInt(from);
26+
from >> res.errorMsg;
27+
if (GET_PROTOCOL_MINOR(version) >= 28) {
28+
auto builtOutputs = read(store, version, from, Phantom<DrvOutputs> {});
29+
res.builtOutputs = builtOutputs;
30+
}
31+
return res;
32+
} else
33+
return read0(store, version, from, Phantom<BuildResult>{}, req);
34+
}
35+
36+
void writeWithoutReq(const Store & store, unsigned int version, Sink & out, const BuildResult & res)
37+
{
38+
if (GET_PROTOCOL_MINOR(version) < 29) {
39+
out << res.status << res.errorMsg;
40+
if (GET_PROTOCOL_MINOR(version) >= 28) {
41+
write(store, version, out, res.builtOutputs);
42+
}
43+
} else
44+
write0(store, version, out, res);
45+
}
46+
2147
BuildResult read(const Store & store, unsigned int version, Source & from, Phantom<BuildResult> _)
2248
{
23-
auto path = worker_proto::read(store, version, from, Phantom<DerivedPath> {});
24-
BuildResult res { .path = path };
25-
res.status = (BuildResult::Status) readInt(from);
26-
from
27-
>> res.errorMsg
28-
>> res.timesBuilt
29-
>> res.isNonDeterministic
30-
>> res.startTime
31-
>> res.stopTime;
32-
res.builtOutputs = worker_proto::read(store, version, from, Phantom<DrvOutputs> {});
33-
return res;
49+
auto req = read(store, version, from, Phantom<DerivedPath> {});
50+
return read(store, version, from, Phantom<BuildResult> {}, req);
3451
}
3552

3653
void write(const Store & store, unsigned int version, Sink & to, const BuildResult & res)
3754
{
38-
worker_proto::write(store, version, to, res.path);
39-
to
40-
<< res.status
41-
<< res.errorMsg
42-
<< res.timesBuilt
43-
<< res.isNonDeterministic
44-
<< res.startTime
45-
<< res.stopTime;
46-
worker_proto::write(store, version, to, res.builtOutputs);
55+
write(store, version, to, res.path);
56+
writeWithoutReq(store, version, to, res);
4757
}
4858

4959

src/libstore/worker-protocol.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ MAKE_WORKER_PROTO(, std::optional<ContentAddress>);
124124

125125
/* These are a non-standard form for historical reasons. */
126126

127+
BuildResult read(const Store & store, unsigned int version, Source & from, Phantom<BuildResult> _, DerivedPath req);
128+
void writeWithoutReq(const Store & store, unsigned int version, Sink & out, const BuildResult & str);
129+
127130
ValidPathInfo readValidPathInfo(const Store & store, unsigned int version, Source & source);
128131
ValidPathInfo readValidPathInfo(const Store & store, unsigned int version, Source & source, StorePath && path);
129132

src/nix-store/nix-store.cc

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -914,14 +914,7 @@ static void opServe(Strings opFlags, Strings opArgs)
914914
MonitorFdHup monitor(in.fd);
915915
auto status = store->buildDerivation(drvPath, drv);
916916

917-
out << status.status << status.errorMsg;
918-
919-
if (GET_PROTOCOL_MINOR(clientVersion) >= 3)
920-
out << status.timesBuilt << status.isNonDeterministic << status.startTime << status.stopTime;
921-
if (GET_PROTOCOL_MINOR(clientVersion >= 6)) {
922-
serve_proto::write(*store, clientVersion, out, status.builtOutputs);
923-
}
924-
917+
serve_proto::writeWithoutReq(*store, clientVersion, out, status);
925918

926919
break;
927920
}

0 commit comments

Comments
 (0)