]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephfs-mirror: gracefully shutdown threads, timers, etc..
authorVenky Shankar <vshankar@redhat.com>
Thu, 28 Jan 2021 08:56:19 +0000 (03:56 -0500)
committerVenky Shankar <vshankar@redhat.com>
Tue, 23 Feb 2021 06:19:27 +0000 (01:19 -0500)
Also, unregister signal handlers before exiting.

Signed-off-by: Venky Shankar <vshankar@redhat.com>
(cherry picked from commit a7de7f809b82de2f695b5328eb3cd39b65e64f65)

src/tools/cephfs_mirror/Mirror.cc
src/tools/cephfs_mirror/Mirror.h
src/tools/cephfs_mirror/main.cc

index 62fdaa9f9ace5902490b507667e3927b613487f4..1cc0c766088ff49a4c7ec80945bde4d504242715 100644 (file)
@@ -35,11 +35,6 @@ public:
     : SafeTimer(cct, timer_lock, true) {
     init();
   }
-
-  ~SafeTimerSingleton() {
-    std::scoped_lock locker(timer_lock);
-    shutdown();
-  }
 };
 
 class ThreadPoolSingleton : public ThreadPool {
@@ -52,13 +47,6 @@ public:
 
     start();
   }
-
-  ~ThreadPoolSingleton() override {
-    work_queue->drain();
-    delete work_queue;
-
-    stop();
-  }
 };
 
 } // anonymous namespace
@@ -212,6 +200,7 @@ Mirror::Mirror(CephContext *cct, const std::vector<const char*> &args,
                          "cephfs::mirror::thread_pool", false, cct));
   auto safe_timer = &(cct->lookup_or_create_singleton_object<SafeTimerSingleton>(
                         "cephfs::mirror::safe_timer", false, cct));
+  m_thread_pool = thread_pool;
   m_work_queue = thread_pool->work_queue;
   m_timer = safe_timer;
   m_timer_lock = &safe_timer->timer_lock;
@@ -221,12 +210,25 @@ Mirror::Mirror(CephContext *cct, const std::vector<const char*> &args,
 
 Mirror::~Mirror() {
   dout(10) << dendl;
+  {
+    std::scoped_lock timer_lock(*m_timer_lock);
+    m_timer->shutdown();
+  }
+
+  m_work_queue->drain();
+  delete m_work_queue;
+  {
+    std::scoped_lock locker(m_lock);
+    m_thread_pool->stop();
+    m_cluster_watcher.reset();
+  }
 }
 
 int Mirror::init_mon_client() {
   dout(20) << dendl;
 
   m_monc->set_messenger(m_msgr);
+  m_msgr->add_dispatcher_head(m_monc);
   m_monc->set_want_keys(CEPH_ENTITY_TYPE_MON);
 
   int r = m_monc->init();
@@ -262,20 +264,14 @@ void Mirror::shutdown() {
   dout(20) << dendl;
 
   std::unique_lock locker(m_lock);
-  if (m_mirror_actions.empty()) {
-    return;
-  }
-
   m_stopping = true;
   m_cond.notify_all();
-  m_cond.wait(locker, [this] {return m_stopped;});
 }
 
 void Mirror::handle_signal(int signum) {
   dout(10) << ": signal=" << signum << dendl;
   ceph_assert(signum == SIGTERM || signum == SIGINT);
   shutdown();
-  ::exit(0);
 }
 
 void Mirror::handle_enable_mirroring(const Filesystem &filesystem,
@@ -537,9 +533,6 @@ void Mirror::run() {
       dout(10) << ": shutdown filesystem=" << filesystem << ", r=" << r << dendl;
     }
   }
-
-  m_stopped = true;
-  m_cond.notify_all();
 }
 
 } // namespace mirror
index 77d6829c7dd2057b38f6149b73dcfb794d3332b9..933b04ac3467e858a6a497514069f7b81bea9b53 100644 (file)
@@ -9,6 +9,7 @@
 #include <vector>
 
 #include "common/ceph_mutex.h"
+#include "common/WorkQueue.h"
 #include "mds/FSMap.h"
 #include "ClusterWatcher.h"
 #include "FSMirror.h"
@@ -88,13 +89,13 @@ private:
   Messenger *m_msgr;
   ClusterListener m_listener;
 
+  ThreadPool *m_thread_pool = nullptr;
   ContextWQ *m_work_queue = nullptr;
   SafeTimer *m_timer = nullptr;
   ceph::mutex *m_timer_lock = nullptr;
   Context *m_timer_task = nullptr;
 
   bool m_stopping = false;
-  bool m_stopped = false;
   std::unique_ptr<ClusterWatcher> m_cluster_watcher;
   std::map<Filesystem, MirrorAction> m_mirror_actions;
 
index d363159a0eb6ba577c6fffa7812434aef7192da1..2301cb7ef48fdde1b9690b0357f6ceb45c6f93c7 100644 (file)
@@ -82,12 +82,18 @@ int main(int argc, const char **argv) {
   }
 
   mirror->run();
+  delete mirror;
 
 cleanup:
   monc.shutdown();
 cleanup_messenger:
   msgr->shutdown();
-  delete mirror;
+  msgr->wait();
+  delete msgr;
+
+  unregister_async_signal_handler(SIGINT, handle_signal);
+  unregister_async_signal_handler(SIGTERM, handle_signal);
+  shutdown_async_signal_handler();
 
   return r < 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }