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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
// 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/other_tools/root_maps/tree_id_git_map.hpp"
#include <filesystem>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "fmt/core.h"
#include "src/buildtool/common/artifact.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/file_system/file_root.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/other_tools/git_operations/git_ops_types.hpp"
#include "src/other_tools/git_operations/git_repo_remote.hpp"
#include "src/utils/cpp/expected.hpp"
#include "src/utils/cpp/tmp_dir.hpp"
namespace {
/// \brief Guarantees it terminates by either calling the setter or calling the
/// logger with fatal.
void UploadToServeAndSetRoot(
ServeApi const& serve,
gsl::not_null<StorageConfig const*> const& native_storage_config,
ArtifactDigest const& digest,
bool ignore_special,
TreeIdGitMap::SetterPtr const& setter,
TreeIdGitMap::LoggerPtr const& logger) {
auto uploaded = serve.UploadTree(digest, native_storage_config->GitRoot());
if (not uploaded.has_value()) {
(*logger)(uploaded.error().Message(), /*fatal=*/true);
return;
}
// set workspace root as absent
auto root = nlohmann::json::array(
{ignore_special ? FileRoot::kGitTreeIgnoreSpecialMarker
: FileRoot::kGitTreeMarker,
digest.hash()});
(*setter)(std::pair(std::move(root), /*is_cache_hit=*/false));
}
/// \brief Guarantees it terminates by either calling the setter or calling the
/// logger with fatal.
void MoveCASTreeToGitAndProcess(
ServeApi const& serve,
gsl::not_null<StorageConfig const*> const& native_storage_config,
ArtifactDigest const& digest,
gsl::not_null<ImportToGitMap*> const& import_to_git_map,
gsl::not_null<IExecutionApi const*> const& local_api,
bool ignore_special,
gsl::not_null<TaskSystem*> const& ts,
TreeIdGitMap::SetterPtr const& setter,
TreeIdGitMap::LoggerPtr const& logger) {
// Move tree from CAS to local Git storage
auto tmp_dir =
native_storage_config->CreateTypedTmpDir("fetch-remote-git-tree");
if (not tmp_dir) {
(*logger)(fmt::format("Failed to create tmp directory for copying "
"git-tree {} from remote CAS",
digest.hash()),
true);
return;
}
if (not local_api->RetrieveToPaths(
{Artifact::ObjectInfo{.digest = digest, .type = ObjectType::Tree}},
{tmp_dir->GetPath()})) {
(*logger)(fmt::format("Failed to copy git-tree {} to {}",
digest.hash(),
tmp_dir->GetPath().string()),
true);
return;
}
CommitInfo c_info{tmp_dir->GetPath(), "tree", digest.hash()};
import_to_git_map->ConsumeAfterKeysReady(
ts,
{std::move(c_info)},
[&serve,
native_storage_config,
tmp_dir, // keep tmp_dir alive
digest,
ignore_special,
setter,
logger](auto const& values) {
if (not values[0]->second) {
(*logger)("Importing to git failed",
/*fatal=*/true);
return;
}
// upload tree from Git cache to remote CAS and tell serve to set up
// the root from the remote CAS tree; set root as absent on success
UploadToServeAndSetRoot(serve,
native_storage_config,
digest,
ignore_special,
setter,
logger);
},
[logger, tmp_dir, tree_id = digest.hash()](auto const& msg,
bool fatal) {
(*logger)(fmt::format(
"While moving git-tree {} from {} to local git:\n{}",
tree_id,
tmp_dir->GetPath().string(),
msg),
fatal);
});
}
} // namespace
auto CreateTreeIdGitMap(
gsl::not_null<GitTreeFetchMap*> const& git_tree_fetch_map,
gsl::not_null<CriticalGitOpMap*> const& critical_git_op_map,
gsl::not_null<ImportToGitMap*> const& import_to_git_map,
bool fetch_absent,
ServeApi const* serve,
gsl::not_null<StorageConfig const*> const& native_storage_config,
gsl::not_null<IExecutionApi const*> const& local_api,
IExecutionApi const* remote_api,
std::size_t jobs) -> TreeIdGitMap {
auto tree_to_git = [git_tree_fetch_map,
critical_git_op_map,
import_to_git_map,
fetch_absent,
serve,
native_storage_config,
local_api,
remote_api](auto ts,
auto setter,
auto logger,
auto /*unused*/,
auto const& key) {
// if root is actually absent, check if serve endpoint knows the tree
// for building against it and only set the workspace root if tree is
// found on the serve endpoint or it can be made available to it;
// otherwise, error out
if (key.absent and not fetch_absent) {
if (serve != nullptr) {
// check serve endpoint
auto const has_tree =
serve->CheckRootTree(key.tree_info.tree_hash.Hash());
if (not has_tree) {
(*logger)(fmt::format(
"Checking that the serve endpoint knows tree "
"{} failed.",
key.tree_info.tree_hash.Hash()),
/*fatal=*/true);
return;
}
if (*has_tree) {
// set workspace root as absent
auto root = nlohmann::json::array(
{key.ignore_special
? FileRoot::kGitTreeIgnoreSpecialMarker
: FileRoot::kGitTreeMarker,
key.tree_info.tree_hash.Hash()});
(*setter)(
std::pair(std::move(root), /*is_cache_hit=*/false));
return;
}
// we cannot continue without a suitable remote set up
if (remote_api == nullptr) {
(*logger)(
fmt::format("Cannot create workspace root {} as absent "
"for the provided serve endpoint.",
key.tree_info.tree_hash.Hash()),
/*fatal=*/true);
return;
}
// check if tree in already in remote CAS
auto const digest = ArtifactDigest{key.tree_info.tree_hash, 0};
if (remote_api->IsAvailable(digest)) {
// tell serve to set up the root from the remote CAS tree;
if (serve->GetTreeFromRemote(digest)) {
// set workspace root as absent
auto root = nlohmann::json::array(
{key.ignore_special
? FileRoot::kGitTreeIgnoreSpecialMarker
: FileRoot::kGitTreeMarker,
key.tree_info.tree_hash.Hash()});
(*setter)(
std::pair(std::move(root), /*is_cache_hit=*/false));
return;
}
(*logger)(
fmt::format("Serve endpoint failed to create workspace "
"root {} that locally was marked absent.",
key.tree_info.tree_hash.Hash()),
/*fatal=*/true);
return;
}
// check if tree is in Git cache;
// ensure Git cache exists
GitOpKey op_key = {
.params =
{
native_storage_config->GitRoot(), // target_path
"", // git_hash
std::nullopt, // message
std::nullopt, // source_path
true // init_bare
},
.op_type = GitOpType::ENSURE_INIT};
critical_git_op_map->ConsumeAfterKeysReady(
ts,
{std::move(op_key)},
[serve,
native_storage_config,
digest,
import_to_git_map,
local_api,
key,
ts,
setter,
logger](auto const& values) {
GitOpValue op_result = *values[0];
// check flag
if (not op_result.result) {
(*logger)("Git cache init failed",
/*fatal=*/true);
return;
}
// Open fake tmp repo to check if tree is known to Git
// cache
auto git_repo = GitRepoRemote::Open(
op_result.git_cas); // link fake repo to odb
if (not git_repo) {
(*logger)(
fmt::format(
"Could not open repository {}",
native_storage_config->GitRoot().string()),
/*fatal=*/true);
return;
}
// setup wrapped logger
auto wrapped_logger =
std::make_shared<AsyncMapConsumerLogger>(
[logger](auto const& msg, bool fatal) {
(*logger)(
fmt::format("While checking tree "
"exists in Git cache:\n{}",
msg),
fatal);
});
// check if the desired tree ID is in Git cache
auto tree_found = git_repo->CheckTreeExists(
key.tree_info.tree_hash.Hash(), wrapped_logger);
if (not tree_found) {
// errors encountered
return;
}
if (*tree_found) {
// upload tree from Git cache to remote CAS and tell
// serve to set up the root from the remote CAS
// tree, then set root as absent
UploadToServeAndSetRoot(*serve,
native_storage_config,
digest,
key.ignore_special,
setter,
logger);
// done!
return;
}
// check if tree is known to local CAS
if (local_api->IsAvailable(digest)) {
// Move tree locally from CAS to Git cache, then
// continue processing it by UploadToServeAndSetRoot
MoveCASTreeToGitAndProcess(*serve,
native_storage_config,
digest,
import_to_git_map,
local_api,
key.ignore_special,
ts,
setter,
logger);
// done!
return;
}
// tree is not know locally, so we cannot
// provide it to the serve endpoint and thus we
// cannot create the absent root
(*logger)(fmt::format("Cannot create workspace root "
"{} as absent for the provided "
"serve endpoint.",
key.tree_info.tree_hash.Hash()),
/*fatal=*/true);
},
[logger, target_path = native_storage_config->GitRoot()](
auto const& msg, bool fatal) {
(*logger)(
fmt::format("While running critical Git op "
"ENSURE_INIT bare for target {}:\n{}",
target_path.string(),
msg),
fatal);
});
// done!
return;
}
// give warning that serve endpoint is missing
(*logger)(fmt::format("Workspace root {} marked absent but no "
"suitable serve endpoint provided.",
key.tree_info.tree_hash.Hash()),
/*fatal=*/false);
// set workspace root as absent
auto root = nlohmann::json::array(
{key.ignore_special ? FileRoot::kGitTreeIgnoreSpecialMarker
: FileRoot::kGitTreeMarker,
key.tree_info.tree_hash.Hash()});
(*setter)(std::pair(std::move(root), false));
return;
}
// if root is not absent, proceed with usual fetch logic: check locally,
// check serve endpoint, check remote-execution endpoint, and lastly
// default to network
git_tree_fetch_map->ConsumeAfterKeysReady(
ts,
{key.tree_info},
[native_storage_config, key, setter](auto const& values) {
// tree is now in Git cache;
// get cache hit info
auto is_cache_hit = *values[0];
// set the workspace root as present
(*setter)(
std::pair(nlohmann::json::array(
{key.ignore_special
? FileRoot::kGitTreeIgnoreSpecialMarker
: FileRoot::kGitTreeMarker,
key.tree_info.tree_hash.Hash(),
native_storage_config->GitRoot().string()}),
is_cache_hit));
},
[logger, hash = key.tree_info.tree_hash.Hash()](auto const& msg,
bool fatal) {
(*logger)(fmt::format(
"While ensuring git-tree {} is in Git cache:\n{}",
hash,
msg),
fatal);
});
};
return AsyncMapConsumer<TreeIdInfo, std::pair<nlohmann::json, bool>>(
tree_to_git, jobs);
}
|