From c028afcc305457fc24ffc62b9b7197f44ba45689 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 4 Sep 2024 10:03:38 +0200 Subject: clean cache slog files on startup (#143) - Bugfix: If we fail to move a temporary file into place, try to re-open the file so we clean it up - Improvement: Clean up cache bucket log files at startup as we store the matching information in the index snapshot for the bucket --- src/zenstore/cache/cachedisklayer.cpp | 50 ++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) (limited to 'src/zenstore/cache/cachedisklayer.cpp') diff --git a/src/zenstore/cache/cachedisklayer.cpp b/src/zenstore/cache/cachedisklayer.cpp index 9c350b11d..37404053e 100644 --- a/src/zenstore/cache/cachedisklayer.cpp +++ b/src/zenstore/cache/cachedisklayer.cpp @@ -257,7 +257,7 @@ public: Oid GenerateNewManifest(std::filesystem::path ManifestPath); IoBuffer MakeSidecarManifest(const Oid& BucketId, uint64_t EntryCount); - uint64_t GetSidecarSize() const { return m_ManifestEntryCount * sizeof(ManifestData); } + uint64_t GetSidecarSize() const { return sizeof(BucketMetaHeader) + m_ManifestEntryCount * sizeof(ManifestData); } void WriteSidecarFile(RwLock::SharedLockScope& BucketLock, const std::filesystem::path& SidecarPath, uint64_t SnapshotLogPosition, @@ -792,11 +792,11 @@ ZenCacheDiskLayer::CacheBucket::OpenOrCreate(std::filesystem::path BucketDir, bo } void -ZenCacheDiskLayer::CacheBucket::WriteIndexSnapshotLocked(const std::function& ClaimDiskReserveFunc) +ZenCacheDiskLayer::CacheBucket::WriteIndexSnapshotLocked(bool FlushLockPosition, const std::function& ClaimDiskReserveFunc) { ZEN_TRACE_CPU("Z$::Bucket::WriteIndexSnapshot"); - const uint64_t LogCount = m_SlogFile.GetLogCount(); + const uint64_t LogCount = FlushLockPosition ? 0 : m_SlogFile.GetLogCount(); if (m_LogFlushPosition == LogCount) { return; @@ -880,19 +880,31 @@ ZenCacheDiskLayer::CacheBucket::WriteIndexSnapshotLocked(const std::function 0 || m_LogFlushPosition != 0) + { + WriteIndexSnapshot(IndexLock, /*Flush log*/ true); + } + m_SlogFile.Open(LogPath, CasLogFile::Mode::kWrite); BlockStore::BlockIndexSet KnownBlocks; @@ -1124,11 +1141,6 @@ ZenCacheDiskLayer::CacheBucket::InitializeIndexFromDisk(RwLock::ExclusiveLockSco } } m_BlockStore.SyncExistingBlocksOnDisk(KnownBlocks); - - if (IsNew || LogEntryCount > 0) - { - WriteIndexSnapshot(IndexLock); - } } void @@ -1833,7 +1845,7 @@ ZenCacheDiskLayer::CacheBucket::SaveSnapshot(const std::function& Cl { RwLock::SharedLockScope IndexLock(m_IndexLock); - WriteIndexSnapshot(IndexLock); + WriteIndexSnapshot(IndexLock, /*Flush log*/ false); // Note: this copy could be eliminated on shutdown to // reduce memory usage and execution time Index = m_Index; @@ -1873,7 +1885,7 @@ ZenCacheDiskLayer::CacheBucket::SaveSnapshot(const std::function& Cl else { RwLock::SharedLockScope IndexLock(m_IndexLock); - WriteIndexSnapshot(IndexLock); + WriteIndexSnapshot(IndexLock, /*Flush log*/ false); const uint64_t EntryCount = m_Index.size(); Buffer = ManifestWriter.MakeSidecarManifest(m_BucketId, EntryCount); uint64_t SidecarSize = ManifestWriter.GetSidecarSize(); -- cgit v1.2.3