]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
osd/PG: clear calc_replicated_acting a bit
authorxie xingguo <xie.xingguo@zte.com.cn>
Thu, 16 Aug 2018 07:14:18 +0000 (15:14 +0800)
committerxie xingguo <xie.xingguo@zte.com.cn>
Fri, 31 Aug 2018 05:51:04 +0000 (13:51 +0800)
- kill usable, use want->size instead
- introduce a (separate) lambda function for sorting

Signed-off-by: xie xingguo <xie.xingguo@zte.com.cn>
src/osd/PG.cc

index 5c12a309662622bb05ba7d54c427a72ebea15ff3..b4221274c0f426a7c3a5200a33ca2663e0609b49 100644 (file)
@@ -1330,7 +1330,7 @@ void PG::calc_replicated_acting(
      << (restrict_to_up_acting ? " restrict_to_up_acting" : "") << std::endl;
   
   // select primary
-  map<pg_shard_t,pg_info_t>::const_iterator primary = all_info.find(up_primary);
+  auto primary = all_info.find(up_primary);
   if (up.size() &&
       !primary->second.is_incomplete() &&
       primary->second.last_update >=
@@ -1347,7 +1347,6 @@ void PG::calc_replicated_acting(
      << " with " << primary->second << std::endl;
   want->push_back(primary->first.osd);
   acting_backfill->insert(primary->first);
-  unsigned usable = 1;
 
   /* We include auth_log_shard->second.log_tail because in GetLog,
    * we will request logs back to the min last_update over our
@@ -1359,10 +1358,8 @@ void PG::calc_replicated_acting(
 
   // select replicas that have log contiguity with primary.
   // prefer up, then acting, then any peer_info osds
-  for (vector<int>::const_iterator i = up.begin();
-       i != up.end();
-       ++i) {
-    pg_shard_t up_cand = pg_shard_t(*i, shard_id_t::NO_SHARD);
+  for (auto i : up) {
+    pg_shard_t up_cand = pg_shard_t(i, shard_id_t::NO_SHARD);
     if (up_cand == primary->first)
       continue;
     const pg_info_t &cur_info = all_info.find(up_cand)->second;
@@ -1372,28 +1369,25 @@ void PG::calc_replicated_acting(
       backfill->insert(up_cand);
       acting_backfill->insert(up_cand);
     } else {
-      want->push_back(*i);
+      want->push_back(i);
       acting_backfill->insert(up_cand);
-      usable++;
-      ss << " osd." << *i << " (up) accepted " << cur_info << std::endl;
+      ss << " osd." << i << " (up) accepted " << cur_info << std::endl;
     }
   }
 
-  if (usable >= size) {
+  if (want->size() >= size) {
     return;
   }
 
   std::vector<std::pair<eversion_t, int>> candidate_by_last_update;
   candidate_by_last_update.reserve(acting.size());
   // This no longer has backfill OSDs, but they are covered above.
-  for (vector<int>::const_iterator i = acting.begin();
-       i != acting.end();
-       ++i) {
-    pg_shard_t acting_cand(*i, shard_id_t::NO_SHARD);
+  for (auto i : acting) {
+    pg_shard_t acting_cand(i, shard_id_t::NO_SHARD);
     // skip up osds we already considered above
     if (acting_cand == primary->first)
       continue;
-    vector<int>::const_iterator up_it = find(up.begin(), up.end(), *i);
+    vector<int>::const_iterator up_it = find(up.begin(), up.end(), i);
     if (up_it != up.end())
       continue;
 
@@ -1403,27 +1397,25 @@ void PG::calc_replicated_acting(
       ss << " shard " << acting_cand << " (acting) REJECTED "
         << cur_info << std::endl;
     } else {
-      candidate_by_last_update.push_back(make_pair(cur_info.last_update, *i));
+      candidate_by_last_update.push_back(make_pair(cur_info.last_update, i));
     }
   }
 
+  auto sort_by_eversion =[](const std::pair<eversion_t, int> &lhs,
+                            const std::pair<eversion_t, int> &rhs) {
+    return lhs.first > rhs.first;
+  };
   // sort by last_update, in descending order.
-  std::sort(candidate_by_last_update.begin(), candidate_by_last_update.end(),
-    [](const std::pair<eversion_t, int> &lhs,
-       const std::pair<eversion_t, int> &rhs) {
-      return lhs.first > rhs.first;
-    }
-  );
-
+  std::sort(candidate_by_last_update.begin(),
+            candidate_by_last_update.end(), sort_by_eversion);
   for (auto &p: candidate_by_last_update) {
-    ceph_assert(usable < size);
+    ceph_assert(want->size() < size);
     want->push_back(p.second);
     pg_shard_t s = pg_shard_t(p.second, shard_id_t::NO_SHARD);
     acting_backfill->insert(s);
     ss << " shard " << s << " (acting) accepted "
        << all_info.find(s)->second << std::endl;
-    usable++;
-    if (usable >= size) {
+    if (want->size() >= size) {
       return;
     }
   }
@@ -1433,27 +1425,26 @@ void PG::calc_replicated_acting(
   }
   candidate_by_last_update.clear();
   candidate_by_last_update.reserve(all_info.size()); // overestimate but fine
-  for (map<pg_shard_t,pg_info_t>::const_iterator i = all_info.begin();
-       i != all_info.end();
-       ++i) {
+  // continue to search stray to find more suitable peers
+  for (auto &i : all_info) {
     // skip up osds we already considered above
-    if (i->first == primary->first)
+    if (i.first == primary->first)
       continue;
-    vector<int>::const_iterator up_it = find(up.begin(), up.end(), i->first.osd);
+    vector<int>::const_iterator up_it = find(up.begin(), up.end(), i.first.osd);
     if (up_it != up.end())
       continue;
     vector<int>::const_iterator acting_it = find(
-      acting.begin(), acting.end(), i->first.osd);
+      acting.begin(), acting.end(), i.first.osd);
     if (acting_it != acting.end())
       continue;
 
-    if (i->second.is_incomplete() ||
-       i->second.last_update < oldest_auth_log_entry) {
-      ss << " shard " << i->first << " (stray) REJECTED "
-        << i->second << std::endl;
+    if (i.second.is_incomplete() ||
+       i.second.last_update < oldest_auth_log_entry) {
+      ss << " shard " << i.first << " (stray) REJECTED " << i.second
+         << std::endl;
     } else {
       candidate_by_last_update.push_back(
-        make_pair(i->second.last_update, i->first.osd));
+        make_pair(i.second.last_update, i.first.osd));
     }
   }
 
@@ -1463,22 +1454,17 @@ void PG::calc_replicated_acting(
   }
 
   // sort by last_update, in descending order.
-  std::sort(candidate_by_last_update.begin(), candidate_by_last_update.end(),
-    [](const std::pair<eversion_t, int> &lhs,
-       const std::pair<eversion_t, int> &rhs) {
-      return lhs.first > rhs.first;
-    }
-  );
+  std::sort(candidate_by_last_update.begin(),
+            candidate_by_last_update.end(), sort_by_eversion);
 
   for (auto &p: candidate_by_last_update) {
-    ceph_assert(usable < size);
+    ceph_assert(want->size() < size);
     want->push_back(p.second);
     pg_shard_t s = pg_shard_t(p.second, shard_id_t::NO_SHARD);
     acting_backfill->insert(s);
     ss << " shard " << s << " (stray) accepted "
        << all_info.find(s)->second << std::endl;
-    usable++;
-    if (usable >= size) {
+    if (want->size() >= size) {
       return;
     }
   }