aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/cache/structuredcachestore.h
blob: 4ab8f90f0e6322722cf66633188d00ca47d00bbf (plain) (blame)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "cachedisklayer.h"
#include "stats/statsreporter.h"

#include <zencore/compactbinary.h>
#include <zencore/iohash.h>
#include <zencore/stats.h>
#include <zenstore/gc.h>
#include <zenutil/cache/cache.h>

#include <atomic>
#include <compare>
#include <filesystem>
#include <string_view>
#include <unordered_map>

namespace zen {

class StatsDaemonClient;

/******************************************************************************

  /$$$$$$$$                        /$$$$$$                   /$$
 |_____ $$                        /$$__  $$                 | $$
	  /$$/  /$$$$$$ /$$$$$$$     | $$  \__/ /$$$$$$  /$$$$$$| $$$$$$$  /$$$$$$
	 /$$/  /$$__  $| $$__  $$    | $$      |____  $$/$$_____| $$__  $$/$$__  $$
	/$$/  | $$$$$$$| $$  \ $$    | $$       /$$$$$$| $$     | $$  \ $| $$$$$$$$
   /$$/   | $$_____| $$  | $$    | $$    $$/$$__  $| $$     | $$  | $| $$_____/
  /$$$$$$$|  $$$$$$| $$  | $$    |  $$$$$$|  $$$$$$|  $$$$$$| $$  | $|  $$$$$$$
 |________/\_______|__/  |__/     \______/ \_______/\_______|__/  |__/\_______/

  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;
class DiskWriteBlocker;
class JobQueue;

/* 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
	{
		ZenCacheDiskLayer::Configuration DiskLayerConfig;
	};
	struct BucketInfo
	{
		ZenCacheDiskLayer::BucketInfo DiskLayerInfo;
	};
	struct Info
	{
		std::filesystem::path	 RootDir;
		Configuration			 Config;
		std::vector<std::string> BucketNames;
		ZenCacheDiskLayer::Info	 DiskLayerInfo;
	};

	struct NamespaceStats
	{
		uint64_t					  HitCount;
		uint64_t					  MissCount;
		uint64_t					  WriteCount;
		metrics::RequestStatsSnapshot PutOps;
		metrics::RequestStatsSnapshot GetOps;
		ZenCacheDiskLayer::DiskStats  DiskStats;
	};

	ZenCacheNamespace(GcManager& Gc, JobQueue& JobQueue, const std::filesystem::path& RootDir, const Configuration& Config);
	~ZenCacheNamespace();

	bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue);
	void Put(std::string_view Bucket, const IoHash& HashKey, const ZenCacheValue& Value, std::span<IoHash> References);

	bool DropBucket(std::string_view Bucket);
	void EnumerateBucketContents(std::string_view																		 Bucket,
								 std::function<void(const IoHash& Key, const CacheValueDetails::ValueDetails& Details)>& Fn) const;

	bool Drop();
	void Flush();

	// 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;

	Configuration			  GetConfig() const { return m_Configuration; }
	Info					  GetInfo() const;
	std::optional<BucketInfo> GetBucketInfo(std::string_view Bucket) const;
	NamespaceStats			  Stats();

	CacheValueDetails::NamespaceDetails GetValueDetails(const std::string_view BucketFilter, const std::string_view ValueFilter) const;

#if ZEN_WITH_TESTS
	void SetAccessTime(std::string_view Bucket, const IoHash& HashKey, GcClock::TimePoint Time);
#endif	// ZEN_WITH_TESTS

private:
	GcManager&			  m_Gc;
	JobQueue&			  m_JobQueue;
	std::filesystem::path m_RootDir;
	Configuration		  m_Configuration;
	ZenCacheDiskLayer	  m_DiskLayer;
	std::atomic<uint64_t> m_HitCount{};
	std::atomic<uint64_t> m_MissCount{};
	std::atomic<uint64_t> m_WriteCount{};
	metrics::RequestStats m_PutOps;
	metrics::RequestStats m_GetOps;
	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 StatsProvider
{
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
	{
		ZenCacheNamespace::Configuration NamespaceConfig;
		bool							 AllowAutomaticCreationOfNamespaces = false;
		struct LogConfig
		{
			bool EnableWriteLog	 = true;
			bool EnableAccessLog = true;
		} Logging;
	};

	struct Info
	{
		std::filesystem::path	 BasePath;
		Configuration			 Config;
		std::vector<std::string> NamespaceNames;
		uint64_t				 DiskEntryCount = 0;
		GcStorageSize			 StorageSize;
	};

	struct NamedNamespaceStats
	{
		std::string						  NamespaceName;
		ZenCacheNamespace::NamespaceStats Stats;
	};

	struct CacheStoreStats
	{
		uint64_t						 HitCount			= 0;
		uint64_t						 MissCount			= 0;
		uint64_t						 WriteCount			= 0;
		uint64_t						 RejectedWriteCount = 0;
		uint64_t						 RejectedReadCount	= 0;
		metrics::RequestStatsSnapshot	 PutOps;
		metrics::RequestStatsSnapshot	 GetOps;
		std::vector<NamedNamespaceStats> NamespaceStats;
	};

	ZenCacheStore(GcManager&				   Gc,
				  JobQueue&					   JobQueue,
				  const std::filesystem::path& BasePath,
				  const Configuration&		   Configuration,
				  const DiskWriteBlocker*	   InDiskWriteBlocker);
	~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,
			 std::span<IoHash>			References);
	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;
	CacheStoreStats Stats(bool IncludeNamespaceStats = true);

	Configuration								 GetConfiguration() const { return m_Configuration; }
	void										 SetLoggingConfig(const Configuration::LogConfig& Loggingconfig);
	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();

	void EnumerateBucketContents(std::string_view																		  Namespace,
								 std::string_view																		  Bucket,
								 std::function<void(const IoHash& Key, const CacheValueDetails::ValueDetails& Details)>&& Fn);

	// StatsProvider
	virtual void ReportMetrics(StatsMetrics& Statsd) override;

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;

	CacheStoreStats									m_LastReportedMetrics;
	const DiskWriteBlocker*							m_DiskWriteBlocker = nullptr;
	mutable RwLock									m_NamespacesLock;
	NamespaceMap									m_Namespaces;
	std::vector<std::unique_ptr<ZenCacheNamespace>> m_DroppedNamespaces;

	GcManager&			  m_Gc;
	JobQueue&			  m_JobQueue;
	std::filesystem::path m_BasePath;
	Configuration		  m_Configuration;
	std::atomic<uint64_t> m_HitCount{};
	std::atomic<uint64_t> m_MissCount{};
	std::atomic<uint64_t> m_WriteCount{};
	std::atomic<uint64_t> m_RejectedWriteCount{};
	std::atomic<uint64_t> m_RejectedReadCount{};
	metrics::RequestStats m_PutOps;
	metrics::RequestStats m_GetOps;

	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;
	std::atomic_bool		   m_WriteLogEnabled;
	std::atomic_bool		   m_AccessLogEnabled;
};

void z$_forcelink();

}  // namespace zen