aboutsummaryrefslogtreecommitdiff
path: root/zenstore/compactcas.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2022-03-23 14:47:44 +0100
committerDan Engelbrecht <[email protected]>2022-03-31 11:29:27 +0200
commit1bf0ea70b8860618002affff331ec98d28de554b (patch)
treefa187b9416f5bb21ba78301c0847533b349f6a0e /zenstore/compactcas.cpp
parentAdd CasLog::Append with multiple entries (diff)
downloadzen-1bf0ea70b8860618002affff331ec98d28de554b.tar.xz
zen-1bf0ea70b8860618002affff331ec98d28de554b.zip
cleanup
Diffstat (limited to 'zenstore/compactcas.cpp')
-rw-r--r--zenstore/compactcas.cpp99
1 files changed, 59 insertions, 40 deletions
diff --git a/zenstore/compactcas.cpp b/zenstore/compactcas.cpp
index c08676b7a..8815b1b53 100644
--- a/zenstore/compactcas.cpp
+++ b/zenstore/compactcas.cpp
@@ -57,6 +57,22 @@ namespace {
return Path.ToPath();
}
+ std::vector<CasDiskIndexEntry> MakeCasDiskEntries(const std::unordered_map<IoHash, BlockStoreDiskLocation>& MovedChunks,
+ const std::unordered_set<IoHash, IoHash::Hasher>& DeletedChunks)
+ {
+ std::vector<CasDiskIndexEntry> result;
+ result.reserve(MovedChunks.size());
+ for (const auto& MovedEntry : MovedChunks)
+ {
+ result.push_back({.Key = MovedEntry.first, .Location = MovedEntry.second});
+ }
+ for (const auto& ChunkHash : DeletedChunks)
+ {
+ result.push_back({.Key = ChunkHash, .Flags = CasDiskIndexEntry::kTombstone});
+ }
+ return result;
+ }
+
} // namespace
//////////////////////////////////////////////////////////////////////////
@@ -325,13 +341,16 @@ CasContainerStrategy::Scrub(ScrubContext& Ctx)
// Deal with bad chunks by removing them from our lookup map
std::vector<IoHash> BadChunkHashes;
+ BadChunkHashes.reserve(BadChunks.size());
- RwLock::ExclusiveLockScope _(m_LocationMapLock);
- for (const CasDiskIndexEntry& Entry : BadChunks)
{
- BadChunkHashes.push_back(Entry.Key);
- m_CasLog.Append({.Key = Entry.Key, .Location = Entry.Location, .Flags = CasDiskIndexEntry::kTombstone});
- m_LocationMap.erase(Entry.Key);
+ RwLock::ExclusiveLockScope _(m_LocationMapLock);
+ for (const CasDiskIndexEntry& Entry : BadChunks)
+ {
+ BadChunkHashes.push_back(Entry.Key);
+ m_CasLog.Append({.Key = Entry.Key, .Location = Entry.Location, .Flags = CasDiskIndexEntry::kTombstone});
+ m_LocationMap.erase(Entry.Key);
+ }
}
// Let whomever it concerns know about the bad chunks. This could
@@ -342,22 +361,19 @@ CasContainerStrategy::Scrub(ScrubContext& Ctx)
}
void
-CasContainerStrategy::UpdateLocationMap(const std::unordered_map<IoHash, BlockStoreDiskLocation>& MovedChunks,
- const std::unordered_set<IoHash, IoHash::Hasher>& DeletedChunks)
+CasContainerStrategy::UpdateLocations(const std::span<CasDiskIndexEntry>& Entries)
{
- for (const auto& MovedEntry : MovedChunks)
+ for (const auto& Entry : Entries)
{
- m_LocationMap[MovedEntry.first] = MovedEntry.second;
- m_CasLog.Append({.Key = MovedEntry.first, .Location = MovedEntry.second});
- }
-
- for (const auto& ChunkHash : DeletedChunks)
- {
- auto KeyIt = m_LocationMap.find(ChunkHash);
- uint64_t ChunkSize = KeyIt->second.GetSize();
- m_CasLog.Append({.Key = ChunkHash, .Location = KeyIt->second, .Flags = CasDiskIndexEntry::kTombstone});
- m_TotalSize.fetch_sub(ChunkSize);
- m_LocationMap.erase(KeyIt);
+ if (Entry.Flags & CasDiskIndexEntry::kTombstone)
+ {
+ auto KeyIt = m_LocationMap.find(Entry.Key);
+ uint64_t ChunkSize = KeyIt->second.GetSize();
+ m_TotalSize.fetch_sub(ChunkSize);
+ m_LocationMap.erase(KeyIt);
+ continue;
+ }
+ m_LocationMap[Entry.Key] = Entry.Location;
}
}
@@ -434,6 +450,7 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
RwLock::SharedLockScope _i(m_InsertLock);
RwLock::SharedLockScope _l(m_LocationMapLock);
Stopwatch Timer;
+ const auto TimerGuard = MakeGuard([&Timer, &WriteBlockTimeUs] { WriteBlockTimeUs += Timer.GetElapsedTimeUs(); });
LocationMap.reserve(m_LocationMap.size());
bool IsWriting = !m_WriteBlock.expired();
uint32_t WritingBlock = m_WriteBlockIndex.load(std::memory_order_acquire);
@@ -446,7 +463,6 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
LocationMap.emplace(Entry.first, Entry.second);
}
BlockCount = m_ChunkBlocks.size();
- WriteBlockTimeUs += Timer.GetElapsedTimeUs();
}
if (LocationMap.empty())
@@ -535,13 +551,15 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
if (KeepMap.empty())
{
std::shared_ptr<BlockStoreFile> BlockFile;
- const auto& DeleteMap = DeleteChunks[ChunkMapIndex];
+ const auto& DeleteMap = DeleteChunks[ChunkMapIndex];
+ auto LogEntries = MakeCasDiskEntries({}, DeleteMap);
{
RwLock::ExclusiveLockScope _i(m_LocationMapLock);
Stopwatch Timer;
- UpdateLocationMap({}, DeleteMap);
+ const auto TimerGuard = MakeGuard([&Timer, &ReadBlockTimeUs] { ReadBlockTimeUs += Timer.GetElapsedTimeUs(); });
+ UpdateLocations(LogEntries);
+ m_CasLog.Append(LogEntries);
m_ChunkBlocks[BlockIndex].swap(BlockFile);
- ReadBlockTimeUs += Timer.GetElapsedTimeUs();
}
DeletedChunks.insert(DeletedChunks.end(), DeleteMap.begin(), DeleteMap.end());
ZEN_DEBUG("marking cas store file for delete {}, block {}", m_ContainerBaseName, std::to_string(BlockIndex));
@@ -573,10 +591,13 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
if (!NewBlockFile || (WriteOffset + Chunk.size() > m_MaxBlockSize))
{
uint32_t NextBlockIndex = m_WriteBlockIndex.load(std::memory_order::memory_order_relaxed);
+ auto LogEntries = MakeCasDiskEntries(MovedBlockChunks, {});
{
RwLock::ExclusiveLockScope _l(m_LocationMapLock);
Stopwatch Timer;
- UpdateLocationMap(MovedBlockChunks, {});
+ const auto TimerGuard = MakeGuard([&Timer, &ReadBlockTimeUs] { ReadBlockTimeUs += Timer.GetElapsedTimeUs(); });
+ UpdateLocations(LogEntries);
+ m_CasLog.Append(LogEntries);
if (m_ChunkBlocks.size() == BlockStoreDiskLocation::MaxBlockIndex)
{
ZEN_ERROR("unable to allocate a new block in {}, count limit {} exeeded",
@@ -591,7 +612,6 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
auto NewBlockPath = BuildUcasPath(m_BlocksBasePath, NextBlockIndex);
NewBlockFile = std::make_shared<BlockStoreFile>(NewBlockPath);
m_ChunkBlocks[NextBlockIndex] = NewBlockFile;
- ReadBlockTimeUs += Timer.GetElapsedTimeUs();
}
for (const auto& MovedEntry : MovedBlockChunks)
@@ -619,8 +639,8 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
NiceBytes(Space.Free));
RwLock::ExclusiveLockScope _l(m_LocationMapLock);
Stopwatch Timer;
+ const auto TimerGuard = MakeGuard([&Timer, &ReadBlockTimeUs] { ReadBlockTimeUs += Timer.GetElapsedTimeUs(); });
m_ChunkBlocks.erase(NextBlockIndex);
- ReadBlockTimeUs += Timer.GetElapsedTimeUs();
return;
}
@@ -646,13 +666,15 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx)
}
Chunk.clear();
- const auto& DeleteMap = DeleteChunks[ChunkMapIndex];
+ const auto& DeleteMap = DeleteChunks[ChunkMapIndex];
+ auto LogEntries = MakeCasDiskEntries(MovedBlockChunks, DeleteMap);
{
RwLock::ExclusiveLockScope _i(m_LocationMapLock);
Stopwatch Timer;
- UpdateLocationMap(MovedBlockChunks, DeleteMap);
+ const auto TimerGuard = MakeGuard([&Timer, &ReadBlockTimeUs] { ReadBlockTimeUs += Timer.GetElapsedTimeUs(); });
+ UpdateLocations(LogEntries);
+ m_CasLog.Append(LogEntries);
m_ChunkBlocks[BlockIndex].reset();
- ReadBlockTimeUs += Timer.GetElapsedTimeUs();
}
for (const auto& MovedEntry : MovedBlockChunks)
{
@@ -792,10 +814,7 @@ CasContainerStrategy::MakeIndexSnapshot()
TCasLogFile<CasDiskIndexEntry> RecoveredCasLog;
RecoveredCasLog.Open(SRecoveredlogPath, true);
- for (const auto& Record : Records)
- {
- RecoveredCasLog.Append(Record);
- }
+ RecoveredCasLog.Append(Records);
RecoveredCasLog.Close();
fs::remove(SlogPath);
@@ -903,14 +922,14 @@ CasContainerStrategy::OpenContainer(bool IsNewStore)
{
if (std::filesystem::is_regular_file(LegacySobsPath))
{
- uint32_t NewBlockIndex = {};
- Stopwatch MigrationTimer;
+ uint32_t NewBlockIndex = {};
+ Stopwatch MigrationTimer;
uint64_t TotalSize = 0;
- const auto Guard = MakeGuard([this, &MigrationTimer, &NewBlockIndex, &TotalSize] {
+ const auto Guard = MakeGuard([this, &MigrationTimer, &NewBlockIndex, &TotalSize] {
ZEN_INFO("migrated store {} to {} chunks in {} ({})",
- m_Config.RootDirectory / m_ContainerBaseName,
- NewBlockIndex + 1,
- NiceTimeSpanMs(MigrationTimer.GetElapsedTimeMs()),
+ m_Config.RootDirectory / m_ContainerBaseName,
+ NewBlockIndex + 1,
+ NiceTimeSpanMs(MigrationTimer.GetElapsedTimeMs()),
NiceBytes(TotalSize));
});
@@ -991,7 +1010,7 @@ CasContainerStrategy::OpenContainer(bool IsNewStore)
m_CasLog.Open(SlogPath, true);
std::unique_ptr<BlockStoreFile> NewBlockFile;
- uint64_t WriteOffset = {};
+ uint64_t WriteOffset = {};
std::vector<uint8_t> Chunk;
for (const auto& ChunkHash : ChunkHashes)