From e51a6938e536d8bb7904d7c54eb729925fc08417 Mon Sep 17 00:00:00 2001 From: Sascha Roloff Date: Mon, 29 Aug 2022 11:58:04 +0200 Subject: Introduced RetrieveToCas function in IExecutionApi to synchronize artifacts between different CASes --- .../execution_api/remote/bazel/bazel_api.cpp | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src/buildtool/execution_api/remote/bazel/bazel_api.cpp') diff --git a/src/buildtool/execution_api/remote/bazel/bazel_api.cpp b/src/buildtool/execution_api/remote/bazel/bazel_api.cpp index b07bcede..a8846325 100644 --- a/src/buildtool/execution_api/remote/bazel/bazel_api.cpp +++ b/src/buildtool/execution_api/remote/bazel/bazel_api.cpp @@ -148,6 +148,76 @@ auto BazelApi::CreateAction( return true; } +// NOLINTNEXTLINE(misc-no-recursion) +[[nodiscard]] auto BazelApi::RetrieveToCas( + std::vector const& artifacts_info, + gsl::not_null const& api) noexcept -> bool { + + // Determine missing artifacts in other CAS. + std::vector digests; + digests.reserve(artifacts_info.size()); + std::unordered_map info_map; + for (auto const& info : artifacts_info) { + digests.push_back(info.digest); + info_map[info.digest] = info; + } + auto const& missing_digests = api->IsAvailable(digests); + std::vector missing_artifacts_info; + missing_artifacts_info.reserve(missing_digests.size()); + for (auto const& digest : missing_digests) { + missing_artifacts_info.push_back(info_map[digest]); + } + + // Recursively process trees. + std::vector blob_digests{}; + for (auto const& info : missing_artifacts_info) { + if (IsTreeObject(info.type)) { + auto const infos = + network_->ReadTreeInfos(info.digest, std::filesystem::path{}); + if (not infos or not RetrieveToCas(infos->second, api)) { + return false; + } + } + + // Object infos created by network_->ReadTreeInfos() will contain 0 as + // size, but this is handled by the remote execution engine, so no need + // to regenerate the digest. + blob_digests.push_back(info.digest); + } + + // Fetch blobs from this CAS. + auto size = blob_digests.size(); + auto reader = network_->ReadBlobs(std::move(blob_digests)); + auto blobs = reader.Next(); + std::size_t count{}; + BlobContainer container{}; + while (not blobs.empty()) { + if (count + blobs.size() > size) { + Logger::Log(LogLevel::Error, "received more blobs than requested."); + return false; + } + for (auto& blob : blobs) { + try { + container.Emplace(std::move(blob)); + } catch (std::exception const& ex) { + Logger::Log( + LogLevel::Error, "failed to emplace blob: ", ex.what()); + return false; + } + } + count += blobs.size(); + blobs = reader.Next(); + } + + if (count != size) { + Logger::Log(LogLevel::Error, "could not retrieve all requested blobs."); + return false; + } + + // Upload blobs to other CAS. + return api->Upload(container, /*skip_find_missing=*/true); +} + [[nodiscard]] auto BazelApi::Upload(BlobContainer const& blobs, bool skip_find_missing) noexcept -> bool { return network_->UploadBlobs(blobs, skip_find_missing); -- cgit v1.2.3