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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "cachedisklayer.h"
#include "cachememorylayer.h"
#include <zencore/compactbinary.h>
#include <zencore/iohash.h>
#include <zenstore/gc.h>
#include <zenutil/cache/cache.h>
#include <atomic>
#include <compare>
#include <filesystem>
#include <string_view>
#include <unordered_map>
namespace zen {
/******************************************************************************
/$$$$$$$$ /$$$$$$ /$$
|_____ $$ /$$__ $$ | $$
/$$/ /$$$$$$ /$$$$$$$ | $$ \__/ /$$$$$$ /$$$$$$| $$$$$$$ /$$$$$$
/$$/ /$$__ $| $$__ $$ | $$ |____ $$/$$_____| $$__ $$/$$__ $$
/$$/ | $$$$$$$| $$ \ $$ | $$ /$$$$$$| $$ | $$ \ $| $$$$$$$$
/$$/ | $$_____| $$ | $$ | $$ $$/$$__ $| $$ | $$ | $| $$_____/
/$$$$$$$| $$$$$$| $$ | $$ | $$$$$$| $$$$$$| $$$$$$| $$ | $| $$$$$$$
|________/\_______|__/ |__/ \______/ \_______/\_______|__/ |__/\_______/
Cache store for UE5. Restricts keys to "{bucket}/{hash}" pairs where the hash
is 40 (hex) chars in size. Values may be opaque blobs or structured objects
which can in turn contain references to other objects (or blobs). Buckets are
organized in namespaces to enable project isolation.
******************************************************************************/
class WorkerThreadPool;
/* Z$ namespace
A namespace scopes a set of buckets, and would typically be used to isolate
projects from each other.
*/
class ZenCacheNamespace final : public GcStorage, public GcContributor
{
public:
struct Configuration
{
std::filesystem::path RootDir;
uint64_t DiskLayerThreshold = 0;
};
struct BucketInfo
{
ZenCacheDiskLayer::BucketInfo DiskLayerInfo;
ZenCacheMemoryLayer::BucketInfo MemoryLayerInfo;
};
struct Info
{
Configuration Config;
std::vector<std::string> BucketNames;
ZenCacheDiskLayer::Info DiskLayerInfo;
ZenCacheMemoryLayer::Info MemoryLayerInfo;
};
ZenCacheNamespace(GcManager& Gc, const std::filesystem::path& RootDir);
~ZenCacheNamespace();
bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue);
void Put(std::string_view Bucket, const IoHash& HashKey, const ZenCacheValue& Value);
bool Drop();
bool DropBucket(std::string_view Bucket);
void Flush();
uint64_t DiskLayerThreshold() const { return m_DiskLayerSizeThreshold; }
// GcContributor
virtual void GatherReferences(GcContext& GcCtx) override;
// GcStorage
virtual void ScrubStorage(ScrubContext& ScrubCtx) override;
virtual void CollectGarbage(GcContext& GcCtx) override;
virtual GcStorageSize StorageSize() const override;
Info GetInfo() const;
std::optional<BucketInfo> GetBucketInfo(std::string_view Bucket) const;
CacheValueDetails::NamespaceDetails GetValueDetails(const std::string_view BucketFilter, const std::string_view ValueFilter) const;
private:
std::filesystem::path m_RootDir;
ZenCacheMemoryLayer m_MemLayer;
ZenCacheDiskLayer m_DiskLayer;
uint64_t m_DiskLayerSizeThreshold = 1 * 1024;
uint64_t m_LastScrubTime = 0;
ZenCacheNamespace(const ZenCacheNamespace&) = delete;
ZenCacheNamespace& operator=(const ZenCacheNamespace&) = delete;
};
/** Cache store interface
This manages a set of namespaces used for derived data caching purposes.
*/
class ZenCacheStore final : public RefCounted
{
public:
static constexpr std::string_view DefaultNamespace =
"!default!"; // This is intentionally not a valid namespace name and will only be used for mapping when no namespace is given
static constexpr std::string_view NamespaceDiskPrefix = "ns_";
struct Configuration
{
std::filesystem::path BasePath;
bool AllowAutomaticCreationOfNamespaces = false;
bool EnableWriteLog = true;
bool EnableAccessLog = true;
};
struct Info
{
Configuration Config;
std::vector<std::string> NamespaceNames;
uint64_t DiskEntryCount = 0;
uint64_t MemoryEntryCount = 0;
GcStorageSize StorageSize;
};
ZenCacheStore(GcManager& Gc, const Configuration& Configuration);
~ZenCacheStore();
bool Get(const CacheRequestContext& Context,
std::string_view Namespace,
std::string_view Bucket,
const IoHash& HashKey,
ZenCacheValue& OutValue);
void Put(const CacheRequestContext& Context,
std::string_view Namespace,
std::string_view Bucket,
const IoHash& HashKey,
const ZenCacheValue& Value);
bool DropBucket(std::string_view Namespace, std::string_view Bucket);
bool DropNamespace(std::string_view Namespace);
void Flush();
void ScrubStorage(ScrubContext& Ctx);
CacheValueDetails GetValueDetails(const std::string_view NamespaceFilter,
const std::string_view BucketFilter,
const std::string_view ValueFilter) const;
GcStorageSize StorageSize() const;
Info GetInfo() const;
std::optional<ZenCacheNamespace::Info> GetNamespaceInfo(std::string_view Namespace);
std::optional<ZenCacheNamespace::BucketInfo> GetBucketInfo(std::string_view Namespace, std::string_view Bucket);
std::vector<std::string> GetNamespaces();
private:
const ZenCacheNamespace* FindNamespace(std::string_view Namespace) const;
ZenCacheNamespace* GetNamespace(std::string_view Namespace);
void IterateNamespaces(const std::function<void(std::string_view Namespace, ZenCacheNamespace& Store)>& Callback) const;
typedef std::unordered_map<std::string, std::unique_ptr<ZenCacheNamespace>> NamespaceMap;
spdlog::logger& m_Log;
spdlog::logger& Log() { return m_Log; }
mutable RwLock m_NamespacesLock;
NamespaceMap m_Namespaces;
std::vector<std::unique_ptr<ZenCacheNamespace>> m_DroppedNamespaces;
GcManager& m_Gc;
Configuration m_Configuration;
struct AccessLogItem
{
const char* Op;
CacheRequestContext Context;
std::string Namespace;
std::string Bucket;
IoHash HashKey;
ZenCacheValue Value;
};
void LogWorker();
RwLock m_LogQueueLock;
std::vector<AccessLogItem> m_LogQueue;
std::atomic_bool m_ExitLogging;
Event m_LogEvent;
std::thread m_AsyncLoggingThread;
};
void z$_forcelink();
} // namespace zen
|