]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-kvstore-tool: use unique_ptr<> manage the lifecycle of bluestore and db 39395/head
authorKefu Chai <kchai@redhat.com>
Tue, 10 Oct 2017 14:28:14 +0000 (22:28 +0800)
committerIgor Fedotov <ifedotov@suse.com>
Wed, 10 Feb 2021 14:28:26 +0000 (14:28 +0000)
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 8dc100d3f334cc90f6ef879a33eea34807c0184f)

 Conflicts:
src/tools/ceph_kvstore_tool.cc

src/tools/ceph_kvstore_tool.cc

index 140a5087ffb973bb4df246dfd1f77033983297db..ee7a3b7439bd476a07175ebb099363ed7e875fcd 100644 (file)
@@ -36,53 +36,54 @@ using namespace std;
 
 class StoreTool
 {
-  boost::scoped_ptr<BlueStore> bluestore;
-
-  // TODO: make KeyValueDB enable_shared_from_this
-  // bluestore will hold *db* also, use unique_ptr/shared_ptr will
-  // double free. 
-  KeyValueDB* db;
+#ifdef HAVE_LIBAIO
+  struct Deleter {
+    BlueStore *bluestore;
+    Deleter(BlueStore *store = nullptr)
+    : bluestore(store)
+    {}
+    void operator()(KeyValueDB *db) {
+      if (bluestore) {
+       bluestore->umount();
+       delete bluestore;
+      } else {
+       delete db;
+      }
+    }
+  };
+  std::unique_ptr<KeyValueDB, Deleter> db;
+#else
+  std::unique_ptr<KeyValueDB> db;
+#endif
 
   string store_path;
 
   public:
   StoreTool(string type, const string &path, bool need_open_db=true) : store_path(path) {
-    KeyValueDB *db_ptr;
     if (type == "bluestore-kv") {
 #ifdef HAVE_LIBAIO
       // note: we'll leak this!  the only user is ceph-kvstore-tool and
       // we don't care.
-      bluestore.reset(new BlueStore(g_ceph_context, path));
+      auto bluestore = new BlueStore(g_ceph_context, path);
+      KeyValueDB *db_ptr;
       int r = bluestore->start_kv_only(&db_ptr, need_open_db);
       if (r < 0) {
        exit(1);
       }
+      db = decltype(db){db_ptr, Deleter(bluestore)};
 #else
       cerr << "bluestore not compiled in" << std::endl;
       exit(1);
 #endif
     } else {
-      db_ptr = KeyValueDB::create(g_ceph_context, type, path);
-      if (need_open_db) {
-        int r = db_ptr->open(std::cerr);
-        if (r < 0) {
-         cerr << "failed to open type " << type << " path " << path << ": "
-              << cpp_strerror(r) << std::endl;
-         exit(1);
-       }
-       db = db_ptr;
-      }
-    }
-  }
-
-  ~StoreTool() {
-    if (bluestore) {
-      bluestore->umount();   
-    }
-    else {
-      if (db) {
-        delete db;
+      auto db_ptr = KeyValueDB::create(g_ceph_context, type, path);
+      int r = db_ptr->open(std::cerr);
+      if (r < 0) {
+       cerr << "failed to open type " << type << " path " << path << ": "
+            << cpp_strerror(r) << std::endl;
+       exit(1);
       }
+      db.reset(db_ptr);
     }
   }