diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2023-10-10 17:26:40 +0200 |
---|---|---|
committer | Alberto Sartori <alberto.sartori@huawei.com> | 2023-11-15 20:19:18 +0100 |
commit | 366111c5c7b407fe0e944ee71de1c51137147805 (patch) | |
tree | 92733338a6bc3969dd5fb1e930aa4ec5b9b43d54 /src/buildtool/serve_api/remote/target_client.cpp | |
parent | 9cc638844836b67ba09504985dd7ad85846046ab (diff) | |
download | justbuild-366111c5c7b407fe0e944ee71de1c51137147805.tar.gz |
just-serve: Extend client and API for services Target and Configuration
Co-authored-by: Alberto Sartori <alberto.sartori@huawei.com>
Diffstat (limited to 'src/buildtool/serve_api/remote/target_client.cpp')
-rw-r--r-- | src/buildtool/serve_api/remote/target_client.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/buildtool/serve_api/remote/target_client.cpp b/src/buildtool/serve_api/remote/target_client.cpp new file mode 100644 index 00000000..bb30907d --- /dev/null +++ b/src/buildtool/serve_api/remote/target_client.cpp @@ -0,0 +1,118 @@ +// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/buildtool/serve_api/remote/target_client.hpp" + +#include <utility> + +#include <nlohmann/json.hpp> + +#include "src/buildtool/common/bazel_types.hpp" +#include "src/buildtool/common/remote/client_common.hpp" + +TargetClient::TargetClient(std::string const& server, Port port) noexcept { + stub_ = justbuild::just_serve::Target::NewStub( + CreateChannelWithCredentials(server, port)); +} + +auto TargetClient::ServeTarget(const TargetCacheKey& key) + -> std::optional<std::pair<TargetCacheEntry, Artifact::ObjectInfo>> { + // make sure the blob containing the key is in the remote cas + if (!local_api_->RetrieveToCas({key.Id()}, &*remote_api_)) { + logger_.Emit(LogLevel::Error, + "failed to retrieve to remote cas ObjectInfo {}", + key.Id().ToString()); + return std::nullopt; + } + + bazel_re::Digest key_dgst{key.Id().digest}; + justbuild::just_serve::ServeTargetRequest request{}; + *(request.mutable_target_cache_key_id()) = std::move(key_dgst); + + auto execution_backend_dgst = ArtifactDigest::Create<ObjectType::File>( + StorageConfig::ExecutionBackendDescription()); + auto const& execution_info = + Artifact::ObjectInfo{.digest = ArtifactDigest{execution_backend_dgst}, + .type = ObjectType::File}; + if (!local_api_->RetrieveToCas({execution_info}, &*remote_api_)) { + logger_.Emit(LogLevel::Error, + "failed to upload blob {} to remote cas", + execution_info.ToString()); + return std::nullopt; + } + + *(request.mutable_execution_backend_description_id()) = + std::move(execution_backend_dgst); + + grpc::ClientContext context; + justbuild::just_serve::ServeTargetResponse response; + auto const& status = stub_->ServeTarget(&context, request, &response); + if (!status.ok()) { + LogStatus(&logger_, LogLevel::Error, status); + return std::nullopt; + } + auto const& target_value_dgst = ArtifactDigest{response.target_value()}; + auto const& obj_info = Artifact::ObjectInfo{.digest = target_value_dgst, + .type = ObjectType::File}; + if (!local_api_->IsAvailable(target_value_dgst)) { + if (!remote_api_->RetrieveToCas({obj_info}, &*local_api_)) { + logger_.Emit(LogLevel::Error, + "failed to retrieve blob {} from remote cas", + obj_info.ToString()); + return std::nullopt; + } + } + + auto const& target_value_str = local_api_->RetrieveToMemory(obj_info); + if (!target_value_str) { + logger_.Emit(LogLevel::Error, + "failed to retrieve blob {} from local cas", + obj_info.ToString()); + return std::nullopt; + } + auto const& result = + TargetCacheEntry::FromJson(nlohmann::json::parse(*target_value_str)); + + return std::make_pair(result, obj_info); +} + +auto TargetClient::ServeTargetVariables(std::string const& target_root_id, + std::string const& target_file, + std::string const& target) + -> std::optional<std::vector<std::string>> { + justbuild::just_serve::ServeTargetVariablesRequest request{}; + request.set_root_tree(target_root_id); + request.set_target_file(target_file); + request.set_target(target); + + grpc::ClientContext context; + justbuild::just_serve::ServeTargetVariablesResponse response; + grpc::Status status = + stub_->ServeTargetVariables(&context, request, &response); + + if (not status.ok()) { + LogStatus(&logger_, LogLevel::Error, status); + return std::nullopt; + } + auto size = response.flexible_config_size(); + if (size == 0) { + return std::vector<std::string>(); + } + std::vector<std::string> res{}; + res.reserve(size); + for (auto const& var : response.flexible_config()) { + res.emplace_back(var); + } + return res; +} |