diff options
author | Sascha Roloff <sascha.roloff@huawei.com> | 2022-12-21 16:24:27 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-01-20 13:57:18 +0100 |
commit | ee549fa1ca1ff4a075c9b2634808525b014818ea (patch) | |
tree | ce709bd351412e73766340439b991a5215e69d1e /src/buildtool/build_engine/target_map/target_cache.cpp | |
parent | 56828d241a618c94f0526b5fd498b7f3ce540230 (diff) | |
download | justbuild-ee549fa1ca1ff4a075c9b2634808525b014818ea.tar.gz |
Refactor target cache key and entry to separate files and targets
Diffstat (limited to 'src/buildtool/build_engine/target_map/target_cache.cpp')
-rw-r--r-- | src/buildtool/build_engine/target_map/target_cache.cpp | 134 |
1 files changed, 16 insertions, 118 deletions
diff --git a/src/buildtool/build_engine/target_map/target_cache.cpp b/src/buildtool/build_engine/target_map/target_cache.cpp index b2f3b209..727b7404 100644 --- a/src/buildtool/build_engine/target_map/target_cache.cpp +++ b/src/buildtool/build_engine/target_map/target_cache.cpp @@ -14,123 +14,20 @@ #include "src/buildtool/build_engine/target_map/target_cache.hpp" -#include <gsl-lite/gsl-lite.hpp> +#include <exception> +#include <vector> -#include "src/buildtool/common/repository_config.hpp" +#include <fmt/core.h> + +#include "src/buildtool/common/artifact_digest.hpp" +#include "src/buildtool/compatibility/native_support.hpp" #include "src/buildtool/execution_api/local/config.hpp" -#include "src/buildtool/execution_api/local/file_storage.hpp" -#include "src/buildtool/execution_api/local/local_cas.hpp" #include "src/buildtool/execution_api/remote/config.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" -#include "src/buildtool/logging/logger.hpp" - -auto TargetCache::Key::Create(BuildMaps::Base::EntityName const& target, - Configuration const& effective_config) noexcept - -> std::optional<TargetCache::Key> { - static auto const& repos = RepositoryConfig::Instance(); - try { - if (auto repo_key = - repos.RepositoryKey(target.GetNamedTarget().repository)) { - // target's repository is content-fixed, we can compute a cache key - auto const& name = target.GetNamedTarget(); - auto target_desc = nlohmann::json{ - {{"repo_key", *repo_key}, - {"target_name", nlohmann::json{name.module, name.name}.dump()}, - {"effective_config", effective_config.ToString()}}}; - static auto const& cas = LocalCAS<ObjectType::File>::Instance(); - if (auto target_key = cas.StoreBlobFromBytes(target_desc.dump(2))) { - return Key{{ArtifactDigest{*target_key}, ObjectType::File}}; - } - } - } catch (std::exception const& ex) { - Logger::Log(LogLevel::Error, - "Creating target cache key failed with:\n{}", - ex.what()); - } - return std::nullopt; -} - -auto TargetCache::Entry::FromTarget( - AnalysedTargetPtr const& target, - std::unordered_map<ArtifactDescription, Artifact::ObjectInfo> const& - replacements) noexcept -> std::optional<TargetCache::Entry> { - auto result = TargetResult{ - target->Artifacts(), target->Provides(), target->RunFiles()}; - if (auto desc = result.ReplaceNonKnownAndToJson(replacements)) { - return Entry{*desc}; - } - return std::nullopt; -} - -auto TargetCache::Entry::ToResult() const noexcept - -> std::optional<TargetResult> { - return TargetResult::FromJson(desc_); -} - -[[nodiscard]] auto ToObjectInfo(nlohmann::json const& json) - -> Artifact::ObjectInfo { - auto const& desc = ArtifactDescription::FromJson(json); - // The assumption is that all artifacts mentioned in a target cache - // entry are KNOWN to the remote side. - gsl_ExpectsAudit(desc and desc->IsKnown()); - auto const& info = desc->ToArtifact().Info(); - gsl_ExpectsAudit(info); - return *info; -} - -[[nodiscard]] auto ScanArtifactMap( - gsl::not_null<std::vector<Artifact::ObjectInfo>*> const& infos, - nlohmann::json const& json) -> bool { - if (not json.is_object()) { - return false; - } - infos->reserve(infos->size() + json.size()); - std::transform(json.begin(), - json.end(), - std::back_inserter(*infos), - [](auto const& item) { return ToObjectInfo(item); }); - return true; -} - -[[nodiscard]] auto ScanProvidesMap( - gsl::not_null<std::vector<Artifact::ObjectInfo>*> const& infos, - nlohmann::json const& json) -> bool { - if (not json.is_object()) { - return false; - } - auto const& nodes = json["nodes"]; - auto const& provided_artifacts = json["provided_artifacts"]; - infos->reserve(infos->size() + provided_artifacts.size()); - std::transform( - provided_artifacts.begin(), - provided_artifacts.end(), - std::back_inserter(*infos), - [&nodes](auto const& item) { - return ToObjectInfo(nodes[item.template get<std::string>()]); - }); - return true; -} - -auto TargetCache::Entry::ToArtifacts( - gsl::not_null<std::vector<Artifact::ObjectInfo>*> const& infos) - const noexcept -> bool { - try { - if (ScanArtifactMap(infos, desc_["artifacts"]) and - ScanArtifactMap(infos, desc_["runfiles"]) and - ScanProvidesMap(infos, desc_["provides"])) { - return true; - } - } catch (std::exception const& ex) { - Logger::Log( - LogLevel::Error, - "Scanning target cache entry for artifacts failed with:\n{}", - ex.what()); - } - return false; -} +#include "src/buildtool/logging/log_level.hpp" -auto TargetCache::Store(Key const& key, Entry const& value) const noexcept - -> bool { +auto TargetCache::Store(TargetCacheKey const& key, + TargetCacheEntry const& value) const noexcept -> bool { // Before a target-cache entry is stored in local CAS, make sure any created // artifact for this target is downloaded from the remote CAS to the local // CAS. @@ -150,8 +47,8 @@ auto TargetCache::Store(Key const& key, Entry const& value) const noexcept return false; } -auto TargetCache::Read(Key const& key) const noexcept - -> std::optional<std::pair<Entry, Artifact::ObjectInfo>> { +auto TargetCache::Read(TargetCacheKey const& key) const noexcept + -> std::optional<std::pair<TargetCacheEntry, Artifact::ObjectInfo>> { auto entry_path = file_store_.GetPath(key.Id().digest.hash()); auto const entry = FileSystemManager::ReadFile(entry_path, ObjectType::File); @@ -165,8 +62,9 @@ auto TargetCache::Read(Key const& key) const noexcept if (auto path = CAS().BlobPath(info->digest)) { if (auto value = FileSystemManager::ReadFile(*path)) { try { - return std::make_pair(Entry{nlohmann::json::parse(*value)}, - std::move(*info)); + return std::make_pair( + TargetCacheEntry{nlohmann::json::parse(*value)}, + std::move(*info)); } catch (std::exception const& ex) { logger_.Emit(LogLevel::Warning, "Parsing entry for key {} failed with:\n{}", @@ -182,8 +80,8 @@ auto TargetCache::Read(Key const& key) const noexcept return std::nullopt; } -auto TargetCache::DownloadKnownArtifacts(Entry const& value) const noexcept - -> bool { +auto TargetCache::DownloadKnownArtifacts( + TargetCacheEntry const& value) const noexcept -> bool { std::vector<Artifact::ObjectInfo> artifacts_info; if (not value.ToArtifacts(&artifacts_info)) { return false; |