aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/cache/cachedisklayer.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-10-03 16:42:57 +0200
committerGitHub Enterprise <[email protected]>2024-10-03 16:42:57 +0200
commitb13b5f48bb497aaf9f9f3d74aceb6e474cf12898 (patch)
tree24b1ed63ece11fb773a0ecf41ce6308969468198 /src/zenstore/cache/cachedisklayer.cpp
parent5.5.9-pre0 (diff)
downloadzen-b13b5f48bb497aaf9f9f3d74aceb6e474cf12898.tar.xz
zen-b13b5f48bb497aaf9f9f3d74aceb6e474cf12898.zip
remove gc v1 (#121)
* kill gc v1 * block use of gc v1 from zen command line * warn and flip to gcv2 if --gc-v2=false is specified for zenserver
Diffstat (limited to 'src/zenstore/cache/cachedisklayer.cpp')
-rw-r--r--src/zenstore/cache/cachedisklayer.cpp504
1 files changed, 0 insertions, 504 deletions
diff --git a/src/zenstore/cache/cachedisklayer.cpp b/src/zenstore/cache/cachedisklayer.cpp
index fed37824a..8a2de34e2 100644
--- a/src/zenstore/cache/cachedisklayer.cpp
+++ b/src/zenstore/cache/cachedisklayer.cpp
@@ -2293,466 +2293,6 @@ ZenCacheDiskLayer::CacheBucket::ScrubStorage(ScrubContext& Ctx)
}
}
-void
-ZenCacheDiskLayer::CacheBucket::GatherReferences(GcContext& GcCtx)
-{
- ZEN_TRACE_CPU("Z$::Bucket::GatherReferences");
-
-#define CALCULATE_BLOCKING_TIME 0
-
-#if CALCULATE_BLOCKING_TIME
- uint64_t WriteBlockTimeUs = 0;
- uint64_t WriteBlockLongestTimeUs = 0;
- uint64_t ReadBlockTimeUs = 0;
- uint64_t ReadBlockLongestTimeUs = 0;
-#endif // CALCULATE_BLOCKING_TIME
-
- Stopwatch TotalTimer;
- const auto _ = MakeGuard([&] {
-#if CALCULATE_BLOCKING_TIME
- ZEN_DEBUG("gathered references from '{}' in {} write lock: {} ({}), read lock: {} ({})",
- m_BucketDir,
- NiceTimeSpanMs(TotalTimer.GetElapsedTimeMs()),
- NiceLatencyNs(WriteBlockTimeUs),
- NiceLatencyNs(WriteBlockLongestTimeUs),
- NiceLatencyNs(ReadBlockTimeUs),
- NiceLatencyNs(ReadBlockLongestTimeUs));
-#else
- ZEN_DEBUG("gathered references from '{}' in {}", m_BucketDir, NiceTimeSpanMs(TotalTimer.GetElapsedTimeMs()));
-#endif // CALCULATE_BLOCKING_TIME
- });
-
- const GcClock::TimePoint ExpireTime = GcCtx.CacheExpireTime();
-
- const GcClock::Tick ExpireTicks = ExpireTime.time_since_epoch().count();
-
- IndexMap Index;
- std::vector<AccessTime> AccessTimes;
- std::vector<BucketPayload> Payloads;
- {
- RwLock::SharedLockScope __(m_IndexLock);
-#if CALCULATE_BLOCKING_TIME
- Stopwatch Timer;
- const auto ___ = MakeGuard([&] {
- uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
- WriteBlockTimeUs += ElapsedUs;
- WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
- });
-#endif // CALCULATE_BLOCKING_TIME
- if (m_Index.empty())
- {
- return;
- }
- Index = m_Index;
- AccessTimes = m_AccessTimes;
- Payloads = m_Payloads;
- }
-
- std::vector<IoHash> ExpiredKeys;
- ExpiredKeys.reserve(1024);
-
- std::vector<IoHash> Cids;
- if (!GcCtx.SkipCid())
- {
- Cids.reserve(1024);
- }
-
- std::vector<std::pair<IoHash, size_t>> StructuredItemsWithUnknownAttachments;
-
- for (const auto& Entry : Index)
- {
- const IoHash& Key = Entry.first;
- size_t PayloadIndex = Entry.second;
- GcClock::Tick AccessTime = AccessTimes[PayloadIndex];
- if (AccessTime < ExpireTicks)
- {
- ExpiredKeys.push_back(Key);
- continue;
- }
-
- if (GcCtx.SkipCid())
- {
- continue;
- }
-
- BucketPayload& Payload = Payloads[PayloadIndex];
- const DiskLocation& Loc = Payload.Location;
-
- if (!Loc.IsFlagSet(DiskLocation::kStructured))
- {
- continue;
- }
- StructuredItemsWithUnknownAttachments.push_back(Entry);
- }
-
- for (const auto& Entry : StructuredItemsWithUnknownAttachments)
- {
- const IoHash& Key = Entry.first;
- BucketPayload& Payload = Payloads[Entry.second];
- const DiskLocation& Loc = Payload.Location;
- {
- IoBuffer Buffer;
- if (Loc.IsFlagSet(DiskLocation::kStandaloneFile))
- {
- if (Buffer = GetStandaloneCacheValue(Loc, Key); !Buffer)
- {
- continue;
- }
- }
- else
- {
- RwLock::SharedLockScope IndexLock(m_IndexLock);
-#if CALCULATE_BLOCKING_TIME
- Stopwatch Timer;
- const auto ___ = MakeGuard([&] {
- uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
- WriteBlockTimeUs += ElapsedUs;
- WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
- });
-#endif // CALCULATE_BLOCKING_TIME
- if (auto It = m_Index.find(Key); It != m_Index.end())
- {
- const BucketPayload& CachedPayload = m_Payloads[It->second];
- if (CachedPayload.MemCached)
- {
- Buffer = m_MemCachedPayloads[CachedPayload.MemCached].Payload;
- ZEN_ASSERT_SLOW(Buffer);
- }
- else
- {
- DiskLocation Location = m_Payloads[It->second].Location;
- IndexLock.ReleaseNow();
- Buffer = GetInlineCacheValue(Location);
- // Don't memcache items when doing GC
- }
- }
- if (!Buffer)
- {
- continue;
- }
- }
-
- ZEN_ASSERT(Buffer);
- ZEN_ASSERT(Buffer.GetContentType() == ZenContentType::kCbObject);
- CbObjectView Obj(Buffer.GetData());
- Obj.IterateAttachments([&Cids](CbFieldView Field) { Cids.push_back(Field.AsAttachment()); });
- if (Cids.size() >= 1024)
- {
- GcCtx.AddRetainedCids(Cids);
- Cids.clear();
- }
- }
- }
-
- GcCtx.AddRetainedCids(Cids);
- GcCtx.SetExpiredCacheKeys(m_BucketDir.string(), std::move(ExpiredKeys));
-}
-
-void
-ZenCacheDiskLayer::CacheBucket::CollectGarbage(GcContext& GcCtx)
-{
- ZEN_TRACE_CPU("Z$::Bucket::CollectGarbage");
-
- ZEN_DEBUG("collecting garbage from '{}'", m_BucketDir);
-
- Stopwatch TotalTimer;
- uint64_t WriteBlockTimeUs = 0;
- uint64_t WriteBlockLongestTimeUs = 0;
- uint64_t ReadBlockTimeUs = 0;
- uint64_t ReadBlockLongestTimeUs = 0;
- uint64_t TotalChunkCount = 0;
- uint64_t DeletedSize = 0;
- GcStorageSize OldTotalSize = StorageSize();
-
- std::unordered_set<IoHash> DeletedChunks;
- uint64_t MovedCount = 0;
-
- const auto _ = MakeGuard([&] {
- ZEN_DEBUG(
- "garbage collect from '{}' DONE after {}, write lock: {} ({}), read lock: {} ({}), collected {} bytes, deleted {} and moved "
- "{} "
- "of {} "
- "entries ({}/{}).",
- m_BucketDir,
- NiceTimeSpanMs(TotalTimer.GetElapsedTimeMs()),
- NiceLatencyNs(WriteBlockTimeUs),
- NiceLatencyNs(WriteBlockLongestTimeUs),
- NiceLatencyNs(ReadBlockTimeUs),
- NiceLatencyNs(ReadBlockLongestTimeUs),
- NiceBytes(DeletedSize),
- DeletedChunks.size(),
- MovedCount,
- TotalChunkCount,
- NiceBytes(OldTotalSize.DiskSize),
- NiceBytes(OldTotalSize.MemorySize));
-
- bool Expected = false;
- if (m_IsFlushing || !m_IsFlushing.compare_exchange_strong(Expected, true))
- {
- return;
- }
- auto FlushingGuard = MakeGuard([&] { m_IsFlushing.store(false); });
-
- try
- {
- SaveSnapshot([&]() { return GcCtx.ClaimGCReserve(); });
- }
- catch (const std::exception& Ex)
- {
- ZEN_WARN("Failed to write index and manifest after GC in '{}'. Reason: '{}'", m_BucketDir, Ex.what());
- }
- });
-
- auto __ = MakeGuard([&]() {
- if (!DeletedChunks.empty())
- {
- // Clean up m_AccessTimes and m_Payloads vectors
- std::vector<BucketPayload> Payloads;
- std::vector<AccessTime> AccessTimes;
- std::vector<BucketMetaData> MetaDatas;
- std::vector<MemCacheData> MemCachedPayloads;
- IndexMap Index;
- {
- RwLock::ExclusiveLockScope IndexLock(m_IndexLock);
- Stopwatch Timer;
- const auto ___ = MakeGuard([&] {
- uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
- WriteBlockTimeUs += ElapsedUs;
- WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
- });
- CompactState(IndexLock, Payloads, AccessTimes, MetaDatas, MemCachedPayloads, Index);
- }
- GcCtx.AddDeletedCids(std::vector<IoHash>(DeletedChunks.begin(), DeletedChunks.end()));
- }
- });
-
- std::span<const IoHash> ExpiredCacheKeySpan = GcCtx.ExpiredCacheKeys(m_BucketDir.string());
- if (ExpiredCacheKeySpan.empty())
- {
- return;
- }
-
- m_SlogFile.Flush();
-
- std::unordered_set<IoHash, IoHash::Hasher> ExpiredCacheKeys(ExpiredCacheKeySpan.begin(), ExpiredCacheKeySpan.end());
-
- std::vector<DiskIndexEntry> ExpiredStandaloneEntries;
- IndexMap IndexSnapshot;
- std::vector<BucketPayload> PayloadsSnapshot;
- BlockStore::ReclaimSnapshotState BlockStoreState;
- {
- bool Expected = false;
- if (m_IsFlushing || !m_IsFlushing.compare_exchange_strong(Expected, true))
- {
- ZEN_DEBUG("garbage collect SKIPPED, for '{}', container is currently flushing", m_BucketDir);
- return;
- }
- auto FlushingGuard = MakeGuard([&] { m_IsFlushing.store(false); });
-
- {
- ZEN_TRACE_CPU("Z$::Bucket::CollectGarbage::State");
- RwLock::SharedLockScope IndexLock(m_IndexLock);
-
- Stopwatch Timer;
- const auto ____ = MakeGuard([&] {
- uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
- WriteBlockTimeUs += ElapsedUs;
- WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
- });
-
- BlockStoreState = m_BlockStore.GetReclaimSnapshotState();
-
- for (const IoHash& Key : ExpiredCacheKeys)
- {
- if (auto It = m_Index.find(Key); It != m_Index.end())
- {
- const BucketPayload& Payload = m_Payloads[It->second];
- if (Payload.Location.Flags & DiskLocation::kStandaloneFile)
- {
- DiskIndexEntry Entry = {.Key = Key, .Location = Payload.Location};
- Entry.Location.Flags |= DiskLocation::kTombStone;
- ExpiredStandaloneEntries.push_back(Entry);
- }
- }
- }
-
- PayloadsSnapshot = m_Payloads;
- IndexSnapshot = m_Index;
-
- if (GcCtx.IsDeletionMode())
- {
- IndexLock.ReleaseNow();
- RwLock::ExclusiveLockScope __(m_IndexLock);
- for (const auto& Entry : ExpiredStandaloneEntries)
- {
- if (m_Index.erase(Entry.Key) == 1)
- {
- m_StandaloneSize.fetch_sub(Entry.Location.Size(), std::memory_order::relaxed);
- DeletedChunks.insert(Entry.Key);
- }
- }
- m_SlogFile.Append(ExpiredStandaloneEntries);
- }
- }
- }
-
- if (GcCtx.IsDeletionMode())
- {
- ZEN_TRACE_CPU("Z$::Bucket::CollectGarbage::Delete");
-
- ExtendablePathBuilder<256> Path;
-
- for (const auto& Entry : ExpiredStandaloneEntries)
- {
- const IoHash& Key = Entry.Key;
-
- Path.Reset();
- BuildPath(Path, Key);
- fs::path FilePath = Path.ToPath();
-
- {
- RwLock::SharedLockScope IndexLock(m_IndexLock);
- Stopwatch Timer;
- const auto ____ = MakeGuard([&] {
- uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
- WriteBlockTimeUs += ElapsedUs;
- WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
- });
- if (m_Index.contains(Key))
- {
- // Someone added it back, let the file on disk be
- ZEN_DEBUG("skipping z$ delete standalone of file '{}' FAILED, it has been added back", Path.ToUtf8());
- continue;
- }
- IndexLock.ReleaseNow();
-
- RwLock::ExclusiveLockScope ValueLock(LockForHash(Key));
- if (fs::is_regular_file(FilePath))
- {
- ZEN_DEBUG("deleting standalone cache file '{}'", Path.ToUtf8());
- std::error_code Ec;
- fs::remove(FilePath, Ec);
- if (Ec)
- {
- ZEN_WARN("delete expired z$ standalone file '{}' FAILED, reason: '{}'", Path.ToUtf8(), Ec.message());
- continue;
- }
- }
- }
- DeletedSize += Entry.Location.Size();
- }
- }
-
- TotalChunkCount = IndexSnapshot.size();
-
- std::vector<BlockStoreLocation> ChunkLocations;
- BlockStore::ChunkIndexArray KeepChunkIndexes;
- std::vector<IoHash> ChunkIndexToChunkHash;
- ChunkLocations.reserve(TotalChunkCount);
- ChunkLocations.reserve(TotalChunkCount);
- ChunkIndexToChunkHash.reserve(TotalChunkCount);
- {
- TotalChunkCount = 0;
- for (const auto& Entry : IndexSnapshot)
- {
- size_t EntryIndex = Entry.second;
- const DiskLocation& DiskLocation = PayloadsSnapshot[EntryIndex].Location;
-
- if (DiskLocation.Flags & DiskLocation::kStandaloneFile)
- {
- continue;
- }
- const IoHash& Key = Entry.first;
- BlockStoreLocation Location = DiskLocation.GetBlockLocation(m_Configuration.PayloadAlignment);
- size_t ChunkIndex = ChunkLocations.size();
- ChunkLocations.push_back(Location);
- ChunkIndexToChunkHash.push_back(Key);
- if (ExpiredCacheKeys.contains(Key))
- {
- continue;
- }
- KeepChunkIndexes.push_back(ChunkIndex);
- }
- }
- TotalChunkCount = ChunkLocations.size();
- size_t DeleteCount = TotalChunkCount - KeepChunkIndexes.size();
-
- const bool PerformDelete = GcCtx.IsDeletionMode() && GcCtx.CollectSmallObjects();
- if (!PerformDelete)
- {
- m_BlockStore.ReclaimSpace(BlockStoreState, ChunkLocations, KeepChunkIndexes, m_Configuration.PayloadAlignment, true);
- GcStorageSize CurrentTotalSize = StorageSize();
- ZEN_DEBUG("garbage collect from '{}' DISABLED, found {} chunks of total {} ({}/{})",
- m_BucketDir,
- DeleteCount,
- TotalChunkCount,
- NiceBytes(CurrentTotalSize.DiskSize),
- NiceBytes(CurrentTotalSize.MemorySize));
- return;
- }
-
- m_BlockStore.ReclaimSpace(
- BlockStoreState,
- ChunkLocations,
- KeepChunkIndexes,
- m_Configuration.PayloadAlignment,
- false,
- [&](const BlockStore::MovedChunksArray& MovedChunks, const BlockStore::ChunkIndexArray& RemovedChunks) {
- std::vector<DiskIndexEntry> LogEntries;
- LogEntries.reserve(MovedChunks.size() + RemovedChunks.size());
- {
- RwLock::ExclusiveLockScope IndexLock(m_IndexLock);
- Stopwatch Timer;
- const auto ____ = MakeGuard([&] {
- uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
- WriteBlockTimeUs += ElapsedUs;
- WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
- });
- for (const auto& Entry : MovedChunks)
- {
- size_t ChunkIndex = Entry.first;
- const BlockStoreLocation& NewLocation = Entry.second;
- const IoHash& ChunkHash = ChunkIndexToChunkHash[ChunkIndex];
- size_t EntryIndex = m_Index[ChunkHash];
- BucketPayload& Payload = m_Payloads[EntryIndex];
- if (PayloadsSnapshot[IndexSnapshot[ChunkHash]].Location != m_Payloads[EntryIndex].Location)
- {
- // Entry has been updated while GC was running, ignore the move
- continue;
- }
- Payload.Location = DiskLocation(NewLocation, m_Configuration.PayloadAlignment, Payload.Location.GetFlags());
- LogEntries.push_back({.Key = ChunkHash, .Location = Payload.Location});
- }
- for (const size_t ChunkIndex : RemovedChunks)
- {
- const IoHash& ChunkHash = ChunkIndexToChunkHash[ChunkIndex];
- size_t EntryIndex = m_Index[ChunkHash];
- BucketPayload& Payload = m_Payloads[EntryIndex];
- if (PayloadsSnapshot[IndexSnapshot[ChunkHash]].Location != Payload.Location)
- {
- // Entry has been updated while GC was running, ignore the delete
- continue;
- }
- const DiskLocation& OldDiskLocation = Payload.Location;
- LogEntries.push_back({.Key = ChunkHash,
- .Location = DiskLocation(OldDiskLocation.GetBlockLocation(m_Configuration.PayloadAlignment),
- m_Configuration.PayloadAlignment,
- OldDiskLocation.GetFlags() | DiskLocation::kTombStone)});
-
- RemoveMemCachedData(IndexLock, Payload);
- RemoveMetaData(IndexLock, Payload);
-
- m_Index.erase(ChunkHash);
- DeletedChunks.insert(ChunkHash);
- }
- }
-
- m_SlogFile.Append(LogEntries);
- m_SlogFile.Flush();
- },
- [&]() { return GcCtx.ClaimGCReserve(); });
-}
-
ZenCacheDiskLayer::BucketStats
ZenCacheDiskLayer::CacheBucket::Stats()
{
@@ -2835,30 +2375,6 @@ ZenCacheDiskLayer::CacheBucket::EnumerateBucketContents(
}
void
-ZenCacheDiskLayer::CollectGarbage(GcContext& GcCtx)
-{
- ZEN_TRACE_CPU("Z$::CollectGarbage");
-
- std::vector<CacheBucket*> Buckets;
- {
- RwLock::SharedLockScope _(m_Lock);
- Buckets.reserve(m_Buckets.size());
- for (auto& Kv : m_Buckets)
- {
- Buckets.push_back(Kv.second.get());
- }
- }
- for (CacheBucket* Bucket : Buckets)
- {
- Bucket->CollectGarbage(GcCtx);
- }
- if (!m_IsMemCacheTrimming)
- {
- MemCacheTrim(Buckets, GcCtx.CacheExpireTime());
- }
-}
-
-void
ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value, std::span<IoHash> References)
{
ZEN_TRACE_CPU("Z$::Bucket::PutStandaloneCacheValue");
@@ -4519,26 +4035,6 @@ ZenCacheDiskLayer::ScrubStorage(ScrubContext& Ctx)
}
}
-void
-ZenCacheDiskLayer::GatherReferences(GcContext& GcCtx)
-{
- ZEN_TRACE_CPU("Z$::GatherReferences");
-
- std::vector<CacheBucket*> Buckets;
- {
- RwLock::SharedLockScope _(m_Lock);
- Buckets.reserve(m_Buckets.size());
- for (auto& Kv : m_Buckets)
- {
- Buckets.push_back(Kv.second.get());
- }
- }
- for (CacheBucket* Bucket : Buckets)
- {
- Bucket->GatherReferences(GcCtx);
- }
-}
-
GcStorageSize
ZenCacheDiskLayer::StorageSize() const
{