]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osdmap: use shared_ptr for addrs, addr vectors
authorSage Weil <sage@newdream.net>
Wed, 25 Apr 2012 22:10:34 +0000 (15:10 -0700)
committerSage Weil <sage@newdream.net>
Thu, 26 Apr 2012 23:55:56 +0000 (16:55 -0700)
We share a lot of identical addresses between map versions because they
don't tend to change very often.  Instead of having a separate copy for
every map, use shared_ptr and share references.  Also use a reference for
the entire addr vector(s) in case no addrs differ at all.

Create new encode/decode macros for vector< shared_ptr<T> >.

Signed-off-by: Sage Weil <sage@newdream.net>
src/include/encoding.h
src/osd/OSDMap.cc
src/osd/OSDMap.h

index 7e5bb3a476f490fd6ea9c01159735e5fab36ac49..9ab05a957aa4101bc7a7deccc53ce353d367eca7 100644 (file)
@@ -469,6 +469,30 @@ inline void decode_nohead(int len, std::vector<T>& v, bufferlist::iterator& p)
     decode(v[i], p);
 }
 
+// vector (shared_ptr)
+template<class T>
+inline void encode(const std::vector<std::tr1::shared_ptr<T> >& v, bufferlist& bl)
+{
+  __u32 n = v.size();
+  encode(n, bl);
+  for (typename std::vector<std::tr1::shared_ptr<T> >::const_iterator p = v.begin(); p != v.end(); ++p)
+    if (*p)
+      encode(**p, bl);
+    else
+      encode(T(), bl);
+}
+template<class T>
+inline void decode(std::vector<std::tr1::shared_ptr<T> >& v, bufferlist::iterator& p)
+{
+  __u32 n;
+  decode(n, p);
+  v.resize(n);
+  for (__u32 i=0; i<n; i++) {
+    v[i].reset(new T());
+    decode(*v[i], p);
+  }
+}
+
 // map (pointers)
 /*
 template<class T, class U>
index a1055ae3c59fdcaf4cd0a72b9cfb095839bae166..db6c7acd569b0909de8005ec82707b975f4c93a2 100644 (file)
@@ -479,9 +479,9 @@ void OSDMap::set_max_osd(int m)
     osd_weight[o] = CEPH_OSD_OUT;
   }
   osd_info.resize(m);
-  osd_addr.resize(m);
-  osd_cluster_addr.resize(m);
-  osd_hb_addr.resize(m);
+  osd_addrs->client_addr.resize(m);
+  osd_addrs->cluster_addr.resize(m);
+  osd_addrs->hb_addr.resize(m);
 
   calc_num_osds();
 }
@@ -548,16 +548,16 @@ void OSDMap::adjust_osd_weights(const map<int,double>& weights, Incremental& inc
 
 int OSDMap::identify_osd(const entity_addr_t& addr) const
 {
-  for (unsigned i=0; i<osd_addr.size(); i++)
-    if ((osd_addr[i] == addr) || (osd_cluster_addr[i] == addr))
+  for (int i=0; i<max_osd; i++)
+    if ((get_addr(i) == addr) || (get_cluster_addr(i) == addr))
       return i;
   return -1;
 }
 
 bool OSDMap::find_osd_on_ip(const entity_addr_t& ip) const
 {
-  for (unsigned i=0; i<osd_addr.size(); i++)
-    if (osd_addr[i].is_same_host(ip) || osd_cluster_addr[i].is_same_host(ip))
+  for (int i=0; i<max_osd; i++)
+    if (get_addr(i).is_same_host(ip) || get_cluster_addr(i).is_same_host(ip))
       return i;
   return -1;
 }
@@ -634,17 +634,17 @@ int OSDMap::apply_incremental(Incremental &inc)
        i != inc.new_up_client.end();
        i++) {
     osd_state[i->first] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
-    osd_addr[i->first] = i->second;
+    osd_addrs->client_addr[i->first].reset(new entity_addr_t(i->second));
     if (inc.new_hb_up.empty())
-      osd_hb_addr[i->first] = i->second;       //this is a backward-compatibility hack
+      osd_addrs->hb_addr[i->first].reset(new entity_addr_t(i->second)); //this is a backward-compatibility hack
     else
-      osd_hb_addr[i->first] = inc.new_hb_up[i->first];
+      osd_addrs->hb_addr[i->first].reset(new entity_addr_t(inc.new_hb_up[i->first]));
     osd_info[i->first].up_from = epoch;
   }
   for (map<int32_t,entity_addr_t>::iterator i = inc.new_up_internal.begin();
        i != inc.new_up_internal.end();
        i++)
-    osd_cluster_addr[i->first] = i->second;
+    osd_addrs->cluster_addr[i->first].reset(new entity_addr_t(i->second));
   // info
   for (map<int32_t,epoch_t>::iterator i = inc.new_up_thru.begin();
        i != inc.new_up_thru.end();
@@ -887,7 +887,7 @@ void OSDMap::encode_client_old(bufferlist& bl) const
   ::encode(max_osd, bl);
   ::encode(osd_state, bl);
   ::encode(osd_weight, bl);
-  ::encode(osd_addr, bl);
+  ::encode(osd_addrs->client_addr, bl);
 
   // for ::encode(pg_temp, bl);
   n = pg_temp.size();
@@ -931,7 +931,7 @@ void OSDMap::encode(bufferlist& bl, uint64_t features) const
   ::encode(max_osd, bl);
   ::encode(osd_state, bl);
   ::encode(osd_weight, bl);
-  ::encode(osd_addr, bl);
+  ::encode(osd_addrs->client_addr, bl);
 
   ::encode(pg_temp, bl);
 
@@ -943,10 +943,10 @@ void OSDMap::encode(bufferlist& bl, uint64_t features) const
   // extended
   __u16 ev = CEPH_OSDMAP_VERSION_EXT;
   ::encode(ev, bl);
-  ::encode(osd_hb_addr, bl);
+  ::encode(osd_addrs->hb_addr, bl);
   ::encode(osd_info, bl);
   ::encode(blacklist, bl);
-  ::encode(osd_cluster_addr, bl);
+  ::encode(osd_addrs->cluster_addr, bl);
   ::encode(cluster_snapshot_epoch, bl);
   ::encode(cluster_snapshot, bl);
 }
@@ -1009,7 +1009,7 @@ void OSDMap::decode(bufferlist::iterator& p)
   ::decode(max_osd, p);
   ::decode(osd_state, p);
   ::decode(osd_weight, p);
-  ::decode(osd_addr, p);
+  ::decode(osd_addrs->client_addr, p);
   if (v <= 5) {
     pg_temp.clear();
     ::decode(n, p);
@@ -1032,16 +1032,16 @@ void OSDMap::decode(bufferlist::iterator& p)
   __u16 ev = 0;
   if (v >= 5)
     ::decode(ev, p);
-  ::decode(osd_hb_addr, p);
+  ::decode(osd_addrs->hb_addr, p);
   ::decode(osd_info, p);
   if (v < 5)
     ::decode(pool_name, p);
 
   ::decode(blacklist, p);
   if (ev >= 6)
-    ::decode(osd_cluster_addr, p);
+    ::decode(osd_addrs->cluster_addr, p);
   else
-    osd_cluster_addr.resize(osd_addr.size());
+    osd_addrs->cluster_addr.resize(osd_addrs->client_addr.size());
 
   if (ev >= 7) {
     ::decode(cluster_snapshot_epoch, p);
index c629230104bb3ce8e64bf2abac8f81d7ac1fc49d..9e622d28f9d36b2bf2fa36ecdfa0ea79ee37fbf3 100644 (file)
@@ -170,9 +170,14 @@ private:
   int num_osd;         // not saved
   int32_t max_osd;
   vector<uint8_t> osd_state;
-  vector<entity_addr_t> osd_addr;
-  vector<entity_addr_t> osd_cluster_addr;
-  vector<entity_addr_t> osd_hb_addr;
+
+  struct addrs_s {
+    vector<std::tr1::shared_ptr<entity_addr_t> > client_addr;
+    vector<std::tr1::shared_ptr<entity_addr_t> > cluster_addr;
+    vector<std::tr1::shared_ptr<entity_addr_t> > hb_addr;
+  };
+  std::tr1::shared_ptr<addrs_s> osd_addrs;
+
   vector<__u32>   osd_weight;   // 16.16 fixed point, 0x10000 = "in", 0 = "out"
   vector<osd_info_t> osd_info;
   map<pg_t,vector<int> > pg_temp;  // temp pg mapping (e.g. while we rebuild)
@@ -198,6 +203,7 @@ private:
             pool_max(-1),
             flags(0),
             num_osd(0), max_osd(0),
+            osd_addrs(new addrs_s),
             cluster_snapshot_epoch(0) { 
     memset(&fsid, 0, sizeof(fsid));
   }
@@ -307,34 +313,29 @@ private:
   }
   const entity_addr_t &get_addr(int osd) const {
     assert(exists(osd));
-    return osd_addr[osd];
+    return *osd_addrs->client_addr[osd];
   }
   const entity_addr_t &get_cluster_addr(int osd) const {
     assert(exists(osd));
-    if (osd_cluster_addr[osd] == entity_addr_t())
+    if (*osd_addrs->cluster_addr[osd] == entity_addr_t())
       return get_addr(osd);
-    return osd_cluster_addr[osd];
+    return *osd_addrs->cluster_addr[osd];
   }
   const entity_addr_t &get_hb_addr(int osd) const {
     assert(exists(osd));
-    return osd_hb_addr[osd];
+    return *osd_addrs->hb_addr[osd];
   }
   entity_inst_t get_inst(int osd) const {
-    assert(exists(osd));
     assert(is_up(osd));
-    return entity_inst_t(entity_name_t::OSD(osd), osd_addr[osd]);
+    return entity_inst_t(entity_name_t::OSD(osd), get_addr(osd));
   }
   entity_inst_t get_cluster_inst(int osd) const {
-    assert(exists(osd));
     assert(is_up(osd));
-    if (osd_cluster_addr[osd] == entity_addr_t())
-      return get_inst(osd);
-    return entity_inst_t(entity_name_t::OSD(osd), osd_cluster_addr[osd]);
+    return entity_inst_t(entity_name_t::OSD(osd), get_cluster_addr(osd));
   }
   entity_inst_t get_hb_inst(int osd) const {
-    assert(exists(osd));
     assert(is_up(osd));
-    return entity_inst_t(entity_name_t::OSD(osd), osd_hb_addr[osd]);
+    return entity_inst_t(entity_name_t::OSD(osd), get_hb_addr(osd));
   }
 
   const epoch_t& get_up_from(int osd) const {