From 3eacda15f4928acb7eb134c26cf889878c99e258 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 7 Mar 2021 21:04:52 +0800 Subject: [PATCH] crimson/osd: factor out "maybe" from maybe_send_completion() for better readability Signed-off-by: Kefu Chai --- src/crimson/osd/watch.cc | 69 ++++++++++++++++++++-------------------- src/crimson/osd/watch.h | 14 ++++---- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/crimson/osd/watch.cc b/src/crimson/osd/watch.cc index bcf535ab1c630..590e992277727 100644 --- a/src/crimson/osd/watch.cc +++ b/src/crimson/osd/watch.cc @@ -135,7 +135,11 @@ seastar::future<> Notify::remove_watcher(WatchRef watch) } [[maybe_unused]] const auto num_removed = watchers.erase(watch); assert(num_removed > 0); - return maybe_send_completion(); + if (watchers.empty()) { + return send_completion(); + } else { + return seastar::now(); + } } @@ -153,43 +157,40 @@ seastar::future<> Notify::complete_watcher( return remove_watcher(std::move(watch)); } -seastar::future<> Notify::maybe_send_completion( +seastar::future<> Notify::send_completion( std::set timedout_watchers) { logger().info("{} -- {} in progress watchers, {} timedout watchers {}", __func__, watchers.size(), timedout_watchers.size()); - if (watchers.empty() || !timedout_watchers.empty()) { - logger().debug("{} sending notify replies: {}", __func__, notify_replies); - complete = true; - timeout_timer.cancel(); - - ceph::bufferlist empty; - auto reply = make_message( - ninfo.cookie, - user_version, - ninfo.notify_id, - CEPH_WATCH_EVENT_NOTIFY_COMPLETE, - empty, - client_gid); - ceph::bufferlist reply_bl; - { - std::vector> missed; - missed.reserve(std::size(timedout_watchers)); - boost::insert( - missed, std::begin(missed), - timedout_watchers | boost::adaptors::transformed([] (auto w) { - return std::make_pair(w->get_watcher_gid(), w->get_cookie()); - })); - ceph::encode(notify_replies, reply_bl); - ceph::encode(missed, reply_bl); - } - reply->set_data(std::move(reply_bl)); - if (!timedout_watchers.empty()) { - reply->return_code = -ETIMEDOUT; - } - return conn->send(std::move(reply)); + logger().debug("{} sending notify replies: {}", __func__, notify_replies); + complete = true; + timeout_timer.cancel(); + + ceph::bufferlist empty; + auto reply = make_message( + ninfo.cookie, + user_version, + ninfo.notify_id, + CEPH_WATCH_EVENT_NOTIFY_COMPLETE, + empty, + client_gid); + ceph::bufferlist reply_bl; + { + std::vector> missed; + missed.reserve(std::size(timedout_watchers)); + boost::insert( + missed, std::begin(missed), + timedout_watchers | boost::adaptors::transformed([] (auto w) { + return std::make_pair(w->get_watcher_gid(), w->get_cookie()); + })); + ceph::encode(notify_replies, reply_bl); + ceph::encode(missed, reply_bl); } - return seastar::now(); + reply->set_data(std::move(reply_bl)); + if (!timedout_watchers.empty()) { + reply->return_code = -ETIMEDOUT; + } + return conn->send(std::move(reply)); } void Notify::do_timeout() @@ -201,7 +202,7 @@ void Notify::do_timeout() for (auto& watcher : watchers) { watcher->cancel_notify(ninfo.notify_id); } - std::ignore = maybe_send_completion(std::move(watchers)); + std::ignore = send_completion(std::move(watchers)); watchers.clear(); } diff --git a/src/crimson/osd/watch.h b/src/crimson/osd/watch.h index 3632f427280c9..b654efa41c436 100644 --- a/src/crimson/osd/watch.h +++ b/src/crimson/osd/watch.h @@ -124,7 +124,7 @@ class Notify { uint64_t get_id() const { return ninfo.notify_id; } /// Sends notify completion if watchers.empty() or timeout - seastar::future<> maybe_send_completion( + seastar::future<> send_completion( std::set timedout_watchers = {}); /// Called on Notify timeout @@ -207,11 +207,13 @@ seastar::future<> Notify::create_n_propagate( begin, end, std::forward(args)...); - return seastar::do_for_each(begin, end, [=] (auto& watchref) { - return watchref->start_notify(notify); - }).then([notify = std::move(notify)] { - return notify->maybe_send_completion(); - }); + if (begin == end) { + return notify->send_completion(); + } else { + return seastar::do_for_each(begin, end, [=] (auto& watchref) { + return watchref->start_notify(notify); + }); + } } } // namespace crimson::osd -- 2.39.5