diff options
| author | Stefan Boberg <[email protected]> | 2021-09-23 13:48:34 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-23 13:48:34 +0200 |
| commit | af88b1a47dfeae66a0b2faa1623ff6c59d435e22 (patch) | |
| tree | 0b0814886697589bf0c55ef85cd61e95bbce753e /zenserver/cache/structuredcachestore.cpp | |
| parent | Added HashBuffer(IoBuffer&) overload with trivial (but inappropriate for the ... (diff) | |
| download | zen-af88b1a47dfeae66a0b2faa1623ff6c59d435e22.tar.xz zen-af88b1a47dfeae66a0b2faa1623ff6c59d435e22.zip | |
Added scrubbing logic to ZenCacheDiskLayer/ZenCacheMemoryLayer
This currently only goes through the motions of hashing the data to verify it, but does not perform recovery nor does it validate referential integrity
Diffstat (limited to 'zenserver/cache/structuredcachestore.cpp')
| -rw-r--r-- | zenserver/cache/structuredcachestore.cpp | 201 |
1 files changed, 152 insertions, 49 deletions
diff --git a/zenserver/cache/structuredcachestore.cpp b/zenserver/cache/structuredcachestore.cpp index 3d80bb14c..4f16b81f2 100644 --- a/zenserver/cache/structuredcachestore.cpp +++ b/zenserver/cache/structuredcachestore.cpp @@ -131,23 +131,18 @@ ZenCacheMemoryLayer::~ZenCacheMemoryLayer() bool ZenCacheMemoryLayer::Get(std::string_view InBucket, const IoHash& HashKey, ZenCacheValue& OutValue) { - CacheBucket* Bucket = nullptr; - - { - RwLock::SharedLockScope _(m_Lock); + RwLock::SharedLockScope _(m_Lock); - auto it = m_Buckets.find(std::string(InBucket)); + auto it = m_Buckets.find(std::string(InBucket)); - if (it != m_Buckets.end()) - { - Bucket = &it->second; - } + if (it == m_Buckets.end()) + { + return false; } - if (Bucket == nullptr) - return false; + CacheBucket* Bucket = Bucket = &it->second; - ZEN_ASSERT(Bucket != nullptr); + _.ReleaseNow(); return Bucket->Get(HashKey, OutValue); } @@ -177,8 +172,6 @@ ZenCacheMemoryLayer::Put(std::string_view InBucket, const IoHash& HashKey, const Bucket = &m_Buckets[std::string(InBucket)]; } - ZEN_ASSERT(Bucket != nullptr); - // Note that since the underlying IoBuffer is retained, the content type is also Bucket->Put(HashKey, Value); @@ -195,7 +188,31 @@ ZenCacheMemoryLayer::DropBucket(std::string_view Bucket) void ZenCacheMemoryLayer::Scrub(ScrubContext& Ctx) { - ZEN_UNUSED(Ctx); + RwLock::SharedLockScope _(m_Lock); + + for (auto& Kv : m_Buckets) + { + Kv.second.Scrub(Ctx); + } +} + +void +ZenCacheMemoryLayer::CacheBucket::Scrub(ScrubContext& Ctx) +{ + std::vector<IoHash> BadHashes; + + for (auto& Kv : m_cacheMap) + { + if (Kv.first != IoHash::HashBuffer(Kv.second)) + { + BadHashes.push_back(Kv.first); + } + } + + if (!BadHashes.empty()) + { + Ctx.ReportBadChunks(BadHashes); + } } bool @@ -203,16 +220,16 @@ ZenCacheMemoryLayer::CacheBucket::Get(const IoHash& HashKey, ZenCacheValue& OutV { RwLock::SharedLockScope _(m_bucketLock); - auto bucketIt = m_cacheMap.find(HashKey); - - if (bucketIt == m_cacheMap.end()) + if (auto bucketIt = m_cacheMap.find(HashKey); bucketIt == m_cacheMap.end()) { return false; } + else + { + OutValue.Value = bucketIt->second; - OutValue.Value = bucketIt->second; - - return true; + return true; + } } void @@ -241,8 +258,19 @@ struct DiskLocation static uint64_t CombineOffsetAndFlags(uint64_t Offset, uint64_t Flags) { return Offset | Flags; } - inline uint64_t Offset() const { return OffsetAndFlags & kOffsetMask; } - inline uint64_t IsFlagSet(uint64_t Flag) const { return OffsetAndFlags & Flag; } + inline uint64_t Offset() const { return OffsetAndFlags & kOffsetMask; } + inline uint64_t IsFlagSet(uint64_t Flag) const { return OffsetAndFlags & Flag; } + inline ZenContentType GetContentType() const + { + ZenContentType ContentType = ZenContentType::kBinary; + + if (IsFlagSet(DiskLocation::kStructured)) + { + ContentType = ZenContentType::kCbObject; + } + + return ContentType; + } }; struct DiskIndexEntry @@ -264,9 +292,14 @@ struct ZenCacheDiskLayer::CacheBucket static bool Delete(std::filesystem::path BucketDir); bool Get(const IoHash& HashKey, ZenCacheValue& OutValue); + + bool GetStandaloneCacheValue(const IoHash& HashKey, ZenCacheValue& OutValue, const DiskLocation& Loc); + + bool GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue); void Put(const IoHash& HashKey, const ZenCacheValue& Value); void Drop(); void Flush(); + void Scrub(ScrubContext& Ctx); inline bool IsOk() const { return m_Ok; } @@ -407,6 +440,37 @@ ZenCacheDiskLayer::CacheBucket::BuildPath(WideStringBuilderBase& Path, const IoH } bool +ZenCacheDiskLayer::CacheBucket::GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue) +{ + if (!Loc.IsFlagSet(DiskLocation::kStandaloneFile)) + { + OutValue.Value = IoBufferBuilder::MakeFromFileHandle(m_SobsFile.Handle(), Loc.Offset(), Loc.Size); + OutValue.Value.SetContentType(Loc.GetContentType()); + + return true; + } + + return false; +} + +bool +ZenCacheDiskLayer::CacheBucket::GetStandaloneCacheValue(const IoHash& HashKey, ZenCacheValue& OutValue, const DiskLocation& Loc) +{ + WideStringBuilder<128> DataFilePath; + BuildPath(DataFilePath, HashKey); + + if (IoBuffer Data = IoBufferBuilder::MakeFromFile(DataFilePath.c_str())) + { + OutValue.Value = Data; + OutValue.Value.SetContentType(Loc.GetContentType()); + + return true; + } + + return false; +} + +bool ZenCacheDiskLayer::CacheBucket::Get(const IoHash& HashKey, ZenCacheValue& OutValue) { if (!m_Ok) @@ -420,35 +484,14 @@ ZenCacheDiskLayer::CacheBucket::Get(const IoHash& HashKey, ZenCacheValue& OutVal { const DiskLocation& Loc = it->second; - ZenContentType ContentType = ZenContentType::kBinary; - - if (Loc.IsFlagSet(DiskLocation::kStructured)) - { - ContentType = ZenContentType::kCbObject; - } - - if (!Loc.IsFlagSet(DiskLocation::kStandaloneFile)) + if (GetInlineCacheValue(Loc, OutValue)) { - OutValue.Value = IoBufferBuilder::MakeFromFileHandle(m_SobsFile.Handle(), Loc.Offset(), Loc.Size); - OutValue.Value.SetContentType(ContentType); - return true; } - else - { - _.ReleaseNow(); - WideStringBuilder<128> DataFilePath; - BuildPath(DataFilePath, HashKey); + _.ReleaseNow(); - if (IoBuffer Data = IoBufferBuilder::MakeFromFile(DataFilePath.c_str())) - { - OutValue.Value = Data; - OutValue.Value.SetContentType(ContentType); - - return true; - } - } + return GetStandaloneCacheValue(HashKey, OutValue, Loc); } return false; @@ -518,9 +561,58 @@ ZenCacheDiskLayer::CacheBucket::Flush() } void -ZenCacheDiskLayer::Scrub(ScrubContext& Ctx) +ZenCacheDiskLayer::CacheBucket::Scrub(ScrubContext& Ctx) { - ZEN_UNUSED(Ctx); + std::vector<DiskIndexEntry> StandaloneFiles; + + std::vector<IoHash> BadChunks; + std::vector<IoBuffer> BadStandaloneChunks; + + { + RwLock::SharedLockScope _(m_IndexLock); + + for (auto& Kv : m_Index) + { + const IoHash& Hash = Kv.first; + const DiskLocation& Loc = Kv.second; + + ZenCacheValue Value; + + if (!GetInlineCacheValue(Loc, Value)) + { + ZEN_ASSERT(Loc.IsFlagSet(DiskLocation::kStandaloneFile)); + StandaloneFiles.push_back({.Key = Hash, .Location = Loc}); + } + else + { + if (GetStandaloneCacheValue(Hash, Value, Loc)) + { + // Hash contents + + const IoHash ComputedHash = HashBuffer(Value.Value); + + if (ComputedHash != Hash) + { + BadChunks.push_back(Hash); + } + } + else + { + // Non-existent + } + } + } + } + + if (Ctx.RunRecovery()) + { + // Clean out bad chunks + } + + if (!BadChunks.empty()) + { + Ctx.ReportBadChunks(BadChunks); + } } void @@ -729,6 +821,17 @@ ZenCacheDiskLayer::Flush() } } +void +ZenCacheDiskLayer::Scrub(ScrubContext& Ctx) +{ + RwLock::SharedLockScope _(m_Lock); + + for (auto& Kv : m_Buckets) + { + Kv.second.Scrub(Ctx); + } +} + ////////////////////////////////////////////////////////////////////////// ZenCacheTracker::ZenCacheTracker(ZenCacheStore& CacheStore) |