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
|
// 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/buildtool/execution_api/remote/bazel/bazel_network.hpp"
#include <functional>
#include <utility>
#include "src/buildtool/execution_api/common/message_limits.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/utils/cpp/back_map.hpp"
BazelNetwork::BazelNetwork(
std::string instance_name,
std::string const& host,
Port port,
gsl::not_null<Auth const*> const& auth,
gsl::not_null<RetryConfig const*> const& retry_config,
ExecutionConfiguration const& exec_config,
HashFunction hash_function,
TmpDir::Ptr temp_space) noexcept
: instance_name_{std::move(instance_name)},
capabilities_{std::make_unique<BazelCapabilitiesClient>(host,
port,
auth,
retry_config)},
cas_{std::make_unique<BazelCasClient>(host,
port,
auth,
retry_config,
capabilities_.get(),
std::move(temp_space))},
ac_{std::make_unique<BazelAcClient>(host, port, auth, retry_config)},
exec_{std::make_unique<BazelExecutionClient>(host,
port,
auth,
retry_config)},
exec_config_{exec_config},
hash_function_{hash_function} {}
auto BazelNetwork::IsAvailable(ArtifactDigest const& digest) const noexcept
-> bool {
return cas_->FindMissingBlobs(instance_name_, {digest}).empty();
}
auto BazelNetwork::FindMissingBlobs(
std::unordered_set<ArtifactDigest> const& digests) const noexcept
-> std::unordered_set<ArtifactDigest> {
return cas_->FindMissingBlobs(instance_name_, digests);
}
auto BazelNetwork::SplitBlob(bazel_re::Digest const& blob_digest) const noexcept
-> std::optional<std::vector<bazel_re::Digest>> {
return cas_->SplitBlob(hash_function_, instance_name_, blob_digest);
}
auto BazelNetwork::SpliceBlob(
bazel_re::Digest const& blob_digest,
std::vector<bazel_re::Digest> const& chunk_digests) const noexcept
-> std::optional<bazel_re::Digest> {
return cas_->SpliceBlob(
hash_function_, instance_name_, blob_digest, chunk_digests);
}
auto BazelNetwork::BlobSplitSupport() const noexcept -> bool {
return cas_->BlobSplitSupport(hash_function_, instance_name_);
}
auto BazelNetwork::BlobSpliceSupport() const noexcept -> bool {
return cas_->BlobSpliceSupport(hash_function_, instance_name_);
}
auto BazelNetwork::DoUploadBlobs(
std::unordered_set<ArtifactBlob> blobs) noexcept -> bool {
if (blobs.empty()) {
return true;
}
try {
// First upload all blobs that must use bytestream api because of their
// size:
for (auto it = blobs.begin(); it != blobs.end();) {
if (it->GetContentSize() <= MessageLimits::kMaxGrpcLength) {
++it;
continue;
}
if (not cas_->UpdateSingleBlob(instance_name_, *it)) {
return false;
}
it = blobs.erase(it);
}
// After uploading via stream api, only small blobs that may be uploaded
// using batch are in the container:
return cas_->BatchUpdateBlobs(instance_name_, blobs) == blobs.size();
} catch (...) {
Logger::Log(LogLevel::Warning, "Unknown exception");
return false;
}
}
auto BazelNetwork::UploadBlobs(std::unordered_set<ArtifactBlob>&& blobs,
bool skip_find_missing) noexcept -> bool {
if (not skip_find_missing) {
auto const back_map = BackMap<ArtifactDigest, ArtifactBlob>::Make(
&blobs, [](ArtifactBlob const& blob) { return blob.GetDigest(); });
if (back_map == nullptr) {
return false;
}
// back_map becomes invalid after this call:
blobs = back_map->GetValues(FindMissingBlobs(back_map->GetKeys()));
}
return DoUploadBlobs(std::move(blobs));
}
auto BazelNetwork::ExecuteBazelActionSync(
bazel_re::Digest const& action) noexcept
-> std::optional<BazelExecutionClient::ExecutionOutput> {
auto response =
exec_->Execute(instance_name_, action, exec_config_, true /*wait*/);
if (response.state ==
BazelExecutionClient::ExecutionResponse::State::Ongoing) {
Logger::Log(
LogLevel::Trace, "Waiting for {}", response.execution_handle);
response = exec_->WaitExecution(response.execution_handle);
}
if (response.state !=
BazelExecutionClient::ExecutionResponse::State::Finished or
not response.output) {
Logger::Log(LogLevel::Warning,
"Failed to execute action with execution id {}.",
action.hash());
return std::nullopt;
}
return response.output;
}
auto BazelNetwork::CreateReader() const noexcept -> BazelNetworkReader {
return BazelNetworkReader{instance_name_, cas_.get(), hash_function_};
}
auto BazelNetwork::GetCachedActionResult(
bazel_re::Digest const& action,
std::vector<std::string> const& output_files) const noexcept
-> std::optional<bazel_re::ActionResult> {
return ac_->GetActionResult(
instance_name_, action, false, false, output_files);
}
|