aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/compactcas.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2025-05-09 13:57:25 +0200
committerGitHub Enterprise <[email protected]>2025-05-09 13:57:25 +0200
commita535a19aae3e4c9573a16099ec19866f7d4b7bc1 (patch)
tree3452d6276a632c2044efa79192fc9fa2cc5d9ba1 /src/zenstore/compactcas.cpp
parentMerge pull request #386 from ue-foundation/zs/plugin-config-hardening (diff)
downloadzen-a535a19aae3e4c9573a16099ec19866f7d4b7bc1.tar.xz
zen-a535a19aae3e4c9573a16099ec19866f7d4b7bc1.zip
flush cas log file (#387)
* make sure we remove the cas log file when writing full index at startup
Diffstat (limited to 'src/zenstore/compactcas.cpp')
-rw-r--r--src/zenstore/compactcas.cpp75
1 files changed, 30 insertions, 45 deletions
diff --git a/src/zenstore/compactcas.cpp b/src/zenstore/compactcas.cpp
index 90e77e48a..a8914ed20 100644
--- a/src/zenstore/compactcas.cpp
+++ b/src/zenstore/compactcas.cpp
@@ -461,7 +461,7 @@ CasContainerStrategy::Flush()
ZEN_TRACE_CPU("CasContainer::Flush");
m_BlockStore.Flush(/*ForceNewBlock*/ false);
m_CasLog.Flush();
- MakeIndexSnapshot();
+ MakeIndexSnapshot(/*FlushLockPosition*/ false);
}
void
@@ -924,12 +924,12 @@ CasContainerStrategy::StorageSize() const
}
void
-CasContainerStrategy::MakeIndexSnapshot()
+CasContainerStrategy::MakeIndexSnapshot(bool FlushLockPosition)
{
ZEN_MEMSCOPE(GetCasContainerTag());
ZEN_TRACE_CPU("CasContainer::MakeIndexSnapshot");
- uint64_t LogCount = m_CasLog.GetLogCount();
+ const uint64_t LogCount = FlushLockPosition ? 0 : m_CasLog.GetLogCount();
if (m_LogFlushPosition == LogCount)
{
return;
@@ -947,27 +947,10 @@ CasContainerStrategy::MakeIndexSnapshot()
namespace fs = std::filesystem;
- fs::path IndexPath = cas::impl::GetIndexPath(m_RootDirectory, m_ContainerBaseName);
- fs::path TempIndexPath = cas::impl::GetTempIndexPath(m_RootDirectory, m_ContainerBaseName);
-
- // Move index away, we keep it if something goes wrong
- if (IsFile(TempIndexPath))
- {
- std::error_code Ec;
- if (!RemoveFile(TempIndexPath, Ec) || Ec)
- {
- ZEN_WARN("snapshot failed to clean up temp snapshot at {}, reason: '{}'", TempIndexPath, Ec.message());
- return;
- }
- }
+ fs::path IndexPath = cas::impl::GetIndexPath(m_RootDirectory, m_ContainerBaseName);
try
{
- if (IsFile(IndexPath))
- {
- RenameFile(IndexPath, TempIndexPath);
- }
-
// Write the current state of the location map to a new index state
std::vector<CasDiskIndexEntry> Entries;
uint64_t IndexLogPosition = 0;
@@ -993,7 +976,8 @@ CasContainerStrategy::MakeIndexSnapshot()
{
throw std::system_error(Ec, fmt::format("Failed to create temp file for index snapshot at '{}'", IndexPath));
}
- CasDiskIndexHeader Header = {.EntryCount = Entries.size(),
+ EntryCount = Entries.size();
+ CasDiskIndexHeader Header = {.EntryCount = EntryCount,
.LogPosition = IndexLogPosition,
.PayloadAlignment = gsl::narrow<uint32_t>(m_PayloadAlignment)};
@@ -1005,36 +989,37 @@ CasContainerStrategy::MakeIndexSnapshot()
ObjectIndexFile.MoveTemporaryIntoPlace(IndexPath, Ec);
if (Ec)
{
- throw std::system_error(Ec, fmt::format("Failed to move temp file '{}' to '{}'", ObjectIndexFile.GetPath(), IndexPath));
+ ZEN_WARN("snapshot failed to rename new snapshot '{}' to '{}', reason: '{}'",
+ ObjectIndexFile.GetPath(),
+ IndexPath,
+ Ec.message());
}
- EntryCount = Entries.size();
- m_LogFlushPosition = IndexLogPosition;
- }
- catch (const std::exception& Err)
- {
- ZEN_WARN("snapshot FAILED, reason: '{}'", Err.what());
-
- // Restore any previous snapshot
-
- if (IsFile(TempIndexPath))
+ if (FlushLockPosition)
{
- std::error_code Ec;
- RemoveFile(IndexPath, Ec); // We don't care if this fails, we try to move the old temp file regardless
- RenameFile(TempIndexPath, IndexPath, Ec);
- if (Ec)
+ std::filesystem::path LogPath = cas::impl::GetLogPath(m_RootDirectory, m_ContainerBaseName);
+
+ if (IsFile(LogPath))
{
- ZEN_WARN("snapshot failed to restore old snapshot from {}, reason: '{}'", TempIndexPath, Ec.message());
+ if (!RemoveFile(LogPath, Ec) || Ec)
+ {
+ ZEN_WARN("snapshot failed to clean log file '{}', removing index at '{}', reason: '{}'",
+ LogPath,
+ IndexPath,
+ Ec.message());
+ std::error_code RemoveIndexEc;
+ RemoveFile(IndexPath, RemoveIndexEc);
+ }
}
}
- }
- if (IsFile(TempIndexPath))
- {
- std::error_code Ec;
- if (!RemoveFile(TempIndexPath, Ec) || Ec)
+ if (!Ec)
{
- ZEN_WARN("snapshot failed to remove temporary file {}, reason: '{}'", TempIndexPath, Ec.message());
+ m_LogFlushPosition = LogCount;
}
}
+ catch (const std::exception& Err)
+ {
+ ZEN_WARN("snapshot FAILED, reason: '{}'", Err.what());
+ }
}
uint64_t
@@ -1229,7 +1214,7 @@ CasContainerStrategy::OpenContainer(bool IsNewStore)
if (IsNewStore || (LogEntryCount > 0))
{
- MakeIndexSnapshot();
+ MakeIndexSnapshot(/*FlushLockPosition*/ true);
}
// TODO: should validate integrity of container files here