From 1a0db6de4b788afeebf98120348eba1a0846b1d6 Mon Sep 17 00:00:00 2001 From: Jie Yao Date: Thu, 7 Sep 2023 05:07:38 -0700 Subject: [PATCH 1/5] add chunk and chunkSelector Header --- src/include/homestore/chunk_selector.h | 30 ++++++++++++++ src/include/homestore/vchunk.h | 38 ++++++++++++++++++ src/lib/blkdata_svc/blkdata_service.cpp | 2 +- src/lib/device/CMakeLists.txt | 1 + src/lib/device/chunk.cpp | 2 +- src/lib/device/chunk.h | 2 +- src/lib/device/device_manager.cpp | 2 +- src/lib/device/journal_vdev.cpp | 2 +- src/lib/device/physical_dev.cpp | 2 +- src/lib/device/round_robin_chunk_selector.cpp | 9 ++--- ...ector.hpp => round_robin_chunk_selector.h} | 24 ++++-------- src/lib/device/vchunk.cpp | 39 +++++++++++++++++++ src/lib/device/virtual_dev.cpp | 6 +-- src/lib/device/virtual_dev.hpp | 2 +- src/lib/index/wb_cache.cpp | 2 +- src/lib/logstore/log_store_service.cpp | 3 +- src/lib/logstore/log_stream.cpp | 3 +- src/lib/meta/meta_blk_service.cpp | 4 +- src/tests/test_device_manager.cpp | 3 +- src/tests/test_pdev.cpp | 3 +- 20 files changed, 139 insertions(+), 40 deletions(-) create mode 100644 src/include/homestore/chunk_selector.h create mode 100644 src/include/homestore/vchunk.h rename src/lib/device/{chunk_selector.hpp => round_robin_chunk_selector.h} (73%) create mode 100644 src/lib/device/vchunk.cpp diff --git a/src/include/homestore/chunk_selector.h b/src/include/homestore/chunk_selector.h new file mode 100644 index 000000000..91fc4e79e --- /dev/null +++ b/src/include/homestore/chunk_selector.h @@ -0,0 +1,30 @@ +/********************************************************************************* + * Modifications Copyright 2017-2019 eBay Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + *********************************************************************************/ +#pragma once + +#include + +namespace homestore { +class Chunk; +class ChunkSelector { +public: + ChunkSelector() = default; + virtual void add_chunk(VChunk chunk) = 0; + virtual void foreach_chunks(std::function< void(VChunk&) >&& cb) = 0; + virtual Chunk* select(blk_count_t nblks, const blk_alloc_hints& hints) = 0; + + virtual ~ChunkSelector() = default; +}; +} // namespace homestore diff --git a/src/include/homestore/vchunk.h b/src/include/homestore/vchunk.h new file mode 100644 index 000000000..5611f32b9 --- /dev/null +++ b/src/include/homestore/vchunk.h @@ -0,0 +1,38 @@ +/********************************************************************************* + * Modifications Copyright 2017-2019 eBay Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + *********************************************************************************/ +#pragma once + +#include +#include +#include + +namespace homestore { +class Chunk; + +class VChunk { +public: + VChunk(cshared< Chunk >&); + ~VChunk() = default; + + void set_user_private(const sisl::blob& data); + uint8_t* get_user_private(); + blk_cap_t available_blks(); + uint32_t getPdevID(); + cshared< Chunk > getInternalChunk(); + +private: + cshared< Chunk > internalChunk; +}; +}// namespace homestore diff --git a/src/lib/blkdata_svc/blkdata_service.cpp b/src/lib/blkdata_svc/blkdata_service.cpp index 45eb7fef5..6b69bb376 100644 --- a/src/lib/blkdata_svc/blkdata_service.cpp +++ b/src/lib/blkdata_svc/blkdata_service.cpp @@ -15,9 +15,9 @@ *********************************************************************************/ #include #include +#include "device/chunk.h" #include "device/virtual_dev.hpp" #include "device/physical_dev.hpp" // vdev_info_block -#include "device/chunk.h" #include "common/homestore_config.hpp" // is_data_drive_hdd #include "common/homestore_assert.hpp" #include "common/error.h" diff --git a/src/lib/device/CMakeLists.txt b/src/lib/device/CMakeLists.txt index 9296a3107..ccd4f9030 100644 --- a/src/lib/device/CMakeLists.txt +++ b/src/lib/device/CMakeLists.txt @@ -11,5 +11,6 @@ target_sources(hs_device PRIVATE journal_vdev.cpp chunk.cpp round_robin_chunk_selector.cpp + vchunk.cpp ) target_link_libraries(hs_device hs_common ${COMMON_DEPS}) diff --git a/src/lib/device/chunk.cpp b/src/lib/device/chunk.cpp index 2549658dc..e89d36aca 100644 --- a/src/lib/device/chunk.cpp +++ b/src/lib/device/chunk.cpp @@ -64,4 +64,4 @@ nlohmann::json Chunk::get_status([[maybe_unused]] int log_level) const { j["slot_alloced?"] = is_busy(); return j; } -} // namespace homestore \ No newline at end of file +} // namespace homestore diff --git a/src/lib/device/chunk.h b/src/lib/device/chunk.h index e9bbfe952..133245f8b 100644 --- a/src/lib/device/chunk.h +++ b/src/lib/device/chunk.h @@ -72,4 +72,4 @@ class Chunk { private: void write_chunk_info(); }; -} // namespace homestore \ No newline at end of file +} // namespace homestore diff --git a/src/lib/device/device_manager.cpp b/src/lib/device/device_manager.cpp index 064a37044..43b192405 100644 --- a/src/lib/device/device_manager.cpp +++ b/src/lib/device/device_manager.cpp @@ -21,9 +21,9 @@ #include #include +#include "device/chunk.h" #include "device/device.h" #include "device/physical_dev.hpp" -#include "device/chunk.h" #include "device/virtual_dev.hpp" #include "common/homestore_utils.hpp" #include "common/homestore_assert.hpp" diff --git a/src/lib/device/journal_vdev.cpp b/src/lib/device/journal_vdev.cpp index 4f913352b..27dc4ee1f 100644 --- a/src/lib/device/journal_vdev.cpp +++ b/src/lib/device/journal_vdev.cpp @@ -23,10 +23,10 @@ #include #include #include +#include "device/chunk.h" #include "device/device.h" #include "device/physical_dev.hpp" #include "device/journal_vdev.hpp" -#include "device/chunk.h" #include "common/error.h" #include "common/homestore_assert.hpp" #include "common/homestore_utils.hpp" diff --git a/src/lib/device/physical_dev.cpp b/src/lib/device/physical_dev.cpp index f44de8d21..163162efd 100644 --- a/src/lib/device/physical_dev.cpp +++ b/src/lib/device/physical_dev.cpp @@ -25,9 +25,9 @@ #include #include +#include "device/chunk.h" #include "device/physical_dev.hpp" #include "device/device.h" -#include "device/chunk.h" #include "common/homestore_utils.hpp" #include "common/homestore_assert.hpp" diff --git a/src/lib/device/round_robin_chunk_selector.cpp b/src/lib/device/round_robin_chunk_selector.cpp index 4d94fbb6b..53129015e 100644 --- a/src/lib/device/round_robin_chunk_selector.cpp +++ b/src/lib/device/round_robin_chunk_selector.cpp @@ -12,8 +12,7 @@ * specific language governing permissions and limitations under the License. * *********************************************************************************/ -#include "device/chunk_selector.hpp" -#include "device/chunk.h" +#include "round_robin_chunk_selector.h" namespace homestore { RoundRobinChunkSelector::RoundRobinChunkSelector(bool dynamic_chunk_add) : m_dynamic_chunk_add{dynamic_chunk_add} { @@ -21,14 +20,14 @@ RoundRobinChunkSelector::RoundRobinChunkSelector(bool dynamic_chunk_add) : m_dyn "Dynamically adding chunk to chunkselector is not supported, need RCU to make it thread safe"); } -void RoundRobinChunkSelector::add_chunk(cshared< Chunk >& chunk) { m_chunks.push_back(chunk); } +void RoundRobinChunkSelector::add_chunk(VChunk chunk) { m_chunks.push_back(chunk); } Chunk* RoundRobinChunkSelector::select(blk_count_t, const blk_alloc_hints&) { if (*m_next_chunk_index >= m_chunks.size()) { *m_next_chunk_index = 0; } - return m_chunks[(*m_next_chunk_index)++].get(); + return m_chunks[(*m_next_chunk_index)++].getInternalChunk().get(); } -void RoundRobinChunkSelector::foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) { +void RoundRobinChunkSelector::foreach_chunks(std::function< void(VChunk&) >&& cb) { for (auto& chunk : m_chunks) { cb(chunk); } diff --git a/src/lib/device/chunk_selector.hpp b/src/lib/device/round_robin_chunk_selector.h similarity index 73% rename from src/lib/device/chunk_selector.hpp rename to src/lib/device/round_robin_chunk_selector.h index af0736053..1f0620c8f 100644 --- a/src/lib/device/chunk_selector.hpp +++ b/src/lib/device/round_robin_chunk_selector.h @@ -14,26 +14,18 @@ *********************************************************************************/ #pragma once +#include + #include #include #include -#include -#include +#include +#include "device/chunk.h" namespace homestore { class Chunk; -class ChunkSelector { -public: - ChunkSelector() = default; - virtual void add_chunk(cshared< Chunk >& chunk) = 0; - virtual void foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) = 0; - virtual Chunk* select(blk_count_t nblks, const blk_alloc_hints& hints) = 0; - - virtual ~ChunkSelector() = default; -}; - class RoundRobinChunkSelector : public ChunkSelector { public: RoundRobinChunkSelector(bool dynamic_chunk_add = false); @@ -43,14 +35,12 @@ class RoundRobinChunkSelector : public ChunkSelector { RoundRobinChunkSelector& operator=(RoundRobinChunkSelector&&) noexcept = delete; ~RoundRobinChunkSelector() = default; - void add_chunk(cshared< Chunk >& chunk) override; - + void add_chunk(VChunk chunk) override; Chunk* select(blk_count_t nblks, const blk_alloc_hints& hints) override; - - void foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) override; + void foreach_chunks(std::function< void(VChunk&) >&& cb) override; private: - std::vector< shared< Chunk > > m_chunks; + std::vector< VChunk > m_chunks; folly::ThreadLocal< uint32_t > m_next_chunk_index; bool m_dynamic_chunk_add; // Can we add chunk dynamically }; diff --git a/src/lib/device/vchunk.cpp b/src/lib/device/vchunk.cpp new file mode 100644 index 000000000..9c9c4bce2 --- /dev/null +++ b/src/lib/device/vchunk.cpp @@ -0,0 +1,39 @@ +/********************************************************************************* + * Modifications Copyright 2017-2019 eBay Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + *********************************************************************************/ +#include +#include "blkalloc/blk_allocator.h" +#include "device/chunk.h" + +namespace homestore { + VChunk::VChunk(cshared< Chunk >& chunk) : internalChunk(chunk){} + + void VChunk::set_user_private(const sisl::blob& data){ + internalChunk->set_user_private(data); + } + + uint8_t* VChunk::get_user_private() { + return internalChunk->user_private(); + }; + + blk_cap_t VChunk::available_blks() { + return internalChunk->blk_allocator()->available_blks(); + } + + uint32_t VChunk::getPdevID() { + return internalChunk->physical_dev()->pdev_id(); + } + + cshared< Chunk > VChunk::getInternalChunk() {return internalChunk;} +}// namespace homestore diff --git a/src/lib/device/virtual_dev.cpp b/src/lib/device/virtual_dev.cpp index 4e3cc75dc..4b80ad7b3 100644 --- a/src/lib/device/virtual_dev.cpp +++ b/src/lib/device/virtual_dev.cpp @@ -34,15 +34,15 @@ #include #include -// #include +#include "device/chunk.h" #include "device/physical_dev.hpp" #include "device/device.h" #include "device/virtual_dev.hpp" -#include "device/chunk.h" #include "common/error.h" #include "common/homestore_assert.hpp" #include "common/homestore_utils.hpp" #include "blkalloc/varsize_blk_allocator.h" +#include "device/round_robin_chunk_selector.h" SISL_LOGGING_DECL(device) @@ -435,7 +435,7 @@ uint64_t VirtualDev::used_size() const { } void VirtualDev::cp_flush() { - m_chunk_selector->foreach_chunks([this](cshared< Chunk >& chunk) { chunk->cp_flush(); }); + m_chunk_selector->foreach_chunks([this](VChunk& vchunk) { vchunk.getInternalChunk()->cp_flush(); }); } std::vector< shared< Chunk > > VirtualDev::get_chunks() const { return m_all_chunks; } diff --git a/src/lib/device/virtual_dev.hpp b/src/lib/device/virtual_dev.hpp index 0f5ec5324..cfd1b613a 100644 --- a/src/lib/device/virtual_dev.hpp +++ b/src/lib/device/virtual_dev.hpp @@ -33,7 +33,7 @@ #include #include "device/device.h" -#include "device/chunk_selector.hpp" +#include namespace homestore { class PhysicalDev; diff --git a/src/lib/index/wb_cache.cpp b/src/lib/index/wb_cache.cpp index e6fd793e6..382344435 100644 --- a/src/lib/index/wb_cache.cpp +++ b/src/lib/index/wb_cache.cpp @@ -17,12 +17,12 @@ #include #include #include +#include "device/chunk.h" #include "common/homestore_assert.hpp" #include "wb_cache.hpp" #include "index_cp.hpp" #include "device/virtual_dev.hpp" -#include "device/chunk.h" #include "common/resource_mgr.hpp" SISL_LOGGING_DECL(wbcache) diff --git a/src/lib/logstore/log_store_service.cpp b/src/lib/logstore/log_store_service.cpp index 9ee191837..044d973e3 100644 --- a/src/lib/logstore/log_store_service.cpp +++ b/src/lib/logstore/log_store_service.cpp @@ -23,13 +23,12 @@ #include #include #include +#include "device/chunk.h" #include "common/homestore_assert.hpp" #include "common/homestore_status_mgr.hpp" -#include "device/device.h" #include "device/journal_vdev.hpp" #include "device/physical_dev.hpp" -#include "device/chunk.h" #include "log_store_family.hpp" #include "log_dev.hpp" diff --git a/src/lib/logstore/log_stream.cpp b/src/lib/logstore/log_stream.cpp index 9a198927a..d7c0ce8e2 100644 --- a/src/lib/logstore/log_stream.cpp +++ b/src/lib/logstore/log_stream.cpp @@ -15,12 +15,13 @@ *********************************************************************************/ #include +#include "device/chunk.h" #include "common/homestore_assert.hpp" #include "common/homestore_config.hpp" #include "common/homestore_utils.hpp" #include "log_dev.hpp" #include "device/journal_vdev.hpp" -#include "device/chunk.h" + namespace homestore { SISL_LOGGING_DECL(logstore) diff --git a/src/lib/meta/meta_blk_service.cpp b/src/lib/meta/meta_blk_service.cpp index 98423ef7f..1c40f5376 100644 --- a/src/lib/meta/meta_blk_service.cpp +++ b/src/lib/meta/meta_blk_service.cpp @@ -29,13 +29,13 @@ #include #include +#include "device/chunk.h" +#include #include "common/homestore_flip.hpp" #include "common/homestore_utils.hpp" #include "device/device.h" #include "device/virtual_dev.hpp" #include "device/physical_dev.hpp" -#include "device/chunk.h" -#include "device/chunk_selector.hpp" #include "blkalloc/blk_allocator.h" #include "meta_sb.hpp" diff --git a/src/tests/test_device_manager.cpp b/src/tests/test_device_manager.cpp index 93a95c61d..e0f55d509 100644 --- a/src/tests/test_device_manager.cpp +++ b/src/tests/test_device_manager.cpp @@ -28,10 +28,11 @@ #include #include +#include "device/chunk.h" + #include "device/device.h" #include "device/physical_dev.hpp" #include "device/virtual_dev.hpp" -#include "device/chunk.h" using namespace homestore; SISL_LOGGING_INIT(HOMESTORE_LOG_MODS) diff --git a/src/tests/test_pdev.cpp b/src/tests/test_pdev.cpp index a90f584af..d5670abaf 100644 --- a/src/tests/test_pdev.cpp +++ b/src/tests/test_pdev.cpp @@ -28,9 +28,10 @@ #include #include +#include "device/chunk.h" + #include "device/device.h" #include "device/physical_dev.hpp" -#include "device/chunk.h" using namespace homestore; SISL_LOGGING_INIT(HOMESTORE_LOG_MODS) From 30232dc503487a424cfc5c522f51c58feddca53f Mon Sep 17 00:00:00 2001 From: Jie Yao Date: Thu, 7 Sep 2023 22:46:14 -0700 Subject: [PATCH 2/5] fix comments --- src/include/homestore/chunk_selector.h | 2 +- src/include/homestore/vchunk.h | 5 +++-- src/lib/device/vchunk.cpp | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/include/homestore/chunk_selector.h b/src/include/homestore/chunk_selector.h index 91fc4e79e..54cb9bd9d 100644 --- a/src/include/homestore/chunk_selector.h +++ b/src/include/homestore/chunk_selector.h @@ -1,5 +1,5 @@ /********************************************************************************* - * Modifications Copyright 2017-2019 eBay Inc. + * Modifications Copyright 2017-2023 eBay Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/include/homestore/vchunk.h b/src/include/homestore/vchunk.h index 5611f32b9..ed080cf17 100644 --- a/src/include/homestore/vchunk.h +++ b/src/include/homestore/vchunk.h @@ -1,5 +1,5 @@ /********************************************************************************* - * Modifications Copyright 2017-2019 eBay Inc. + * Modifications Copyright 2017-2023 eBay Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,8 @@ class Chunk; class VChunk { public: - VChunk(cshared< Chunk >&); + VChunk(cshared< Chunk > const&); + ~VChunk() = default; void set_user_private(const sisl::blob& data); diff --git a/src/lib/device/vchunk.cpp b/src/lib/device/vchunk.cpp index 9c9c4bce2..793159e82 100644 --- a/src/lib/device/vchunk.cpp +++ b/src/lib/device/vchunk.cpp @@ -1,5 +1,5 @@ /********************************************************************************* - * Modifications Copyright 2017-2019 eBay Inc. + * Modifications Copyright 2017-2023 eBay Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 68d6fd78c1fe418cdcc5ce2b41372b52cfb658ae Mon Sep 17 00:00:00 2001 From: Jie Yao Date: Sat, 9 Sep 2023 04:32:01 -0700 Subject: [PATCH 3/5] fix comments --- src/include/homestore/chunk_selector.h | 2 +- src/include/homestore/vchunk.h | 10 +++++----- src/lib/device/round_robin_chunk_selector.cpp | 4 ++-- src/lib/device/round_robin_chunk_selector.h | 2 +- src/lib/device/vchunk.cpp | 10 +++++----- src/lib/device/virtual_dev.cpp | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/include/homestore/chunk_selector.h b/src/include/homestore/chunk_selector.h index 54cb9bd9d..818ea5e69 100644 --- a/src/include/homestore/chunk_selector.h +++ b/src/include/homestore/chunk_selector.h @@ -23,7 +23,7 @@ class ChunkSelector { ChunkSelector() = default; virtual void add_chunk(VChunk chunk) = 0; virtual void foreach_chunks(std::function< void(VChunk&) >&& cb) = 0; - virtual Chunk* select(blk_count_t nblks, const blk_alloc_hints& hints) = 0; + virtual Chunk* select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) = 0; virtual ~ChunkSelector() = default; }; diff --git a/src/include/homestore/vchunk.h b/src/include/homestore/vchunk.h index ed080cf17..8064d922b 100644 --- a/src/include/homestore/vchunk.h +++ b/src/include/homestore/vchunk.h @@ -27,11 +27,11 @@ class VChunk { ~VChunk() = default; - void set_user_private(const sisl::blob& data); - uint8_t* get_user_private(); - blk_cap_t available_blks(); - uint32_t getPdevID(); - cshared< Chunk > getInternalChunk(); + void set_user_private(const sisl::blob& data) const; + uint8_t* get_user_private() const; + blk_cap_t available_blks() const; + uint32_t get_pdev_id() const; + cshared< Chunk > get_internal_chunk () const; private: cshared< Chunk > internalChunk; diff --git a/src/lib/device/round_robin_chunk_selector.cpp b/src/lib/device/round_robin_chunk_selector.cpp index 53129015e..bd3701912 100644 --- a/src/lib/device/round_robin_chunk_selector.cpp +++ b/src/lib/device/round_robin_chunk_selector.cpp @@ -22,9 +22,9 @@ RoundRobinChunkSelector::RoundRobinChunkSelector(bool dynamic_chunk_add) : m_dyn void RoundRobinChunkSelector::add_chunk(VChunk chunk) { m_chunks.push_back(chunk); } -Chunk* RoundRobinChunkSelector::select(blk_count_t, const blk_alloc_hints&) { +Chunk* RoundRobinChunkSelector::select_chunk(blk_count_t, const blk_alloc_hints&) { if (*m_next_chunk_index >= m_chunks.size()) { *m_next_chunk_index = 0; } - return m_chunks[(*m_next_chunk_index)++].getInternalChunk().get(); + return m_chunks[(*m_next_chunk_index)++].get_internal_chunk().get(); } void RoundRobinChunkSelector::foreach_chunks(std::function< void(VChunk&) >&& cb) { diff --git a/src/lib/device/round_robin_chunk_selector.h b/src/lib/device/round_robin_chunk_selector.h index 1f0620c8f..cc916c9d3 100644 --- a/src/lib/device/round_robin_chunk_selector.h +++ b/src/lib/device/round_robin_chunk_selector.h @@ -36,7 +36,7 @@ class RoundRobinChunkSelector : public ChunkSelector { ~RoundRobinChunkSelector() = default; void add_chunk(VChunk chunk) override; - Chunk* select(blk_count_t nblks, const blk_alloc_hints& hints) override; + Chunk* select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) override; void foreach_chunks(std::function< void(VChunk&) >&& cb) override; private: diff --git a/src/lib/device/vchunk.cpp b/src/lib/device/vchunk.cpp index 793159e82..fbd50c7aa 100644 --- a/src/lib/device/vchunk.cpp +++ b/src/lib/device/vchunk.cpp @@ -19,21 +19,21 @@ namespace homestore { VChunk::VChunk(cshared< Chunk >& chunk) : internalChunk(chunk){} - void VChunk::set_user_private(const sisl::blob& data){ + void VChunk::set_user_private(const sisl::blob& data) const{ internalChunk->set_user_private(data); } - uint8_t* VChunk::get_user_private() { + uint8_t* VChunk::get_user_private() const { return internalChunk->user_private(); }; - blk_cap_t VChunk::available_blks() { + blk_cap_t VChunk::available_blks() const { return internalChunk->blk_allocator()->available_blks(); } - uint32_t VChunk::getPdevID() { + uint32_t VChunk::get_pdev_id() const { return internalChunk->physical_dev()->pdev_id(); } - cshared< Chunk > VChunk::getInternalChunk() {return internalChunk;} + cshared< Chunk > VChunk::get_internal_chunk() const {return internalChunk;} }// namespace homestore diff --git a/src/lib/device/virtual_dev.cpp b/src/lib/device/virtual_dev.cpp index 4b80ad7b3..a7a0af6ae 100644 --- a/src/lib/device/virtual_dev.cpp +++ b/src/lib/device/virtual_dev.cpp @@ -175,7 +175,7 @@ BlkAllocStatus VirtualDev::do_alloc_blk(blk_count_t nblks, const blk_alloc_hints size_t attempt{0}; do { - chunk = m_chunk_selector->select(nblks, hints); + chunk = m_chunk_selector->select_chunk(nblks, hints); if (chunk == nullptr) { status = BlkAllocStatus::SPACE_FULL; } status = alloc_blk_from_chunk(nblks, hints, out_blkid, chunk); @@ -435,7 +435,7 @@ uint64_t VirtualDev::used_size() const { } void VirtualDev::cp_flush() { - m_chunk_selector->foreach_chunks([this](VChunk& vchunk) { vchunk.getInternalChunk()->cp_flush(); }); + m_chunk_selector->foreach_chunks([this](VChunk& vchunk) { vchunk.get_internal_chunk()->cp_flush(); }); } std::vector< shared< Chunk > > VirtualDev::get_chunks() const { return m_all_chunks; } From bf6004f7f815d450aa1d4529ea62e2b7d2f4ceef Mon Sep 17 00:00:00 2001 From: Jie Yao Date: Sun, 10 Sep 2023 22:09:11 -0700 Subject: [PATCH 4/5] fix comments --- src/include/homestore/vchunk.h | 4 ++-- src/lib/device/chunk.h | 2 +- src/lib/device/vchunk.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/include/homestore/vchunk.h b/src/include/homestore/vchunk.h index 8064d922b..a6ff0eb48 100644 --- a/src/include/homestore/vchunk.h +++ b/src/include/homestore/vchunk.h @@ -27,8 +27,8 @@ class VChunk { ~VChunk() = default; - void set_user_private(const sisl::blob& data) const; - uint8_t* get_user_private() const; + void set_user_private(const sisl::blob& data); + const uint8_t* get_user_private() const; blk_cap_t available_blks() const; uint32_t get_pdev_id() const; cshared< Chunk > get_internal_chunk () const; diff --git a/src/lib/device/chunk.h b/src/lib/device/chunk.h index 133245f8b..edb3ad6d2 100644 --- a/src/lib/device/chunk.h +++ b/src/lib/device/chunk.h @@ -53,7 +53,7 @@ class Chunk { uint16_t chunk_id() const { return static_cast< uint16_t >(m_chunk_info.chunk_id); } uint64_t end_of_chunk() const { return m_chunk_info.end_of_chunk_size; } uint32_t pdev_ordinal() const { return m_chunk_info.chunk_ordinal; } - uint8_t* user_private() { return &m_chunk_info.user_private[0]; } + const uint8_t* user_private() { return &m_chunk_info.user_private[0]; } uint32_t stream_id() const { return m_stream_id; } uint32_t slot_number() const { return m_chunk_slot; } uint32_t vdev_ordinal() const { return m_vdev_ordinal; } diff --git a/src/lib/device/vchunk.cpp b/src/lib/device/vchunk.cpp index fbd50c7aa..31860b446 100644 --- a/src/lib/device/vchunk.cpp +++ b/src/lib/device/vchunk.cpp @@ -19,11 +19,11 @@ namespace homestore { VChunk::VChunk(cshared< Chunk >& chunk) : internalChunk(chunk){} - void VChunk::set_user_private(const sisl::blob& data) const{ + void VChunk::set_user_private(const sisl::blob& data){ internalChunk->set_user_private(data); } - uint8_t* VChunk::get_user_private() const { + const uint8_t* VChunk::get_user_private() const { return internalChunk->user_private(); }; From 87070a1a9e280d80a3a868b465e89b032bf90b43 Mon Sep 17 00:00:00 2001 From: Jie Yao Date: Tue, 12 Sep 2023 03:38:26 -0700 Subject: [PATCH 5/5] use cshared< Chunk > for both select/add api --- src/include/homestore/chunk_selector.h | 6 +++--- src/include/homestore/vchunk.h | 2 +- src/lib/device/round_robin_chunk_selector.cpp | 8 ++++---- src/lib/device/round_robin_chunk_selector.h | 10 ++++------ src/lib/device/virtual_dev.cpp | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/include/homestore/chunk_selector.h b/src/include/homestore/chunk_selector.h index 818ea5e69..5c3a21a20 100644 --- a/src/include/homestore/chunk_selector.h +++ b/src/include/homestore/chunk_selector.h @@ -21,9 +21,9 @@ class Chunk; class ChunkSelector { public: ChunkSelector() = default; - virtual void add_chunk(VChunk chunk) = 0; - virtual void foreach_chunks(std::function< void(VChunk&) >&& cb) = 0; - virtual Chunk* select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) = 0; + virtual void add_chunk(cshared< Chunk >&) = 0; + virtual void foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) = 0; + virtual cshared< Chunk > select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) = 0; virtual ~ChunkSelector() = default; }; diff --git a/src/include/homestore/vchunk.h b/src/include/homestore/vchunk.h index a6ff0eb48..ed58a99a3 100644 --- a/src/include/homestore/vchunk.h +++ b/src/include/homestore/vchunk.h @@ -31,7 +31,7 @@ class VChunk { const uint8_t* get_user_private() const; blk_cap_t available_blks() const; uint32_t get_pdev_id() const; - cshared< Chunk > get_internal_chunk () const; + cshared< Chunk > get_internal_chunk() const; private: cshared< Chunk > internalChunk; diff --git a/src/lib/device/round_robin_chunk_selector.cpp b/src/lib/device/round_robin_chunk_selector.cpp index 3edc9ae3a..aa73f7b56 100644 --- a/src/lib/device/round_robin_chunk_selector.cpp +++ b/src/lib/device/round_robin_chunk_selector.cpp @@ -20,14 +20,14 @@ RoundRobinChunkSelector::RoundRobinChunkSelector(bool dynamic_chunk_add) : m_dyn "Dynamically adding chunk to chunkselector is not supported, need RCU to make it thread safe"); } -void RoundRobinChunkSelector::add_chunk(VChunk chunk) { m_chunks.push_back(chunk); } +void RoundRobinChunkSelector::add_chunk(cshared< Chunk >& chunk) { m_chunks.emplace_back(std::move(chunk)); } -Chunk* RoundRobinChunkSelector::select_chunk(blk_count_t, const blk_alloc_hints&) { +cshared< Chunk > RoundRobinChunkSelector::select_chunk(blk_count_t, const blk_alloc_hints&) { if (*m_next_chunk_index >= m_chunks.size()) { *m_next_chunk_index = 0; } - return m_chunks[(*m_next_chunk_index)++].get_internal_chunk().get(); + return m_chunks[(*m_next_chunk_index)++]; } -void RoundRobinChunkSelector::foreach_chunks(std::function< void(VChunk&) >&& cb) { +void RoundRobinChunkSelector::foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) { for (auto& chunk : m_chunks) { cb(chunk); } diff --git a/src/lib/device/round_robin_chunk_selector.h b/src/lib/device/round_robin_chunk_selector.h index cc916c9d3..5a8f7a530 100644 --- a/src/lib/device/round_robin_chunk_selector.h +++ b/src/lib/device/round_robin_chunk_selector.h @@ -24,8 +24,6 @@ #include "device/chunk.h" namespace homestore { -class Chunk; - class RoundRobinChunkSelector : public ChunkSelector { public: RoundRobinChunkSelector(bool dynamic_chunk_add = false); @@ -35,12 +33,12 @@ class RoundRobinChunkSelector : public ChunkSelector { RoundRobinChunkSelector& operator=(RoundRobinChunkSelector&&) noexcept = delete; ~RoundRobinChunkSelector() = default; - void add_chunk(VChunk chunk) override; - Chunk* select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) override; - void foreach_chunks(std::function< void(VChunk&) >&& cb) override; + void add_chunk(cshared< Chunk >&) override; + cshared< Chunk > select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) override; + void foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) override; private: - std::vector< VChunk > m_chunks; + std::vector< shared< Chunk > > m_chunks; folly::ThreadLocal< uint32_t > m_next_chunk_index; bool m_dynamic_chunk_add; // Can we add chunk dynamically }; diff --git a/src/lib/device/virtual_dev.cpp b/src/lib/device/virtual_dev.cpp index cedf21e64..0d771d4ab 100644 --- a/src/lib/device/virtual_dev.cpp +++ b/src/lib/device/virtual_dev.cpp @@ -197,7 +197,7 @@ BlkAllocStatus VirtualDev::do_alloc_blk(blk_count_t nblks, const blk_alloc_hints size_t attempt{0}; do { - chunk = m_chunk_selector->select_chunk(nblks, hints); + chunk = m_chunk_selector->select_chunk(nblks, hints).get(); if (chunk == nullptr) { status = BlkAllocStatus::SPACE_FULL; } status = alloc_blk_from_chunk(nblks, hints, out_blkid, chunk);