diff options
| author | Dan Engelbrecht <[email protected]> | 2022-12-16 10:37:43 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-16 01:37:43 -0800 |
| commit | c5a974fa97bc23861464a48f89c93851f9f0ab63 (patch) | |
| tree | b8d9d6d51873d6c4421faf9179cc53c82f5d3eb8 /zenserver/cache/structuredcachestore.cpp | |
| parent | oplog level GC (#209) (diff) | |
| download | zen-c5a974fa97bc23861464a48f89c93851f9f0ab63.tar.xz zen-c5a974fa97bc23861464a48f89c93851f9f0ab63.zip | |
Fix log index snapshot (#210)
* Fix log reading for structured cache store
Make sure cache is flushed at exit
* dont flush index to disk unless new entries have been written
* changelog
Diffstat (limited to 'zenserver/cache/structuredcachestore.cpp')
| -rw-r--r-- | zenserver/cache/structuredcachestore.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/zenserver/cache/structuredcachestore.cpp b/zenserver/cache/structuredcachestore.cpp index 9f6707a4a..de1243ddd 100644 --- a/zenserver/cache/structuredcachestore.cpp +++ b/zenserver/cache/structuredcachestore.cpp @@ -50,7 +50,7 @@ namespace { struct CacheBucketIndexHeader { static constexpr uint32_t ExpectedMagic = 0x75696478; // 'uidx'; - static constexpr uint32_t CurrentVersion = 1; + static constexpr uint32_t CurrentVersion = 2; uint32_t Magic = ExpectedMagic; uint32_t Version = CurrentVersion; @@ -649,6 +649,12 @@ ZenCacheDiskLayer::CacheBucket::OpenOrCreate(std::filesystem::path BucketDir, bo void ZenCacheDiskLayer::CacheBucket::MakeIndexSnapshot() { + uint64_t LogCount = m_SlogFile.GetLogCount(); + if (m_LogFlushPosition == LogCount) + { + return; + } + ZEN_DEBUG("write store snapshot for '{}'", m_BucketDir / m_BucketName); uint64_t EntryCount = 0; Stopwatch Timer; @@ -676,10 +682,7 @@ ZenCacheDiskLayer::CacheBucket::MakeIndexSnapshot() try { - m_SlogFile.Flush(); - // Write the current state of the location map to a new index state - uint64_t LogCount = 0; std::vector<DiskIndexEntry> Entries; { @@ -692,8 +695,6 @@ ZenCacheDiskLayer::CacheBucket::MakeIndexSnapshot() IndexEntry.Key = Entry.first; IndexEntry.Location = Entry.second.Location; } - - LogCount = m_SlogFile.GetLogCount(); } BasicFile ObjectIndexFile; @@ -708,7 +709,8 @@ ZenCacheDiskLayer::CacheBucket::MakeIndexSnapshot() ObjectIndexFile.Write(Entries.data(), Entries.size() * sizeof(DiskIndexEntry), sizeof(CacheBucketIndexHeader)); ObjectIndexFile.Flush(); ObjectIndexFile.Close(); - EntryCount = Entries.size(); + EntryCount = Entries.size(); + m_LogFlushPosition = LogCount; } catch (std::exception& Err) { @@ -787,9 +789,10 @@ ZenCacheDiskLayer::CacheBucket::ReadLog(uint64_t SkipEntryCount) std::filesystem::path LogPath = GetLogPath(m_BucketDir, m_BucketName); if (std::filesystem::is_regular_file(LogPath)) { + uint64_t LogEntryCount = 0; Stopwatch Timer; const auto _ = MakeGuard([&] { - ZEN_INFO("read store '{}' log containing #{} entries in {}", LogPath, m_Index.size(), NiceTimeSpanMs(Timer.GetElapsedTimeMs())); + ZEN_INFO("read store '{}' log containing #{} entries in {}", LogPath, LogEntryCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); TCasLogFile<DiskIndexEntry> CasLog; CasLog.Open(LogPath, CasLogFile::Mode::kRead); @@ -801,8 +804,8 @@ ZenCacheDiskLayer::CacheBucket::ReadLog(uint64_t SkipEntryCount) ZEN_WARN("reading full log at '{}', reason: Log position from index snapshot is out of range", LogPath); SkipEntryCount = 0; } - uint64_t ReadCount = EntryCount - SkipEntryCount; - m_Index.reserve(ReadCount); + LogEntryCount = EntryCount - SkipEntryCount; + m_Index.reserve(LogEntryCount); uint64_t InvalidEntryCount = 0; CasLog.Replay( [&](const DiskIndexEntry& Record) { @@ -825,6 +828,7 @@ ZenCacheDiskLayer::CacheBucket::ReadLog(uint64_t SkipEntryCount) { ZEN_WARN("found #{} invalid entries in '{}'", InvalidEntryCount, m_BucketDir / m_BucketName); } + return LogEntryCount; } } return 0; @@ -849,8 +853,8 @@ ZenCacheDiskLayer::CacheBucket::OpenLog(const fs::path& BucketDir, const bool Is fs::remove_all(m_BlocksBasePath); } - uint64_t LogPosition = ReadIndexFile(); - uint64_t LogEntryCount = ReadLog(LogPosition); + m_LogFlushPosition = ReadIndexFile(); + uint64_t LogEntryCount = ReadLog(m_LogFlushPosition); CreateDirectories(m_BucketDir); @@ -987,6 +991,7 @@ ZenCacheDiskLayer::CacheBucket::Flush() m_BlockStore.Flush(); RwLock::SharedLockScope _(m_IndexLock); + m_SlogFile.Flush(); MakeIndexSnapshot(); SaveManifest(); } |