From 58dc60e790212e61b9880fd68163c78fde764c69 Mon Sep 17 00:00:00 2001 From: Matan Breizman Date: Sun, 19 Nov 2023 15:19:29 +0000 Subject: [PATCH] osd/OSD: rewrite build_incremental_map_msg() * use lambda to maintain size limits. * avoid copying by std::move the bl into the message. Signed-off-by: Matan Breizman --- src/osd/OSD.cc | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index c3d03d8623369..7d3bbe085a7e4 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -1377,6 +1377,19 @@ void OSDService::got_stop_ack() MOSDMap *OSDService::build_incremental_map_msg(epoch_t since, epoch_t to, OSDSuperblock& sblock) { + auto get_map_and_adjust_counters = [] ( + bufferlist& bl, + int& max, + ssize_t& max_bytes, + std::function map_getter) { + if (!map_getter()) { + return false; + } + max--; + max_bytes -= bl.length(); + return true; + }; + MOSDMap *m = new MOSDMap(monc->get_fsid(), osdmap->get_encoding_features()); m->cluster_osdmap_trim_lower_bound = sblock.cluster_osdmap_trim_lower_bound; @@ -1388,36 +1401,34 @@ MOSDMap *OSDService::build_incremental_map_msg(epoch_t since, epoch_t to, if (since < m->cluster_osdmap_trim_lower_bound) { // we don't have the next map the target wants, so start with a // full map. - bufferlist bl; dout(10) << __func__ << " cluster osdmap lower bound " << sblock.cluster_osdmap_trim_lower_bound << " > since " << since << ", starting with full map" << dendl; since = m->cluster_osdmap_trim_lower_bound; - if (!get_map_bl(since, bl)) { - derr << __func__ << " missing full map " << since << dendl; + if (bufferlist bl; + get_map_and_adjust_counters(bl, max, max_bytes, [&] { return get_map_bl(since, bl);})) { + m->maps[since] = std::move(bl); + ++since; + } else { + derr << __func__ << " missing full map after map gap " << since << dendl; goto panic; } - max--; - max_bytes -= bl.length(); - m->maps[since] = std::move(bl); } - for (epoch_t e = since; e <= to; ++e) { - bufferlist bl; - if (get_inc_map_bl(e, bl)) { - m->incremental_maps[e] = bl; + + for (epoch_t e = since; e <= to && max > 0 && max_bytes > 0; ++e) { + if (bufferlist bl; + get_map_and_adjust_counters(bl, max, max_bytes, [&] { return get_inc_map_bl(e, bl);})) { + m->incremental_maps[e] = std::move(bl); } else { dout(10) << __func__ << " missing incremental map " << e << dendl; - if (!get_map_bl(e, bl)) { - derr << __func__ << " also missing full map " << e << dendl; - goto panic; + if (bufferlist bl; + get_map_and_adjust_counters(bl, max, max_bytes, [&] { return get_map_bl(e, bl);})) { + m->maps[e] = std::move(bl); + } else { + derr << __func__ << " also missing full map " << e << dendl; + goto panic; } - m->maps[e] = bl; - } - max--; - max_bytes -= bl.length(); - if (max <= 0 || max_bytes <= 0) { - break; } } return m; -- 2.39.5