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
|
// Copyright 2022 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/distdir_git_map.hpp"
#include <algorithm>
#include "fmt/core.h"
#include "src/buildtool/common/artifact.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/execution_api/common/execution_common.hpp"
#include "src/buildtool/file_system/file_root.hpp"
#include "src/buildtool/file_system/file_storage.hpp"
#include "src/buildtool/file_system/git_repo.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/buildtool/serve_api/remote/serve_api.hpp"
#include "src/buildtool/storage/config.hpp"
#include "src/buildtool/storage/fs_utils.hpp"
#include "src/buildtool/storage/storage.hpp"
#include "src/other_tools/just_mr/progress_reporting/progress.hpp"
#include "src/other_tools/just_mr/progress_reporting/statistics.hpp"
#include "src/other_tools/ops_maps/content_cas_map.hpp"
#include "src/other_tools/ops_maps/critical_git_op_map.hpp"
#include "src/utils/cpp/tmp_dir.hpp"
namespace {
/// \brief Create links from CAS content to distdir tmp directory
[[nodiscard]] auto LinkToCAS(
std::shared_ptr<std::unordered_map<std::string, std::string>> const&
content_list,
std::filesystem::path const& tmp_dir) noexcept -> bool {
auto const& cas = Storage::Instance().CAS();
return std::all_of(content_list->begin(),
content_list->end(),
[&cas, tmp_dir](auto const& kv) {
auto content_path =
cas.BlobPath(ArtifactDigest(kv.second, 0, false),
/*is_executable=*/false);
if (content_path) {
return FileSystemManager::CreateFileHardlink(
*content_path, // from: cas_path/content_id
tmp_dir / kv.first); // to: tmp_dir/name
}
return false;
});
}
void ImportFromCASAndSetRoot(
std::shared_ptr<std::unordered_map<std::string, std::string>> const&
content_list,
std::string const& content_id,
std::filesystem::path const& distdir_tree_id_file,
gsl::not_null<ImportToGitMap*> const& import_to_git_map,
gsl::not_null<TaskSystem*> const& ts,
DistdirGitMap::SetterPtr const& setter,
DistdirGitMap::LoggerPtr const& logger) {
// create the links to CAS
auto tmp_dir = StorageUtils::CreateTypedTmpDir("distdir");
if (not tmp_dir) {
(*logger)(fmt::format("Failed to create tmp path for "
"distdir target {}",
content_id),
/*fatal=*/true);
return;
}
// link content from CAS into tmp dir
if (not LinkToCAS(content_list, tmp_dir->GetPath())) {
(*logger)(
fmt::format("Failed to create links to CAS content!", content_id),
/*fatal=*/true);
return;
}
// do import to git
CommitInfo c_info{tmp_dir->GetPath(), "distdir", content_id};
import_to_git_map->ConsumeAfterKeysReady(
ts,
{std::move(c_info)},
[tmp_dir, // keep tmp_dir alive
distdir_tree_id_file,
setter,
logger](auto const& values) {
// check for errors
if (not values[0]->second) {
(*logger)("Importing to git failed",
/*fatal=*/true);
return;
}
// only the tree is of interest
std::string distdir_tree_id = values[0]->first;
// write to tree id file
if (not StorageUtils::WriteTreeIDFile(distdir_tree_id_file,
distdir_tree_id)) {
(*logger)(fmt::format("Failed to write tree id to file {}",
distdir_tree_id_file.string()),
/*fatal=*/true);
return;
}
// set the workspace root as present
(*setter)(std::pair(
nlohmann::json::array({FileRoot::kGitTreeMarker,
distdir_tree_id,
StorageConfig::GitRoot().string()}),
false /*no cache hit*/));
},
[logger, target_path = tmp_dir->GetPath()](auto const& msg,
bool fatal) {
(*logger)(fmt::format("While importing target {} to git:\n{}",
target_path.string(),
msg),
fatal);
});
}
} // namespace
auto CreateDistdirGitMap(
gsl::not_null<ContentCASMap*> const& content_cas_map,
gsl::not_null<ImportToGitMap*> const& import_to_git_map,
gsl::not_null<CriticalGitOpMap*> const& critical_git_op_map,
bool serve_api_exists,
gsl::not_null<IExecutionApi*> const& local_api,
std::optional<gsl::not_null<IExecutionApi*>> const& remote_api,
std::size_t jobs) -> DistdirGitMap {
auto distdir_to_git = [content_cas_map,
import_to_git_map,
critical_git_op_map,
serve_api_exists,
local_api,
remote_api](auto ts,
auto setter,
auto logger,
auto /* unused */,
auto const& key) {
auto distdir_tree_id_file =
StorageUtils::GetDistdirTreeIDFile(key.content_id);
if (FileSystemManager::Exists(distdir_tree_id_file)) {
// read distdir_tree_id from file tree_id_file
auto distdir_tree_id =
FileSystemManager::ReadFile(distdir_tree_id_file);
if (not distdir_tree_id) {
(*logger)(fmt::format("Failed to read tree id from file {}",
distdir_tree_id_file.string()),
/*fatal=*/true);
return;
}
// ensure Git cache
// define Git operation to be done
GitOpKey op_key = {.params =
{
StorageConfig::GitRoot(), // target_path
"", // git_hash
"", // branch
std::nullopt, // message
true // init_bare
},
.op_type = GitOpType::ENSURE_INIT};
critical_git_op_map->ConsumeAfterKeysReady(
ts,
{std::move(op_key)},
[distdir_tree_id = *distdir_tree_id,
absent = key.absent,
setter,
logger](auto const& values) {
GitOpValue op_result = *values[0];
// check flag
if (not op_result.result) {
(*logger)("Git init failed",
/*fatal=*/true);
return;
}
// subdir is ".", so no need to deal with the Git cache
// set the workspace root
auto root = nlohmann::json::array(
{FileRoot::kGitTreeMarker, distdir_tree_id});
if (not absent) {
root.emplace_back(StorageConfig::GitRoot().string());
}
(*setter)(std::pair(std::move(root), true /*cache hit*/));
},
[logger, target_path = StorageConfig::GitRoot()](
auto const& msg, bool fatal) {
(*logger)(fmt::format("While running critical Git op "
"ENSURE_INIT for target {}:\n{}",
target_path.string(),
msg),
fatal);
});
}
else {
// create in-memory Git tree of distdir content to get the tree id
GitRepo::tree_entries_t entries{};
entries.reserve(key.content_list->size());
for (auto const& kv : *key.content_list) {
// tree_entries_t type expects raw ids
auto raw_id = FromHexString(kv.second);
if (not raw_id) {
(*logger)(fmt::format("While processing distdir {}: "
"Unexpected failure in conversion to "
"raw id of distfile content {}",
key.content_id,
kv.second),
/*is_fatal=*/true);
return;
}
entries[*raw_id].emplace_back(kv.first, ObjectType::File);
}
auto tree = GitRepo::CreateShallowTree(entries);
if (not tree) {
(*logger)(fmt::format("Failed to construct in-memory tree for "
"distdir content {}",
key.content_id),
/*is_fatal=*/true);
return;
}
// get hash from raw_id
auto tree_id = ToHexString(tree->first);
// if pure absent, we simply set the root tree
if (key.absent) {
(*setter)(std::pair(
nlohmann::json::array({FileRoot::kGitTreeMarker, tree_id}),
false /*no cache hit*/));
return;
}
// otherwise, we check first the serve endpoint
if (serve_api_exists) {
if (auto served_tree_id = ServeApi::RetrieveTreeFromDistdir(
key.content_list, /*sync_tree=*/true)) {
// sanity check
if (*served_tree_id != tree_id) {
(*logger)(
fmt::format("Unexpected served tree id for distdir "
"content {}:\nExpected {}, but got {}",
key.content_id,
tree_id,
*served_tree_id),
/*is_fatal=*/true);
return;
}
// get the ditfiles from remote CAS in bulk
std::vector<Artifact::ObjectInfo> objects{};
objects.reserve(key.content_list->size());
for (auto const& kv : *key.content_list) {
objects.emplace_back(Artifact::ObjectInfo{
.digest =
ArtifactDigest{kv.second, 0, /*is_tree=*/false},
.type = ObjectType::File});
}
if (remote_api and
remote_api.value()->RetrieveToCas(objects, local_api)) {
ImportFromCASAndSetRoot(key.content_list,
key.content_id,
distdir_tree_id_file,
import_to_git_map,
ts,
setter,
logger);
// done!
return;
}
// just serve should have made the blobs available in the
// remote CAS, so log this attempt and revert to default
// fetch each blob individually
(*logger)(fmt::format("Tree {} marked as served, but not "
"all distfile blobs found on remote",
*served_tree_id),
/*fatal=*/false);
}
else {
// give warning
(*logger)(
fmt::format("Distdir content {} could not be served",
key.content_id),
/*fatal=*/false);
}
}
else {
// give warning
(*logger)(
fmt::format("Missing serve endpoint for distdir {} marked "
"absent requires slower network fetch.",
key.content_id),
/*fatal=*/false);
}
// revert to fetching the gathered distdir repos into CAS
content_cas_map->ConsumeAfterKeysReady(
ts,
*key.repos_to_fetch,
[distdir_tree_id_file,
content_id = key.content_id,
content_list = key.content_list,
import_to_git_map,
ts,
setter,
logger]([[maybe_unused]] auto const& values) mutable {
// repos are in CAS
ImportFromCASAndSetRoot(content_list,
content_id,
distdir_tree_id_file,
import_to_git_map,
ts,
setter,
logger);
},
[logger, content_id = key.content_id](auto const& msg,
bool fatal) {
(*logger)(fmt::format("While fetching archives for distdir "
"content {}:\n{}",
content_id,
msg),
fatal);
});
}
};
return AsyncMapConsumer<DistdirInfo, std::pair<nlohmann::json, bool>>(
distdir_to_git, jobs);
}
|