]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/tools/store_nbd: replace wait_pending with seastar::gate 41911/head
authorKefu Chai <kchai@redhat.com>
Thu, 17 Jun 2021 07:08:32 +0000 (15:08 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 18 Jun 2021 01:05:35 +0000 (09:05 +0800)
the inc_pending + promise<> solution is pratically identical to
seastar::gate, so let's use the prepackaged solution instead.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/crimson/tools/store_nbd/store-nbd.cc

index 39b203b72177151bb1484b161ccf4b39b8b1fc1d..e1ff3899db8fa07a18b747f4a971dbe0d4743a7a 100644 (file)
@@ -164,25 +164,12 @@ struct request_context_t {
 struct RequestWriter {
   seastar::rwlock lock;
   seastar::output_stream<char> stream;
-  std::optional<seastar::promise<>> wait_pending;
-  int pending = 0;
+  seastar::gate gate;
 
   RequestWriter(
     seastar::output_stream<char> &&stream) : stream(std::move(stream)) {}
   RequestWriter(RequestWriter &&) = default;
 
-  void inc_pending() {
-    ++pending;
-  }
-
-  void dec_pending() {
-    ceph_assert(pending > 0);
-    --pending;
-    if (pending == 0 && wait_pending) {
-      (*wait_pending).set_value();
-    }
-  }
-
   seastar::future<> complete(request_context_t::ref &&req) {
     auto &request = *req;
     return lock.write_lock(
@@ -196,14 +183,7 @@ struct RequestWriter {
   }
 
   seastar::future<> close() {
-    ceph_assert(!wait_pending);
-    auto do_wait_pending = seastar::now();
-    if (pending > 0) {
-      wait_pending = seastar::promise<>();
-      do_wait_pending = (*wait_pending).get_future();
-    }
-    return do_wait_pending.then([this] {
-      ceph_assert(pending == 0);
+    return gate.close().then([this] {
       return stream.close();
     });
   }
@@ -400,25 +380,20 @@ seastar::future<> handle_commands(
   RequestWriter &out)
 {
   logger().debug("handle_commands");
-  return seastar::keep_doing(
-    [&] {
-      logger().debug("waiting for command");
-      auto request_ref = request_context_t::make_ref();
-      auto &request = *request_ref;
-      return request.read_request(in
-      ).then([&, request_ref=std::move(request_ref)]() mutable {
-       out.inc_pending();
-       static_cast<void>(
-         handle_command(
-           backend, std::move(request_ref), out
-         ).finally([&out] {
-           out.dec_pending();
-         })
-       );
-       logger().debug("handle_commands after fork");
-       return seastar::now();
+  return seastar::keep_doing([&] {
+    logger().debug("waiting for command");
+    auto request_ref = request_context_t::make_ref();
+    auto &request = *request_ref;
+    return request.read_request(in).then(
+      [&, request_ref=std::move(request_ref)]() mutable {
+      // keep running in background
+      (void)seastar::try_with_gate(out.gate,
+        [&backend, &out, request_ref=std::move(request_ref)]() mutable {
+        return handle_command(backend, std::move(request_ref), out);
       });
+      logger().debug("handle_commands after fork");
     });
+  }).handle_exception_type([](const seastar::gate_closed_exception&) {});
 }
 
 void NBDHandler::run()