aboutsummaryrefslogtreecommitdiff
path: root/zenserver/cache/cachetracking.cpp
blob: 9119e3122d683ce4e3b33c2f5e33ebafb32b33ed (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright Epic Games, Inc. All Rights Reserved.

#include "cachetracking.h"

#if ZEN_USE_CACHE_TRACKER

#	include <zencore/compactbinarybuilder.h>
#	include <zencore/compactbinaryvalue.h>
#	include <zencore/endian.h>
#	include <zencore/filesystem.h>
#	include <zencore/logging.h>
#	include <zencore/scopeguard.h>
#	include <zencore/string.h>

#	include <zencore/testing.h>
#	include <zencore/testutils.h>

ZEN_THIRD_PARTY_INCLUDES_START
#	pragma comment(lib, "Rpcrt4.lib")	// RocksDB made me do this
#	include <fmt/format.h>
#	include <rocksdb/db.h>
#	include <tsl/robin_map.h>
#	include <tsl/robin_set.h>
#	include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

namespace zen {

namespace rocksdb = ROCKSDB_NAMESPACE;

static constinit auto Epoch = std::chrono::time_point<std::chrono::system_clock>{};

static uint64_t
GetCurrentCacheTimeStamp()
{
	auto	 Duration = std::chrono::system_clock::now() - Epoch;
	uint64_t Millis	  = std::chrono::duration_cast<std::chrono::milliseconds>(Duration).count();

	return Millis;
}

struct CacheAccessSnapshot
{
public:
	void TrackAccess(std::string_view BucketSegment, const IoHash& HashKey)
	{
		BucketTracker* Tracker = GetBucket(std::string(BucketSegment));

		Tracker->Track(HashKey);
	}

	bool SerializeSnapshot(CbObjectWriter& Cbo)
	{
		bool					   Serialized = false;
		RwLock::ExclusiveLockScope _(m_Lock);

		for (const auto& Kv : m_BucketMapping)
		{
			if (m_Buckets[Kv.second]->Size())
			{
				Cbo.BeginArray(Kv.first);
				m_Buckets[Kv.second]->SerializeSnapshotAndClear(Cbo);
				Cbo.EndArray();
				Serialized = true;
			}
		}

		return Serialized;
	}

private:
	struct BucketTracker
	{
		mutable RwLock		   Lock;
		tsl::robin_set<IoHash> AccessedKeys;

		void Track(const IoHash& HashKey)
		{
			if (RwLock::SharedLockScope _(Lock); AccessedKeys.contains(HashKey))
			{
				return;
			}

			RwLock::ExclusiveLockScope _(Lock);

			AccessedKeys.insert(HashKey);
		}

		void SerializeSnapshotAndClear(CbObjectWriter& Cbo)
		{
			RwLock::ExclusiveLockScope _(Lock);

			for (const IoHash& Hash : AccessedKeys)
			{
				Cbo.AddHash(Hash);
			}

			AccessedKeys.clear();
		}

		size_t Size() const
		{
			RwLock::SharedLockScope _(Lock);
			return AccessedKeys.size();
		}
	};

	BucketTracker* GetBucket(const std::string& BucketName)
	{
		RwLock::SharedLockScope _(m_Lock);

		if (auto It = m_BucketMapping.find(BucketName); It == m_BucketMapping.end())
		{
			_.ReleaseNow();

			return AddNewBucket(BucketName);
		}
		else
		{
			return m_Buckets[It->second].get();
		}
	}

	BucketTracker* AddNewBucket(const std::string& BucketName)
	{
		RwLock::ExclusiveLockScope _(m_Lock);

		if (auto It = m_BucketMapping.find(BucketName); It == m_BucketMapping.end())
		{
			const uint32_t BucketIndex = gsl::narrow<uint32_t>(m_Buckets.size());
			m_Buckets.emplace_back(std::make_unique<BucketTracker>());
			m_BucketMapping[BucketName] = BucketIndex;

			return m_Buckets[BucketIndex].get();
		}
		else
		{
			return m_Buckets[It->second].get();
		}
	}

	RwLock										m_Lock;
	std::vector<std::unique_ptr<BucketTracker>> m_Buckets;
	tsl::robin_map<std::string, uint32_t>		m_BucketMapping;
};

struct ZenCacheTracker::Impl
{
	Impl(std::filesystem::path StateDirectory)
	{
		std::filesystem::path StatsDbPath{StateDirectory / ".zdb"};

		std::string RocksdbPath = StatsDbPath.string();

		ZEN_DEBUG("opening tracker db at '{}'", RocksdbPath);

		rocksdb::DB*	   Db = nullptr;
		rocksdb::DBOptions Options;
		Options.create_if_missing = true;

		std::vector<std::string> ExistingColumnFamilies;
		rocksdb::Status			 Status = rocksdb::DB::ListColumnFamilies(Options, RocksdbPath, &ExistingColumnFamilies);

		std::vector<rocksdb::ColumnFamilyDescriptor> ColumnDescriptors;

		if (Status.IsPathNotFound())
		{
			ColumnDescriptors.emplace_back(rocksdb::ColumnFamilyDescriptor{rocksdb::kDefaultColumnFamilyName, {}});
		}
		else if (Status.ok())
		{
			for (const std::string& Column : ExistingColumnFamilies)
			{
				rocksdb::ColumnFamilyDescriptor ColumnFamily;
				ColumnFamily.name = Column;
				ColumnDescriptors.push_back(ColumnFamily);
			}
		}
		else
		{
			throw std::runtime_error(fmt::format("column family iteration failed for '{}': '{}'", RocksdbPath, Status.getState()).c_str());
		}

		Status = rocksdb::DB::Open(Options, RocksdbPath, ColumnDescriptors, &m_RocksDbColumnHandles, &Db);

		if (!Status.ok())
		{
			throw std::runtime_error(fmt::format("database open failed for '{}': '{}'", RocksdbPath, Status.getState()).c_str());
		}

		m_RocksDb.reset(Db);
	}

	~Impl()
	{
		for (auto* Column : m_RocksDbColumnHandles)
		{
			delete Column;
		}

		m_RocksDbColumnHandles.clear();
	}

	struct KeyStruct
	{
		uint64_t TimestampLittleEndian;
	};

	void TrackAccess(std::string_view BucketSegment, const IoHash& HashKey) { m_CurrentSnapshot.TrackAccess(BucketSegment, HashKey); }

	void SaveSnapshot()
	{
		CbObjectWriter Cbo;

		if (m_CurrentSnapshot.SerializeSnapshot(Cbo))
		{
			IoBuffer SnapshotBuffer = Cbo.Save().GetBuffer().AsIoBuffer();

			const KeyStruct Key{.TimestampLittleEndian = ToNetworkOrder(GetCurrentCacheTimeStamp())};
			rocksdb::Slice	KeySlice{(const char*)&Key, sizeof Key};
			rocksdb::Slice	ValueSlice{(char*)SnapshotBuffer.Data(), SnapshotBuffer.Size()};

			rocksdb::WriteOptions Wo;
			m_RocksDb->Put(Wo, KeySlice, ValueSlice);
		}
	}

	void IterateSnapshots(std::function<void(uint64_t TimeStamp, CbObject Snapshot)>&& Callback)
	{
		rocksdb::ManagedSnapshot Snap(m_RocksDb.get());

		rocksdb::ReadOptions Ro;
		Ro.snapshot = Snap.snapshot();

		std::unique_ptr<rocksdb::Iterator> It{m_RocksDb->NewIterator(Ro)};

		const KeyStruct ZeroKey{.TimestampLittleEndian = 0};
		rocksdb::Slice	ZeroKeySlice{(const char*)&ZeroKey, sizeof ZeroKey};

		It->Seek(ZeroKeySlice);

		while (It->Valid())
		{
			rocksdb::Slice KeySlice	  = It->key();
			rocksdb::Slice ValueSlice = It->value();

			if (KeySlice.size() == sizeof(KeyStruct))
			{
				IoBuffer ValueBuffer(IoBuffer::Wrap, ValueSlice.data(), ValueSlice.size());

				CbObject Value = LoadCompactBinaryObject(ValueBuffer);

				uint64_t Key = FromNetworkOrder(*reinterpret_cast<const uint64_t*>(KeySlice.data()));

				Callback(Key, Value);
			}

			It->Next();
		}
	}

	std::unique_ptr<rocksdb::DB>			  m_RocksDb;
	std::vector<rocksdb::ColumnFamilyHandle*> m_RocksDbColumnHandles;
	CacheAccessSnapshot						  m_CurrentSnapshot;
};

ZenCacheTracker::ZenCacheTracker(std::filesystem::path StateDirectory) : m_Impl(new Impl(StateDirectory))
{
}

ZenCacheTracker::~ZenCacheTracker()
{
	delete m_Impl;
}

void
ZenCacheTracker::TrackAccess(std::string_view BucketSegment, const IoHash& HashKey)
{
	m_Impl->TrackAccess(BucketSegment, HashKey);
}

void
ZenCacheTracker::SaveSnapshot()
{
	m_Impl->SaveSnapshot();
}

void
ZenCacheTracker::IterateSnapshots(std::function<void(uint64_t TimeStamp, CbObject Snapshot)>&& Callback)
{
	m_Impl->IterateSnapshots(std::move(Callback));
}

#	if ZEN_WITH_TESTS

TEST_CASE("z$.tracker")
{
	using namespace std::literals;

	const uint64_t t0 = GetCurrentCacheTimeStamp();

	ScopedTemporaryDirectory TempDir;

	ZenCacheTracker Zcs(TempDir.Path());

	tsl::robin_set<IoHash> KeyHashes;

	for (int i = 0; i < 10000; ++i)
	{
		IoHash KeyHash = IoHash::HashBuffer(&i, sizeof i);

		KeyHashes.insert(KeyHash);

		Zcs.TrackAccess("foo"sv, KeyHash);
	}

	for (int i = 0; i < 10000; ++i)
	{
		IoHash KeyHash = IoHash::HashBuffer(&i, sizeof i);

		Zcs.TrackAccess("foo"sv, KeyHash);
	}

	Zcs.SaveSnapshot();

	for (int n = 0; n < 10; ++n)
	{
		for (int i = 0; i < 1000; ++i)
		{
			const int Index	  = i + n * 1000;
			IoHash	  KeyHash = IoHash::HashBuffer(&Index, sizeof Index);

			Zcs.TrackAccess("foo"sv, KeyHash);
		}

		Zcs.SaveSnapshot();
	}

	Zcs.SaveSnapshot();

	const uint64_t t1 = GetCurrentCacheTimeStamp();

	int SnapshotCount = 0;

	Zcs.IterateSnapshots([&](uint64_t TimeStamp, CbObject Snapshot) {
		CHECK(TimeStamp >= t0);
		CHECK(TimeStamp <= t1);

		for (auto& Field : Snapshot)
		{
			CHECK_EQ(Field.GetName(), "foo"sv);

			const CbArray& Array = Field.AsArray();

			for (const auto& Element : Array)
			{
				CHECK(KeyHashes.contains(Element.GetValue().AsHash()));
			}
		}

		++SnapshotCount;
	});

	CHECK_EQ(SnapshotCount, 11);
}

#	endif

void
cachetracker_forcelink()
{
}

}  // namespace zen

#endif	// ZEN_USE_CACHE_TRACKER