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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zencore/compactbinary.h>
#include <zencore/uid.h>
#include <zencore/xxhash.h>
#include <zenhttp/httpserver.h>
#include <zenstore/gc.h>
ZEN_THIRD_PARTY_INCLUDES_START
#include <tsl/robin_map.h>
ZEN_THIRD_PARTY_INCLUDES_END
#include <map>
#include <unordered_map>
namespace zen {
class CbPackage;
class CidStore;
class AuthMgr;
class ScrubContext;
class JobQueue;
class OpenProcessCache;
enum class HttpResponseCode;
struct OplogEntry
{
uint32_t OpLsn;
uint32_t OpCoreOffset; // note: Multiple of alignment!
uint32_t OpCoreSize;
uint32_t OpCoreHash; // Used as checksum
Oid OpKeyHash;
uint32_t Reserved;
inline bool IsTombstone() const { return OpCoreOffset == 0 && OpCoreSize == 0 && OpLsn == 0; }
inline void MakeTombstone() { OpLsn = OpCoreOffset = OpCoreSize = OpCoreHash = Reserved = 0; }
};
struct OplogEntryAddress
{
uint64_t Offset;
uint64_t Size;
};
static_assert(IsPow2(sizeof(OplogEntry)));
/** Project Store
A project store consists of a number of Projects.
Each project contains a number of oplogs (short for "operation log"). UE uses
one oplog per target platform to store the output of the cook process.
An oplog consists of a sequence of "op" entries. Each entry is a structured object
containing references to attachments. Attachments are typically the serialized
package data split into separate chunks for bulk data, exports and header
information.
*/
class ProjectStore : public RefCounted, public GcStorage, public GcReferencer, public GcReferenceLocker
{
struct OplogStorage;
public:
struct Configuration
{
};
typedef std::function<CidStore&(std::string_view Context)> GetCidStoreFunc;
ProjectStore(GetCidStoreFunc&& GetCidStore,
std::filesystem::path BasePath,
GcManager& Gc,
JobQueue& JobQueue,
OpenProcessCache& InOpenProcessCache,
const Configuration& Config);
~ProjectStore();
struct Project;
struct Oplog
{
Oplog(std::string_view Id,
Project* Project,
CidStore& Store,
std::filesystem::path BasePath,
const std::filesystem::path& MarkerPath);
~Oplog();
[[nodiscard]] static bool ExistsAt(const std::filesystem::path& BasePath);
bool Exists() const;
void Read();
void Write();
void Update(const std::filesystem::path& MarkerPath);
bool Reset();
struct ChunkInfo
{
Oid ChunkId;
uint64_t ChunkSize;
};
struct Paging
{
int32_t Start = -1;
int32_t Count = -1;
};
std::vector<ChunkInfo> GetAllChunksInfo();
void IterateChunkMap(std::function<void(const Oid&, const IoHash& Hash)>&& Fn);
void IterateFileMap(std::function<void(const Oid&, const std::string_view& ServerPath, const std::string_view& ClientPath)>&& Fn);
void IterateOplog(std::function<void(CbObjectView)>&& Fn, const Paging& EntryPaging);
void IterateOplogWithKey(std::function<void(uint32_t, const Oid&, CbObjectView)>&& Fn);
void IterateOplogWithKey(std::function<void(uint32_t, const Oid&, CbObjectView)>&& Fn, const Paging& EntryPaging);
void IterateOplogLocked(std::function<void(CbObjectView)>&& Fn, const Paging& EntryPaging);
size_t GetOplogEntryCount() const;
std::optional<CbObject> GetOpByKey(const Oid& Key);
std::optional<CbObject> GetOpByIndex(uint32_t Index);
std::optional<uint32_t> GetOpIndexByKey(const Oid& Key);
IoBuffer FindChunk(const Oid& ChunkId, uint64_t* OptOutModificationTag);
IoBuffer GetChunkByRawHash(const IoHash& RawHash);
bool IterateChunks(std::span<IoHash> RawHashes,
bool IncludeModTag,
const std::function<bool(size_t Index, const IoBuffer& Payload, uint64_t ModTag)>& AsyncCallback,
WorkerThreadPool* OptionalWorkerPool,
uint64_t LargeSizeLimit);
bool IterateChunks(std::span<Oid> ChunkIds,
bool IncludeModTag,
const std::function<bool(size_t Index, const IoBuffer& Payload, uint64_t ModTag)>& AsyncCallback,
WorkerThreadPool* OptionalWorkerPool,
uint64_t LargeSizeLimit);
inline static const uint32_t kInvalidOp = ~0u;
/** Persist a new oplog entry
*
* Returns the oplog LSN assigned to the new entry, or kInvalidOp if the entry is rejected
*/
uint32_t AppendNewOplogEntry(CbPackage Op);
uint32_t AppendNewOplogEntry(CbObjectView Core);
std::vector<uint32_t> AppendNewOplogEntries(std::span<CbObjectView> Cores);
enum UpdateType
{
kUpdateNewEntry,
kUpdateReplay
};
const std::string& OplogId() const { return m_OplogId; }
const std::filesystem::path& TempPath() const { return m_TempPath; }
const std::filesystem::path& MarkerPath() const { return m_MarkerPath; }
LoggerRef Log() { return m_OuterProject->Log(); }
void Flush();
void Scrub(ScrubContext& Ctx);
static uint64_t TotalSize(const std::filesystem::path& BasePath);
uint64_t TotalSize() const;
std::size_t OplogCount() const
{
RwLock::SharedLockScope _(m_OplogLock);
return m_LatestOpMap.size();
}
void ResetState();
bool PrepareForDelete(std::filesystem::path& OutRemoveDirectory);
void AddChunkMappings(const std::unordered_map<Oid, IoHash, Oid::Hasher>& ChunkMappings);
void EnableUpdateCapture();
void DisableUpdateCapture();
void CaptureAddedAttachments(std::span<const IoHash> AttachmentHashes);
std::vector<IoHash> GetCapturedAttachmentsLocked();
std::vector<IoHash> CheckPendingChunkReferences(std::span<const IoHash> ChunkHashes, const GcClock::Duration& RetainTime);
void RemovePendingChunkReferences(std::span<const IoHash> ChunkHashes);
std::vector<IoHash> GetPendingChunkReferencesLocked();
RwLock::SharedLockScope GetGcReferencerLock() { return RwLock::SharedLockScope(m_OplogLock); }
uint32_t GetUnusedSpacePercent() const;
void Compact(bool DryRun, bool RetainLSNs, std::string_view LogPrefix);
void GetAttachmentsLocked(std::vector<IoHash>& OutAttachments, bool StoreMetaDataOnDisk);
Project* GetOuterProject() const { return m_OuterProject; }
void CompactIfUnusedExceeds(bool DryRun, uint32_t CompactUnusedThreshold, std::string_view LogPrefix);
static std::optional<CbObject> ReadStateFile(const std::filesystem::path& BasePath, std::function<LoggerRef()>&& Log);
struct ChunkMapping
{
Oid Id;
IoHash Hash;
};
struct FileMapping
{
Oid Id;
IoHash Hash; // This is either zero or a cid
std::string ServerPath; // If Hash is valid then this should be empty
std::string ClientPath;
};
struct ValidationResult
{
uint32_t OpCount = 0;
uint32_t LSNLow = 0;
uint32_t LSNHigh = 0;
std::vector<std::pair<Oid, FileMapping>> MissingFiles;
std::vector<std::pair<Oid, ChunkMapping>> MissingChunks;
std::vector<std::pair<Oid, ChunkMapping>> MissingMetas;
std::vector<std::pair<Oid, IoHash>> MissingAttachments;
std::vector<std::pair<Oid, std::string>> OpKeys;
bool IsEmpty() const
{
return MissingFiles.empty() && MissingChunks.empty() && MissingMetas.empty() && MissingAttachments.empty();
}
};
ValidationResult Validate(std::atomic_bool& IsCancelledFlag, WorkerThreadPool* OptionalWorkerPool);
private:
struct FileMapEntry
{
std::string ServerPath;
std::string ClientPath;
};
template<class V>
using OidMap = tsl::robin_map<Oid, V, Oid::Hasher>;
Project* m_OuterProject = nullptr;
const std::string m_OplogId;
CidStore& m_CidStore;
const std::filesystem::path m_BasePath;
std::filesystem::path m_MarkerPath;
std::filesystem::path m_TempPath;
std::filesystem::path m_MetaPath;
mutable RwLock m_OplogLock;
OidMap<IoHash> m_ChunkMap; // output data chunk id -> CAS address
OidMap<IoHash> m_MetaMap; // meta chunk id -> CAS address
OidMap<FileMapEntry> m_FileMap; // file id -> file map entry
int32_t m_ManifestVersion; // File system manifest version
tsl::robin_map<uint32_t, OplogEntryAddress> m_OpAddressMap; // Index LSN -> op data in ops blob file
OidMap<uint32_t> m_LatestOpMap; // op key -> latest op LSN for key
std::atomic<bool> m_MetaValid = false;
uint32_t m_UpdateCaptureRefCounter = 0;
std::unique_ptr<std::vector<uint32_t>> m_CapturedLSNs;
std::unique_ptr<std::vector<IoHash>> m_CapturedAttachments;
std::unordered_set<IoHash, IoHash::Hasher> m_PendingPrepOpAttachments;
GcClock::TimePoint m_PendingPrepOpAttachmentsRetainEnd;
RefPtr<OplogStorage> m_Storage;
uint64_t m_LogFlushPosition = 0;
RefPtr<OplogStorage> GetStorage();
/** Scan oplog and register each entry, thus updating the in-memory tracking tables
*/
uint32_t GetUnusedSpacePercentLocked() const;
void WriteIndexSnapshot();
void ReadIndexSnapshot();
struct OplogEntryMapping
{
std::vector<ChunkMapping> Chunks;
std::vector<ChunkMapping> Meta;
std::vector<FileMapping> Files;
};
OplogEntryMapping GetMapping(CbObjectView Core);
/** Update tracking metadata for a new oplog entry
*
* This is used during replay (and gets called as part of new op append)
*
* Returns the oplog LSN assigned to the new entry, or kInvalidOp if the entry is rejected
*/
uint32_t RegisterOplogEntry(RwLock::ExclusiveLockScope& OplogLock, const OplogEntryMapping& OpMapping, const OplogEntry& OpEntry);
void AddFileMapping(const RwLock::ExclusiveLockScope& OplogLock,
const Oid& FileId,
const IoHash& Hash,
std::string_view ServerPath,
std::string_view ClientPath);
void AddChunkMapping(const RwLock::ExclusiveLockScope& OplogLock, const Oid& ChunkId, const IoHash& Hash);
void AddMetaMapping(const RwLock::ExclusiveLockScope& OplogLock, const Oid& ChunkId, const IoHash& Hash);
void Compact(RwLock::ExclusiveLockScope& Lock, bool DryRun, bool RetainLSNs, std::string_view LogPrefix);
void IterateCapturedLSNsLocked(std::function<bool(const CbObjectView& UpdateOp)>&& Callback);
friend class ProjectStoreOplogReferenceChecker;
friend class ProjectStoreReferenceChecker;
friend class ProjectStoreOplogReferenceValidator;
};
struct Project : public RefCounted
{
std::string Identifier;
std::filesystem::path RootDir;
std::filesystem::path EngineRootDir;
std::filesystem::path ProjectRootDir;
std::filesystem::path ProjectFilePath;
Oplog* NewOplog(std::string_view OplogId, const std::filesystem::path& MarkerPath);
Oplog* OpenOplog(std::string_view OplogId, bool AllowCompact, bool VerifyPathOnDisk);
bool DeleteOplog(std::string_view OplogId);
bool RemoveOplog(std::string_view OplogId, std::filesystem::path& OutDeletePath);
void IterateOplogs(std::function<void(const RwLock::SharedLockScope&, const Oplog&)>&& Fn) const;
void IterateOplogs(std::function<void(const RwLock::SharedLockScope&, Oplog&)>&& Fn);
std::vector<std::string> ScanForOplogs() const;
bool IsExpired(const GcClock::TimePoint ExpireTime) const;
bool IsExpired(const GcClock::TimePoint ExpireTime, const ProjectStore::Oplog& Oplog) const;
bool IsExpired(const GcClock::TimePoint ExpireTime, std::string_view OplogId) const;
bool IsOplogTouchedSince(const GcClock::TimePoint TouchTime, std::string_view Oplog) const;
void TouchProject();
void TouchOplog(std::string_view Oplog);
GcClock::TimePoint LastOplogAccessTime(std::string_view Oplog) const;
Project(ProjectStore* PrjStore, CidStore& Store, std::filesystem::path BasePath);
virtual ~Project();
CidStore& GetCidStore() { return m_CidStore; };
void Read();
void Write();
[[nodiscard]] static bool Exists(const std::filesystem::path& BasePath);
void Flush();
void Scrub(ScrubContext& Ctx);
LoggerRef Log() const;
static uint64_t TotalSize(const std::filesystem::path& BasePath);
uint64_t TotalSize() const;
bool PrepareForDelete(std::filesystem::path& OutDeletePath);
void EnableUpdateCapture();
void DisableUpdateCapture();
std::vector<std::string> GetCapturedOplogsLocked();
std::vector<RwLock::SharedLockScope> GetGcReferencerLocks();
void AddOplogToCompact(std::string_view OplogId)
{
m_OplogsToCompactLock.WithExclusiveLock([&]() { m_OplogsToCompact.insert(std::string(OplogId)); });
}
std::vector<std::string> GetOplogsToCompact()
{
std::vector<std::string> Result;
m_OplogsToCompactLock.WithExclusiveLock([&]() {
Result.reserve(m_OplogsToCompact.size());
Result.insert(Result.end(), m_OplogsToCompact.begin(), m_OplogsToCompact.end());
m_OplogsToCompact.clear();
});
return Result;
}
private:
ProjectStore* m_ProjectStore;
CidStore& m_CidStore;
mutable RwLock m_ProjectLock;
std::map<std::string, std::unique_ptr<Oplog>> m_Oplogs;
std::vector<std::unique_ptr<Oplog>> m_DeletedOplogs;
std::filesystem::path m_OplogStoragePath;
mutable RwLock m_LastAccessTimesLock;
mutable tsl::robin_map<std::string, GcClock::Tick> m_LastAccessTimes;
uint32_t m_UpdateCaptureRefCounter = 0;
std::unique_ptr<std::vector<std::string>> m_CapturedOplogs;
RwLock m_OplogsToCompactLock;
std::unordered_set<std::string> m_OplogsToCompact;
std::filesystem::path BasePathForOplog(std::string_view OplogId) const;
bool IsExpired(const std::string& EntryName, const std::filesystem::path& MarkerPath, const GcClock::TimePoint ExpireTime) const;
void WriteAccessTimes();
void ReadAccessTimes();
friend class ProjectStoreOplogReferenceChecker;
friend class ProjectStoreReferenceChecker;
friend class ProjectStoreOplogReferenceValidator;
friend class ProjectStoreGcStoreCompactor;
};
Ref<Project> OpenProject(std::string_view ProjectId);
Ref<Project> NewProject(const std::filesystem::path& BasePath,
std::string_view ProjectId,
const std::filesystem::path& RootDir,
const std::filesystem::path& EngineRootDir,
const std::filesystem::path& ProjectRootDir,
const std::filesystem::path& ProjectFilePath);
bool UpdateProject(std::string_view ProjectId,
const std::filesystem::path& RootDir,
const std::filesystem::path& EngineRootDir,
const std::filesystem::path& ProjectRootDir,
const std::filesystem::path& ProjectFilePath);
bool RemoveProject(std::string_view ProjectId, std::filesystem::path& OutDeletePath);
bool DeleteProject(std::string_view ProjectId);
bool Exists(std::string_view ProjectId);
void Flush();
void DiscoverProjects();
void IterateProjects(std::function<void(Project& Prj)>&& Fn);
LoggerRef Log() { return m_Log; }
const std::filesystem::path& BasePath() const { return m_ProjectBasePath; }
// GcStorage
virtual void ScrubStorage(ScrubContext& Ctx) override;
virtual GcStorageSize StorageSize() const override;
virtual std::string GetGcName(GcCtx& Ctx) override;
virtual GcStoreCompactor* RemoveExpiredData(GcCtx& Ctx, GcStats& Stats) override;
virtual std::vector<GcReferenceChecker*> CreateReferenceCheckers(GcCtx& Ctx) override;
virtual std::vector<GcReferenceValidator*> CreateReferenceValidators(GcCtx& Ctx) override;
virtual std::vector<RwLock::SharedLockScope> LockState(GcCtx& Ctx) override;
CbArray GetProjectsList();
std::pair<HttpResponseCode, std::string> GetProjectFiles(const std::string_view ProjectId,
const std::string_view OplogId,
const std::unordered_set<std::string>& WantedFieldNames,
CbObject& OutPayload);
std::pair<HttpResponseCode, std::string> GetProjectChunkInfos(const std::string_view ProjectId,
const std::string_view OplogId,
const std::unordered_set<std::string>& WantedFieldNames,
CbObject& OutPayload);
std::pair<HttpResponseCode, std::string> GetChunkInfo(const std::string_view ProjectId,
const std::string_view OplogId,
const std::string_view ChunkId,
CbObject& OutPayload);
std::pair<HttpResponseCode, std::string> GetChunkRange(const std::string_view ProjectId,
const std::string_view OplogId,
const Oid ChunkId,
uint64_t Offset,
uint64_t Size,
ZenContentType AcceptType,
CompositeBuffer& OutChunk,
ZenContentType& OutContentType,
uint64_t* OptionalInOutModificationTag);
std::pair<HttpResponseCode, std::string> GetChunkRange(const std::string_view ProjectId,
const std::string_view OplogId,
const std::string_view ChunkId,
uint64_t Offset,
uint64_t Size,
ZenContentType AcceptType,
CompositeBuffer& OutChunk,
ZenContentType& OutContentType,
uint64_t* OptionalInOutModificationTag);
std::pair<HttpResponseCode, std::string> GetChunk(const std::string_view ProjectId,
const std::string_view OplogId,
const std::string_view Cid,
IoBuffer& OutChunk,
uint64_t* OptionalInOutModificationTag);
std::pair<HttpResponseCode, std::string> PutChunk(const std::string_view ProjectId,
const std::string_view OplogId,
const std::string_view Cid,
ZenContentType ContentType,
IoBuffer&& Chunk);
std::pair<HttpResponseCode, std::string> WriteOplog(const std::string_view ProjectId,
const std::string_view OplogId,
IoBuffer&& Payload,
CbObject& OutResponse);
std::pair<HttpResponseCode, std::string> ReadOplog(const std::string_view ProjectId,
const std::string_view OplogId,
const HttpServerRequest::QueryParams& Params,
CbObject& OutResponse);
std::pair<HttpResponseCode, std::string> GetChunks(const std::string_view ProjectId,
const std::string_view OplogId,
const CbObject& RequestObject,
CbPackage& OutResponsePackage);
bool Rpc(HttpServerRequest& HttpReq,
const std::string_view ProjectId,
const std::string_view OplogId,
IoBuffer&& Payload,
AuthMgr& AuthManager);
std::pair<HttpResponseCode, std::string> Export(Ref<ProjectStore::Project> Project,
ProjectStore::Oplog& Oplog,
CbObjectView&& Params,
AuthMgr& AuthManager);
std::pair<HttpResponseCode, std::string> Import(ProjectStore::Project& Project,
ProjectStore::Oplog& Oplog,
CbObjectView&& Params,
AuthMgr& AuthManager);
bool AreDiskWritesAllowed() const;
void EnableUpdateCapture();
void DisableUpdateCapture();
std::vector<std::string> GetCapturedProjectsLocked();
private:
LoggerRef m_Log;
GcManager& m_Gc;
GetCidStoreFunc m_GetCidStore;
JobQueue& m_JobQueue;
OpenProcessCache& m_OpenProcessCache;
std::filesystem::path m_ProjectBasePath;
const Configuration m_Config;
mutable RwLock m_ProjectsLock;
std::map<std::string, Ref<Project>> m_Projects;
const DiskWriteBlocker* m_DiskWriteBlocker = nullptr;
uint32_t m_UpdateCaptureRefCounter = 0;
std::unique_ptr<std::vector<std::string>> m_CapturedProjects;
std::filesystem::path BasePathForProject(std::string_view ProjectId);
friend class ProjectStoreGcStoreCompactor;
friend class ProjectStoreOplogReferenceChecker;
friend class ProjectStoreReferenceChecker;
};
Oid OpKeyStringAsOid(std::string_view OpKey);
void prj_forcelink();
} // namespace zen
|