]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mon/ConfigKeyService: store mon and paxos using reference
authorKefu Chai <kchai@redhat.com>
Sat, 28 Nov 2020 13:01:12 +0000 (21:01 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 6 Dec 2020 15:15:40 +0000 (23:15 +0800)
before this change, we always have a valid pointer as member variable.
so, a reference serve this purpose better.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mon/ConfigKeyService.cc
src/mon/ConfigKeyService.h
src/mon/Monitor.cc

index 10ddf362ed31349de875f39aa3d4ea425dd1f103..4f5c937001fe326d2c26fa583c35d37421daad72 100644 (file)
@@ -58,16 +58,16 @@ using ceph::mono_time;
 using ceph::parse_timespan;
 using ceph::timespan_str;
 
-static ostream& _prefix(std::ostream *_dout, const Monitor *mon,
+static ostream& _prefix(std::ostream *_dout, const Monitor &mon,
                         const ConfigKeyService *service) {
-  return *_dout << "mon." << mon->name << "@" << mon->rank
-               << "(" << mon->get_state_name() << ")." << service->get_name()
+  return *_dout << "mon." << mon.name << "@" << mon.rank
+               << "(" << mon.get_state_name() << ")." << service->get_name()
                 << "(" << service->get_epoch() << ") ";
 }
 
 const string CONFIG_PREFIX = "mon_config_key";
 
-ConfigKeyService::ConfigKeyService(Monitor *m, Paxos *p)
+ConfigKeyService::ConfigKeyService(Monitor &m, Paxos &p)
   : mon(m),
     paxos(p),
     tick_period(g_conf()->mon_tick_interval)
@@ -95,7 +95,7 @@ bool ConfigKeyService::dispatch(MonOpRequestRef op) {
 
 bool ConfigKeyService::in_quorum() const
 {
-  return (mon->is_leader() || mon->is_peon());
+  return (mon.is_leader() || mon.is_peon());
 }
 
 void ConfigKeyService::start_tick()
@@ -106,13 +106,13 @@ void ConfigKeyService::start_tick()
   if (tick_period <= 0)
     return;
 
-  tick_event = new C_MonContext{mon, [this](int r) {
+  tick_event = new C_MonContext{&mon, [this](int r) {
     if (r < 0) {
       return;
     }
     tick();
   }};
-  mon->timer.add_event_after(tick_period, tick_event);
+  mon.timer.add_event_after(tick_period, tick_event);
 }
 
 void ConfigKeyService::set_update_period(double t)
@@ -123,7 +123,7 @@ void ConfigKeyService::set_update_period(double t)
 void ConfigKeyService::cancel_tick()
 {
   if (tick_event)
-    mon->timer.cancel_event(tick_event);
+    mon.timer.cancel_event(tick_event);
   tick_event = nullptr;
 }
 
@@ -142,7 +142,7 @@ void ConfigKeyService::shutdown()
 
 int ConfigKeyService::store_get(const string &key, bufferlist &bl)
 {
-  return mon->store->get(CONFIG_PREFIX, key, bl);
+  return mon.store->get(CONFIG_PREFIX, key, bl);
 }
 
 void ConfigKeyService::get_store_prefixes(set<string>& s) const
@@ -152,20 +152,20 @@ void ConfigKeyService::get_store_prefixes(set<string>& s) const
 
 void ConfigKeyService::store_put(const string &key, bufferlist &bl, Context *cb)
 {
-  MonitorDBStore::TransactionRef t = paxos->get_pending_transaction();
+  MonitorDBStore::TransactionRef t = paxos.get_pending_transaction();
   t->put(CONFIG_PREFIX, key, bl);
   if (cb)
-    paxos->queue_pending_finisher(cb);
-  paxos->trigger_propose();
+    paxos.queue_pending_finisher(cb);
+  paxos.trigger_propose();
 }
 
 void ConfigKeyService::store_delete(const string &key, Context *cb)
 {
-  MonitorDBStore::TransactionRef t = paxos->get_pending_transaction();
+  MonitorDBStore::TransactionRef t = paxos.get_pending_transaction();
   store_delete(t, key);
   if (cb)
-    paxos->queue_pending_finisher(cb);
-  paxos->trigger_propose();
+    paxos.queue_pending_finisher(cb);
+  paxos.trigger_propose();
 }
 
 void ConfigKeyService::store_delete(
@@ -177,13 +177,13 @@ void ConfigKeyService::store_delete(
 
 bool ConfigKeyService::store_exists(const string &key)
 {
-  return mon->store->exists(CONFIG_PREFIX, key);
+  return mon.store->exists(CONFIG_PREFIX, key);
 }
 
 void ConfigKeyService::store_list(stringstream &ss)
 {
   KeyValueDB::Iterator iter =
-    mon->store->get_iterator(CONFIG_PREFIX);
+    mon.store->get_iterator(CONFIG_PREFIX);
 
   JSONFormatter f(true);
   f.open_array_section("keys");
@@ -200,7 +200,7 @@ void ConfigKeyService::store_list(stringstream &ss)
 bool ConfigKeyService::store_has_prefix(const string &prefix)
 {
   KeyValueDB::Iterator iter =
-    mon->store->get_iterator(CONFIG_PREFIX);
+    mon.store->get_iterator(CONFIG_PREFIX);
 
   while (iter->valid()) {
     string key(iter->key());
@@ -227,7 +227,7 @@ static bool is_binary_string(const string& s)
 void ConfigKeyService::store_dump(stringstream &ss, const string& prefix)
 {
   KeyValueDB::Iterator iter =
-    mon->store->get_iterator(CONFIG_PREFIX);
+    mon.store->get_iterator(CONFIG_PREFIX);
 
   dout(10) << __func__ << " prefix '" << prefix << "'" << dendl;
   if (prefix.size()) {
@@ -261,7 +261,7 @@ void ConfigKeyService::store_delete_prefix(
     const string &prefix)
 {
   KeyValueDB::Iterator iter =
-    mon->store->get_iterator(CONFIG_PREFIX);
+    mon.store->get_iterator(CONFIG_PREFIX);
 
   while (iter->valid()) {
     string key(iter->key());
@@ -282,7 +282,7 @@ bool ConfigKeyService::service_dispatch(MonOpRequestRef op)
 
   if (!in_quorum()) {
     dout(1) << __func__ << " not in quorum -- waiting" << dendl;
-    paxos->wait_for_readable(op, new Monitor::C_RetryMessage(mon, op));
+    paxos.wait_for_readable(op, new Monitor::C_RetryMessage(&mon, op));
     return false;
   }
 
@@ -318,8 +318,8 @@ bool ConfigKeyService::service_dispatch(MonOpRequestRef op)
 
   } else if (prefix == "config-key put" ||
             prefix == "config-key set") {
-    if (!mon->is_leader()) {
-      mon->forward_request_leader(op);
+    if (!mon.is_leader()) {
+      mon.forward_request_leader(op);
       // we forward the message; so return now.
       return true;
     }
@@ -345,14 +345,14 @@ bool ConfigKeyService::service_dispatch(MonOpRequestRef op)
 
     // we'll reply to the message once the proposal has been handled
     store_put(key, data,
-             new Monitor::C_Command(*mon, op, 0, ss.str(), 0));
+             new Monitor::C_Command(mon, op, 0, ss.str(), 0));
     // return for now; we'll put the message once it's done.
     return true;
 
   } else if (prefix == "config-key del" ||
              prefix == "config-key rm") {
-    if (!mon->is_leader()) {
-      mon->forward_request_leader(op);
+    if (!mon.is_leader()) {
+      mon.forward_request_leader(op);
       return true;
     }
 
@@ -361,7 +361,7 @@ bool ConfigKeyService::service_dispatch(MonOpRequestRef op)
       ss << "no such key '" << key << "'";
       goto out;
     }
-    store_delete(key, new Monitor::C_Command(*mon, op, 0, "key deleted", 0));
+    store_delete(key, new Monitor::C_Command(mon, op, 0, "key deleted", 0));
     // return for now; we'll put the message once it's done
     return true;
 
@@ -396,7 +396,7 @@ bool ConfigKeyService::service_dispatch(MonOpRequestRef op)
 out:
   if (!cmd->get_source().is_mon()) {
     string rs = ss.str();
-    mon->reply_command(op, ret, rs, rdata, 0);
+    mon.reply_command(op, ret, rs, rdata, 0);
   }
 
   return (ret == 0);
@@ -428,12 +428,12 @@ void ConfigKeyService::do_osd_destroy(int32_t id, uuid_d& uuid)
   string daemon_prefix =
     "daemon-private/osd." + stringify(id) + "/";
 
-  MonitorDBStore::TransactionRef t = paxos->get_pending_transaction();
+  MonitorDBStore::TransactionRef t = paxos.get_pending_transaction();
   for (auto p : { dmcrypt_prefix, daemon_prefix }) {
     store_delete_prefix(t, p);
   }
 
-  paxos->trigger_propose();
+  paxos.trigger_propose();
 }
 
 int ConfigKeyService::validate_osd_new(
@@ -467,7 +467,7 @@ void ConfigKeyService::do_osd_new(
     const uuid_d& uuid,
     const string& dmcrypt_key)
 {
-  ceph_assert(paxos->is_plugged());
+  ceph_assert(paxos.is_plugged());
 
   string dmcrypt_key_prefix = _get_dmcrypt_prefix(uuid, "luks");
   bufferlist dmcrypt_key_value;
index c2eaddb8e57fb736d094907b81b58f042169864d..3d0dfebe3bf44a8c840a6558d4d0a5b759c78cf6 100644 (file)
@@ -29,7 +29,7 @@ public:
     SERVICE_TIMECHECK  = 0x02,
     SERVICE_CONFIG_KEY = 0x03,
   };
-  ConfigKeyService(Monitor *m, Paxos *p);
+  ConfigKeyService(Monitor &m, Paxos &p);
   ~ConfigKeyService() {}
 
   void start(epoch_t new_epoch);
@@ -67,8 +67,8 @@ protected:
   void service_shutdown() { }
 
 private:
-  Monitor *mon;
-  Paxos *paxos;
+  Monitor &mon;
+  Paxos &paxos;
   epoch_t epoch = 0;
 
   bool in_quorum() const;
index da934d978d2d9fc9d132ad5f5321f78874d344fd..6d8b83ca8b6ab3ecf7cea9510d789f9ce08caced 100644 (file)
@@ -249,7 +249,7 @@ Monitor::Monitor(CephContext* cct_, string nm, MonitorDBStore *s,
   paxos_service[PAXOS_HEALTH].reset(new HealthMonitor(*this, *paxos, "health"));
   paxos_service[PAXOS_CONFIG].reset(new ConfigMonitor(*this, *paxos, "config"));
 
-  config_key_service = std::make_unique<ConfigKeyService>(this, paxos.get());
+  config_key_service = std::make_unique<ConfigKeyService>(*this, *paxos);
 
   bool r = mon_caps.parse("allow *", NULL);
   ceph_assert(r);