From 1c69c21da9835b19a4c4dd22b337f13c4d5b697b Mon Sep 17 00:00:00 2001 From: lylei Date: Mon, 13 Mar 2017 19:19:33 +0800 Subject: [PATCH] Con-currency clean up when ns version mismatch #880 (#880) --- src/chunkserver/block_manager.cc | 16 +++++++++++----- src/chunkserver/block_manager.h | 1 + src/chunkserver/data_block.cc | 8 ++------ src/chunkserver/data_block.h | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/chunkserver/block_manager.cc b/src/chunkserver/block_manager.cc index ca04604b..46b3e0f5 100644 --- a/src/chunkserver/block_manager.cc +++ b/src/chunkserver/block_manager.cc @@ -306,22 +306,28 @@ StatusCode BlockManager::RemoveBlock(int64_t block_id) { return kOK; } -// TODO: concurrent & async cleanup bool BlockManager::CleanUp(int64_t namespace_version) { + ThreadPool tp(10); for (auto it = block_map_.begin(); it != block_map_.end();) { Block* block = it->second; - if (block->CleanUp(namespace_version)) { - file_cache_->EraseFileCache(block->GetFilePath()); - block->DecRef(); + if (block->GetNamespaceVersion() != namespace_version) { + tp.AddTask(std::bind(&BlockManager::ClenaUpBlockAsync, this, block)); block_map_.erase(it++); } else { ++it; } } - LOG(INFO, "CleanUp done"); + tp.Stop(true); + LOG(INFO, "CleanUp done. block_map_ size = %lu", block_map_.size()); return true; } +void BlockManager::ClenaUpBlockAsync(Block* block) { + file_cache_->EraseFileCache(block->GetFilePath()); + block->SetDeleted(); + block->DecRef(); +} + bool BlockManager::AddBlock(int64_t block_id, Disk* disk, BlockMeta meta) { Block* block = new Block(meta, disk, file_cache_); block->AddRef(); diff --git a/src/chunkserver/block_manager.h b/src/chunkserver/block_manager.h index d8142024..11aebb75 100644 --- a/src/chunkserver/block_manager.h +++ b/src/chunkserver/block_manager.h @@ -50,6 +50,7 @@ class BlockManager { void Stat(std::string* str); private: void CheckStorePath(const std::string& store_path); + void ClenaUpBlockAsync(Block* block); Disk* PickDisk(int64_t block_id); int64_t FindSmallest(std::vector& iters, int32_t* idx); void LogStatus(); diff --git a/src/chunkserver/data_block.cc b/src/chunkserver/data_block.cc index 86d0c8cd..658ad4a8 100644 --- a/src/chunkserver/data_block.cc +++ b/src/chunkserver/data_block.cc @@ -137,12 +137,8 @@ int64_t Block::DiskUsed() const { return disk_file_size_; } -bool Block::CleanUp(int64_t namespace_version) { - if (namespace_version != disk_->NamespaceVersion()) { - SetDeleted(); - return true; - } - return false; +int64_t Block::GetNamespaceVersion() { + return disk_->NamespaceVersion(); } StatusCode Block::SetDeleted() { diff --git a/src/chunkserver/data_block.h b/src/chunkserver/data_block.h index 62541140..0e0ef19a 100644 --- a/src/chunkserver/data_block.h +++ b/src/chunkserver/data_block.h @@ -47,7 +47,7 @@ class Block { std::string GetFilePath() const; BlockMeta GetMeta() const; int64_t DiskUsed() const; - bool CleanUp(int64_t namespace_version); + int64_t GetNamespaceVersion(); StatusCode SetDeleted(); void SetVersion(int64_t version); int GetVersion() const;