From ad91c6ac597b9b3474609bff0af2f68144e9a466 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Sat, 28 Nov 2026 11:42:54 +0300 Subject: [PATCH 1/3] osd: Send queued replies before a peering calculation osd_t::loop() ran handle_peers() before send_replies(). When the peering yield timer fired in a pass that also handled incoming requests, their replies were queued in that pass but sent only after the next PG calculation, so under load a ping waited for two calculations instead of one. Send the queued replies and submit before handle_peers(). --- src/osd/osd.cpp | 3 +++ src/test/mock/messenger.cpp | 5 ++++ src/test/osd_test.cpp | 54 +++++++++++++++++++++++++++++++++++++ src/test/osd_test_fixture.h | 8 +++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/osd/osd.cpp b/src/osd/osd.cpp index 2037c531..4c1c6311 100644 --- a/src/osd/osd.cpp +++ b/src/osd/osd.cpp @@ -611,6 +611,9 @@ bool osd_t::shutdown() void osd_t::loop() { + // A peering calc in handle_peers() blocks the loop: send the replies queued in this pass first + msgr.send_replies(); + ringloop->submit(); handle_peers(); msgr.read_requests(); msgr.send_replies(); diff --git a/src/test/mock/messenger.cpp b/src/test/mock/messenger.cpp index a500aa10..723db579 100644 --- a/src/test/mock/messenger.cpp +++ b/src/test/mock/messenger.cpp @@ -57,8 +57,13 @@ void osd_messenger_t::read_requests() { } +// Set by osd_test: runs where the real messenger puts the queued replies into send SQEs +std::function mock_send_replies_hook; + void osd_messenger_t::send_replies() { + if (mock_send_replies_hook) + mock_send_replies_hook(); } json11::Json::object osd_messenger_t::read_config(const json11::Json & config) diff --git a/src/test/osd_test.cpp b/src/test/osd_test.cpp index f4426e76..260eb756 100644 --- a/src/test/osd_test.cpp +++ b/src/test/osd_test.cpp @@ -2549,6 +2549,59 @@ void test_peering_yields_between_pgs() printf("test_peering_yields_between_pgs passed\n"); } +extern std::function mock_send_replies_hook; + +// A reply queued in the pass where the yield timer fires must reach the ring before the next +// calc starts: otherwise a ping answered in that pass waits for that calc too +void test_peering_sends_replies_before_calc() +{ + printf("test_peering_sends_replies_before_calc\n"); + + osd_test_fixture_t f; + f.configure_replicated_pool(1, 2, 1, 2, { { 1, 2 }, { 1, 2 } }); + f.start(json11::Json::object { + { "osd_num", 1 }, + { "etcd_address", "127.0.0.1:2379" }, + { "immediate_commit", "none" }, + { "block_size", 131072 }, + { "bitmap_granularity", 4096 }, + }); + f.connect_peer(2); + f.complete_peering_empty(false); + + f.ringloop->wakeup(); + f.ringloop->loop(); + assert((f.pg(1, 1).state == PG_PEERING) != (f.pg(1, 2).state == PG_PEERING)); + pg_t & second = f.pg(1, 1).state == PG_PEERING ? f.pg(1, 1) : f.pg(1, 2); + + // Like the real messenger, the first send_replies() of the pass puts the queued replies + // into a send SQE + bool queued = false, sent_before_calc = false; + mock_send_replies_hook = [&]() + { + if (queued) + return; + queued = true; + io_uring_sqe *sqe = f.ringloop->get_sqe(); + io_uring_prep_nop(sqe); + ((ring_data_t*)sqe->user_data)->callback = [](ring_data_t *) {}; + }; + f.on_submit = [&](io_uring_sqe *sqe) + { + if (second.state == PG_PEERING) + sent_before_calc = true; + f.ringloop->mark_completed((ring_data_t*)sqe->user_data); + }; + f.tfd->tick((timespec){ .tv_sec = 0, .tv_nsec = 1000000 }); + f.ringloop->loop(); + mock_send_replies_hook = NULL; + f.on_submit = NULL; + assert(sent_before_calc); + assert(f.pg(1, 1).state == PG_ACTIVE); + assert(f.pg(1, 2).state == PG_ACTIVE); + printf("test_peering_sends_replies_before_calc passed\n"); +} + // Invariant (3) via stop_pg void test_pg_stop_drops_dirty_client() { @@ -5627,5 +5680,6 @@ int main(int narg, char *args[]) test_tombstone_inline_misplaced(); test_tombstone_forget_counters(); test_peering_yields_between_pgs(); + test_peering_sends_replies_before_calc(); return 0; } diff --git a/src/test/osd_test_fixture.h b/src/test/osd_test_fixture.h index c234e880..a11c959e 100644 --- a/src/test/osd_test_fixture.h +++ b/src/test/osd_test_fixture.h @@ -113,13 +113,19 @@ struct osd_test_fixture_t capturing_bs_t *bs = nullptr; // Simulated peers advertise tombstone support unless tests disable it bool peer_tombstones = true; + // Sees every SQE submitted to the mock ring + std::function on_submit; osd_test_fixture_t() { // Created without a handler: no timerfd, timers fire on tick() (pump) tfd = new timerfd_manager_t(NULL); tfd->set_fd_handler = [](int, bool, std::function) {}; - ringloop = new ring_loop_mock_t(RINGLOOP_DEFAULT_SIZE, [](io_uring_sqe *) {}); + ringloop = new ring_loop_mock_t(RINGLOOP_DEFAULT_SIZE, [this](io_uring_sqe *sqe) + { + if (on_submit) + on_submit(sqe); + }); st_cli = new etcd_state_client_mock_t(etcd_store); } -- 2.47.3 From 0571c14e18d261cce44eca4663d4ff97ddd81565 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Sat, 28 Nov 2026 16:22:12 +0300 Subject: [PATCH 2/3] osd: Send replies early only while PGs are peering The early send_replies() and submit() in osd_t::loop() cost a second io_uring_enter in every busy pass that also queues new receives. Do them only while OSD_PEERING_PGS is set: handle_peers() can start a calc only then. --- src/osd/osd.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/osd/osd.cpp b/src/osd/osd.cpp index 4c1c6311..809aa153 100644 --- a/src/osd/osd.cpp +++ b/src/osd/osd.cpp @@ -611,9 +611,12 @@ bool osd_t::shutdown() void osd_t::loop() { - // A peering calc in handle_peers() blocks the loop: send the replies queued in this pass first - msgr.send_replies(); - ringloop->submit(); + if (peering_state & OSD_PEERING_PGS) + { + // A peering calc in handle_peers() blocks the loop: send the replies queued in this pass first + msgr.send_replies(); + ringloop->submit(); + } handle_peers(); msgr.read_requests(); msgr.send_replies(); -- 2.47.3 From 046bba7a8f0d770f4768e1cbc437d58853690089 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Sat, 28 Nov 2026 18:19:34 +0300 Subject: [PATCH 3/3] osd: Send queued replies right before each peering calc continue_pg() calculates a re-peered PG, which keeps the lists of the peers that stay, from a completion callback (a lock reply, an etcd event), so loop() reached its early flush only after the calc, and the replies queued earlier in that pass waited for it. Send them in continue_pg_peering() right before the calc instead: that covers both paths and needs no peering_state check in loop(). --- src/osd/osd.cpp | 6 ------ src/osd/osd_peering.cpp | 3 +++ src/test/osd_test.cpp | 28 ++++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/osd/osd.cpp b/src/osd/osd.cpp index 809aa153..2037c531 100644 --- a/src/osd/osd.cpp +++ b/src/osd/osd.cpp @@ -611,12 +611,6 @@ bool osd_t::shutdown() void osd_t::loop() { - if (peering_state & OSD_PEERING_PGS) - { - // A peering calc in handle_peers() blocks the loop: send the replies queued in this pass first - msgr.send_replies(); - ringloop->submit(); - } handle_peers(); msgr.read_requests(); msgr.send_replies(); diff --git a/src/osd/osd_peering.cpp b/src/osd/osd_peering.cpp index 47cbe148..38e851b5 100644 --- a/src/osd/osd_peering.cpp +++ b/src/osd/osd_peering.cpp @@ -364,6 +364,9 @@ bool osd_t::continue_pg_peering(pg_t & pg) if (pg.peering_state->lists_done) { auto history_size = pg.target_history.size(); + // The calc blocks the loop, and continue_pg() runs it from a completion: send the queued replies first + msgr.send_replies(); + ringloop->submit(); pg.calc_object_states(log_level); // Two-phase delete needs tombstone support in stores of all PG members pg.enable_tombstones = (pg.state & PG_ACTIVE) && bs->supports_tombstones(); diff --git a/src/test/osd_test.cpp b/src/test/osd_test.cpp index 260eb756..ccf1b22c 100644 --- a/src/test/osd_test.cpp +++ b/src/test/osd_test.cpp @@ -2572,12 +2572,14 @@ void test_peering_sends_replies_before_calc() f.ringloop->wakeup(); f.ringloop->loop(); assert((f.pg(1, 1).state == PG_PEERING) != (f.pg(1, 2).state == PG_PEERING)); - pg_t & second = f.pg(1, 1).state == PG_PEERING ? f.pg(1, 1) : f.pg(1, 2); + pg_t *first = f.pg(1, 1).state == PG_PEERING ? &f.pg(1, 2) : &f.pg(1, 1); + pg_t *second = first == &f.pg(1, 1) ? &f.pg(1, 2) : &f.pg(1, 1); // Like the real messenger, the first send_replies() of the pass puts the queued replies // into a send SQE + pg_t *calc_pg = second; bool queued = false, sent_before_calc = false; - mock_send_replies_hook = [&]() + auto queue_reply = [&]() { if (queued) return; @@ -2586,12 +2588,30 @@ void test_peering_sends_replies_before_calc() io_uring_prep_nop(sqe); ((ring_data_t*)sqe->user_data)->callback = [](ring_data_t *) {}; }; - f.on_submit = [&](io_uring_sqe *sqe) + auto check_submit = [&](io_uring_sqe *sqe) { - if (second.state == PG_PEERING) + if (calc_pg->state == PG_PEERING) sent_before_calc = true; f.ringloop->mark_completed((ring_data_t*)sqe->user_data); }; + + // A re-peer keeps the lists of the peers that stay, so continue_pg() calculates the + // waiting PG at once, outside handle_peers() + f.tfd->tick((timespec){ .tv_sec = 0, .tv_nsec = 1000000 }); + mock_send_replies_hook = queue_reply; + f.on_submit = check_submit; + f.repeer_pgs(2); + mock_send_replies_hook = NULL; + f.on_submit = NULL; + assert(sent_before_calc); + assert(second->state == PG_ACTIVE); + + // handle_peers() calculates the other PG when its lists arrive + f.complete_peering_empty(false); + calc_pg = first; + queued = sent_before_calc = false; + mock_send_replies_hook = queue_reply; + f.on_submit = check_submit; f.tfd->tick((timespec){ .tv_sec = 0, .tv_nsec = 1000000 }); f.ringloop->loop(); mock_send_replies_hook = NULL; -- 2.47.3