From 9b3b09ce120679c1927a8bc194d04ca97d0131c8 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Thu, 26 Nov 2026 11:40:11 +0300 Subject: [PATCH] osd: Make recovery_pg_switch switch to the next PG pick_next_recovery never remembered which PG it took an object from, so recovery_last_pg.pg_num++ only advanced {0, N} and lower_bound() found the first PG again: PGs were recovered one by one. Since the tombstone cleanup the kind also rotates after every pick, and every kind without work reset the shared position. Keep a separate position for each recovery kind, remember the PG on every pick, scan the next PG from its first object, skip a backfillfull pool from its first PG, and rescan the kind from the beginning when nothing is found after the position. Based on a fix by Vitaliy Idelson. --- .gitea/workflows/test.yml | 19 ++++++++++++++ src/osd/osd.h | 7 ++--- src/osd/osd_flush.cpp | 35 +++++++++++++++++-------- src/osd/osd_peering.cpp | 4 +-- src/osd/osd_primary.cpp | 4 +-- src/osd/osd_primary_sync.cpp | 4 +-- src/test/test_pg_chaos.cpp | 4 +-- tests/run_tests.sh | 1 + tests/test_recovery_pg_switch.sh | 44 ++++++++++++++++++++++++++++++++ 9 files changed, 100 insertions(+), 22 deletions(-) create mode 100755 tests/test_recovery_pg_switch.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..de02a42e 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -2093,6 +2093,25 @@ jobs: echo "" done + test_recovery_pg_switch: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_recovery_pg_switch.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + + test_scrub: runs-on: ubuntu-latest needs: build diff --git a/src/osd/osd.h b/src/osd/osd.h index fbd7f65b..9fa4b453 100644 --- a/src/osd/osd.h +++ b/src/osd/osd.h @@ -220,9 +220,10 @@ class osd_t robin_hood::unordered_set pending_owned_ops; std::map scrub_ops; int recovery_pick = 0; - pool_pg_num_t recovery_last_pg = {}; - object_id recovery_last_oid = {}; - int recovery_pg_done = 0, recovery_done = 0; + pool_pg_num_t recovery_last_pg[OSD_REC_MAX] = {}; + object_id recovery_last_oid[OSD_REC_MAX] = {}; + int recovery_pg_done[OSD_REC_MAX] = {}; + int recovery_done = 0; int recovery_retry_timer_id = -1; osd_op_t *autosync_op = NULL; int autosync_copies_to_delete = 0; diff --git a/src/osd/osd_flush.cpp b/src/osd/osd_flush.cpp index 085cb363..4d26d174 100644 --- a/src/osd/osd_flush.cpp +++ b/src/osd/osd_flush.cpp @@ -284,9 +284,13 @@ bool osd_t::pick_next_recovery(osd_recovery_op_t &op, uint64_t now_us) kind == OSD_REC_MISPLACED ? (PG_ACTIVE | PG_DEGRADED | PG_HAS_MISPLACED) : (PG_ACTIVE | PG_HAS_TOMB); auto check = kind == OSD_REC_DEGRADED ? (PG_ACTIVE | PG_HAS_DEGRADED) : kind == OSD_REC_MISPLACED ? (PG_ACTIVE | PG_HAS_MISPLACED) : (PG_ACTIVE | PG_HAS_TOMB); + // Each kind keeps its own position, the kinds take turns after every pick + auto & last_pg = recovery_last_pg[kind]; + auto & last_oid = recovery_last_oid[kind]; + bool from_start = !last_pg.pool_id; // Restart scanning from the same PG as the last time restart: - for (auto pg_it = pgs.lower_bound(recovery_last_pg); pg_it != pgs.end(); pg_it++) + for (auto pg_it = pgs.lower_bound(last_pg); pg_it != pgs.end(); pg_it++) { auto & src = kind == OSD_REC_DEGRADED ? pg_it->second.degraded_objects : kind == OSD_REC_MISPLACED ? pg_it->second.misplaced_objects : pg_it->second.tomb_objects; @@ -297,11 +301,12 @@ bool osd_t::pick_next_recovery(osd_recovery_op_t &op, uint64_t now_us) if (pool_it != st_cli->pool_config.end() && pool_it->second.backfillfull) { // Skip the pool - recovery_last_pg.pool_id++; + last_pg = { .pool_id = pg_it->first.pool_id+1, .pg_num = 0 }; + last_oid = {}; goto restart; } // Restart scanning from the next object - for (auto obj_it = src.upper_bound(recovery_last_oid); obj_it != src.end(); obj_it++) + for (auto obj_it = src.upper_bound(last_oid); obj_it != src.end(); obj_it++) { if (kind == OSD_REC_TOMBSTONE) { @@ -315,24 +320,32 @@ bool osd_t::pick_next_recovery(osd_recovery_op_t &op, uint64_t now_us) { op.kind = kind; recovery_pick = (kind + 1) % OSD_REC_MAX; - recovery_last_oid = op.oid = obj_it->first; - recovery_pg_done++; + last_pg = pg_it->first; + last_oid = op.oid = obj_it->first; // Switch to another PG after recovery_pg_switch operations // to always mix all PGs during recovery but still benefit // from recovery queue depth greater than 1 - if (recovery_pg_done >= recovery_pg_switch) + if (++recovery_pg_done[kind] >= recovery_pg_switch) { - recovery_pg_done = 0; - recovery_last_pg.pg_num++; - recovery_last_oid = {}; + recovery_pg_done[kind] = 0; + last_pg.pg_num++; + last_oid = {}; } return true; } } } + // The next PG is scanned from its beginning + last_oid = {}; + } + last_pg = {}; + last_oid = {}; + if (!from_start) + { + // Rescan the PGs before the position + from_start = true; + goto restart; } - recovery_last_pg = {}; - recovery_last_oid = {}; } return false; } diff --git a/src/osd/osd_peering.cpp b/src/osd/osd_peering.cpp index 97905088..df2934e6 100644 --- a/src/osd/osd_peering.cpp +++ b/src/osd/osd_peering.cpp @@ -399,8 +399,8 @@ bool osd_t::continue_pg_peering(pg_t & pg) { // Restart recovery from degraded objects this->recovery_pick = 0; - this->recovery_last_pg = {}; - this->recovery_last_oid = {}; + this->recovery_last_pg[OSD_REC_DEGRADED] = {}; + this->recovery_last_oid[OSD_REC_DEGRADED] = {}; } } return true; diff --git a/src/osd/osd_primary.cpp b/src/osd/osd_primary.cpp index 641d9e8d..2f162f6c 100644 --- a/src/osd/osd_primary.cpp +++ b/src/osd/osd_primary.cpp @@ -792,8 +792,8 @@ pg_osd_set_state_t* osd_t::add_object_to_set(pg_t & pg, const object_id oid, con { // Restart recovery from degraded objects recovery_pick = 0; - recovery_last_pg = {}; - recovery_last_oid = {}; + recovery_last_pg[OSD_REC_DEGRADED] = {}; + recovery_last_oid[OSD_REC_DEGRADED] = {}; } ringloop->wakeup(); } diff --git a/src/osd/osd_primary_sync.cpp b/src/osd/osd_primary_sync.cpp index 4a3ae72c..7f3d65c9 100644 --- a/src/osd/osd_primary_sync.cpp +++ b/src/osd/osd_primary_sync.cpp @@ -299,8 +299,8 @@ resume_6: if (ready) { // Re-scan from the start: the cursor may sit past the covered oids - recovery_last_pg = {}; - recovery_last_oid = {}; + recovery_last_pg[OSD_REC_TOMBSTONE] = {}; + recovery_last_oid[OSD_REC_TOMBSTONE] = {}; ringloop->set_immediate([this]() { continue_recovery(); }); } } diff --git a/src/test/test_pg_chaos.cpp b/src/test/test_pg_chaos.cpp index ae429929..febdd428 100644 --- a/src/test/test_pg_chaos.cpp +++ b/src/test/test_pg_chaos.cpp @@ -3997,8 +3997,8 @@ void chaos_t::run(uint64_t steps) "peering_state=%x no_recovery=%d no_rebalance=%d\n", pg.state, pg.dead_peers.size(), pg.tomb_objects.size(), pg.copies_to_delete_after_sync.size(), f.osd->recovery_ops.size(), - f.osd->recovery_pick, f.osd->recovery_last_pg.pool_id, f.osd->recovery_last_pg.pg_num, - f.osd->recovery_last_oid.inode, f.osd->recovery_last_oid.stripe, + f.osd->recovery_pick, f.osd->recovery_last_pg[OSD_REC_TOMBSTONE].pool_id, f.osd->recovery_last_pg[OSD_REC_TOMBSTONE].pg_num, + f.osd->recovery_last_oid[OSD_REC_TOMBSTONE].inode, f.osd->recovery_last_oid[OSD_REC_TOMBSTONE].stripe, f.osd->peering_state, (int)f.osd->no_recovery, (int)f.osd->no_rebalance); for (auto & it: pg.tomb_objects) { diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..db3656cd 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -162,6 +162,7 @@ OLD=1 IMMEDIATE_COMMIT=1 ./test_enospc.sh OLD=1 IMMEDIATE_COMMIT=1 SCHEME=xor ./test_enospc.sh ./test_recovery_enospc.sh +./test_recovery_pg_switch.sh ./test_scrub.sh ZERO_OSD=2 ./test_scrub.sh diff --git a/tests/test_recovery_pg_switch.sh b/tests/test_recovery_pg_switch.sh new file mode 100755 index 00000000..03c71eb7 --- /dev/null +++ b/tests/test_recovery_pg_switch.sh @@ -0,0 +1,44 @@ +#!/bin/bash -ex + +# Recovery must switch to another PG after recovery_pg_switch operations instead of +# recovering PGs one by one + +OSD_COUNT=4 +PG_COUNT=16 +PG_MINSIZE=1 +IMMEDIATE_COMMIT=1 +GLOBAL_CONFIG=',"client_enable_writeback":false,"osd_out_time":1,"recovery_pg_switch":32' + +. `dirname $0`/run_3osds.sh + +pool_clean() +{ + $ETCDCTL get /vitastor/pg/state/1/ --prefix --print-value-only | jq -s -e '[ .[] | select(.state == ["active"] or .state == ["active", "left_on_dead"]) ] | length == '$PG_COUNT +} + +# 128 objects of 128 KB in each PG +$VITASTOR_FIO -bs=1M -direct=1 -iodepth=4 -rw=write -pool=1 -inode=1 -size=256M + +kill -9 $OSD4_PID +sleep 5 +wait_condition 120 pool_clean "Recovery" + +# Longest run of operations in one PG and the number of PGs, for each OSD and kind +for i in 1 2 3; do + for kind in degraded misplaced; do + perl -ne 'if (/Submitting recovery operation for [0-9a-f]+:([0-9a-f]+) \('$kind'\)/) { + $pg = int(hex($1) / 131072) % '$PG_COUNT'; if ($pg ne $last) { $last = $pg; $run = 0 } + $run++; $max = $run if $run > $max; $seen{$pg} = 1 } + END { print "$max ".scalar(keys %seen)."\n" if $max }' ./testdata/osd$i.log + done +done > ./testdata/pg_runs.txt +cat ./testdata/pg_runs.txt + +if ! awk '$2 > 1 { n++ } END { exit !n }' ./testdata/pg_runs.txt; then + format_error "No OSD recovered more than one PG, the test didn't check anything" +fi +if awk '$2 > 1 && $1 > 32 { bad++ } END { exit !bad }' ./testdata/pg_runs.txt; then + format_error "Recovery didn't switch to another PG after recovery_pg_switch operations" +fi + +format_green OK -- 2.47.3