1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// 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.
#ifndef BOOTSTRAP_BUILD_TOOL
#include "src/buildtool/serve_api/remote/target_client.hpp"
#include <exception>
#include <utility>
#include "fmt/core.h"
#include "nlohmann/json.hpp"
#include "src/buildtool/common/artifact_digest_factory.hpp"
#include "src/buildtool/common/bazel_types.hpp"
#include "src/buildtool/common/remote/client_common.hpp"
#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/logging/log_level.hpp"
namespace {
[[nodiscard]] auto GetTargetValue(
HashFunction::Type hash_type,
justbuild::just_serve::ServeTargetResponse const& response) noexcept
-> std::optional<ArtifactDigest> {
if (not response.has_target_value()) {
return std::nullopt;
}
auto result =
ArtifactDigestFactory::FromBazel(hash_type, response.target_value());
if (not result) {
return std::nullopt;
}
return *std::move(result);
}
} // namespace
TargetClient::TargetClient(
ServerAddress const& address,
gsl::not_null<Storage const*> const& storage,
gsl::not_null<RemoteContext const*> const& remote_context,
gsl::not_null<ApiBundle const*> const& apis) noexcept
: storage_{*storage},
exec_config_{*remote_context->exec_config},
apis_{*apis} {
stub_ = justbuild::just_serve::Target::NewStub(CreateChannelWithCredentials(
address.host, address.port, remote_context->auth));
}
auto TargetClient::ServeTarget(const TargetCacheKey& key,
const ArtifactDigest& repo_key) const noexcept
-> std::optional<serve_target_result_t> {
// make sure the blob containing the key is in the remote cas
if (not apis_.local->RetrieveToCas({key.Id()}, *apis_.remote)) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Failed to retrieve to remote cas ObjectInfo {}",
key.Id().ToString())};
}
// make sure the repository configuration blob is in the remote cas
if (not apis_.local->RetrieveToCas(
{Artifact::ObjectInfo{.digest = repo_key,
.type = ObjectType::File}},
*apis_.remote)) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Failed to retrieve to remote cas blob {}",
repo_key.hash())};
}
// add target cache key to request
justbuild::just_serve::ServeTargetRequest request{};
*request.mutable_target_cache_key_id() =
ArtifactDigestFactory::ToBazel(key.Id().digest);
// add execution properties to request
for (auto const& [k, v] : exec_config_.platform_properties) {
auto* prop = request.add_execution_properties();
prop->set_name(k);
prop->set_value(v);
}
// add dispatch information to request, while ensuring blob is uploaded
// to remote cas
std::optional<ArtifactDigest> dispatch_digest;
try {
auto dispatch_list = nlohmann::json::array();
for (auto const& [props, endpoint] : exec_config_.dispatch) {
auto entry = nlohmann::json::array();
entry.push_back(nlohmann::json(props));
entry.push_back(endpoint.ToJson());
dispatch_list.push_back(entry);
}
dispatch_digest = storage_.CAS().StoreBlob(dispatch_list.dump(2));
} catch (std::exception const& ex) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Populating dispatch JSON array failed with:\n{}",
ex.what())};
}
if (not dispatch_digest) {
return serve_target_result_t{
std::in_place_index<1>, "Failed to add dispatch info to local cas"};
}
auto const dispatch_info = Artifact::ObjectInfo{.digest = *dispatch_digest,
.type = ObjectType::File};
if (not apis_.local->RetrieveToCas({dispatch_info}, *apis_.remote)) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Failed to upload blob {} to remote cas",
dispatch_info.ToString())};
}
(*request.mutable_dispatch_info()) =
ArtifactDigestFactory::ToBazel(*dispatch_digest);
// call rpc
grpc::ClientContext context;
justbuild::just_serve::ServeTargetResponse response;
auto const& status = stub_->ServeTarget(&context, request, &response);
// differentiate status codes
switch (status.error_code()) {
case grpc::StatusCode::OK: {
// if log has been set, pass it along as index 0
if (response.has_log()) {
auto log_digest = ArtifactDigestFactory::FromBazel(
storage_.GetHashFunction().GetType(), response.log());
if (not log_digest) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Failed to convert log digest: {}",
std::move(log_digest).error())};
}
return serve_target_result_t{std::in_place_index<0>,
log_digest->hash()};
}
// if no log has been set, it must have the target cache value
auto const target_value_dgst =
GetTargetValue(storage_.GetHashFunction().GetType(), response);
if (not target_value_dgst) {
return serve_target_result_t{
std::in_place_index<1>,
"Serve endpoint failed to set expected response field"};
}
auto const obj_info = Artifact::ObjectInfo{
.digest = *target_value_dgst, .type = ObjectType::File};
if (not apis_.local->IsAvailable(*target_value_dgst)) {
if (not apis_.remote->RetrieveToCas({obj_info}, *apis_.local)) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format(
"Failed to retrieve blob {} from remote cas",
obj_info.ToString())};
}
}
auto const& target_value_str =
apis_.local->RetrieveToMemory(obj_info);
if (not target_value_str) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Failed to retrieve blob {} from local cas",
obj_info.ToString())};
}
try {
auto const result = TargetCacheEntry::FromJson(
storage_.GetHashFunction().GetType(),
nlohmann::json::parse(*target_value_str));
// return the target cache value information
return serve_target_result_t{std::in_place_index<3>,
std::make_pair(result, obj_info)};
} catch (std::exception const& ex) {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format("Parsing target cache value failed with:\n{}",
ex.what())};
}
}
case grpc::StatusCode::INTERNAL: {
return serve_target_result_t{
std::in_place_index<1>,
fmt::format(
"Serve endpoint reported the fatal internal error:\n{}",
status.error_message())};
}
case grpc::StatusCode::NOT_FOUND: {
return std::nullopt; // this might allow a local build to continue
}
default:; // fallthrough
}
return serve_target_result_t{
std::in_place_index<2>,
fmt::format("Serve endpoint failed with:\n{}", status.error_message())};
}
auto TargetClient::ServeTargetVariables(std::string const& target_root_id,
std::string const& target_file,
std::string const& target)
const noexcept -> 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;
}
auto TargetClient::ServeTargetDescription(
std::string const& target_root_id,
std::string const& target_file,
std::string const& target) const noexcept -> std::optional<ArtifactDigest> {
justbuild::just_serve::ServeTargetDescriptionRequest request{};
request.set_root_tree(target_root_id);
request.set_target_file(target_file);
request.set_target(target);
grpc::ClientContext context;
justbuild::just_serve::ServeTargetDescriptionResponse response;
grpc::Status status =
stub_->ServeTargetDescription(&context, request, &response);
if (not status.ok()) {
LogStatus(&logger_, LogLevel::Error, status);
return std::nullopt;
}
auto result = ArtifactDigestFactory::FromBazel(
storage_.GetHashFunction().GetType(), response.description_id());
if (not result) {
logger_.Emit(LogLevel::Error, "{}", std::move(result).error());
return std::nullopt;
}
return *std::move(result);
}
#endif // BOOTSTRAP_BUILD_TOOL
|