]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
crimson/osd/heartbeat: avoid racing when connecting to peer
authorKefu Chai <kchai@redhat.com>
Thu, 19 Sep 2019 08:06:05 +0000 (16:06 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 19 Sep 2019 10:23:20 +0000 (18:23 +0800)
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 <kchai@redhat.com>
src/crimson/osd/heartbeat.cc

index 367c2ffce789d7090eb693acc04388d6cf563312..32ba959996d3954279d4b0bad7c7ca1041239ab7 100644 (file)
@@ -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();
   }
 }