From 0801640520014e4ae7ca0e30089c7bbe8dfcb60b Mon Sep 17 00:00:00 2001 From: xie xingguo Date: Thu, 16 Aug 2018 15:14:18 +0800 Subject: [PATCH] osd/PG: clear calc_replicated_acting a bit - kill usable, use want->size instead - introduce a (separate) lambda function for sorting Signed-off-by: xie xingguo --- src/osd/PG.cc | 78 +++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 46 deletions(-) diff --git a/src/osd/PG.cc b/src/osd/PG.cc index 5c12a30966262..b4221274c0f42 100644 --- a/src/osd/PG.cc +++ b/src/osd/PG.cc @@ -1330,7 +1330,7 @@ void PG::calc_replicated_acting( << (restrict_to_up_acting ? " restrict_to_up_acting" : "") << std::endl; // select primary - map::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::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> candidate_by_last_update; candidate_by_last_update.reserve(acting.size()); // This no longer has backfill OSDs, but they are covered above. - for (vector::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::const_iterator up_it = find(up.begin(), up.end(), *i); + vector::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 &lhs, + const std::pair &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 &lhs, - const std::pair &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::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::const_iterator up_it = find(up.begin(), up.end(), i->first.osd); + vector::const_iterator up_it = find(up.begin(), up.end(), i.first.osd); if (up_it != up.end()) continue; vector::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 &lhs, - const std::pair &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; } } -- 2.39.5