diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-11-18 17:52:40 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-12-04 18:03:44 +0100 |
commit | ee4634c8e9fe90a7360bf3e272972f0758e7fd3b (patch) | |
tree | 0132300170a64d1d2334757c307ec4c5362de321 /src/buildtool/execution_api/serve/mr_local_api.cpp | |
parent | a8112042b57b7a1be8b37cce46d279fd0ff9e861 (diff) | |
download | justbuild-ee4634c8e9fe90a7360bf3e272972f0758e7fd3b.tar.gz |
Add RehashDigest function
Diffstat (limited to 'src/buildtool/execution_api/serve/mr_local_api.cpp')
-rw-r--r-- | src/buildtool/execution_api/serve/mr_local_api.cpp | 149 |
1 files changed, 10 insertions, 139 deletions
diff --git a/src/buildtool/execution_api/serve/mr_local_api.cpp b/src/buildtool/execution_api/serve/mr_local_api.cpp index 55dddafc..2d80e0ac 100644 --- a/src/buildtool/execution_api/serve/mr_local_api.cpp +++ b/src/buildtool/execution_api/serve/mr_local_api.cpp @@ -15,16 +15,12 @@ #include "src/buildtool/execution_api/serve/mr_local_api.hpp" #include <utility> -#include <variant> #include "src/buildtool/common/protocol_traits.hpp" -#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp" #include "src/buildtool/execution_api/utils/rehash_utils.hpp" -#include "src/buildtool/file_system/object_type.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/logger.hpp" #include "src/buildtool/storage/config.hpp" -#include "src/buildtool/storage/storage.hpp" #include "src/utils/cpp/expected.hpp" MRLocalApi::MRLocalApi( @@ -83,142 +79,17 @@ auto MRLocalApi::RetrieveToCas( return compat_local_api_->RetrieveToCas(artifacts_info, api); } - // in compatible mode: if passed native digests, one must rehash them; - // first, set up needed callbacks for caching digest mappings - auto read_rehashed = [native_storage = native_context_->storage_config, - compat_storage = compat_context_->storage_config]( - ArtifactDigest const& digest) - -> expected<std::optional<Artifact::ObjectInfo>, std::string> { - return RehashUtils::ReadRehashedDigest( - digest, *native_storage, *compat_storage); - }; - auto store_rehashed = - [native_storage = native_context_->storage_config, - compat_storage = compat_context_->storage_config]( - ArtifactDigest const& source_digest, - ArtifactDigest const& target_digest, - ObjectType obj_type) -> std::optional<std::string> { - return RehashUtils::StoreRehashedDigest(source_digest, - target_digest, - obj_type, - *native_storage, - *compat_storage); - }; - - // collect the native blobs and rehash them as compatible to be able to - // check what is missing in the other api - std::vector<Artifact::ObjectInfo> compat_artifacts; - compat_artifacts.reserve(artifacts_info.size()); - for (auto const& native_obj : artifacts_info) { - // check if we know already the compatible digest - auto cached_obj = read_rehashed(native_obj.digest); - if (not cached_obj) { - Logger::Log(LogLevel::Error, - "MRLocalApi: {}", - std::move(cached_obj).error()); - return false; - } - if (*cached_obj) { - // add object to the vector of compatible artifacts - compat_artifacts.emplace_back(std::move(cached_obj)->value()); - } - else { - // process object; trees need to be handled appropriately - if (IsTreeObject(native_obj.type)) { - // set up all the callbacks needed - auto read_git = [cas = &native_context_->storage->CAS()]( - ArtifactDigest const& digest, - ObjectType type) - -> std::optional< - std::variant<std::filesystem::path, std::string>> { - return IsTreeObject(type) - ? cas->TreePath(digest) - : cas->BlobPath(digest, - IsExecutableObject(type)); - }; - auto store_file = - [cas = &compat_context_->storage->CAS()]( - std::variant<std::filesystem::path, std::string> const& - data, - bool is_exec) -> std::optional<ArtifactDigest> { - if (not std::holds_alternative<std::filesystem::path>( - data)) { - return std::nullopt; - } - return cas->StoreBlob(std::get<std::filesystem::path>(data), - is_exec); - }; - BazelMsgFactory::TreeStoreFunc store_dir = - [cas = &compat_context_->storage->CAS()]( - std::string const& content) - -> std::optional<ArtifactDigest> { - return cas->StoreTree(content); - }; - BazelMsgFactory::SymlinkStoreFunc store_symlink = - [cas = &compat_context_->storage->CAS()]( - std::string const& content) - -> std::optional<ArtifactDigest> { - return cas->StoreBlob(content); - }; - // get the directory digest - auto tree_digest = - BazelMsgFactory::CreateDirectoryDigestFromGitTree( - native_obj.digest, - read_git, - store_file, - store_dir, - store_symlink, - read_rehashed, - store_rehashed); - if (not tree_digest) { - Logger::Log(LogLevel::Error, - "MRLocalApi: {}", - std::move(tree_digest).error()); - return false; - } - // add object to the vector of compatible artifacts - compat_artifacts.emplace_back( - Artifact::ObjectInfo{.digest = *std::move(tree_digest), - .type = ObjectType::Tree}); - } - else { - // blobs can be directly rehashed - auto const is_exec = IsExecutableObject(native_obj.type); - auto path = native_context_->storage->CAS().BlobPath( - native_obj.digest, is_exec); - if (not path) { - Logger::Log( - LogLevel::Error, - "MRLocalApi: failed to get path of CAS entry {}", - native_obj.digest.hash()); - return false; - } - auto blob_digest = - compat_context_->storage->CAS().StoreBlob(*path, is_exec); - if (not blob_digest) { - Logger::Log(LogLevel::Error, - "MRLocalApi: failed to rehash CAS entry {}", - native_obj.digest.hash()); - return false; - } - // cache the digest association - if (auto error_msg = store_rehashed( - native_obj.digest, *blob_digest, native_obj.type)) { - Logger::Log(LogLevel::Error, - "MRLocalApi: {}", - *std::move(error_msg)); - return false; - } - // add object to the vector of compatible artifacts - compat_artifacts.emplace_back( - Artifact::ObjectInfo{.digest = *std::move(blob_digest), - .type = native_obj.type}); - } - } + auto compat_artifacts = + RehashUtils::RehashDigest(artifacts_info, + *native_context_->storage_config, + *compat_context_->storage_config); + if (not compat_artifacts) { + Logger::Log(LogLevel::Error, + "MRLocalApi: {}", + std::move(compat_artifacts).error()); + return false; } - // now that we have gathered all the compatible object infos, simply pass - // them to the compatible local api - return compat_local_api_->RetrieveToCas(compat_artifacts, api); + return compat_local_api_->RetrieveToCas(*compat_artifacts, api); } // NOLINTNEXTLINE(google-default-arguments) |