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
|
// 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.
#ifndef INCLUDED_SRC_BUILDTOOL_STORAGE_TARGET_CACHE_HPP
#define INCLUDED_SRC_BUILDTOOL_STORAGE_TARGET_CACHE_HPP
#include <filesystem>
#include <functional>
#include <optional>
#include <utility>
#include <nlohmann/json.hpp>
#include "src/buildtool/common/artifact.hpp"
#include "src/buildtool/file_system/file_storage.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/storage/config.hpp"
#include "src/buildtool/storage/garbage_collector.hpp"
#include "src/buildtool/storage/local_cas.hpp"
#include "src/buildtool/storage/target_cache_entry.hpp"
#include "src/buildtool/storage/target_cache_key.hpp"
#include "src/utils/cpp/gsl.hpp"
#include <gsl/gsl>
/// \brief The high-level target cache for storing export target's data.
/// Supports global uplinking across all generations using the garbage
/// collector. The uplink is automatically performed for every entry that is
/// read and already exists in an older generation.
/// \tparam kDoGlobalUplink Enable global uplinking via garbage collector.
template <bool kDoGlobalUplink>
class TargetCache {
public:
/// Local target cache generation used by GC without global uplink.
using LocalGenerationTC = TargetCache</*kDoGlobalUplink=*/false>;
/// Callback type for downloading known artifacts to local CAS.
using ArtifactDownloader =
std::function<bool(std::vector<Artifact::ObjectInfo> const&)>;
TargetCache(std::shared_ptr<LocalCAS<kDoGlobalUplink>> cas,
std::filesystem::path const& store_path)
: cas_{std::move(cas)}, file_store_{store_path / ComputeShard()} {
if constexpr (kDoGlobalUplink) {
// write backend description (shard) to CAS
[[maybe_unused]] auto id =
cas_->StoreBlob(StorageConfig::ExecutionBackendDescription());
EnsuresAudit(id and ArtifactDigest{*id}.hash() == ComputeShard());
}
}
TargetCache(TargetCache const&) = default;
TargetCache(TargetCache&&) noexcept = default;
auto operator=(TargetCache const&) -> TargetCache& = default;
auto operator=(TargetCache&&) noexcept -> TargetCache& = default;
~TargetCache() noexcept = default;
/// \brief Store new key-entry pair in the target cache.
/// \param key The target-cache key.
/// \param value The target-cache value to store.
/// \param downloader Callback for obtaining known artifacts to local CAS.
/// \returns true on success.
[[nodiscard]] auto Store(
TargetCacheKey const& key,
TargetCacheEntry const& value,
ArtifactDownloader const& downloader) const noexcept -> bool;
/// \brief Read existing entry and object info from the target cache.
/// \param key The target-cache key to read the entry from.
/// \returns Pair of cache entry and its object info on success or nullopt.
[[nodiscard]] auto Read(TargetCacheKey const& key) const noexcept
-> std::optional<std::pair<TargetCacheEntry, Artifact::ObjectInfo>>;
/// \brief Uplink entry from this to latest target cache generation.
/// This function is only available for instances that are used as local GC
/// generations (i.e., disabled global uplink).
/// \tparam kIsLocalGeneration True if this instance is a local generation.
/// \param latest The latest target cache generation.
/// \param key The target-cache key for the entry to uplink.
/// \returns True if entry was successfully uplinked.
template <bool kIsLocalGeneration = not kDoGlobalUplink>
requires(kIsLocalGeneration) [[nodiscard]] auto LocalUplinkEntry(
LocalGenerationTC const& latest,
TargetCacheKey const& key) const noexcept -> bool;
private:
// By default, overwrite existing entries. Unless this is a generation
// (disabled global uplink), then we never want to overwrite any entries.
static constexpr auto kStoreMode =
kDoGlobalUplink ? StoreMode::LastWins : StoreMode::FirstWins;
std::shared_ptr<Logger> logger_{std::make_shared<Logger>("TargetCache")};
gsl::not_null<std::shared_ptr<LocalCAS<kDoGlobalUplink>>> cas_;
FileStorage<ObjectType::File,
kStoreMode,
/*kSetEpochTime=*/false>
file_store_;
[[nodiscard]] static auto ComputeShard() noexcept -> std::string {
return ArtifactDigest::Create<ObjectType::File>(
StorageConfig::ExecutionBackendDescription())
.hash();
}
[[nodiscard]] auto DownloadKnownArtifacts(
TargetCacheEntry const& value,
ArtifactDownloader const& downloader) const noexcept -> bool;
};
#include "src/buildtool/storage/target_cache.tpp"
namespace std {
template <>
struct hash<TargetCacheKey> {
[[nodiscard]] auto operator()(TargetCacheKey const& k) const {
return std::hash<Artifact::ObjectInfo>{}(k.Id());
}
};
} // namespace std
#endif // INCLUDED_SRC_BUILDTOOL_STORAGE_TARGET_CACHE_HPP
|