From e2733f3b94ac398bf6ebd23d64baa6e854f980bc Mon Sep 17 00:00:00 2001 From: Ronen Friedman Date: Tue, 25 Mar 2025 09:41:23 -0500 Subject: [PATCH] osd: use with_array_sections() ... in a couple of functions, as a demonstration of its usage. Signed-off-by: Ronen Friedman --- src/osd/ECMsgTypes.cc | 127 ++++++++++++++++---------------- src/osd/scrubber/pg_scrubber.cc | 20 ++--- 2 files changed, 70 insertions(+), 77 deletions(-) diff --git a/src/osd/ECMsgTypes.cc b/src/osd/ECMsgTypes.cc index 704a2aabd2da5..c3ec354dcd113 100644 --- a/src/osd/ECMsgTypes.cc +++ b/src/osd/ECMsgTypes.cc @@ -22,6 +22,8 @@ using std::set; using ceph::bufferlist; using ceph::Formatter; +using namespace std::literals; + void ECSubWrite::encode(bufferlist &bl) const { ENCODE_START(4, 1, bl); @@ -242,32 +244,28 @@ std::ostream &operator<<( void ECSubRead::dump(Formatter *f) const { + using extent_t = boost::tuple; + f->dump_stream("from") << from; f->dump_unsigned("tid", tid); - f->open_array_section("objects"); - for (auto i = to_read.cbegin(); i != to_read.cend(); ++i) { - f->open_object_section("object"); - f->dump_stream("oid") << i->first; - f->open_array_section("extents"); - for (auto j = i->second.cbegin(); j != i->second.cend(); ++j) { - f->open_object_section("extent"); - f->dump_unsigned("off", j->get<0>()); - f->dump_unsigned("len", j->get<1>()); - f->dump_unsigned("flags", j->get<2>()); - f->close_section(); - } - f->close_section(); - f->close_section(); - } - f->close_section(); - f->open_array_section("object_attrs_requested"); - for (auto i = attrs_to_read.cbegin(); i != attrs_to_read.cend(); ++i) { - f->open_object_section("object"); - f->dump_stream("oid") << *i; - f->close_section(); - } - f->close_section(); + // 'to_read' (map>>) + f->with_obj_array_section( + "object"sv, to_read, + [](Formatter& f, const hobject_t& oid, const list& extents) { + f.dump_stream("oid") << oid; + f.with_obj_array_section( + "extent", extents, [](Formatter& f, const extent_t& extent) { + f.dump_unsigned("off", extent.get<0>()); + f.dump_unsigned("len", extent.get<1>()); + f.dump_unsigned("flags", extent.get<2>()); + }); + }); + + // 'object_attrs_requested': 'attrs_to_read' (set) + f->with_obj_array_section( + "object_attrs_requested"sv, attrs_to_read, + [](Formatter& f, const hobject_t& oid) { f.dump_stream("oid") << oid; }); } void ECSubRead::generate_test_instances(list& o) @@ -321,50 +319,51 @@ std::ostream &operator<<( << ")"; } -void ECSubReadReply::dump(Formatter *f) const + +void ECSubReadReply::dump(Formatter* f) const { + using offset_pair_t = pair; + using extents_list_t = list; + f->dump_stream("from") << from; f->dump_unsigned("tid", tid); - f->open_array_section("buffers_read"); - for (auto i = buffers_read.cbegin(); i != buffers_read.cend(); ++i) { - f->open_object_section("object"); - f->dump_stream("oid") << i->first; - f->open_array_section("data"); - for (auto j = i->second.cbegin(); j != i->second.cend(); ++j) { - f->open_object_section("extent"); - f->dump_unsigned("off", j->first); - f->dump_unsigned("buf_len", j->second.length()); - f->close_section(); - } - f->close_section(); - f->close_section(); - } - f->close_section(); - - f->open_array_section("attrs_returned"); - for (auto i = attrs_read.cbegin(); i != attrs_read.cend(); ++i) { - f->open_object_section("object_attrs"); - f->dump_stream("oid") << i->first; - f->open_array_section("attrs"); - for (auto j = i->second.cbegin(); j != i->second.cend(); ++j) { - f->open_object_section("attr"); - f->dump_string("attr", j->first); - f->dump_unsigned("val_len", j->second.length()); - f->close_section(); - } - f->close_section(); - f->close_section(); - } - f->close_section(); - - f->open_array_section("errors"); - for (auto i = errors.cbegin(); i != errors.cend(); ++i) { - f->open_object_section("error_pair"); - f->dump_stream("oid") << i->first; - f->dump_int("error", i->second); - f->close_section(); - } - f->close_section(); + + // 'buffers_read' (map>>) + f->with_obj_array_section( + "object"sv, buffers_read, + [](Formatter& f, const hobject_t& oid, const extents_list_t& l) { + f.dump_stream("oid") << oid; + f.with_obj_array_section( + "extent", l, + [](Formatter& f, const offset_pair_t& offset_n_bl) { + const auto& [off, bl] = offset_n_bl; + f.dump_unsigned("off", off); + f.dump_unsigned("buf_len", bl.length()); + }); + }); + + // "attrs_returned" (mapping hobject_t to a table) + f->with_obj_array_section( + "object_attrs"sv, attrs_read, + [](Formatter& f, const hobject_t& oid, + const std::map>& m) { + f.dump_stream("oid") << oid; + f.with_obj_array_section( + "attr", m, + [](Formatter& f, const std::string& attr, + const ceph::buffer::list& bl) { + f.dump_string("attr", attr); + f.dump_unsigned("val_len", bl.length()); + }); + }); + + // "errors": map + f->with_obj_array_section( + "error_pair"sv, errors, + [](Formatter& f, const hobject_t& oid, int err) { + f.dump_stream("oid") << oid; + f.dump_int("error", err); + }); } void ECSubReadReply::generate_test_instances(list& o) diff --git a/src/osd/scrubber/pg_scrubber.cc b/src/osd/scrubber/pg_scrubber.cc index 0a218098f8885..c799dee56e2f8 100644 --- a/src/osd/scrubber/pg_scrubber.cc +++ b/src/osd/scrubber/pg_scrubber.cc @@ -2342,12 +2342,10 @@ void PgScrubber::dump_active_scrubber(ceph::Formatter* f) const f->dump_int("shallow_errors", m_shallow_errors); f->dump_int("deep_errors", m_deep_errors); f->dump_int("fixed", m_fixed_count); - { - Formatter::ArraySection waiting_on_whom{*f, "waiting_on_whom"sv}; - for (const auto& p : m_maps_status.get_awaited()) { - f->dump_stream("shard") << p; - } - } + f->with_array_section( + "waiting_on_whom"sv, m_maps_status.get_awaited(), + [](Formatter& f, const pg_shard_t& sh) { f.dump_stream("shard") << sh; }); + if (m_scrub_job->blocked) { f->dump_string("schedule", "blocked"); } else { @@ -2480,13 +2478,9 @@ void PgScrubber::handle_query_state(ceph::Formatter* f) f->dump_stream("scrubber.max_end") << m_max_end; f->dump_stream("scrubber.subset_last_update") << m_subset_last_update; f->dump_bool("scrubber.deep", m_is_deep); - { - Formatter::ArraySection waiting_on_whom{*f, "waiting_on_whom"sv}; - for (const auto& p : m_maps_status.get_awaited()) { - f->dump_stream("shard") << p; - } - } - + f->with_array_section( + "waiting_on_whom"sv, m_maps_status.get_awaited(), + [](Formatter& f, const pg_shard_t& sh) { f.dump_stream("shard") << sh; }); f->dump_string("comment", "DEPRECATED - may be removed in the next release"); } -- 2.39.5