]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_context: add prefork/postfork wathcher support
authorHaomai Wang <haomai@xsky.com>
Tue, 12 Jul 2016 04:26:30 +0000 (12:26 +0800)
committerHaomai Wang <haomai@xsky.com>
Tue, 16 Aug 2016 15:19:25 +0000 (23:19 +0800)
Because daemon() will termniate all existing threads, it will make something go
wrong.

So we want to add hook at CephContext, do something before/after fork.

Signed-off-by: Haomai Wang <haomai@xsky.com>
src/common/ceph_context.cc
src/common/ceph_context.h
src/global/global_init.cc

index abf9f07e05bcae1f3a87eb84efccadad5b51cdc4..b4cb9cabe2060fa6facd4c32008e1155d9e332d3 100644 (file)
@@ -472,6 +472,7 @@ CephContext::CephContext(uint32_t module_type_, int init_flags_)
 {
   ceph_spin_init(&_service_thread_lock);
   ceph_spin_init(&_associated_objs_lock);
+  ceph_spin_init(&_fork_watchers_lock);
   ceph_spin_init(&_feature_lock);
   ceph_spin_init(&_cct_perf_lock);
 
@@ -575,6 +576,7 @@ CephContext::~CephContext()
 
   delete _conf;
   ceph_spin_destroy(&_service_thread_lock);
+  ceph_spin_destroy(&_fork_watchers_lock);
   ceph_spin_destroy(&_associated_objs_lock);
   ceph_spin_destroy(&_feature_lock);
   ceph_spin_destroy(&_cct_perf_lock);
index 86c1a4a736de806c9c4266367f7a6dcf5ff1aca1..6bf03b3214b81306d2a9cc7d6a7ce770aeef5533 100644 (file)
@@ -177,6 +177,33 @@ public:
     return _set_gid_string;
   }
 
+  class ForkWatcher {
+   public:
+    virtual ~ForkWatcher() {}
+    virtual void handle_pre_fork() = 0;
+    virtual void handle_post_fork() = 0;
+  };
+
+  void register_fork_watcher(ForkWatcher *w) {
+    ceph_spin_lock(&_fork_watchers_lock);
+    _fork_watchers.push_back(w);
+    ceph_spin_unlock(&_fork_watchers_lock);
+  }
+
+  void notify_pre_fork() {
+    ceph_spin_lock(&_fork_watchers_lock);
+    for (auto &&t : _fork_watchers)
+      t->handle_pre_fork();
+    ceph_spin_unlock(&_fork_watchers_lock);
+  }
+
+  void notify_post_fork() {
+    ceph_spin_lock(&_fork_watchers_lock);
+    for (auto &&t : _fork_watchers)
+      t->handle_post_fork();
+    ceph_spin_unlock(&_fork_watchers_lock);
+  }
+
 private:
   struct SingletonWrapper : boost::noncopyable {
     virtual ~SingletonWrapper() {}
@@ -235,6 +262,9 @@ private:
   ceph_spinlock_t _associated_objs_lock;
   std::map<std::string, SingletonWrapper*> _associated_objs;
 
+  ceph_spinlock_t _fork_watchers_lock;
+  std::vector<ForkWatcher*> _fork_watchers;
+
   // crypto
   CryptoHandler *_crypto_none;
   CryptoHandler *_crypto_aes;
index cda4438da90395697987b94f6a9bac58ebbeba73..033516240b6c5b6fecb8ffb63d67292b9f743ce2 100644 (file)
@@ -339,6 +339,7 @@ int global_init_prefork(CephContext *cct)
     return -1;
   }
 
+  cct->notify_pre_fork();
   // stop log thread
   cct->_log->flush();
   cct->_log->stop();
@@ -370,6 +371,7 @@ void global_init_postfork_start(CephContext *cct)
 {
   // restart log thread
   cct->_log->start();
+  cct->notify_post_fork();
 
   /* This is the old trick where we make file descriptors 0, 1, and possibly 2
    * point to /dev/null.