]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: MonMap: add priority to mon_into_t
authorKefu Chai <kchai@redhat.com>
Wed, 28 Jun 2017 05:16:52 +0000 (13:16 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 29 Jun 2017 05:48:06 +0000 (13:48 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mon/MonMap.cc
src/mon/MonMap.h

index 836c6901c9cb64a5169726ea935308fe23880e8b..218df9aa843df7d5c838fd66645a00489f881ded 100644 (file)
@@ -20,9 +20,10 @@ using ceph::Formatter;
 
 void mon_info_t::encode(bufferlist& bl, uint64_t features) const
 {
-  ENCODE_START(1, 1, bl);
+  ENCODE_START(2, 1, bl);
   ::encode(name, bl);
   ::encode(public_addr, bl, features);
+  ::encode(priority, bl);
   ENCODE_FINISH(bl);
 }
 
@@ -31,13 +32,17 @@ void mon_info_t::decode(bufferlist::iterator& p)
   DECODE_START(1, p);
   ::decode(name, p);
   ::decode(public_addr, p);
+  if (struct_v >= 2) {
+    ::decode(priority, p);
+  }
   DECODE_FINISH(p);
 }
 
 void mon_info_t::print(ostream& out) const
 {
   out << "mon." << name
-      << " public " << public_addr;
+      << " public " << public_addr
+      << " priority " << priority;
 }
 
 void MonMap::sanitize_mons(map<string,entity_addr_t>& o)
@@ -247,7 +252,7 @@ void MonMap::generate_test_instances(list<MonMap*>& o)
     entity_addr_t local_pub_addr;
     local_pub_addr.parse(local_pub_addr_s, &end_p);
 
-    m->add("filled_pub_addr", local_pub_addr);
+    m->add(mon_info_t("filled_pub_addr", local_pub_addr, 1));
 
     m->add("empty_addr_zero", entity_addr_t());
   }
@@ -510,13 +515,23 @@ int MonMap::build_initial(CephContext *cct, ostream& errout)
     if (addr.get_port() == 0)
       addr.set_port(CEPH_MON_PORT);
 
+    uint16_t priority = 0;
+    if (!conf->get_val_from_conf_file(sections, "mon priority", val, false)) {
+      try {
+        priority = std::stoul(val);
+      } catch (std::logic_error&) {
+        errout << "unable to parse priority for mon." << *m
+               << ": priority='" << val << "'" << std::endl;
+        continue;
+      }
+    }
     // the make sure this mon isn't already in the map
     if (contains(addr))
       remove(get_name(addr));
     if (contains(*m))
       remove(*m);
 
-    add(m->c_str(), addr);
+    add(mon_info_t{*m, addr, priority});
   }
 
   if (size() == 0) {
@@ -530,16 +545,18 @@ int MonMap::build_initial(CephContext *cct, ostream& errout)
       srv_name = srv_name.substr(0, idx);
     }
 
-    map<string, entity_addr_t> addrs;
+    map<string, DNSResolver::Record> records;
     if (DNSResolver::get_instance()->resolve_srv_hosts(cct, srv_name,
-        DNSResolver::SRV_Protocol::TCP, domain, &addrs) != 0) {
+        DNSResolver::SRV_Protocol::TCP, domain, &records) != 0) {
 
       errout << "unable to get monitor info from DNS SRV with service name: " << 
           "ceph-mon" << std::endl;
     }
     else {
-      for (const auto& addr : addrs) {
-        add(addr.first, addr.second);
+      for (const auto& record : records) {
+        add(mon_info_t{record.first,
+                       record.second.addr,
+                       record.second.priority});
       }
     }
   }
index 52b4e232fed2dd59ef7018b112d0bf292b68d096..63cc2209021a2e701241786a2fc30fa6eb8e13ba 100644 (file)
@@ -39,8 +39,15 @@ struct mon_info_t {
    * and other monitors.
    */
   entity_addr_t public_addr;
+  /**
+   * the priority of the mon, the lower value the more preferred
+   */
+  uint16_t priority{0};
 
-  mon_info_t(string &n, entity_addr_t& p_addr)
+  mon_info_t(const string& n, const entity_addr_t& p_addr, uint16_t p)
+    : name(n), public_addr(p_addr), priority(p)
+  {}
+  mon_info_t(const string &n, const entity_addr_t& p_addr)
     : name(n), public_addr(p_addr)
   { }
 
@@ -136,6 +143,18 @@ public:
     }
   }
 
+  /**
+   * Add new monitor to the monmap
+   *
+   * @param m monitor info of the new monitor
+   */
+  void add(mon_info_t &&m) {
+    assert(mon_info.count(m.name) == 0);
+    assert(addr_mons.count(m.public_addr) == 0);
+    mon_info[m.name] = std::move(m);
+    calc_ranks();
+  }
+
   /**
    * Add new monitor to the monmap
    *
@@ -143,14 +162,9 @@ public:
    * @param addr Monitor's public address
    */
   void add(const string &name, const entity_addr_t &addr) {
-    assert(mon_info.count(name) == 0);
-    assert(addr_mons.count(addr) == 0);
-    mon_info_t &m = mon_info[name];
-    m.name = name;
-    m.public_addr = addr;
-    calc_ranks();
+    add(mon_info_t(name, addr));
   }
+
   /**
    * Remove monitor from the monmap
    *