From 45c7f6c6987fd24ed4f444dac34b13216bab108a Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 5 Oct 2023 14:58:42 +0200 Subject: Fix curruption of disk cache bucket index on GC (#448) * make sure we hold the index lock when reading payload data in reclaim space * don't use index snapshot when updating index in reclaim space * check that things have not moved under our feet * don't touch m_Payloads without a lock * start write block index on the highest block index * we don't need to bump writeblockindex when stopping write to a block, we will bump appropriately when we start a new block * changelog --- src/zenstore/blockstore.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/zenstore/blockstore.cpp') diff --git a/src/zenstore/blockstore.cpp b/src/zenstore/blockstore.cpp index f99b0bc4a..968e919d6 100644 --- a/src/zenstore/blockstore.cpp +++ b/src/zenstore/blockstore.cpp @@ -171,6 +171,7 @@ BlockStore::Initialize(const std::filesystem::path& BlocksBasePath, uint64_t Max if (std::filesystem::is_directory(m_BlocksBasePath)) { + uint32_t NextBlockIndex = 0; std::vector FoldersToScan; FoldersToScan.push_back(m_BlocksBasePath); size_t FolderOffset = 0; @@ -202,10 +203,15 @@ BlockStore::Initialize(const std::filesystem::path& BlocksBasePath, uint64_t Max m_TotalSize.fetch_add(BlockFile->FileSize(), std::memory_order::relaxed); m_ChunkBlocks[BlockIndex] = BlockFile; FoundBlocks[BlockIndex] = BlockFile->FileSize(); + if (BlockIndex >= NextBlockIndex) + { + NextBlockIndex = (BlockIndex + 1) & (m_MaxBlockCount - 1); + } } } ++FolderOffset; } + m_WriteBlockIndex.store(NextBlockIndex, std::memory_order_release); } else { @@ -363,14 +369,11 @@ BlockStore::Flush(bool ForceNewBlock) RwLock::ExclusiveLockScope _(m_InsertLock); if (m_CurrentInsertOffset > 0) { - uint32_t WriteBlockIndex = m_WriteBlockIndex.load(std::memory_order_acquire); - WriteBlockIndex = (WriteBlockIndex + 1) & (m_MaxBlockCount - 1); if (m_WriteBlock) { m_WriteBlock->Flush(); } - m_WriteBlock = nullptr; - m_WriteBlockIndex.store(WriteBlockIndex, std::memory_order_release); + m_WriteBlock = nullptr; m_CurrentInsertOffset = 0; } return; -- cgit v1.2.3