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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
// Copyright 2024 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/computed_roots/evaluate.hpp"
#include <algorithm>
#include <cstddef>
#include <filesystem>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <set>
#include <shared_mutex>
#include <sstream>
#include <utility>
#include <variant>
#include <vector>
#include "fmt/core.h"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/base_maps/entity_name_data.hpp"
#include "src/buildtool/build_engine/expression/configuration.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/build_engine/target_map/configured_target.hpp"
#include "src/buildtool/common/artifact.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/common/artifact_digest_factory.hpp"
#include "src/buildtool/common/cli.hpp"
#include "src/buildtool/common/protocol_traits.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/computed_roots/analyse_and_build.hpp"
#include "src/buildtool/computed_roots/lookup_cache.hpp"
#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/execution_api/common/api_bundle.hpp"
#include "src/buildtool/execution_api/common/execution_api.hpp"
#include "src/buildtool/execution_api/local/config.hpp"
#include "src/buildtool/execution_api/local/context.hpp"
#include "src/buildtool/execution_api/local/local_api.hpp"
#include "src/buildtool/execution_api/utils/rehash_utils.hpp"
#include "src/buildtool/file_system/file_root.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/file_system/git_repo.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/file_system/precomputed_root.hpp"
#include "src/buildtool/graph_traverser/graph_traverser.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/log_sink.hpp"
#include "src/buildtool/logging/log_sink_file.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/main/analyse_context.hpp"
#include "src/buildtool/multithreading/async_map_consumer.hpp"
#include "src/buildtool/multithreading/async_map_utils.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/buildtool/progress_reporting/base_progress_reporter.hpp"
#include "src/buildtool/progress_reporting/progress.hpp"
#include "src/buildtool/storage/storage.hpp"
#include "src/buildtool/tree_structure/tree_structure_cache.hpp"
#include "src/buildtool/tree_structure/tree_structure_utils.hpp"
#include "src/utils/cpp/expected.hpp"
#include "src/utils/cpp/tmp_dir.hpp"
#include "src/utils/cpp/vector.hpp"
namespace {
// Add the description of a computed root to a vector, if the given
// root is computed.
void AddDescriptionIfPrecomputed(
FileRoot const& root,
gsl::not_null<std::vector<PrecomputedRoot>*> croots) {
if (auto desc = root.GetPrecomputedDescription()) {
croots->emplace_back(*std::move(desc));
}
}
// Traverse, starting from a given repository, in order to find the
// computed roots it depends on.
void TraverseRepoForComputedRoots(
std::string const& name,
gsl::not_null<RepositoryConfig*> const& repository_config,
gsl::not_null<std::vector<PrecomputedRoot>*> roots,
gsl::not_null<std::set<std::string>*> seen) {
if (seen->contains(name)) {
return;
}
seen->insert(name);
const auto* info = repository_config->Info(name);
if (info == nullptr) {
Logger::Log(LogLevel::Warning,
"Ignoring unknown repository {} while determining the "
"derived roots to compute",
nlohmann::json(name).dump());
return;
}
AddDescriptionIfPrecomputed(info->workspace_root, roots);
AddDescriptionIfPrecomputed(info->target_root, roots);
AddDescriptionIfPrecomputed(info->rule_root, roots);
AddDescriptionIfPrecomputed(info->expression_root, roots);
for (auto const& [k, v] : info->name_mapping) {
TraverseRepoForComputedRoots(v, repository_config, roots, seen);
}
}
// For a given repository, return the list of computed roots it directly depends
// upon
auto GetRootDeps(std::string const& name,
gsl::not_null<RepositoryConfig*> const& repository_config)
-> std::vector<PrecomputedRoot> {
std::vector<PrecomputedRoot> result{};
std::set<std::string> seen{};
TraverseRepoForComputedRoots(name, repository_config, &result, &seen);
sort_and_deduplicate(&result);
Logger::Log(LogLevel::Debug, [&]() {
std::ostringstream msg{};
msg << "Roots for " << nlohmann::json(name) << ", total of "
<< result.size() << ":";
for (auto const& root : result) {
msg << "\n - " << root.ToString();
}
return msg.str();
});
return result;
}
// For each computed root, we have to determine the git tree identifier; it has
// to be given as string, as this is the format needed to update a repository
// root.
using RootMap = AsyncMapConsumer<PrecomputedRoot, std::string>;
auto WhileHandling(PrecomputedRoot const& root,
AsyncMapConsumerLoggerPtr const& logger)
-> AsyncMapConsumerLoggerPtr {
return std::make_shared<AsyncMapConsumerLogger>(
[root, logger](auto const& msg, auto fatal) {
(*logger)(fmt::format(
"While materializing {}:\n{}", root.ToString(), msg),
fatal);
});
}
void ComputeAndFill(
ComputedRoot const& key,
gsl::not_null<RepositoryConfig*> const& repository_config,
gsl::not_null<const GraphTraverser::CommandLineArguments*> const&
traverser_args,
ServeApi const* serve,
gsl::not_null<const ExecutionContext*> const& context,
gsl::not_null<const StorageConfig*> const& storage_config,
gsl::not_null<std::optional<RehashUtils::Rehasher>*> const& rehash,
gsl::not_null<std::shared_mutex*> const& config_lock,
gsl::not_null<std::mutex*> const& git_lock,
std::size_t jobs,
RootMap::LoggerPtr const& logger,
RootMap::SetterPtr const& setter) {
auto tmpdir = storage_config->CreateTypedTmpDir("computed-root");
if (not tmpdir) {
(*logger)("Failed to create temporary directory", true);
return;
}
auto root_dir = tmpdir->GetPath() / "root";
auto log_file = tmpdir->GetPath() / "log";
auto storage = Storage::Create(storage_config);
auto statistics = Statistics();
auto progress = Progress();
auto reporter = BaseProgressReporter::Reporter([]() {});
BuildMaps::Target::ConfiguredTarget target{
BuildMaps::Base::EntityName{
key.repository, key.target_module, key.target_name},
Configuration{Expression::FromJson(key.config)}};
AnalyseContext analyse_context{.repo_config = repository_config,
.storage = &storage,
.statistics = &statistics,
.progress = &progress,
.serve = serve};
Logger build_logger = Logger(
target.ToString(),
std::vector<LogSinkFactory>{LogSinkFile::CreateFactory(log_file)});
auto root_build_args = *traverser_args;
root_build_args.stage =
StageArguments{.output_dir = root_dir, .remember = true};
root_build_args.rebuild = std::nullopt;
root_build_args.build.print_to_stdout = std::nullopt;
root_build_args.build.print_unique = false;
root_build_args.build.dump_artifacts = std::nullopt;
root_build_args.build.show_runfiles = false;
auto root_exec_context = ExecutionContext{context->repo_config,
context->apis,
context->remote_context,
&statistics,
&progress};
auto cache_lookup =
expected<std::optional<std::string>, std::monostate>(std::nullopt);
{
std::shared_lock computing{*config_lock};
cache_lookup =
LookupCache(target, repository_config, storage, logger, *rehash);
}
if (not cache_lookup) {
// prerequisite failure; fatal logger call already handled by
// LookupCache
return;
}
if (*cache_lookup) {
std::string root = **cache_lookup;
auto tree_present =
GitRepo::IsTreeInRepo(storage_config->GitRoot(), root);
if (not tree_present) {
(*logger)(fmt::format("While checking presence of tree {} in local "
"git repo:\n{}",
root,
std::move(tree_present).error()),
/*fatal=*/true);
return;
}
if (*tree_present) {
Logger::Log(LogLevel::Performance,
"Root {} taken from cache to be {}",
target.ToString(),
root);
auto root_result =
FileRoot::FromGit(storage_config->GitRoot(), root);
if (not root_result) {
(*logger)(fmt::format("Failed to create git root for {}", root),
/*fatal=*/true);
return;
}
{
std::unique_lock setting{*config_lock};
repository_config->SetPrecomputedRoot(PrecomputedRoot{key},
*root_result);
}
(*setter)(std::move(root));
return;
}
}
GraphTraverser traverser{
root_build_args, &root_exec_context, reporter, &build_logger};
std::optional<AnalyseAndBuildResult> build_result{};
{
std::shared_lock computing{*config_lock};
build_result = AnalyseAndBuild(&analyse_context,
traverser,
target,
jobs,
context->apis,
&build_logger);
}
auto log_blob = storage.CAS().StoreBlob(log_file, false);
std::string log_desc{};
if (not log_blob) {
(*logger)(fmt::format("Failed to store log file {} to CAS",
log_file.string()),
false);
log_desc = "???";
}
else {
log_desc = log_blob->hash();
if (not build_result) {
// synchronize the build log to a remote endpoint, if specified
if (not context->apis->local->RetrieveToCas(
{Artifact::ObjectInfo{.digest = *log_blob,
.type = ObjectType::File,
.failed = false}},
*context->apis->remote)) {
(*logger)(fmt::format("Failed to upload build log {} to remote",
log_desc),
false);
}
}
}
if (not build_result) {
(*logger)(fmt::format("Build failed, see {} for details", log_desc),
true);
return;
}
auto result = GitRepo::ImportToGit(
*storage_config, root_dir, "computed root", git_lock);
if (not result) {
(*logger)(std::move(result).error(), /*fatal=*/true);
return;
}
Logger::Log(LogLevel::Performance,
"Root {} evaluated to {}, log {}",
target.ToString(),
*result,
log_desc);
auto root_result = FileRoot::FromGit(storage_config->GitRoot(), *result);
if (not root_result) {
(*logger)(fmt::format("Failed to create git root for {}", *result),
/*fatal=*/true);
return;
}
{
// For setting, we need an exclusiver lock; so get one after we
// dropped the shared one
std::unique_lock setting{*config_lock};
repository_config->SetPrecomputedRoot(PrecomputedRoot{key},
*root_result);
}
(*setter)(*std::move(result));
}
void ComputeTreeStructureAndFill(
TreeStructureRoot const& key,
gsl::not_null<RepositoryConfig*> const& repository_config,
gsl::not_null<StorageConfig const*> const& storage_config,
gsl::not_null<std::shared_mutex*> const& config_lock,
gsl::not_null<std::mutex*> const& git_lock,
RootMap::LoggerPtr const& logger,
RootMap::SetterPtr const& setter) {
// Obtain the file root that the key root is referring to
FileRoot ref_root;
{
std::shared_lock lock{*config_lock};
auto const* root = repository_config->WorkspaceRoot(key.repository);
if (root == nullptr) {
std::invoke(
*logger,
fmt::format("Failed to get referenced repository for {}",
key.ToString()),
/*fatal=*/true);
return;
}
ref_root = *root;
}
// TODO(denisov): absent roots
if (ref_root.IsAbsent()) {
std::invoke(*logger, "Absent roots aren't supported yet", true);
return;
}
auto const hash = ref_root.GetTreeHash();
if (not hash) {
std::invoke(*logger,
fmt::format("Failed to get the hash of the referenced git "
"tree for {}",
key.ToString()),
/*fatal=*/true);
return;
}
auto const digest =
ArtifactDigestFactory::Create(HashFunction::Type::GitSHA1,
*hash,
/*size_unknown=*/0,
/*is_tree=*/true);
if (not digest) {
std::invoke(*logger, digest.error(), /*fatal=*/true);
return;
}
// Create a native storage config if needed
// Since tree structure works with git trees, a native storage is required.
std::optional<StorageConfig> substitution_storage_config;
if (not ProtocolTraits::IsNative(storage_config->hash_function.GetType())) {
auto config = StorageConfig::Builder::Rebuild(*storage_config)
.SetHashType(HashFunction::Type::GitSHA1)
.Build();
if (not config) {
std::invoke(
*logger,
fmt::format("Failed to create a native storage config for {}",
key.ToString()),
/*fatal=*/true);
return;
}
substitution_storage_config.emplace(*config);
}
StorageConfig const& native_storage_config =
substitution_storage_config.has_value()
? substitution_storage_config.value()
: *storage_config;
auto const storage = Storage::Create(&native_storage_config);
LocalExecutionConfig const dummy_exec_config{};
LocalContext const local_context{.exec_config = &dummy_exec_config,
.storage_config = &native_storage_config,
.storage = &storage};
LocalApi const native_local_api(&local_context, /*repo_config=*/nullptr);
TreeStructureCache const tree_structure_cache(&native_storage_config);
std::optional<std::string> resolved_hash;
// Check the result is in cache already:
if (auto const cache_entry = tree_structure_cache.Get(*digest)) {
// Ensure the entry is present in git. If it is in cache, it must be
// present in the CAS, so just import from CAS to git:
auto to_git = TreeStructureUtils::ImportToGit(
*cache_entry, native_local_api, native_storage_config, git_lock);
if (not to_git.has_value()) {
std::invoke(*logger, std::move(to_git).error(), /*fatal=*/true);
return;
}
resolved_hash = cache_entry->hash();
}
if (not resolved_hash) {
// If the tree is not in the storage, it must be added:
if (not storage.CAS().TreePath(*digest).has_value()) {
std::vector<std::filesystem::path> known_repos;
auto const path_to_git_cas = ref_root.GetGitCasRoot();
if (not path_to_git_cas) {
std::invoke(*logger,
fmt::format("Failed to obtain the path to the git "
"cas for {}",
key.ToString()),
true);
return;
}
known_repos.push_back(*path_to_git_cas);
known_repos.push_back(native_storage_config.GitRoot());
auto in_cas = TreeStructureUtils::ExportFromGit(
*digest, known_repos, native_local_api);
if (not in_cas.has_value()) {
std::invoke(*logger, std::move(in_cas).error(), /*fatal=*/true);
return;
}
}
if (storage.CAS().TreePath(*digest).has_value()) {
// Compute tree structure and add it to the cache:
auto const tree_structure = TreeStructureUtils::Compute(
*digest, storage, tree_structure_cache);
if (not tree_structure) {
std::invoke(*logger, tree_structure.error(), /*fatal=*/true);
return;
}
auto to_git = TreeStructureUtils::ImportToGit(*tree_structure,
native_local_api,
native_storage_config,
git_lock);
if (not to_git.has_value()) {
std::invoke(*logger, std::move(to_git).error(), /*fatal=*/true);
return;
}
resolved_hash = tree_structure->hash();
}
}
if (not resolved_hash) {
std::invoke(*logger,
fmt::format("Failed to calculate tree structure for {}",
key.ToString()),
true);
return;
}
auto const root_result =
FileRoot::FromGit(native_storage_config.GitRoot(), *resolved_hash);
if (not root_result) {
(*logger)(
fmt::format("Failed to create git root for {}", *resolved_hash),
/*fatal=*/true);
return;
}
{
// For setting, we need an exclusive lock; so get one after we
// dropped the shared one
std::unique_lock setting{*config_lock};
repository_config->SetPrecomputedRoot(PrecomputedRoot{key},
*root_result);
}
(*setter)(*std::move(resolved_hash));
}
auto FillRoots(
std::size_t jobs,
gsl::not_null<RepositoryConfig*> const& repository_config,
gsl::not_null<const GraphTraverser::CommandLineArguments*> const&
traverser_args,
gsl::not_null<const ExecutionContext*> const& context,
ServeApi const* serve,
gsl::not_null<const StorageConfig*> const& storage_config,
gsl::not_null<std::optional<RehashUtils::Rehasher>*> const& rehash,
gsl::not_null<std::shared_mutex*> const& config_lock,
gsl::not_null<std::mutex*> const& git_lock) -> RootMap {
RootMap::ValueCreator fill_roots = [storage_config,
rehash,
repository_config,
traverser_args,
serve,
context,
config_lock,
git_lock,
jobs](auto /*ts*/,
auto setter,
auto logger,
auto subcaller,
PrecomputedRoot const& key) {
auto annotated_logger = WhileHandling(key, logger);
std::vector<PrecomputedRoot> roots{};
{
std::shared_lock reading{*config_lock};
roots =
GetRootDeps(key.GetReferencedRepository(), repository_config);
}
(*subcaller)(
roots,
[key,
repository_config,
traverser_args,
context,
serve,
storage_config,
config_lock,
rehash,
git_lock,
jobs,
logger = annotated_logger,
setter](auto /*values*/) {
if (auto const computed = key.AsComputed()) {
ComputeAndFill(*computed,
repository_config,
traverser_args,
serve,
context,
storage_config,
rehash,
config_lock,
git_lock,
jobs,
logger,
setter);
}
if (auto const tree_structure = key.AsTreeStructure()) {
ComputeTreeStructureAndFill(*tree_structure,
repository_config,
storage_config,
config_lock,
git_lock,
logger,
setter);
}
},
annotated_logger);
};
return RootMap{fill_roots, jobs};
}
} // namespace
auto EvaluatePrecomputedRoots(
gsl::not_null<RepositoryConfig*> const& repository_config,
std::string const& main_repo,
ServeApi const* serve,
StorageConfig const& storage_config,
GraphTraverser::CommandLineArguments const& traverser_args,
gsl::not_null<const ExecutionContext*> const& context,
std::size_t jobs) -> bool {
auto roots = GetRootDeps(main_repo, repository_config);
if (not roots.empty()) {
Logger::Log(LogLevel::Info,
"Repository {} depends on {} top-level computed roots",
nlohmann::json(main_repo).dump(),
roots.size());
// First, ensure the local git repository is present
if (not FileSystemManager::CreateDirectory(storage_config.GitRoot())) {
Logger::Log(LogLevel::Error,
"Failed to create directory {}",
storage_config.GitRoot().string());
return false;
}
if (not GitRepo::InitAndOpen(storage_config.GitRoot(),
/*is_bare=*/true)) {
Logger::Log(LogLevel::Error,
"Failed to init and open git repository {}",
storage_config.GitRoot().string());
return false;
}
// Prepare rehash-function, if rehashing is required
std::optional<RehashUtils::Rehasher> rehash;
if (not ProtocolTraits::IsNative(
storage_config.hash_function.GetType())) {
auto native_storage_config =
StorageConfig::Builder::Rebuild(storage_config)
.SetHashType(HashFunction::Type::GitSHA1)
.Build();
if (not native_storage_config) {
Logger::Log(
LogLevel::Error,
"Failed to create native storage config for rehashing:\n{}",
std::move(native_storage_config).error());
return false;
}
rehash.emplace(storage_config,
*std::move(native_storage_config),
context->apis);
}
// Our RepositoryConfig is a bit problematic: it cannot be copied, hence
// we have to change in place. Moreover, it is tread-safe for read
// access, but not for writing, so we have to synchronize access out of
// bound.
std::shared_mutex repo_config_access{};
std::mutex git_access{};
auto root_map = FillRoots(jobs,
repository_config,
&traverser_args,
context,
serve,
&storage_config,
&rehash,
&repo_config_access,
&git_access);
bool failed = false;
bool done = false;
{
TaskSystem ts{jobs};
root_map.ConsumeAfterKeysReady(
&ts,
roots,
[&roots, &done](auto values) {
Logger::Log(LogLevel::Progress,
"Computed roots evaluated, {} top level",
roots.size());
Logger::Log(LogLevel::Debug, [&]() {
std::ostringstream msg{};
msg << "Top-level computed roots";
for (int i = 0; i < roots.size(); i++) {
auto const& root = roots[i];
msg << "\n - " << root.ToString()
<< " evaluates to " << *values[i];
}
return msg.str();
});
done = true;
},
[&failed](auto const& msg, bool fatal) {
Logger::Log(
fatal ? LogLevel::Error : LogLevel::Warning,
"While materializing top-level computed roots:\n{}",
msg);
failed = failed or fatal;
}
);
}
if (failed) {
return false;
}
if (not done) {
const std::function k_root_printer =
[](PrecomputedRoot const& x) -> std::string {
return x.ToString();
};
auto cycle_msg = DetectAndReportCycle(
"computed roots", root_map, k_root_printer);
if (cycle_msg) {
Logger::Log(LogLevel::Error, "{}", *cycle_msg);
}
else {
DetectAndReportPending(
"computed roots", root_map, k_root_printer);
}
return false;
}
return true;
}
return true;
}
|