diff options
Diffstat (limited to 'src/zenstore/cache/cachedisklayer.cpp')
| -rw-r--r-- | src/zenstore/cache/cachedisklayer.cpp | 50 |
1 files changed, 31 insertions, 19 deletions
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<uint64_t()>& ClaimDiskReserveFunc) +ZenCacheDiskLayer::CacheBucket::WriteIndexSnapshotLocked(bool FlushLockPosition, const std::function<uint64_t()>& 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<uin { std::filesystem::path TempFilePath = ObjectIndexFile.GetPath(); ZEN_WARN("snapshot failed to rename new snapshot '{}' to '{}', reason: '{}'", TempFilePath, IndexPath, Ec.message()); - - if (std::filesystem::is_regular_file(TempFilePath)) - { - if (!std::filesystem::remove(TempFilePath, Ec) || Ec) - { - ZEN_WARN("snapshot failed to remove temporary file {}, reason: '{}'", TempFilePath, Ec.message()); - } - } } else { // We must only update the log flush position once the snapshot write succeeds - m_LogFlushPosition = LogCount; + if (FlushLockPosition) + { + std::filesystem::path LogPath = GetLogPath(m_BucketDir, m_BucketName); + + if (std::filesystem::is_regular_file(LogPath)) + { + if (!std::filesystem::remove(LogPath, Ec) || Ec) + { + ZEN_WARN("snapshot failed to clean log file '{}', removing index at '{}', reason: '{}'", + LogPath, + IndexPath, + Ec.message()); + std::error_code RemoveIndexEc; + std::filesystem::remove(IndexPath, RemoveIndexEc); + } + } + } + if (!Ec) + { + m_LogFlushPosition = LogCount; + } } } catch (const std::exception& Err) @@ -1104,6 +1116,11 @@ ZenCacheDiskLayer::CacheBucket::InitializeIndexFromDisk(RwLock::ExclusiveLockSco } } + if (IsNew || LogEntryCount > 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<uint64_t()>& 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<uint64_t()>& 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(); |