1111
1212using RadosIo = ceph::io_exerciser::RadosIo;
1313
14+ namespace {
15+ template <typename S>
16+ int send_osd_command (int osd, S& s, librados::Rados& rados, const char * name,
17+ ceph::buffer::list& inbl, ceph::buffer::list* outbl,
18+ Formatter* f) {
19+ std::ostringstream oss;
20+ encode_json (name, s, f);
21+ f->flush (oss);
22+ int rc = rados.osd_command (osd, oss.str (), inbl, outbl, nullptr );
23+ return rc;
24+ }
25+
26+ template <typename S>
27+ int send_mon_command (S& s, librados::Rados& rados, const char * name,
28+ ceph::buffer::list& inbl, ceph::buffer::list* outbl,
29+ Formatter* f) {
30+ std::ostringstream oss;
31+ encode_json (name, s, f);
32+ f->flush (oss);
33+ int rc = rados.mon_command (oss.str (), inbl, outbl, nullptr );
34+ return rc;
35+ }
36+ } // namespace
37+
1438RadosIo::RadosIo (librados::Rados& rados, boost::asio::io_context& asio,
1539 const std::string& pool, const std::string& oid,
1640 const std::optional<std::vector<int >>& cached_shard_order,
@@ -293,15 +317,13 @@ void RadosIo::applyReadWriteOp(IoOp& op) {
293317void RadosIo::applyInjectOp (IoOp& op) {
294318 bufferlist osdmap_inbl, inject_inbl, osdmap_outbl, inject_outbl;
295319 auto formatter = std::make_unique<JSONFormatter>(false );
296- std::ostringstream oss;
297320
298321 int osd = -1 ;
299322 std::vector<int > shard_order;
300323
301324 ceph::messaging::osd::OSDMapRequest osdMapRequest{pool, get_oid (), " " };
302- encode_json (" OSDMapRequest" , osdMapRequest, formatter.get ());
303- formatter->flush (oss);
304- int rc = rados.mon_command (oss.str (), osdmap_inbl, &osdmap_outbl, nullptr );
325+ int rc = send_mon_command (osdMapRequest, rados, " OSDMapRequest" , osdmap_inbl,
326+ &osdmap_outbl, formatter.get ());
305327 ceph_assert (rc == 0 );
306328
307329 JSONParser p;
@@ -322,22 +344,22 @@ void RadosIo::applyInjectOp(IoOp& op) {
322344 ceph::messaging::osd::InjectECErrorRequest<InjectOpType::ReadEIO>
323345 injectErrorRequest{pool, oid, errorOp.shard ,
324346 errorOp.type , errorOp.when , errorOp.duration };
325- encode_json (" InjectECErrorRequest" , injectErrorRequest,
326- formatter.get ());
347+ int rc = send_osd_command (osd, injectErrorRequest, rados,
348+ " InjectECErrorRequest" , inject_inbl,
349+ &inject_outbl, formatter.get ());
350+ ceph_assert (rc == 0 );
327351 } else if (errorOp.type == 1 ) {
328352 ceph::messaging::osd::InjectECErrorRequest<
329353 InjectOpType::ReadMissingShard>
330354 injectErrorRequest{pool, oid, errorOp.shard ,
331355 errorOp.type , errorOp.when , errorOp.duration };
332- encode_json (" InjectECErrorRequest" , injectErrorRequest,
333- formatter.get ());
356+ int rc = send_osd_command (osd, injectErrorRequest, rados,
357+ " InjectECErrorRequest" , inject_inbl,
358+ &inject_outbl, formatter.get ());
359+ ceph_assert (rc == 0 );
334360 } else {
335361 ceph_abort_msg (" Unsupported inject type" );
336362 }
337- formatter->flush (oss);
338- int rc = rados.osd_command (osd, oss.str (), inject_inbl, &inject_outbl,
339- nullptr );
340- ceph_assert (rc == 0 );
341363 break ;
342364 }
343365 case OpType::InjectWriteError: {
@@ -348,14 +370,18 @@ void RadosIo::applyInjectOp(IoOp& op) {
348370 InjectOpType::WriteFailAndRollback>
349371 injectErrorRequest{pool, oid, errorOp.shard ,
350372 errorOp.type , errorOp.when , errorOp.duration };
351- encode_json (" InjectECErrorRequest" , injectErrorRequest,
352- formatter.get ());
373+ int rc = send_osd_command (osd, injectErrorRequest, rados,
374+ " InjectECErrorRequest" , inject_inbl,
375+ &inject_outbl, formatter.get ());
376+ ceph_assert (rc == 0 );
353377 } else if (errorOp.type == 3 ) {
354378 ceph::messaging::osd::InjectECErrorRequest<InjectOpType::WriteOSDAbort>
355379 injectErrorRequest{pool, oid, errorOp.shard ,
356380 errorOp.type , errorOp.when , errorOp.duration };
357- encode_json (" InjectECErrorRequest" , injectErrorRequest,
358- formatter.get ());
381+ int rc = send_osd_command (osd, injectErrorRequest, rados,
382+ " InjectECErrorRequest" , inject_inbl,
383+ &inject_outbl, formatter.get ());
384+ ceph_assert (rc == 0 );
359385
360386 // This inject is sent directly to the shard we want to inject the error
361387 // on
@@ -364,10 +390,6 @@ void RadosIo::applyInjectOp(IoOp& op) {
364390 ceph_abort (" Unsupported inject type" );
365391 }
366392
367- formatter->flush (oss);
368- int rc = rados.osd_command (osd, oss.str (), inject_inbl, &inject_outbl,
369- nullptr );
370- ceph_assert (rc == 0 );
371393 break ;
372394 }
373395 case OpType::ClearReadErrorInject: {
@@ -377,22 +399,22 @@ void RadosIo::applyInjectOp(IoOp& op) {
377399 if (errorOp.type == 0 ) {
378400 ceph::messaging::osd::InjectECClearErrorRequest<InjectOpType::ReadEIO>
379401 clearErrorInject{pool, oid, errorOp.shard , errorOp.type };
380- encode_json (" InjectECClearErrorRequest" , clearErrorInject,
381- formatter.get ());
402+ int rc = send_osd_command (osd, clearErrorInject, rados,
403+ " InjectECClearErrorRequest" , inject_inbl,
404+ &inject_outbl, formatter.get ());
405+ ceph_assert (rc == 0 );
382406 } else if (errorOp.type == 1 ) {
383407 ceph::messaging::osd::InjectECClearErrorRequest<
384408 InjectOpType::ReadMissingShard>
385409 clearErrorInject{pool, oid, errorOp.shard , errorOp.type };
386- encode_json (" InjectECClearErrorRequest" , clearErrorInject,
387- formatter.get ());
410+ int rc = send_osd_command (osd, clearErrorInject, rados,
411+ " InjectECClearErrorRequest" , inject_inbl,
412+ &inject_outbl, formatter.get ());
413+ ceph_assert (rc == 0 );
388414 } else {
389415 ceph_abort (" Unsupported inject type" );
390416 }
391417
392- formatter->flush (oss);
393- int rc = rados.osd_command (osd, oss.str (), inject_inbl, &inject_outbl,
394- nullptr );
395- ceph_assert (rc == 0 );
396418 break ;
397419 }
398420 case OpType::ClearWriteErrorInject: {
@@ -403,22 +425,22 @@ void RadosIo::applyInjectOp(IoOp& op) {
403425 ceph::messaging::osd::InjectECClearErrorRequest<
404426 InjectOpType::WriteFailAndRollback>
405427 clearErrorInject{pool, oid, errorOp.shard , errorOp.type };
406- encode_json (" InjectECClearErrorRequest" , clearErrorInject,
407- formatter.get ());
428+ int rc = send_osd_command (osd, clearErrorInject, rados,
429+ " InjectECClearErrorRequest" , inject_inbl,
430+ &inject_outbl, formatter.get ());
431+ ceph_assert (rc == 0 );
408432 } else if (errorOp.type == 3 ) {
409433 ceph::messaging::osd::InjectECClearErrorRequest<
410434 InjectOpType::WriteOSDAbort>
411435 clearErrorInject{pool, oid, errorOp.shard , errorOp.type };
412- encode_json (" InjectECClearErrorRequest" , clearErrorInject,
413- formatter.get ());
436+ int rc = send_osd_command (osd, clearErrorInject, rados,
437+ " InjectECClearErrorRequest" , inject_inbl,
438+ &inject_outbl, formatter.get ());
439+ ceph_assert (rc == 0 );
414440 } else {
415441 ceph_abort (" Unsupported inject type" );
416442 }
417443
418- formatter->flush (oss);
419- int rc = rados.osd_command (osd, oss.str (), inject_inbl, &inject_outbl,
420- nullptr );
421- ceph_assert (rc == 0 );
422444 break ;
423445 }
424446 default :
0 commit comments