From 19e874a9854c0d7f49d79fa98177a84b6997ce9a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 20 Aug 2020 02:32:51 +0100 Subject: feat(tvix): Introduce build event streams in worker protocol Introduces a new `BuildEvent` proto type which is streamed in response to calls that trigger builds of derivations. This type can currently supply build statuses, log lines and information about builds starting. This is in preparation for threading build logs through the processes. Since we have nowhere to send the logs (yet), a null sink is used instead. Co-authored-by: Griffin Smith Change-Id: If7332337b89506c7e404cd20174acdaa1a3be4e8 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1793 Tested-by: BuildkiteCI Reviewed-by: glittershark Reviewed-by: kanepyork --- third_party/nix/src/libstore/rpc-store.cc | 51 +++++++++++++++++++++++++++---- third_party/nix/src/libstore/store-api.cc | 4 +-- third_party/nix/src/libstore/store-api.hh | 4 +-- 3 files changed, 49 insertions(+), 10 deletions(-) (limited to 'third_party/nix/src/libstore') diff --git a/third_party/nix/src/libstore/rpc-store.cc b/third_party/nix/src/libstore/rpc-store.cc index 2d1278f40c78..f3fc5582f3ef 100644 --- a/third_party/nix/src/libstore/rpc-store.cc +++ b/third_party/nix/src/libstore/rpc-store.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -326,8 +327,32 @@ absl::Status RpcStore::buildPaths(const PathSet& paths, BuildMode buildMode) { google::protobuf::Empty response; request.set_mode(nix::BuildModeToProto(buildMode)); - return nix::util::proto::GRPCStatusToAbsl( - stub_->BuildPaths(&ctx, request, &response)); + + // TODO(tazjin): Temporary no-op sink used to discard build output, + // but satisfy the compiler. A real one is needed. + // + // Maybe this should just be stderr, considering that this is the + // *client*, but I'm not sure. + std::ostream discard_logs = DiscardLogsSink(); + + std::unique_ptr> reader = + stub_->BuildPaths(&ctx, request); + + proto::BuildEvent event; + while (reader->Read(&event)) { + if (event.has_build_log()) { + // TODO(tazjin): Include .path()? + discard_logs << event.build_log().line(); + } else { + discard_logs << std::endl + << "Building path: " << event.building_path().path() + << std::endl; + } + + // has_result() is not in use in this call (for now) + } + + return nix::util::proto::GRPCStatusToAbsl(reader->Finish()); } BuildResult RpcStore::buildDerivation(const Path& drvPath, @@ -339,11 +364,25 @@ BuildResult RpcStore::buildDerivation(const Path& drvPath, auto proto_drv = drv.to_proto(); request.set_allocated_derivation(&proto_drv); request.set_build_mode(BuildModeToProto(buildMode)); - proto::BuildDerivationResponse response; - SuccessOrThrow(stub_->BuildDerivation(&ctx, request, &response), - __FUNCTION__); - const auto result = BuildResult::FromProto(response); + // Same note as in ::buildPaths ... + std::ostream discard_logs = DiscardLogsSink(); + + std::unique_ptr> reader = + stub_->BuildDerivation(&ctx, request); + + std::optional result; + + proto::BuildEvent event; + while (reader->Read(&event)) { + if (event.has_build_log()) { + discard_logs << event.build_log().line(); + } else if (event.has_result()) { + result = BuildResult::FromProto(event.result()); + } + } + SuccessOrThrow(reader->Finish(), __FUNCTION__); + if (!result.has_value()) { throw Error("Invalid response from daemon for buildDerivation"); } diff --git a/third_party/nix/src/libstore/store-api.cc b/third_party/nix/src/libstore/store-api.cc index 3ada63532ca2..7972f5083657 100644 --- a/third_party/nix/src/libstore/store-api.cc +++ b/third_party/nix/src/libstore/store-api.cc @@ -92,7 +92,7 @@ nix::proto::BuildStatus BuildResult::status_to_proto() { } std::optional BuildResult::FromProto( - const nix::proto::BuildDerivationResponse& resp) { + const nix::proto::BuildResult& resp) { BuildResult result; switch (resp.status()) { case proto::BuildStatus::Built: @@ -125,7 +125,7 @@ std::optional BuildResult::FromProto( return {}; } - result.errorMsg = resp.error_message(); + result.errorMsg = resp.msg(); return result; } diff --git a/third_party/nix/src/libstore/store-api.hh b/third_party/nix/src/libstore/store-api.hh index 39769dfb3cd0..1d81b228f292 100644 --- a/third_party/nix/src/libstore/store-api.hh +++ b/third_party/nix/src/libstore/store-api.hh @@ -189,7 +189,7 @@ struct ValidPathInfo { virtual ~ValidPathInfo() {} }; -typedef std::list ValidPathInfos; +using ValidPathInfos = std::list; enum BuildMode { bmNormal, bmRepair, bmCheck }; @@ -243,7 +243,7 @@ struct BuildResult { nix::proto::BuildStatus status_to_proto(); static std::optional FromProto( - const nix::proto::BuildDerivationResponse& resp); + const nix::proto::BuildResult& resp); }; class Store : public std::enable_shared_from_this, public Config { -- cgit 1.4.1