aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/cache/cachedisklayer.h
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-10-30 09:32:54 +0100
committerGitHub <[email protected]>2023-10-30 09:32:54 +0100
commit3a6a5855cf36967c6bde31292669bfaf832c6f0b (patch)
tree593e7c21e6840e7ad312207fddc63e1934e19d85 /src/zenserver/cache/cachedisklayer.h
parentset up arch properly when running tests (mac) (#505) (diff)
downloadzen-3a6a5855cf36967c6bde31292669bfaf832c6f0b.tar.xz
zen-3a6a5855cf36967c6bde31292669bfaf832c6f0b.zip
New GC implementation (#459)
- Feature: New garbage collection implementation, still in evaluation mode. Enabled by `--gc-v2` command line option
Diffstat (limited to 'src/zenserver/cache/cachedisklayer.h')
-rw-r--r--src/zenserver/cache/cachedisklayer.h57
1 files changed, 40 insertions, 17 deletions
diff --git a/src/zenserver/cache/cachedisklayer.h b/src/zenserver/cache/cachedisklayer.h
index cc6653e28..d8f51c398 100644
--- a/src/zenserver/cache/cachedisklayer.h
+++ b/src/zenserver/cache/cachedisklayer.h
@@ -151,7 +151,7 @@ public:
uint64_t MemorySize;
};
- explicit ZenCacheDiskLayer(JobQueue& JobQueue, const std::filesystem::path& RootDir, const Configuration& Config);
+ explicit ZenCacheDiskLayer(GcManager& Gc, JobQueue& JobQueue, const std::filesystem::path& RootDir, const Configuration& Config);
~ZenCacheDiskLayer();
bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue);
@@ -174,24 +174,28 @@ public:
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:
/** A cache bucket manages a single directory containing
metadata and data for that bucket
*/
- struct CacheBucket
+ struct CacheBucket : public GcReferencer
{
- CacheBucket(std::string BucketName, const BucketConfiguration& Config);
+ CacheBucket(GcManager& Gc, std::atomic_uint64_t& OuterCacheMemoryUsage, std::string BucketName, const BucketConfiguration& Config);
~CacheBucket();
bool OpenOrCreate(std::filesystem::path BucketDir, bool AllowCreate = true);
- bool Get(const IoHash& HashKey, ZenCacheValue& OutValue, std::atomic_uint64_t& CacheMemoryUsage);
- void Put(const IoHash& HashKey, const ZenCacheValue& Value, std::span<IoHash> References, std::atomic_uint64_t& CacheMemoryUsage);
- void MemCacheTrim(GcClock::TimePoint ExpireTime, std::atomic_uint64_t& CacheMemoryUsage);
- bool Drop(std::atomic_uint64_t& CacheMemoryUsage);
+ bool Get(const IoHash& HashKey, ZenCacheValue& OutValue);
+ void Put(const IoHash& HashKey, const ZenCacheValue& Value, std::span<IoHash> References);
+ void MemCacheTrim(GcClock::TimePoint ExpireTime);
+ bool Drop();
void Flush();
- void ScrubStorage(ScrubContext& Ctx, std::atomic_uint64_t& CacheMemoryUsage);
+ void ScrubStorage(ScrubContext& Ctx);
void GatherReferences(GcContext& GcCtx);
- void CollectGarbage(GcContext& GcCtx, std::atomic_uint64_t& CacheMemoryUsage);
+ void CollectGarbage(GcContext& GcCtx);
inline GcStorageSize StorageSize() const
{
@@ -205,8 +209,13 @@ private:
void EnumerateBucketContents(std::function<void(const IoHash& Key, const CacheValueDetails::ValueDetails& Details)>& Fn) const;
void GetUsageByAccess(GcClock::TimePoint TickStart, GcClock::Duration SectionLength, std::vector<uint64_t>& InOutUsageSlots);
+#if ZEN_WITH_TESTS
+ void SetAccessTime(const IoHash& HashKey, GcClock::TimePoint Time);
+#endif // ZEN_WITH_TESTS
private:
+ GcManager& m_Gc;
+ std::atomic_uint64_t& m_OuterCacheMemoryUsage;
std::string m_BucketName;
std::filesystem::path m_BucketDir;
std::filesystem::path m_BlocksBasePath;
@@ -258,16 +267,13 @@ private:
std::atomic_uint64_t m_StandaloneSize{};
std::atomic_uint64_t m_MemCachedSize{};
+ virtual void RemoveExpiredData(GcCtx& Ctx) override;
+ virtual std::vector<GcReferenceChecker*> CreateReferenceCheckers(GcCtx& Ctx) override;
+
void BuildPath(PathBuilderBase& Path, const IoHash& HashKey) const;
- void PutStandaloneCacheValue(const IoHash& HashKey,
- const ZenCacheValue& Value,
- std::span<IoHash> References,
- std::atomic_uint64_t& CacheMemoryUsage);
+ void PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value, std::span<IoHash> References);
IoBuffer GetStandaloneCacheValue(ZenContentType ContentType, const IoHash& HashKey) const;
- void PutInlineCacheValue(const IoHash& HashKey,
- const ZenCacheValue& Value,
- std::span<IoHash> References,
- std::atomic_uint64_t& CacheMemoryUsage);
+ void PutInlineCacheValue(const IoHash& HashKey, const ZenCacheValue& Value, std::span<IoHash> References);
IoBuffer GetInlineCacheValue(const DiskLocation& Loc) const;
void MakeIndexSnapshot();
uint64_t ReadIndexFile(const std::filesystem::path& IndexPath, uint32_t& OutVersion);
@@ -289,6 +295,7 @@ private:
}
size_t AllocateReferenceEntry(RwLock::ExclusiveLockScope&, const IoHash& Key);
bool LockedGetReferences(std::size_t FirstReferenceIndex, std::vector<IoHash>& OutReferences) const;
+ void ClearReferenceCache();
void CompactState(std::vector<BucketPayload>& TmpPayloads,
std::vector<AccessTime>& TmpAccessTimes,
@@ -297,6 +304,17 @@ private:
IndexMap& TmpIndex,
RwLock::ExclusiveLockScope& IndexLock);
+ void AddMemCacheUsage(uint64_t ValueSize)
+ {
+ m_MemCachedSize.fetch_add(ValueSize, std::memory_order::relaxed);
+ m_OuterCacheMemoryUsage.fetch_add(ValueSize, std::memory_order::relaxed);
+ }
+ void RemoveMemCacheUsage(uint64_t ValueSize)
+ {
+ m_MemCachedSize.fetch_sub(ValueSize, std::memory_order::relaxed);
+ m_OuterCacheMemoryUsage.fetch_sub(ValueSize, std::memory_order::relaxed);
+ }
+
// These locks are here to avoid contention on file creation, therefore it's sufficient
// that we take the same lock for the same hash
//
@@ -305,6 +323,8 @@ private:
// an issue in practice
mutable RwLock m_ShardedLocks[256];
inline RwLock& LockForHash(const IoHash& Hash) const { return m_ShardedLocks[Hash.Hash[19]]; }
+
+ friend class DiskBucketReferenceChecker;
};
inline void TryMemCacheTrim()
@@ -326,6 +346,7 @@ private:
void MemCacheTrim();
void MemCacheTrim(std::vector<CacheBucket*>& Buckets, GcClock::TimePoint ExpireTime);
+ GcManager& m_Gc;
JobQueue& m_JobQueue;
std::filesystem::path m_RootDir;
Configuration m_Configuration;
@@ -338,6 +359,8 @@ private:
ZenCacheDiskLayer(const ZenCacheDiskLayer&) = delete;
ZenCacheDiskLayer& operator=(const ZenCacheDiskLayer&) = delete;
+
+ friend class DiskBucketReferenceChecker;
};
} // namespace zen