From b6977f75dbacc93754a83c7197db413ba719aff3 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 28 Nov 2020 17:09:12 +0800 Subject: [PATCH] mon: use unique_ptr<> to manage lifecycle of Monitor::paxos it is less error-prone, and has better readability. Signed-off-by: Kefu Chai --- src/mon/Monitor.cc | 26 ++++++++++++-------------- src/mon/Monitor.h | 4 ++-- src/mon/OSDMonitor.cc | 4 ++-- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 66d3a190efbf2..ba6ab1ebe78e7 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -238,19 +238,19 @@ Monitor::Monitor(CephContext* cct_, string nm, MonitorDBStore *s, g_conf().get_val("mon_op_history_slow_op_size"), g_conf().get_val("mon_op_history_slow_op_threshold").count()); - paxos = new Paxos(this, "paxos"); + paxos = std::make_unique(this, "paxos"); - paxos_service[PAXOS_MDSMAP].reset(new MDSMonitor(this, paxos, "mdsmap")); - paxos_service[PAXOS_MONMAP].reset(new MonmapMonitor(this, paxos, "monmap")); - paxos_service[PAXOS_OSDMAP].reset(new OSDMonitor(cct, this, paxos, "osdmap")); - paxos_service[PAXOS_LOG].reset(new LogMonitor(this, paxos, "logm")); - paxos_service[PAXOS_AUTH].reset(new AuthMonitor(this, paxos, "auth")); - paxos_service[PAXOS_MGR].reset(new MgrMonitor(this, paxos, "mgr")); - paxos_service[PAXOS_MGRSTAT].reset(new MgrStatMonitor(this, paxos, "mgrstat")); - paxos_service[PAXOS_HEALTH].reset(new HealthMonitor(this, paxos, "health")); - paxos_service[PAXOS_CONFIG].reset(new ConfigMonitor(this, paxos, "config")); + paxos_service[PAXOS_MDSMAP].reset(new MDSMonitor(this, paxos.get(), "mdsmap")); + paxos_service[PAXOS_MONMAP].reset(new MonmapMonitor(this, paxos.get(), "monmap")); + paxos_service[PAXOS_OSDMAP].reset(new OSDMonitor(cct, this, paxos.get(), "osdmap")); + paxos_service[PAXOS_LOG].reset(new LogMonitor(this, paxos.get(), "logm")); + paxos_service[PAXOS_AUTH].reset(new AuthMonitor(this, paxos.get(), "auth")); + paxos_service[PAXOS_MGR].reset(new MgrMonitor(this, paxos.get(), "mgr")); + paxos_service[PAXOS_MGRSTAT].reset(new MgrStatMonitor(this, paxos.get(), "mgrstat")); + paxos_service[PAXOS_HEALTH].reset(new HealthMonitor(this, paxos.get(), "health")); + paxos_service[PAXOS_CONFIG].reset(new ConfigMonitor(this, paxos.get(), "config")); - config_key_service = new ConfigKeyService(this, paxos); + config_key_service = std::make_unique(this, paxos.get()); bool r = mon_caps.parse("allow *", NULL); ceph_assert(r); @@ -285,8 +285,6 @@ Monitor::~Monitor() { op_tracker.on_shutdown(); - delete config_key_service; - delete paxos; delete logger; ceph_assert(session_map.sessions.empty()); } @@ -1370,7 +1368,7 @@ set Monitor::get_sync_targets_names() for (auto& svc : paxos_service) { svc->get_store_prefixes(targets); } - ConfigKeyService *config_key_service_ptr = dynamic_cast(config_key_service); + auto config_key_service_ptr = dynamic_cast(config_key_service.get()); ceph_assert(config_key_service_ptr); config_key_service_ptr->get_store_prefixes(targets); return targets; diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index 936af1ee91d45..cf1f9867bde29 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -218,7 +218,7 @@ public: // -- elector -- private: - Paxos *paxos; + std::unique_ptr paxos; Elector elector; friend class Elector; @@ -684,7 +684,7 @@ public: friend class LogMonitor; friend class ConfigKeyService; - QuorumService *config_key_service; + std::unique_ptr config_key_service; // -- sessions -- MonSessionMap session_map; diff --git a/src/mon/OSDMonitor.cc b/src/mon/OSDMonitor.cc index 1ab9957cd84ee..ebc82e669a958 100644 --- a/src/mon/OSDMonitor.cc +++ b/src/mon/OSDMonitor.cc @@ -9409,7 +9409,7 @@ int OSDMonitor::prepare_command_osd_new( } if (has_lockbox) { - svc = (ConfigKeyService*)mon->config_key_service; + svc = (ConfigKeyService*)(mon->config_key_service.get()); err = svc->validate_osd_new(uuid, dmcrypt_key, ss); if (err < 0) { return err; @@ -9594,7 +9594,7 @@ int OSDMonitor::prepare_command_osd_destroy( } } - ConfigKeyService *svc = (ConfigKeyService*)mon->config_key_service; + auto svc = (ConfigKeyService*)(mon->config_key_service.get()); err = svc->validate_osd_destroy(id, uuid); if (err < 0) { ceph_assert(err == -ENOENT); -- 2.39.5