aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/cache/cachedisklayer.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-09-04 10:03:38 +0200
committerGitHub Enterprise <[email protected]>2024-09-04 10:03:38 +0200
commitc028afcc305457fc24ffc62b9b7197f44ba45689 (patch)
treeba7e1929196e67d208745f8d7f065424677408b8 /src/zenstore/cache/cachedisklayer.cpp
parentmove gc logs to gc logger (#142) (diff)
downloadzen-c028afcc305457fc24ffc62b9b7197f44ba45689.tar.xz
zen-c028afcc305457fc24ffc62b9b7197f44ba45689.zip
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
Diffstat (limited to 'src/zenstore/cache/cachedisklayer.cpp')
-rw-r--r--src/zenstore/cache/cachedisklayer.cpp50
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();