diff options
| author | Dan Engelbrecht <[email protected]> | 2022-03-22 22:51:18 +0100 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2022-03-31 11:29:26 +0200 |
| commit | dcf0d935c2c0b9be07707312a5adcdad850b4840 (patch) | |
| tree | 08da8b62b1243d3a6e689abe1f280edc820c7f4c /zenstore/compactcas.cpp | |
| parent | memory order for atomic values (diff) | |
| download | zen-dcf0d935c2c0b9be07707312a5adcdad850b4840.tar.xz zen-dcf0d935c2c0b9be07707312a5adcdad850b4840.zip | |
Proper stats for Moved Chunks
Diffstat (limited to 'zenstore/compactcas.cpp')
| -rw-r--r-- | zenstore/compactcas.cpp | 100 |
1 files changed, 54 insertions, 46 deletions
diff --git a/zenstore/compactcas.cpp b/zenstore/compactcas.cpp index 170687600..00149593b 100644 --- a/zenstore/compactcas.cpp +++ b/zenstore/compactcas.cpp @@ -606,6 +606,7 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) const bool PerformDelete = GcCtx.IsDeletionMode() && GcCtx.CollectSmallObjects(); uint64_t DeleteCount = {}; + uint64_t MoveCount = {}; uint64_t NewTotalSize = 0; GcCtx.FilterCas(TotalChunkHashes, [&](const IoHash& ChunkHash, bool Keep) { @@ -639,24 +640,21 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) return; } - // { - // RwLock::ExclusiveLockScope _i(m_InsertLock); - // m_CasLog.Flush(); - // } - // Move all chunks in blocks that have chunks removed to new blocks std::shared_ptr<ChunkBlock> NewBlockFile; uint64_t WriteOffset = {}; uint32_t NewBlockIndex = {}; - std::unordered_map<IoHash, CasDiskLocation> MovedChunks; std::vector<IoHash> DeletedChunks; DeletedChunks.reserve(DeleteCount); + std::vector<IoHash> MovedChunks; + DeletedChunks.reserve(MoveCount); + std::unordered_map<IoHash, CasDiskLocation> MovedBlockChunks; for (auto BlockIndex : BlocksToReWrite) { - const size_t ChunkMapIndex = BlockIndexToChunkMapIndex[BlockIndex]; - const auto& KeepMap = KeepChunks[ChunkMapIndex]; + const size_t ChunkMapIndex = BlockIndexToChunkMapIndex[BlockIndex]; + const auto& KeepMap = KeepChunks[ChunkMapIndex]; if (KeepMap.empty()) { std::shared_ptr<ChunkBlock> BlockFile; @@ -684,26 +682,21 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) continue; } - std::shared_ptr<ChunkBlock> BlockFile; - std::unordered_map<IoHash, CasLocation, IoHash::Hasher> Chunks; - Chunks.reserve(KeepMap.size()); + std::shared_ptr<ChunkBlock> OldBlockFile; { RwLock::SharedLockScope _i(m_LocationMapLock); - for (const auto& ChunkHash : KeepMap) - { - auto KeyIt = m_LocationMap.find(ChunkHash); - Chunks.emplace(KeyIt->first, KeyIt->second.Get(m_PayloadAlignment)); - } - BlockFile = m_ChunkBlocks[BlockIndex]; - BlockFile->Open(); + OldBlockFile = m_ChunkBlocks[BlockIndex]; + OldBlockFile->Open(); } + MovedChunks.reserve(MovedChunks.size() + KeepMap.size()); std::vector<uint8_t> Chunk; - for (auto& Entry : Chunks) + for (const auto& ChunkHash : KeepMap) { - const CasLocation ChunkLocation = Entry.second; + auto KeyIt = LocationMap.find(ChunkHash); + const CasLocation ChunkLocation = KeyIt->second; Chunk.resize(ChunkLocation.Size); - BlockFile->Read(Chunk.data(), Chunk.size(), ChunkLocation.Offset); + OldBlockFile->Read(Chunk.data(), Chunk.size(), ChunkLocation.Offset); if (!NewBlockFile || (WriteOffset + Chunk.size() > m_MaxBlockSize)) { @@ -712,10 +705,11 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) RwLock::ExclusiveLockScope _l(m_LocationMapLock); if (NewBlockFile) { - for (const auto& MovedEntry : MovedChunks) + for (const auto& MovedEntry : MovedBlockChunks) { m_LocationMap[MovedEntry.first] = MovedEntry.second; m_CasLog.Append({.Key = MovedEntry.first, .Location = MovedEntry.second}); + MovedChunks.push_back(MovedEntry.first); } if (m_ChunkBlocks.size() == CasDiskLocation::MaxBlockIndex) { @@ -772,54 +766,60 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) NewBlockFile->Create(m_MaxBlockSize); } NewBlockIndex = NextBlockIndex; - MovedChunks.clear(); + MovedBlockChunks.clear(); WriteOffset = 0; } NewBlockFile->Write(Chunk.data(), Chunk.size(), WriteOffset); CasLocation NewChunkLocation(NewBlockIndex, WriteOffset, Chunk.size()); - MovedChunks.emplace(Entry.first, CasDiskLocation(NewChunkLocation, m_PayloadAlignment)); + MovedBlockChunks.emplace(ChunkHash, CasDiskLocation(NewChunkLocation, m_PayloadAlignment)); WriteOffset = RoundUp(WriteOffset + Chunk.size(), m_PayloadAlignment); } Chunk.clear(); - // Remap moved chunks to the new block file - RwLock::ExclusiveLockScope _l(m_LocationMapLock); if (NewBlockFile) { - for (const auto& MovedEntry : MovedChunks) - { - m_LocationMap[MovedEntry.first] = MovedEntry.second; - m_CasLog.Append({.Key = MovedEntry.first, .Location = MovedEntry.second}); - } - const auto& DeleteMap = DeleteChunks[ChunkMapIndex]; - for (const auto& ChunkHash : DeleteMap) + // Remap moved chunks to the new block file { - auto KeyIt = m_LocationMap.find(ChunkHash); - const CasLocation& DeleteChunkLocation = KeyIt->second.Get(m_PayloadAlignment); - m_CasLog.Append({.Key = ChunkHash, .Location = KeyIt->second, .Flags = CasDiskIndexEntry::kTombstone}); - m_TotalSize.fetch_sub(static_cast<uint64_t>(DeleteChunkLocation.Size)); - m_LocationMap.erase(KeyIt); + RwLock::ExclusiveLockScope _l(m_LocationMapLock); + for (const auto& MovedEntry : MovedBlockChunks) + { + m_LocationMap[MovedEntry.first] = MovedEntry.second; + m_CasLog.Append({.Key = MovedEntry.first, .Location = MovedEntry.second}); + MovedChunks.push_back(MovedEntry.first); + } + + for (const auto& ChunkHash : DeleteMap) + { + auto KeyIt = m_LocationMap.find(ChunkHash); + const CasLocation& DeleteChunkLocation = KeyIt->second.Get(m_PayloadAlignment); + m_CasLog.Append({.Key = ChunkHash, .Location = KeyIt->second, .Flags = CasDiskIndexEntry::kTombstone}); + m_TotalSize.fetch_sub(static_cast<uint64_t>(DeleteChunkLocation.Size)); + m_LocationMap.erase(KeyIt); + } + m_ChunkBlocks[BlockIndex].reset(); } + MovedBlockChunks.clear(); DeletedChunks.insert(DeletedChunks.end(), DeleteMap.begin(), DeleteMap.end()); } ZEN_DEBUG("marking cas store file for delete {}, block index {}", m_ContainerBaseName, BlockIndex); std::error_code Ec; - BlockFile->MarkAsDeleteOnClose(Ec); + OldBlockFile->MarkAsDeleteOnClose(Ec); if (Ec) { - ZEN_WARN("Failed to flag file '{}' for deletion: '{}'", BlockFile->GetPath(), Ec.message()); + ZEN_WARN("Failed to flag file '{}' for deletion: '{}'", OldBlockFile->GetPath(), Ec.message()); } - BlockFile.reset(); + OldBlockFile.reset(); } GcCtx.DeletedCas(DeletedChunks); - ZEN_INFO("garbage collection complete '{}', deleted {} and moved {} chunks", + ZEN_INFO("garbage collection complete '{}', deleted {} and moved {} of {} chunks", m_Config.RootDirectory / m_ContainerBaseName, DeletedChunks.size(), - MovedChunks.size()); + MovedChunks.size(), + TotalChunkCount); } void @@ -2135,10 +2135,18 @@ TEST_CASE("compactcas.threadedinsert") // * doctest::skip(true)) int32_t C = 0; while (C < KeepHashes.size()) { - if (C % 3 == 0 && C < KeepHashes.size() - 1) + if (C % 155 == 0) { - KeepHashes[C] = KeepHashes[KeepHashes.size() - 1]; - KeepHashes.pop_back(); + if (C < KeepHashes.size() - 1) + { + KeepHashes[C] = KeepHashes[KeepHashes.size() - 1]; + KeepHashes.pop_back(); + } + if (C + 3 < KeepHashes.size() - 1) + { + KeepHashes[C + 3] = KeepHashes[KeepHashes.size() - 1]; + KeepHashes.pop_back(); + } } C++; } |