diff --git a/src/osd/CMakeLists.txt b/src/osd/CMakeLists.txt index 67b374f3..622db5a5 100644 --- a/src/osd/CMakeLists.txt +++ b/src/osd/CMakeLists.txt @@ -79,6 +79,6 @@ if (ISAL_LIBRARIES) endif (ISAL_LIBRARIES) # osd_peering_pg_test -add_executable(osd_peering_pg_test EXCLUDE_FROM_ALL osd_peering_pg_test.cpp osd_peering_pg.cpp) +add_executable(osd_peering_pg_test EXCLUDE_FROM_ALL osd_peering_pg_test.cpp) add_dependencies(build_tests osd_peering_pg_test) add_test(NAME osd_peering_pg_test COMMAND osd_peering_pg_test) diff --git a/src/osd/osd_peering_pg.cpp b/src/osd/osd_peering_pg.cpp index 3dbf9d6a..024a3d7c 100644 --- a/src/osd/osd_peering_pg.cpp +++ b/src/osd/osd_peering_pg.cpp @@ -28,6 +28,81 @@ inline bool operator < (const obj_ver_role & a, const obj_ver_role & b) ); } +// A sorted run of one OSD's object list +struct obj_ver_run_t +{ + obj_ver_id *cur, *end; + osd_num_t osd_num; + bool is_stable, is_tomb; + + obj_ver_role head() const + { + return { .oid = cur->oid, .version = cur->version, .osd_num = osd_num, .is_stable = is_stable, .is_tomb = is_tomb }; + } +}; + +// Merges object lists in the walk order: copying and sorting millions of versions takes seconds +struct obj_ver_merge_t +{ + std::vector runs; + std::vector heap; + + static bool run_after(const obj_ver_run_t *a, const obj_ver_run_t *b) + { + return b->head() < a->head(); + } + + void add(osd_num_t osd_num, obj_ver_id *buf, uint64_t n, bool is_stable, bool is_tomb) + { + // Same OSD in a run, so osd_num doesn't take part in the order + auto less = [](const obj_ver_id & a, const obj_ver_id & b) + { + return (obj_ver_role){ .oid = a.oid, .version = a.version } < (obj_ver_role){ .oid = b.oid, .version = b.version }; + }; + size_t first = runs.size(); + uint64_t start = 0; + for (uint64_t i = 1; i <= n; i++) + { + if (i == n || less(buf[i], buf[i-1])) + { + if (runs.size()-first >= 16) + { + // Not a few sorted runs (the new store lists objects unordered): sort it + runs.resize(first); + std::sort(buf, buf+n, less); + runs.push_back({ .cur = buf, .end = buf+n, .osd_num = osd_num, .is_stable = is_stable, .is_tomb = is_tomb }); + return; + } + runs.push_back({ .cur = buf+start, .end = buf+i, .osd_num = osd_num, .is_stable = is_stable, .is_tomb = is_tomb }); + start = i; + } + } + } + + void start() + { + heap.clear(); + for (auto & run: runs) + heap.push_back(&run); + std::make_heap(heap.begin(), heap.end(), run_after); + } + + bool next(obj_ver_role & ov) + { + if (!heap.size()) + return false; + std::pop_heap(heap.begin(), heap.end(), run_after); + auto run = heap.back(); + ov = run->head(); + run->cur++; + if (run->cur < run->end) + std::push_heap(heap.begin(), heap.end(), run_after); + else + heap.pop_back(); + return true; + } +}; + struct obj_piece_ver_t { uint64_t max_ver = 0; @@ -39,6 +114,8 @@ struct pg_obj_state_check_t { pg_t *pg; bool replicated = false; + obj_ver_merge_t merge; + // Versions of the current object std::vector list; int list_pos; int obj_start = 0, obj_end = 0, ver_start = 0, ver_end = 0; @@ -53,6 +130,7 @@ struct pg_obj_state_check_t int log_level; void walk(); + void check_object(); void start_object(); void handle_version(); void finish_object(); @@ -63,22 +141,20 @@ void pg_obj_state_check_t::walk() pg->clean_count = 0; pg->total_count = 0; pg->state = 0; - for (list_pos = 0; list_pos < list.size(); list_pos++) + obj_ver_role ov; + merge.start(); + while (merge.next(ov)) { - if (oid.inode != list[list_pos].oid.inode || - oid.stripe != (list[list_pos].oid.stripe & ~STRIPE_MASK)) + if (list.size() && (list[0].oid.inode != ov.oid.inode || + (list[0].oid.stripe & ~STRIPE_MASK) != (ov.oid.stripe & ~STRIPE_MASK))) { - if (oid.inode != 0) - { - finish_object(); - } - start_object(); + check_object(); } - handle_version(); + list.push_back(ov); } - if (oid.inode != 0) + if (list.size()) { - finish_object(); + check_object(); } if (pg->state & PG_HAS_INVALID) { @@ -99,6 +175,18 @@ void pg_obj_state_check_t::walk() } } +void pg_obj_state_check_t::check_object() +{ + list_pos = 0; + start_object(); + for (; list_pos < list.size(); list_pos++) + { + handle_version(); + } + finish_object(); + list.clear(); +} + void pg_obj_state_check_t::start_object() { obj_start = list_pos; @@ -492,7 +580,6 @@ retry: // FIXME: Write at least some tests for this function void pg_t::calc_object_states(int log_level) { - // Copy all object lists into one array pg_obj_state_check_t st; st.log_level = log_level; st.pg = this; @@ -502,37 +589,32 @@ void pg_t::calc_object_states(int log_level) // Bumped epochs are reported to etcd before any write with a higher version starts, // so a recreated object can never allocate versions below its own history epoch = reported_epoch; - for (auto it: ps->list_results) + for (auto & it: ps->list_results) { - auto nstab = it.second.stable_count; - auto ntomb = it.second.tombstone_count; auto n = it.second.total_count; - auto osd_num = it.first; - uint64_t start = st.list.size(); - st.list.resize(start + n); - obj_ver_id *ov = it.second.buf; - for (uint64_t i = 0; i < n; i++, ov++) + auto nstab = std::min(it.second.stable_count, n); + auto ntomb = std::min(it.second.tombstone_count, n-nstab); + obj_ver_id *buf = it.second.buf; + for (uint64_t i = 0; i < n; i++) { - if ((ov->version >> (64-PG_EPOCH_BITS)) > epoch) + if ((buf[i].version >> (64-PG_EPOCH_BITS)) > epoch) { - epoch = (ov->version >> (64-PG_EPOCH_BITS)); + epoch = (buf[i].version >> (64-PG_EPOCH_BITS)); } - st.list[start+i] = { - .oid = ov->oid, - .version = ov->version, - .osd_num = osd_num, - .is_stable = i < nstab || i >= n - ntomb, - .is_tomb = ntomb > 0 && i >= n - ntomb, - }; } + // LIST returns [stable][unstable][tombstones] + st.merge.add(it.first, buf, nstab, true, false); + st.merge.add(it.first, buf+nstab, n-nstab-ntomb, false, false); + st.merge.add(it.first, buf+n-ntomb, ntomb, true, true); + } + // Walk over merged lists and check object states + st.walk(); + for (auto & it: ps->list_results) + { free(it.second.buf); it.second.buf = NULL; } ps->list_results.clear(); - // Sort - std::sort(st.list.begin(), st.list.end()); - // Walk over it and check object states - st.walk(); if (this->state != PG_ACTIVE) { assert(epoch != (((uint64_t)1 << PG_EPOCH_BITS)-1)); diff --git a/src/osd/osd_peering_pg_test.cpp b/src/osd/osd_peering_pg_test.cpp index 467a2604..23a8cec8 100644 --- a/src/osd/osd_peering_pg_test.cpp +++ b/src/osd/osd_peering_pg_test.cpp @@ -5,8 +5,10 @@ #define _LARGEFILE64_SOURCE #endif +#include +#include #include "malloc_or_die.h" -#include "osd_peering_pg.h" +#include "osd_peering_pg.cpp" #define STRIPE_SHIFT 12 /** @@ -22,7 +24,94 @@ * v1=1s,2s,6s -> misplaced * 2) ... */ -int main(int argc, char *argv[]) + +static uint64_t test_rand_state; + +static uint64_t test_rand() +{ + test_rand_state ^= test_rand_state << 13; + test_rand_state ^= test_rand_state >> 7; + test_rand_state ^= test_rand_state << 17; + return test_rand_state; +} + +// Merged object lists must come out in the same order as after sorting them as a whole, +// whatever the order inside each list: sorted, two sorted parts (store v1 stable list), +// sorted by version ascending (store v1 unstable list), or unordered (the new store) +static void test_merge_order(uint64_t seed) +{ + test_rand_state = seed; + obj_ver_merge_t merge; + std::vector expected; + std::vector bufs; + osd_num_t osd_count = 1 + test_rand() % 8; + for (osd_num_t osd_num = 1; osd_num <= osd_count; osd_num++) + { + std::vector segs[3]; + for (int seg = 0; seg < 3; seg++) + { + std::set uniq; + uint64_t n = test_rand() % 3 ? test_rand() % 2000 : test_rand() % 20; + for (uint64_t i = 0; i < n; i++) + { + uniq.insert((obj_ver_id){ + .oid = { .inode = 1 + test_rand() % 3, .stripe = ((test_rand() % 300) << STRIPE_SHIFT) | (test_rand() % 4) }, + // Segments of one LIST don't share versions + .version = 1 + seg*10 + test_rand() % 4, + }); + } + auto & list = segs[seg]; + list.assign(uniq.begin(), uniq.end()); + int shape = test_rand() % 4; + if (shape == 0 || shape == 1) + { + std::sort(list.begin(), list.end(), [](const obj_ver_id & a, const obj_ver_id & b) + { + return (obj_ver_role){ .oid = a.oid, .version = a.version } < (obj_ver_role){ .oid = b.oid, .version = b.version }; + }); + } + if (shape == 1) + { + std::stable_partition(list.begin(), list.end(), [](const obj_ver_id & ov) { return (ov.oid.stripe >> STRIPE_SHIFT) % 7 != 0; }); + } + else if (shape == 3) + { + for (size_t i = list.size(); i > 1; i--) + std::swap(list[i-1], list[test_rand() % i]); + } + for (auto & ov: list) + { + expected.push_back({ .oid = ov.oid, .version = ov.version, .osd_num = osd_num, .is_stable = seg != 1, .is_tomb = seg == 2 }); + } + } + // One buffer per OSD, [stable][unstable][tombstones], as LIST returns it + size_t n0 = segs[0].size(), n1 = segs[1].size(), n2 = segs[2].size(); + obj_ver_id *buf = (obj_ver_id*)malloc_or_die(sizeof(obj_ver_id) * (n0+n1+n2 ? n0+n1+n2 : 1)); + bufs.push_back(buf); + std::copy(segs[0].begin(), segs[0].end(), buf); + std::copy(segs[1].begin(), segs[1].end(), buf+n0); + std::copy(segs[2].begin(), segs[2].end(), buf+n0+n1); + merge.add(osd_num, buf, n0, true, false); + merge.add(osd_num, buf+n0, n1, false, false); + merge.add(osd_num, buf+n0+n1, n2, true, true); + } + std::sort(expected.begin(), expected.end()); + merge.start(); + obj_ver_role ov; + size_t i = 0; + while (merge.next(ov)) + { + assert(i < expected.size()); + assert(ov.oid == expected[i].oid && ov.version == expected[i].version && ov.osd_num == expected[i].osd_num && + ov.is_stable == expected[i].is_stable && ov.is_tomb == expected[i].is_tomb); + i++; + } + assert(i == expected.size()); + for (auto buf: bufs) + free(buf); +} + +static void test_large_xor() { pg_t pg = { .state = PG_PEERING, @@ -30,6 +119,7 @@ int main(int argc, char *argv[]) .pg_cursize = 3, .pg_size = 3, .pg_minsize = 2, + .pg_data_size = 2, .pg_num = 1, .target_set = { 1, 2, 3 }, .cur_set = { 1, 2, 3 }, @@ -60,6 +150,55 @@ int main(int argc, char *argv[]) { printf("dev: state=%jx\n", it.second.state); } - delete pg.peering_state; + assert(pg.clean_count == 1024*1024*8-10); + assert(pg.state_dict.size() == 1 && pg.state_dict.begin()->second.state == OBJ_DEGRADED); + assert(pg.degraded_objects.size() == 10); + // The unstable v2 on one OSD of 3 is rolled back; that OSD has no v1 of its part + assert(pg.ver_override.size() == 10 && pg.flush_actions.size() == 10); + for (auto & fa: pg.flush_actions) + { + assert(fa.first.osd_num == 1 && fa.second.rollback && fa.second.rollback_to == 0); + } +} + +// Counts reported by a peer beyond its list must not make the merge read past the buffer +static void test_bad_counts() +{ + pg_t pg = { + .state = PG_PEERING, + .scheme = POOL_SCHEME_XOR, + .pg_cursize = 3, + .pg_size = 3, + .pg_minsize = 2, + .pg_data_size = 2, + .pg_num = 1, + .target_set = { 1, 2, 3 }, + .cur_set = { 1, 2, 3 }, + .peering_state = new pg_peering_state_t(), + }; + for (osd_num_t osd_num = 1; osd_num <= 3; osd_num++) + { + pg_list_result_t r = { + .buf = (obj_ver_id*)malloc_or_die(sizeof(obj_ver_id)), + .total_count = 1, + .stable_count = (uint64_t)(osd_num == 1 ? 5 : 1), + .tombstone_count = (uint64_t)(osd_num == 1 ? 5 : 0), + }; + r.buf[0] = { .oid = { .inode = 1, .stripe = osd_num-1 }, .version = 1 }; + pg.peering_state->list_results[osd_num] = r; + } + pg.calc_object_states(0); + assert(pg.clean_count == 1 && pg.state_dict.size() == 0); +} + +int main(int argc, char *argv[]) +{ + for (uint64_t seed = 1; seed <= 200; seed++) + { + test_merge_order(seed); + } + printf("merge order: ok\n"); + test_large_xor(); + test_bad_counts(); return 0; }