From bf8e6fdf9a1a417dd5aaf7be7328b4d015dd1caa Mon Sep 17 00:00:00 2001 From: Mehdi Hosseini <116847813+shosseinimotlagh@users.noreply.github.com> Date: Thu, 5 Oct 2023 10:43:17 -0700 Subject: [PATCH] Add cp flush during shutdown in index btree (#194) --- src/include/homestore/index/index_internal.hpp | 1 + src/include/homestore/index/index_table.hpp | 6 ++++++ src/include/homestore/index_service.hpp | 3 +++ src/lib/homestore.cpp | 6 ++++++ src/lib/index/index_service.cpp | 16 ++++++++++++++++ src/tests/test_index_btree.cpp | 1 - 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/include/homestore/index/index_internal.hpp b/src/include/homestore/index/index_internal.hpp index 036be4494..2c8a09849 100644 --- a/src/include/homestore/index/index_internal.hpp +++ b/src/include/homestore/index/index_internal.hpp @@ -54,6 +54,7 @@ class IndexTableBase { virtual ~IndexTableBase() = default; virtual uuid_t uuid() const = 0; virtual uint64_t used_size() const = 0; + virtual void destroy() = 0; }; enum class index_buf_state_t : uint8_t { diff --git a/src/include/homestore/index/index_table.hpp b/src/include/homestore/index/index_table.hpp index fdd0582d0..7818490da 100644 --- a/src/include/homestore/index/index_table.hpp +++ b/src/include/homestore/index/index_table.hpp @@ -53,6 +53,12 @@ class IndexTable : public IndexTableBase, public Btree< K, V > { Btree< K, V >::set_root_node_info(BtreeLinkInfo{m_sb->root_node, m_sb->link_version}); } + void destroy() override { + auto cpg = hs()->cp_mgr().cp_guard(); + auto op_context = (void*)cpg.context(cp_consumer_t::INDEX_SVC); + Btree< K, V >::destroy_btree(op_context); + } + btree_status_t init() { auto cp = hs()->cp_mgr().cp_guard(); auto ret = Btree< K, V >::init((void*)cp.context(cp_consumer_t::INDEX_SVC)); diff --git a/src/include/homestore/index_service.hpp b/src/include/homestore/index_service.hpp index 466c52cdb..9952e3852 100644 --- a/src/include/homestore/index_service.hpp +++ b/src/include/homestore/index_service.hpp @@ -59,6 +59,9 @@ class IndexService { // Start the Index Service void start(); + // Stop the Index Service + void stop(); + // Add/Remove Index Table to/from the index service void add_index_table(const std::shared_ptr< IndexTableBase >& tbl); void remove_index_table(const std::shared_ptr< IndexTableBase >& tbl); diff --git a/src/lib/homestore.cpp b/src/lib/homestore.cpp index 699b61927..502547e19 100644 --- a/src/lib/homestore.cpp +++ b/src/lib/homestore.cpp @@ -213,6 +213,12 @@ void HomeStore::do_start() { void HomeStore::shutdown() { LOGINFO("Homestore shutdown is started"); + + if (has_index_service()) { + m_index_service->stop(); +// m_index_service.reset(); + } + if (has_log_service()) { m_log_service->stop(); m_log_service.reset(); diff --git a/src/lib/index/index_service.cpp b/src/lib/index/index_service.cpp index 602026114..d3d0984b5 100644 --- a/src/lib/index/index_service.cpp +++ b/src/lib/index/index_service.cpp @@ -75,11 +75,27 @@ void IndexService::start() { std::move(std::make_unique< IndexCPCallbacks >(m_wb_cache.get()))); } +void IndexService::stop() { + std::unique_lock lg(m_index_map_mtx); + auto fut = homestore::hs()->cp_mgr().trigger_cp_flush(true /* force */); + auto success = std::move(fut).get(); + HS_REL_ASSERT_EQ(success, true, "CP Flush failed"); + LOGINFO("CP Flush completed"); + + for (auto [id, tbl] : m_index_map) { tbl->destroy(); } +} void IndexService::add_index_table(const std::shared_ptr< IndexTableBase >& tbl) { std::unique_lock lg(m_index_map_mtx); m_index_map.insert(std::make_pair(tbl->uuid(), tbl)); } +void IndexService::remove_index_table(const std::shared_ptr< IndexTableBase >& tbl) { + std::unique_lock lg(m_index_map_mtx); + auto cpg = hs()->cp_mgr().cp_guard(); + auto op_context = (void*)cpg.context(cp_consumer_t::INDEX_SVC); + m_index_map.erase(tbl->uuid()); +} + uint32_t IndexService::node_size() const { return hs()->device_mgr()->atomic_page_size(HSDevType::Fast); } uint64_t IndexService::used_size() const { diff --git a/src/tests/test_index_btree.cpp b/src/tests/test_index_btree.cpp index 5de8b3126..6805f2b0e 100644 --- a/src/tests/test_index_btree.cpp +++ b/src/tests/test_index_btree.cpp @@ -136,7 +136,6 @@ struct BtreeTest : public testing::Test { } void TearDown() override { - this->destroy_btree(); test_common::HSTestHelper::shutdown_homestore(); }