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
|
// 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/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/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;
});
}
} // 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,
std::size_t jobs) -> DistdirGitMap {
auto distdir_to_git = [content_cas_map,
import_to_git_map,
critical_git_op_map](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));
},
[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 {
// fetch 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,
absent = key.absent,
import_to_git_map,
ts,
setter,
logger]([[maybe_unused]] auto const& values) mutable {
// repos are in CAS
// 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,
absent,
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
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), false));
},
[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);
});
},
[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);
}
|