// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "cachedisklayer.h" #include "cachememorylayer.h" #include #include #include #include #include #include #include #include #include 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 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 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 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 GetNamespaceInfo(std::string_view Namespace); std::optional GetBucketInfo(std::string_view Namespace, std::string_view Bucket); std::vector GetNamespaces(); private: const ZenCacheNamespace* FindNamespace(std::string_view Namespace) const; ZenCacheNamespace* GetNamespace(std::string_view Namespace); void IterateNamespaces(const std::function& Callback) const; typedef std::unordered_map> NamespaceMap; spdlog::logger& m_Log; spdlog::logger& Log() { return m_Log; } mutable RwLock m_NamespacesLock; NamespaceMap m_Namespaces; std::vector> 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 m_LogQueue; std::atomic_bool m_ExitLogging; Event m_LogEvent; std::thread m_AsyncLoggingThread; }; void z$_forcelink(); } // namespace zen