From 799f361e11bb1fbda6264965849bd0c09480789d Mon Sep 17 00:00:00 2001 From: Igor Fedotov Date: Sat, 13 Jan 2024 18:37:14 +0300 Subject: [PATCH] tools/ceph-kvstore-tool: open DB in read-only whenever sufficient Signed-off-by: Igor Fedotov (cherry picked from commit 410d4748f9de94261d421576ed5433d2d0b0d4b3) --- src/os/bluestore/BlueStore.cc | 4 ++-- src/os/bluestore/BlueStore.h | 2 +- src/os/bluestore/bluestore_tool.cc | 6 +++--- src/tools/ceph_kvstore_tool.cc | 13 ++++++++++++- src/tools/kvstore_tool.cc | 10 ++++++---- src/tools/kvstore_tool.h | 3 ++- 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/os/bluestore/BlueStore.cc b/src/os/bluestore/BlueStore.cc index 32cfa23d975..635ae13cf3b 100644 --- a/src/os/bluestore/BlueStore.cc +++ b/src/os/bluestore/BlueStore.cc @@ -7785,10 +7785,10 @@ void BlueStore::_close_around_db() _close_path(); } -int BlueStore::open_db_environment(KeyValueDB **pdb, bool to_repair) +int BlueStore::open_db_environment(KeyValueDB **pdb, bool read_only, bool to_repair) { _kv_only = true; - int r = _open_db_and_around(false, to_repair); + int r = _open_db_and_around(read_only, to_repair); if (r == 0) { *pdb = db; } else { diff --git a/src/os/bluestore/BlueStore.h b/src/os/bluestore/BlueStore.h index c085c8c3e60..91653f30b19 100644 --- a/src/os/bluestore/BlueStore.h +++ b/src/os/bluestore/BlueStore.h @@ -3014,7 +3014,7 @@ public: } int umount() override; - int open_db_environment(KeyValueDB **pdb, bool to_repair); + int open_db_environment(KeyValueDB **pdb, bool read_only, bool to_repair); int close_db_environment(); BlueFS* get_bluefs(); diff --git a/src/os/bluestore/bluestore_tool.cc b/src/os/bluestore/bluestore_tool.cc index b3636b5b5c2..2126c436d17 100644 --- a/src/os/bluestore/bluestore_tool.cc +++ b/src/os/bluestore/bluestore_tool.cc @@ -242,7 +242,7 @@ static void bluefs_import( } BlueStore bluestore(cct, path); KeyValueDB *db_ptr; - r = bluestore.open_db_environment(&db_ptr, false); + r = bluestore.open_db_environment(&db_ptr, false, false); if (r < 0) { cerr << "error preparing db environment: " << cpp_strerror(r) << std::endl; exit(EXIT_FAILURE); @@ -1173,7 +1173,7 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } } - int r = bluestore.open_db_environment(&db_ptr, true); + int r = bluestore.open_db_environment(&db_ptr, false, true); if (r < 0) { cerr << "error preparing db environment: " << cpp_strerror(r) << std::endl; exit(EXIT_FAILURE); @@ -1191,7 +1191,7 @@ int main(int argc, char **argv) } else if (action == "show-sharding") { BlueStore bluestore(cct.get(), path); KeyValueDB *db_ptr; - int r = bluestore.open_db_environment(&db_ptr, false); + int r = bluestore.open_db_environment(&db_ptr, false, false); if (r < 0) { cerr << "error preparing db environment: " << cpp_strerror(r) << std::endl; exit(EXIT_FAILURE); diff --git a/src/tools/ceph_kvstore_tool.cc b/src/tools/ceph_kvstore_tool.cc index d009069de71..bcb90c8fe46 100644 --- a/src/tools/ceph_kvstore_tool.cc +++ b/src/tools/ceph_kvstore_tool.cc @@ -98,9 +98,20 @@ int main(int argc, const char *argv[]) return 1; } + bool read_only = + cmd == "list" || + cmd == "list-crc" || + cmd == "dump" || + cmd == "exists" || + cmd == "get" || + cmd == "crc" || + cmd == "get-size" || + cmd == "store-crc" || + cmd == "stats" || + cmd == "histogram"; bool to_repair = (cmd == "destructive-repair"); bool need_stats = (cmd == "stats"); - StoreTool st(type, path, to_repair, need_stats); + StoreTool st(type, path, read_only, to_repair, need_stats); if (cmd == "destructive-repair") { int ret = st.destructive_repair(); diff --git a/src/tools/kvstore_tool.cc b/src/tools/kvstore_tool.cc index b426d73f47f..720d1150650 100644 --- a/src/tools/kvstore_tool.cc +++ b/src/tools/kvstore_tool.cc @@ -16,6 +16,7 @@ using namespace std; StoreTool::StoreTool(const string& type, const string& path, + bool read_only, bool to_repair, bool need_stats) : store_path(path) @@ -28,7 +29,7 @@ StoreTool::StoreTool(const string& type, if (type == "bluestore-kv") { #ifdef WITH_BLUESTORE - if (load_bluestore(path, to_repair) != 0) + if (load_bluestore(path, read_only, to_repair) != 0) exit(1); #else cerr << "bluestore not compiled in" << std::endl; @@ -37,7 +38,8 @@ StoreTool::StoreTool(const string& type, } else { auto db_ptr = KeyValueDB::create(g_ceph_context, type, path); if (!to_repair) { - if (int r = db_ptr->open(std::cerr); r < 0) { + int r = read_only ? db_ptr->open_read_only(std::cerr) : db_ptr->open(std::cerr); + if (r < 0) { cerr << "failed to open type " << type << " path " << path << ": " << cpp_strerror(r) << std::endl; exit(1); @@ -47,11 +49,11 @@ StoreTool::StoreTool(const string& type, } } -int StoreTool::load_bluestore(const string& path, bool to_repair) +int StoreTool::load_bluestore(const string& path, bool read_only, bool to_repair) { auto bluestore = new BlueStore(g_ceph_context, path); KeyValueDB *db_ptr; - int r = bluestore->open_db_environment(&db_ptr, to_repair); + int r = bluestore->open_db_environment(&db_ptr, read_only, to_repair); if (r < 0) { return -EINVAL; } diff --git a/src/tools/kvstore_tool.h b/src/tools/kvstore_tool.h index 330cba7c98d..a0b906e9658 100644 --- a/src/tools/kvstore_tool.h +++ b/src/tools/kvstore_tool.h @@ -43,9 +43,10 @@ class StoreTool public: StoreTool(const std::string& type, const std::string& path, + bool read_only, bool need_open_db = true, bool need_stats = false); - int load_bluestore(const std::string& path, bool need_open_db); + int load_bluestore(const std::string& path, bool read_only, bool need_open_db); uint32_t traverse(const std::string& prefix, const bool do_crc, const bool do_value_dump, -- 2.39.5