]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
osd: use with_array_sections() 62498/head
authorRonen Friedman <rfriedma@redhat.com>
Tue, 25 Mar 2025 14:41:23 +0000 (09:41 -0500)
committerRonen Friedman <rfriedma@redhat.com>
Mon, 31 Mar 2025 15:23:18 +0000 (10:23 -0500)
... in a couple of functions, as a demonstration of its usage.

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
src/osd/ECMsgTypes.cc
src/osd/scrubber/pg_scrubber.cc

index 704a2aabd2da5410a595a0ca3edd92431375e0a7..c3ec354dcd1134df4eaf896eb6cc422ac0b802c8 100644 (file)
@@ -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<uint64_t, uint64_t, uint32_t>;
+
   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<hobject_t, list<tuple<offset, length, flags>>>)
+  f->with_obj_array_section(
+      "object"sv, to_read,
+      [](Formatter& f, const hobject_t& oid, const list<extent_t>& 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<hobject_t>)
+  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<ECSubRead*>& 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<uint64_t, bufferlist>;
+  using extents_list_t = list<offset_pair_t>;
+
   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<hobject_t, list<pair<uint64_t, bufferlist>>>)
+  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 <string to bl> table)
+  f->with_obj_array_section(
+      "object_attrs"sv, attrs_read,
+      [](Formatter& f, const hobject_t& oid,
+        const std::map<std::string, ceph::buffer::list, std::less<>>& 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<hobject_t, int>
+  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<ECSubReadReply*>& o)
index 0a218098f888522c40f3cb226fd82d0a786614d1..c799dee56e2f89b5dd7fbee6cf882a863f2d0ad5 100644 (file)
@@ -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");
 }