aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/cache
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
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')
-rw-r--r--src/zenstore/cache/cachedisklayer.cpp504
-rw-r--r--src/zenstore/cache/structuredcachestore.cpp300
2 files changed, 17 insertions, 787 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
{
diff --git a/src/zenstore/cache/structuredcachestore.cpp b/src/zenstore/cache/structuredcachestore.cpp
index ac8b70c1c..d30bd93cc 100644
--- a/src/zenstore/cache/structuredcachestore.cpp
+++ b/src/zenstore/cache/structuredcachestore.cpp
@@ -127,14 +127,12 @@ ZenCacheNamespace::ZenCacheNamespace(GcManager& Gc, JobQueue& JobQueue, const st
m_DiskLayer.DiscoverBuckets();
- m_Gc.AddGcContributor(this);
m_Gc.AddGcStorage(this);
}
ZenCacheNamespace::~ZenCacheNamespace()
{
m_Gc.RemoveGcStorage(this);
- m_Gc.RemoveGcContributor(this);
}
struct ZenCacheNamespace::PutBatchHandle
@@ -307,26 +305,6 @@ ZenCacheNamespace::ScrubStorage(ScrubContext& Ctx)
m_DiskLayer.ScrubStorage(Ctx);
}
-void
-ZenCacheNamespace::GatherReferences(GcContext& GcCtx)
-{
- ZEN_TRACE_CPU("Z$::ZenCacheNamespace::GatherReferences");
-
- Stopwatch Timer;
- const auto Guard =
- MakeGuard([&] { ZEN_DEBUG("cache gathered all references from '{}' in {}", m_RootDir, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); });
-
- m_DiskLayer.GatherReferences(GcCtx);
-}
-
-void
-ZenCacheNamespace::CollectGarbage(GcContext& GcCtx)
-{
- ZEN_TRACE_CPU("Z$::Namespace::CollectGarbage");
-
- m_DiskLayer.CollectGarbage(GcCtx);
-}
-
GcStorageSize
ZenCacheNamespace::StorageSize() const
{
@@ -1452,186 +1430,7 @@ TEST_CASE("cachestore.size")
}
}
-TEST_CASE("cachestore.gc")
-{
- using namespace testutils;
-
- auto JobQueue = MakeJobQueue(1, "testqueue");
-
- SUBCASE("gather references does NOT add references for expired cache entries")
- {
- ScopedTemporaryDirectory TempDir;
- std::vector<IoHash> Cids{CreateKey(1), CreateKey(2), CreateKey(3)};
-
- const auto CollectAndFilter = [](GcManager& Gc,
- GcClock::TimePoint Time,
- GcClock::Duration MaxDuration,
- std::span<const IoHash> Cids,
- std::vector<IoHash>& OutKeep) {
- GcContext GcCtx(Time - MaxDuration, Time - MaxDuration);
- Gc.CollectGarbage(GcCtx);
- OutKeep.clear();
- GcCtx.FilterCids(Cids, [&OutKeep](const IoHash& Hash) { OutKeep.push_back(Hash); });
- };
-
- {
- GcManager Gc;
- ZenCacheNamespace Zcs(Gc, *JobQueue, TempDir.Path() / "cache", {});
- const auto Bucket = "teardrinker"sv;
-
- // Create a cache record
- const IoHash Key = CreateKey(42);
- CbObjectWriter Record;
- Record << "Key"sv
- << "SomeRecord"sv;
-
- for (size_t Idx = 0; auto& Cid : Cids)
- {
- Record.AddBinaryAttachment(fmt::format("attachment-{}", Idx++), Cid);
- }
-
- IoBuffer Buffer = Record.Save().GetBuffer().AsIoBuffer();
- Buffer.SetContentType(ZenContentType::kCbObject);
-
- Zcs.Put(Bucket, Key, {.Value = Buffer}, Cids);
-
- std::vector<IoHash> Keep;
-
- // Collect garbage with 1 hour max cache duration
- {
- CollectAndFilter(Gc, GcClock::Now(), std::chrono::hours(1), Cids, Keep);
- CHECK_EQ(Cids.size(), Keep.size());
- }
-
- // Move forward in time
- {
- CollectAndFilter(Gc, GcClock::Now() + std::chrono::hours(2), std::chrono::hours(1), Cids, Keep);
- CHECK_EQ(0, Keep.size());
- }
- }
-
- // Expect timestamps to be serialized
- {
- GcManager Gc;
- ZenCacheNamespace Zcs(Gc, *JobQueue, TempDir.Path() / "cache", {});
- std::vector<IoHash> Keep;
-
- // Collect garbage with 1 hour max cache duration
- {
- CollectAndFilter(Gc, GcClock::Now(), std::chrono::hours(1), Cids, Keep);
- CHECK_EQ(3, Keep.size());
- }
-
- // Move forward in time
- {
- CollectAndFilter(Gc, GcClock::Now() + std::chrono::hours(2), std::chrono::hours(1), Cids, Keep);
- CHECK_EQ(0, Keep.size());
- }
- }
- }
-
- SUBCASE("gc removes standalone values")
- {
- ScopedTemporaryDirectory TempDir;
- GcManager Gc;
- ZenCacheNamespace Zcs(Gc, *JobQueue, TempDir.Path() / "cache", {});
- const auto Bucket = "fortysixandtwo"sv;
- const GcClock::TimePoint CurrentTime = GcClock::Now();
-
- std::vector<IoHash> Keys{CreateKey(1), CreateKey(2), CreateKey(3)};
-
- for (const auto& Key : Keys)
- {
- IoBuffer Value = CreateRandomBlob(128 << 10);
- Zcs.Put(Bucket, Key, {.Value = Value}, {});
- }
-
- {
- GcContext GcCtx(CurrentTime - std::chrono::hours(46), CurrentTime - std::chrono::hours(46));
-
- Gc.CollectGarbage(GcCtx);
-
- for (const auto& Key : Keys)
- {
- ZenCacheValue CacheValue;
- const bool Exists = Zcs.Get(Bucket, Key, CacheValue);
- CHECK(Exists);
- }
- }
-
- // Move forward in time and collect again
- {
- GcContext GcCtx(CurrentTime + std::chrono::minutes(2), CurrentTime + std::chrono::minutes(2));
- Gc.CollectGarbage(GcCtx);
-
- for (const auto& Key : Keys)
- {
- ZenCacheValue CacheValue;
- const bool Exists = Zcs.Get(Bucket, Key, CacheValue);
- CHECK(!Exists);
- }
-
- CHECK_EQ(0, Zcs.StorageSize().DiskSize);
- }
- }
-
- SUBCASE("gc removes small objects")
- {
- ScopedTemporaryDirectory TempDir;
- GcManager Gc;
- {
- ZenCacheNamespace Zcs(Gc, *JobQueue, TempDir.Path() / "cache", {});
- const auto Bucket = "rightintwo"sv;
-
- std::vector<IoHash> Keys{CreateKey(1), CreateKey(2), CreateKey(3)};
-
- for (const auto& Key : Keys)
- {
- IoBuffer Value = CreateRandomBlob(128);
- Zcs.Put(Bucket, Key, {.Value = Value}, {});
- }
-
- {
- GcContext GcCtx(GcClock::Now() - std::chrono::hours(2), GcClock::Now() - std::chrono::hours(2));
- GcCtx.CollectSmallObjects(true);
-
- Gc.CollectGarbage(GcCtx);
-
- for (const auto& Key : Keys)
- {
- ZenCacheValue CacheValue;
- const bool Exists = Zcs.Get(Bucket, Key, CacheValue);
- CHECK(Exists);
- }
- }
-
- // Move forward in time and collect again
- {
- GcContext GcCtx(GcClock::Now() + std::chrono::minutes(2), GcClock::Now() + std::chrono::minutes(2));
- GcCtx.CollectSmallObjects(true);
-
- Zcs.Flush();
- Gc.CollectGarbage(GcCtx);
-
- for (const auto& Key : Keys)
- {
- ZenCacheValue CacheValue;
- const bool Exists = Zcs.Get(Bucket, Key, CacheValue);
- CHECK(!Exists);
- }
- // GC could not remove the currently written block so size will not be zero
- CHECK_NE(0, Zcs.StorageSize().DiskSize);
- }
- }
- {
- // Unreferenced blocks will be pruned so size should now be zero
- ZenCacheNamespace Zcs(Gc, *JobQueue, TempDir.Path() / "cache", {});
- CHECK_EQ(0, Zcs.StorageSize().DiskSize);
- }
- }
-}
-
-TEST_CASE_TEMPLATE("cachestore.threadedinsert", GCV2, FalseType, TrueType) // * doctest::skip(true))
+TEST_CASE("cachestore.threadedinsert") // * doctest::skip(true))
{
// for (uint32_t i = 0; i < 100; ++i)
{
@@ -1699,39 +1498,24 @@ TEST_CASE_TEMPLATE("cachestore.threadedinsert", GCV2, FalseType, TrueType) // *
}
}
- auto DoGC = [](GcManager& Gc,
- ZenCacheNamespace& Zcs,
- std::unordered_map<IoHash, std::string, IoHash::Hasher>& GcChunkHashes,
- const std::vector<IoHash>& KeepHashes) {
- if (GCV2::Enabled)
+ auto DoGC = [](GcManager& Gc, ZenCacheNamespace& Zcs, std::unordered_map<IoHash, std::string, IoHash::Hasher>& GcChunkHashes) {
+ GcSettings Settings = {.CacheExpireTime = GcClock::Now() - std::chrono::hours(24),
+ .ProjectStoreExpireTime = GcClock::Now() - std::chrono::hours(24),
+ .CollectSmallObjects = true,
+ .IsDeleteMode = true,
+ .CompactBlockUsageThresholdPercent = 100};
+ Gc.CollectGarbage(Settings);
+ // Cheating as we don't get the list of deleted hashes back from this call
+ std::unordered_map<IoHash, std::string, IoHash::Hasher> RemainingChunkHashes;
+ for (const auto& It : GcChunkHashes)
{
- GcSettings Settings = {.CacheExpireTime = GcClock::Now() - std::chrono::hours(24),
- .ProjectStoreExpireTime = GcClock::Now() - std::chrono::hours(24),
- .CollectSmallObjects = true,
- .IsDeleteMode = true,
- .CompactBlockUsageThresholdPercent = 100};
- Gc.CollectGarbage(Settings);
- // Cheating as we don't get the list of deleted hashes back from this call
- std::unordered_map<IoHash, std::string, IoHash::Hasher> RemainingChunkHashes;
- for (const auto& It : GcChunkHashes)
+ ZenCacheValue Tmp;
+ if (Zcs.Get(It.second, It.first, Tmp))
{
- ZenCacheValue Tmp;
- if (Zcs.Get(It.second, It.first, Tmp))
- {
- RemainingChunkHashes.insert(It);
- }
+ RemainingChunkHashes.insert(It);
}
- GcChunkHashes.swap(RemainingChunkHashes);
- }
- else
- {
- GcContext GcCtx(GcClock::Now() - std::chrono::hours(24), GcClock::Now() - std::chrono::hours(24));
- GcCtx.CollectSmallObjects(true);
- GcCtx.AddRetainedCids(KeepHashes);
- Zcs.CollectGarbage(GcCtx);
- const HashKeySet& Deleted = GcCtx.DeletedCids();
- Deleted.IterateHashes([&GcChunkHashes](const IoHash& ChunkHash) { GcChunkHashes.erase(ChunkHash); });
}
+ GcChunkHashes.swap(RemainingChunkHashes);
};
const uint64_t TotalSize = Zcs.StorageSize().DiskSize;
@@ -1813,32 +1597,7 @@ TEST_CASE_TEMPLATE("cachestore.threadedinsert", GCV2, FalseType, TrueType) // *
GcChunkHashes[Chunk.first] = Chunk.second.Bucket;
}
}
- std::vector<IoHash> KeepHashes;
- KeepHashes.reserve(GcChunkHashes.size());
- for (const auto& Entry : GcChunkHashes)
- {
- KeepHashes.push_back(Entry.first);
- }
- size_t C = 0;
- while (C < KeepHashes.size())
- {
- if (C % 155 == 0)
- {
- if (C < KeepHashes.size() - 1)
- {
- KeepHashes[C] = KeepHashes[KeepHashes.size() - 1];
- KeepHashes.pop_back();
- }
- if (C + 3 < KeepHashes.size() - 1)
- {
- KeepHashes[C + 3] = KeepHashes[KeepHashes.size() - 1];
- KeepHashes.pop_back();
- }
- }
- C++;
- }
-
- DoGC(Gc, Zcs, GcChunkHashes, KeepHashes);
+ DoGC(Gc, Zcs, GcChunkHashes);
}
while (WorkCompleted < NewChunks.size() + Chunks.size())
@@ -1856,32 +1615,7 @@ TEST_CASE_TEMPLATE("cachestore.threadedinsert", GCV2, FalseType, TrueType) // *
GcChunkHashes[Chunk.first] = Chunk.second.Bucket;
}
}
- std::vector<IoHash> KeepHashes;
- KeepHashes.reserve(GcChunkHashes.size());
- for (const auto& Entry : GcChunkHashes)
- {
- KeepHashes.push_back(Entry.first);
- }
- size_t C = 0;
- while (C < KeepHashes.size())
- {
- if (C % 155 == 0)
- {
- if (C < KeepHashes.size() - 1)
- {
- KeepHashes[C] = KeepHashes[KeepHashes.size() - 1];
- KeepHashes.pop_back();
- }
- if (C + 3 < KeepHashes.size() - 1)
- {
- KeepHashes[C + 3] = KeepHashes[KeepHashes.size() - 1];
- KeepHashes.pop_back();
- }
- }
- C++;
- }
-
- DoGC(Gc, Zcs, GcChunkHashes, KeepHashes);
+ DoGC(Gc, Zcs, GcChunkHashes);
}
}
{