diff options
| author | Dan Engelbrecht <[email protected]> | 2022-03-23 12:04:46 +0100 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2022-03-31 11:29:27 +0200 |
| commit | 3e71dd2c60e2568274fac1b7d6af248163873b72 (patch) | |
| tree | 3d464bc20ff4483b10fad0198e26eecd336848ba /zenstore/compactcas.cpp | |
| parent | Use blockstore in compactcas (diff) | |
| download | zen-3e71dd2c60e2568274fac1b7d6af248163873b72.tar.xz zen-3e71dd2c60e2568274fac1b7d6af248163873b72.zip | |
cleanup duplicate code in CollectGarbage
Diffstat (limited to 'zenstore/compactcas.cpp')
| -rw-r--r-- | zenstore/compactcas.cpp | 99 |
1 files changed, 46 insertions, 53 deletions
diff --git a/zenstore/compactcas.cpp b/zenstore/compactcas.cpp index fbdac491e..dc16305d0 100644 --- a/zenstore/compactcas.cpp +++ b/zenstore/compactcas.cpp @@ -341,6 +341,26 @@ CasContainerStrategy::Scrub(ScrubContext& Ctx) } void +CasContainerStrategy::UpdateLocationMap(const std::unordered_map<IoHash, BlockStoreDiskLocation>& MovedChunks, + const std::unordered_set<IoHash, IoHash::Hasher>& DeletedChunks) +{ + for (const auto& MovedEntry : MovedChunks) + { + 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); + } +} + +void CasContainerStrategy::CollectGarbage(GcContext& GcCtx) { namespace fs = std::filesystem; @@ -483,20 +503,13 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) if (KeepMap.empty()) { std::shared_ptr<BlockStoreFile> BlockFile; + const auto& DeleteMap = DeleteChunks[ChunkMapIndex]; { RwLock::ExclusiveLockScope _i(m_LocationMapLock); - const auto& DeleteMap = DeleteChunks[ChunkMapIndex]; - for (const auto& ChunkHash : DeleteMap) - { - 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); - } - DeletedChunks.insert(DeletedChunks.end(), DeleteMap.begin(), DeleteMap.end()); + UpdateLocationMap({}, DeleteMap); m_ChunkBlocks[BlockIndex].swap(BlockFile); } + DeletedChunks.insert(DeletedChunks.end(), DeleteMap.begin(), DeleteMap.end()); ZEN_DEBUG("marking cas store file for delete {}, block {}", m_ContainerBaseName, std::to_string(BlockIndex)); std::error_code Ec; BlockFile->MarkAsDeleteOnClose(Ec); @@ -528,25 +541,13 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) uint32_t NextBlockIndex = m_WriteBlockIndex.load(std::memory_order::memory_order_relaxed); { RwLock::ExclusiveLockScope _l(m_LocationMapLock); - if (NewBlockFile) - { - 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() == BlockStoreDiskLocation::MaxBlockIndex) - { - ZEN_ERROR("unable to allocate a new block in {}, count limit {} exeeded", - m_ContainerBaseName, - static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1); - return; - } - } + UpdateLocationMap(MovedBlockChunks, {}); if (m_ChunkBlocks.size() == BlockStoreDiskLocation::MaxBlockIndex) { - throw std::runtime_error(fmt::format("unable to allocate a new block in {}", m_ContainerBaseName)); + ZEN_ERROR("unable to allocate a new block in {}, count limit {} exeeded", + m_ContainerBaseName, + static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1); + return; } while (m_ChunkBlocks.contains(NextBlockIndex)) { @@ -557,6 +558,12 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) m_ChunkBlocks[NextBlockIndex] = NewBlockFile; } + for (const auto& MovedEntry : MovedBlockChunks) + { + MovedChunks.push_back(MovedEntry.first); + } + MovedBlockChunks.clear(); + std::error_code Error; DiskSpace Space = DiskSpaceInfo(m_Config.RootDirectory, Error); if (Error) @@ -591,8 +598,7 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) NewBlockFile->Create(m_MaxBlockSize); } NewBlockIndex = NextBlockIndex; - MovedBlockChunks.clear(); - WriteOffset = 0; + WriteOffset = 0; } NewBlockFile->Write(Chunk.data(), Chunk.size(), WriteOffset); @@ -602,32 +608,19 @@ CasContainerStrategy::CollectGarbage(GcContext& GcCtx) } Chunk.clear(); - if (NewBlockFile) + const auto& DeleteMap = DeleteChunks[ChunkMapIndex]; { - const auto& DeleteMap = DeleteChunks[ChunkMapIndex]; - // Remap moved chunks to the new block file - { - 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); - 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); - } - m_ChunkBlocks[BlockIndex].reset(); - } - MovedBlockChunks.clear(); - DeletedChunks.insert(DeletedChunks.end(), DeleteMap.begin(), DeleteMap.end()); + RwLock::ExclusiveLockScope _i(m_LocationMapLock); + UpdateLocationMap(MovedBlockChunks, DeleteMap); + m_ChunkBlocks[BlockIndex].reset(); + } + for (const auto& MovedEntry : MovedBlockChunks) + { + MovedChunks.push_back(MovedEntry.first); } + DeletedChunks.insert(DeletedChunks.end(), DeleteMap.begin(), DeleteMap.end()); + MovedBlockChunks.clear(); + ZEN_DEBUG("marking cas store file for delete {}, block index {}", m_ContainerBaseName, BlockIndex); std::error_code Ec; OldBlockFile->MarkAsDeleteOnClose(Ec); |