From b102c44291902bd070a2c7df9ae00143a9e8d36f Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 19 Sep 2019 16:06:05 +0800 Subject: [PATCH] crimson/osd/heartbeat: avoid racing when connecting to peer before this change, we could initialize connections to newly added peers in parallel repeatly, if an OSD is listed in up/acting of multiple PGs served by current OSD. after this change, we try to add the peer to `Heartbeat.peers` first, and then try to connect to it with the hb front/back addresses, to avoid potential repeatly connecting to peer. Signed-off-by: Kefu Chai --- src/crimson/osd/heartbeat.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/crimson/osd/heartbeat.cc b/src/crimson/osd/heartbeat.cc index 367c2ffce78..32ba959996d 100644 --- a/src/crimson/osd/heartbeat.cc +++ b/src/crimson/osd/heartbeat.cc @@ -90,8 +90,10 @@ void Heartbeat::set_require_authorizer(bool require_authorizer) seastar::future<> Heartbeat::add_peer(osd_id_t peer, epoch_t epoch) { - auto found = peers.find(peer); - if (found == peers.end()) { + auto [peer_info, added] = peers.try_emplace(peer); + auto& info = peer_info->second; + info.epoch = epoch; + if (added) { logger().info("add_peer({})", peer); auto osdmap = service.get_osdmap_service().get_map(); // TODO: use addrs @@ -100,16 +102,12 @@ seastar::future<> Heartbeat::add_peer(osd_id_t peer, epoch_t epoch) CEPH_ENTITY_TYPE_OSD), back_msgr.connect(osdmap->get_hb_back_addrs(peer).front(), CEPH_ENTITY_TYPE_OSD)) - .then([this, peer, epoch] (auto xcon_front, auto xcon_back) { - PeerInfo info; + .then([this, &info=peer_info->second] (auto xcon_front, auto xcon_back) { // sharded-messenger compatible mode info.con_front = xcon_front->release(); info.con_back = xcon_back->release(); - info.epoch = epoch; - peers.emplace(peer, std::move(info)); }); } else { - found->second.epoch = epoch; return seastar::now(); } } -- 2.39.5