aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/projectstore.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2025-12-01 16:32:09 +0100
committerGitHub Enterprise <[email protected]>2025-12-01 16:32:09 +0100
commitba00b4a54660150325eb734b6157333ffdead53a (patch)
tree10aaaa3ffc89d989c195b5c1bf35f7cb2ddfb7e4 /src/zenstore/projectstore.cpp
parentuse scope guards to secure that BasicFile::Detach is called even on exception... (diff)
downloadzen-ba00b4a54660150325eb734b6157333ffdead53a.tar.xz
zen-ba00b4a54660150325eb734b6157333ffdead53a.zip
make sure we use exclusive lock in projectstore when flushing/writing snapshot (#673)
Diffstat (limited to 'src/zenstore/projectstore.cpp')
-rw-r--r--src/zenstore/projectstore.cpp45
1 files changed, 35 insertions, 10 deletions
diff --git a/src/zenstore/projectstore.cpp b/src/zenstore/projectstore.cpp
index 7e9ff50bb..2f97de5de 100644
--- a/src/zenstore/projectstore.cpp
+++ b/src/zenstore/projectstore.cpp
@@ -1113,9 +1113,32 @@ ProjectStore::Oplog::Oplog(const LoggerRef& InLog,
ProjectStore::Oplog::~Oplog()
{
- if (m_Storage)
+ try
+ {
+ RwLock::ExclusiveLockScope Lock(m_OplogLock);
+ if (m_Storage)
+ {
+ if (m_Mode == EMode::kFull)
+ {
+ m_Storage->Flush();
+ if (!m_MetaValid)
+ {
+ std::error_code DummyEc;
+ RemoveFile(m_MetaPath, DummyEc);
+ }
+
+ uint64_t LogCount = m_Storage->LogCount();
+ if (m_LogFlushPosition != LogCount || m_IsLegacySnapshot)
+ {
+ WriteIndexSnapshot(Lock);
+ }
+ }
+ m_Storage = {};
+ }
+ }
+ catch (const std::exception& Ex)
{
- Flush();
+ ZEN_ERROR("Oplog::~Oplog threw exception: '{}'", Ex.what());
}
}
@@ -1128,7 +1151,7 @@ ProjectStore::Oplog::Flush()
if (m_Mode == EMode::kFull)
{
- RwLock::SharedLockScope Lock(m_OplogLock);
+ RwLock::ExclusiveLockScope Lock(m_OplogLock);
if (!m_Storage)
{
@@ -1145,7 +1168,7 @@ ProjectStore::Oplog::Flush()
uint64_t LogCount = m_Storage->LogCount();
if (m_LogFlushPosition != LogCount || m_IsLegacySnapshot)
{
- WriteIndexSnapshot();
+ WriteIndexSnapshot(Lock);
}
}
}
@@ -1417,7 +1440,9 @@ ProjectStore::Oplog::Read()
ZEN_ASSERT(!m_LsnToPayloadOffsetMap);
- ReadIndexSnapshot();
+ RwLock::ExclusiveLockScope Lock(m_OplogLock);
+
+ ReadIndexSnapshot(Lock);
if (m_Mode == EMode::kFull)
{
@@ -1455,7 +1480,7 @@ ProjectStore::Oplog::Read()
if (m_Storage->LogCount() != m_LogFlushPosition || m_IsLegacySnapshot)
{
- WriteIndexSnapshot();
+ WriteIndexSnapshot(Lock);
}
}
else
@@ -1742,7 +1767,7 @@ ProjectStore::Oplog::Validate(const std::filesystem::path& ProjectRootDir,
}
void
-ProjectStore::Oplog::WriteIndexSnapshot()
+ProjectStore::Oplog::WriteIndexSnapshot(RwLock::ExclusiveLockScope&)
{
ZEN_MEMSCOPE(GetProjectstoreTag());
ZEN_TRACE_CPU("Oplog::WriteIndexSnapshot");
@@ -1883,7 +1908,7 @@ ProjectStore::Oplog::WriteIndexSnapshot()
}
void
-ProjectStore::Oplog::ReadIndexSnapshot()
+ProjectStore::Oplog::ReadIndexSnapshot(RwLock::ExclusiveLockScope&)
{
ZEN_MEMSCOPE(GetProjectstoreTag());
ZEN_TRACE_CPU("Oplog::ReadIndexSnapshot");
@@ -2212,7 +2237,7 @@ ProjectStore::Oplog::Compact(bool DryRun, bool RetainLSNs, std::string_view LogP
}
void
-ProjectStore::Oplog::Compact(RwLock::ExclusiveLockScope&, bool DryRun, bool RetainLSNs, std::string_view LogPrefix)
+ProjectStore::Oplog::Compact(RwLock::ExclusiveLockScope& Lock, bool DryRun, bool RetainLSNs, std::string_view LogPrefix)
{
ZEN_ASSERT(m_Mode == EMode::kFull);
@@ -2250,7 +2275,7 @@ ProjectStore::Oplog::Compact(RwLock::ExclusiveLockScope&, bool DryRun, bool Reta
m_OpToPayloadOffsetMap.swap(OpToPayloadOffsetMap);
m_OpLogPayloads.swap(OpPayloads);
m_LsnToPayloadOffsetMap.reset();
- WriteIndexSnapshot();
+ WriteIndexSnapshot(Lock);
}
uint64_t PostSize = TotalSize();