Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osd: replace vectors_equal() with operator==(vector<>, vector<>) #18064

Merged
merged 2 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/include/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,40 @@ DEFINE_MEMORY_POOLS_HELPER(P)

};


// the elements allocated by mempool is in the same memory space as the ones
// allocated by the default allocator. so compare them in an efficient way:
// libstdc++'s std::equal is specialized to use memcmp if T is integer or
// pointer. this is good enough for our usecase. use
// std::is_trivially_copyable<T> to expand the support to more types if
// nececssary.
template<typename T, mempool::pool_index_t pool_index>
bool operator==(const std::vector<T, std::allocator<T>>& lhs,
const std::vector<T, mempool::pool_allocator<pool_index, T>>& rhs)
{
return (lhs.size() == rhs.size() &&
std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}

template<typename T, mempool::pool_index_t pool_index>
bool operator!=(const std::vector<T, std::allocator<T>>& lhs,
const std::vector<T, mempool::pool_allocator<pool_index, T>>& rhs)
{
return !(lhs == rhs);
}

template<typename T, mempool::pool_index_t pool_index>
bool operator==(const std::vector<T, mempool::pool_allocator<pool_index, T>>& lhs,
const std::vector<T, std::allocator<T>>& rhs)
{
return rhs == lhs;
}

template<typename T, mempool::pool_index_t pool_index>
bool operator!=(const std::vector<T, mempool::pool_allocator<pool_index, T>>& lhs,
const std::vector<T, std::allocator<T>>& rhs)
{
return !(lhs == rhs);
}

// Use this for any type that is contained by a container (unless it
// is a class you defined; see below).
Expand Down
2 changes: 1 addition & 1 deletion src/mon/OSDMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2690,7 +2690,7 @@ bool OSDMonitor::preprocess_pgtemp(MonOpRequestRef op)
// an existing pg_primary field to imply a change
if (p->second.size() &&
(osdmap.pg_temp->count(p->first) == 0 ||
!vectors_equal(osdmap.pg_temp->get(p->first), p->second) ||
osdmap.pg_temp->get(p->first) != p->second ||
osdmap.primary_temp->count(p->first)))
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/osd/OSDMap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ void OSDMap::clean_temps(CephContext *cct,
vector<int> raw_up;
int primary;
tmpmap.pg_to_raw_up(pg.first, &raw_up, &primary);
if (vectors_equal(raw_up, pg.second)) {
if (raw_up == pg.second) {
ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first << " "
<< pg.second << " that matches raw_up mapping" << dendl;
if (osdmap.pg_temp->count(pg.first))
Expand Down Expand Up @@ -3712,7 +3712,7 @@ int OSDMap::clean_pg_upmaps(
vector<int> raw;
int primary;
pg_to_raw_osds(p.first, &raw, &primary);
if (vectors_equal(raw, p.second)) {
if (raw == p.second) {
ldout(cct, 10) << " removing redundant pg_upmap " << p.first << " "
<< p.second << dendl;
pending_inc->old_pg_upmap.insert(p.first);
Expand Down
12 changes: 0 additions & 12 deletions src/osd/OSDMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,6 @@ class CephContext;
class CrushWrapper;
class health_check_map_t;

// FIXME C++11 does not have std::equal for two differently-typed containers.
// use this until we move to c++14
template<typename A, typename B>
bool vectors_equal(A a, B b)
{
return
a.size() == b.size() &&
(a.empty() ||
memcmp((char*)&a[0], (char*)&b[0], sizeof(a[0]) * a.size()) == 0);
}


/*
* we track up to two intervals during which the osd was alive and
* healthy. the most recent is [up_from,up_thru), where up_thru is
Expand Down