]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd-mirror: templatize Threads helper class for mock tests
authorJason Dillaman <dillaman@redhat.com>
Mon, 28 Nov 2016 21:02:07 +0000 (16:02 -0500)
committerJason Dillaman <dillaman@redhat.com>
Thu, 16 Mar 2017 20:45:12 +0000 (16:45 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
16 files changed:
src/test/rbd_mirror/test_ImageReplayer.cc
src/test/rbd_mirror/test_fixture.cc
src/test/rbd_mirror/test_fixture.h
src/test/rbd_mirror/test_mock_LeaderWatcher.cc
src/tools/rbd_mirror/ImageReplayer.cc
src/tools/rbd_mirror/ImageReplayer.h
src/tools/rbd_mirror/Instances.cc
src/tools/rbd_mirror/Instances.h
src/tools/rbd_mirror/LeaderWatcher.cc
src/tools/rbd_mirror/LeaderWatcher.h
src/tools/rbd_mirror/Mirror.cc
src/tools/rbd_mirror/Mirror.h
src/tools/rbd_mirror/Replayer.cc
src/tools/rbd_mirror/Replayer.h
src/tools/rbd_mirror/Threads.cc
src/tools/rbd_mirror/Threads.h

index 35139384149e5d563bfbbb2d7dfc7670ce9f9e66..14a511a1456b484a81d2a691e04cf4c281de2254 100644 (file)
@@ -112,7 +112,7 @@ public:
                                false, features, &order, 0, 0));
     m_remote_image_id = get_image_id(m_remote_ioctx, m_image_name);
 
-    m_threads = new rbd::mirror::Threads(reinterpret_cast<CephContext*>(
+    m_threads = new rbd::mirror::Threads<>(reinterpret_cast<CephContext*>(
       m_local_ioctx.cct()));
 
     m_image_deleter.reset(new rbd::mirror::ImageDeleter(m_threads->work_queue,
@@ -360,7 +360,7 @@ public:
 
   static int _image_number;
 
-  rbd::mirror::Threads *m_threads = nullptr;
+  rbd::mirror::Threads<> *m_threads = nullptr;
   std::shared_ptr<rbd::mirror::ImageDeleter> m_image_deleter;
   std::shared_ptr<librados::Rados> m_local_cluster;
   librados::Rados m_remote_cluster;
index 0d6fc4d5a9ba7dace18eb16d43f93ce0aa78db28..563194f766941bbab12818bdfa12cf4dd19eef4c 100644 (file)
@@ -63,7 +63,7 @@ void TestFixture::SetUp() {
   ASSERT_EQ(0, _rados->ioctx_create(_remote_pool_name.c_str(), m_remote_io_ctx));
   m_image_name = get_temp_image_name();
 
-  m_threads = new rbd::mirror::Threads(reinterpret_cast<CephContext*>(
+  m_threads = new rbd::mirror::Threads<>(reinterpret_cast<CephContext*>(
     m_local_io_ctx.cct()));
 }
 
index 41526dbdc2217e7bab0bcea8ac1cf5a4ecf6b83d..5cc99e454cbfe8db5fe595ac5332e0d3d1a006ce 100644 (file)
@@ -17,7 +17,7 @@ class RBD;
 namespace rbd {
 namespace mirror {
 
-class Threads;
+template <typename> class Threads;
 
 class TestFixture : public ::testing::Test {
 public:
@@ -37,7 +37,7 @@ public:
 
   std::set<librbd::ImageCtx *> m_image_ctxs;
 
-  Threads *m_threads = nullptr;
+  Threads<librbd::ImageCtx> *m_threads = nullptr;
 
 
   int create_image(librbd::RBD &rbd, librados::IoCtx &ioctx,
index 7adc2737b79af81c3ea36e5a573d892766e5f377..0796045834b7378cc16cacbada31222645458fd2 100644 (file)
@@ -163,6 +163,18 @@ struct ManagedLock<MockTestImageCtx> {
 namespace rbd {
 namespace mirror {
 
+template <>
+struct Threads<librbd::MockTestImageCtx> {
+  Mutex &timer_lock;
+  SafeTimer *timer;
+  ContextWQ *work_queue;
+
+  Threads(Threads<librbd::ImageCtx> *threads)
+    : timer_lock(threads->timer_lock), timer(threads->timer),
+      work_queue(threads->work_queue) {
+  }
+};
+
 template <>
 struct MirrorStatusWatcher<librbd::MockTestImageCtx> {
   static MirrorStatusWatcher* s_instance;
@@ -194,7 +206,8 @@ template <>
 struct Instances<librbd::MockTestImageCtx> {
   static Instances* s_instance;
 
-  static Instances *create(Threads *threads, librados::IoCtx &ioctx) {
+  static Instances *create(Threads<librbd::MockTestImageCtx> *threads,
+                           librados::IoCtx &ioctx) {
     assert(s_instance != nullptr);
     return s_instance;
   }
@@ -260,6 +273,17 @@ public:
   typedef MirrorStatusWatcher<librbd::MockTestImageCtx> MockMirrorStatusWatcher;
   typedef Instances<librbd::MockTestImageCtx> MockInstances;
   typedef LeaderWatcher<librbd::MockTestImageCtx> MockLeaderWatcher;
+  typedef Threads<librbd::MockTestImageCtx> MockThreads;
+
+  void SetUp() override {
+    TestMockFixture::SetUp();
+    m_mock_threads = new MockThreads(m_threads);
+  }
+
+  void TearDown() override {
+    delete m_mock_threads;
+    TestMockFixture::TearDown();
+  }
 
   void expect_construct(MockManagedLock &mock_managed_lock) {
     EXPECT_CALL(mock_managed_lock, construct());
@@ -377,12 +401,12 @@ public:
 
   void expect_init(MockMirrorStatusWatcher &mock_mirror_status_watcher, int r) {
     EXPECT_CALL(mock_mirror_status_watcher, init(_))
-      .WillOnce(CompleteContext(m_threads->work_queue, r));
+      .WillOnce(CompleteContext(m_mock_threads->work_queue, r));
   }
 
   void expect_shut_down(MockMirrorStatusWatcher &mock_mirror_status_watcher, int r) {
     EXPECT_CALL(mock_mirror_status_watcher, shut_down(_))
-      .WillOnce(CompleteContext(m_threads->work_queue, r));
+      .WillOnce(CompleteContext(m_mock_threads->work_queue, r));
     expect_destroy(mock_mirror_status_watcher);
   }
 
@@ -392,12 +416,12 @@ public:
 
   void expect_init(MockInstances &mock_instances, int r) {
     EXPECT_CALL(mock_instances, init(_))
-      .WillOnce(CompleteContext(m_threads->work_queue, r));
+      .WillOnce(CompleteContext(m_mock_threads->work_queue, r));
   }
 
   void expect_shut_down(MockInstances &mock_instances, int r) {
     EXPECT_CALL(mock_instances, shut_down(_))
-      .WillOnce(CompleteContext(m_threads->work_queue, r));
+      .WillOnce(CompleteContext(m_mock_threads->work_queue, r));
     expect_destroy(mock_instances);
   }
 
@@ -416,6 +440,8 @@ public:
       .WillOnce(CompleteContext(r));
     expect_is_leader(mock_managed_lock, false, false);
   }
+
+  MockThreads *m_mock_threads;
 };
 
 TEST_F(TestMockLeaderWatcher, InitShutdown) {
@@ -430,7 +456,7 @@ TEST_F(TestMockLeaderWatcher, InitShutdown) {
   InSequence seq;
 
   expect_construct(mock_managed_lock);
-  MockLeaderWatcher leader_watcher(m_threads, m_local_io_ctx, &listener);
+  MockLeaderWatcher leader_watcher(m_mock_threads, m_local_io_ctx, &listener);
 
   // Inint
   C_SaferCond on_heartbeat_finish;
@@ -468,7 +494,7 @@ TEST_F(TestMockLeaderWatcher, InitReleaseShutdown) {
   InSequence seq;
 
   expect_construct(mock_managed_lock);
-  MockLeaderWatcher leader_watcher(m_threads, m_local_io_ctx, &listener);
+  MockLeaderWatcher leader_watcher(m_mock_threads, m_local_io_ctx, &listener);
 
   // Inint
   C_SaferCond on_heartbeat_finish;
@@ -514,7 +540,7 @@ TEST_F(TestMockLeaderWatcher, AcquireError) {
   InSequence seq;
 
   expect_construct(mock_managed_lock);
-  MockLeaderWatcher leader_watcher(m_threads, m_local_io_ctx, &listener);
+  MockLeaderWatcher leader_watcher(m_mock_threads, m_local_io_ctx, &listener);
 
   // Inint
   C_SaferCond on_get_locker_finish;
@@ -544,7 +570,7 @@ TEST_F(TestMockLeaderWatcher, ReleaseError) {
   InSequence seq;
 
   expect_construct(mock_managed_lock);
-  MockLeaderWatcher leader_watcher(m_threads, m_local_io_ctx, &listener);
+  MockLeaderWatcher leader_watcher(m_mock_threads, m_local_io_ctx, &listener);
 
   // Inint
   C_SaferCond on_heartbeat_finish;
@@ -599,7 +625,7 @@ TEST_F(TestMockLeaderWatcher, Break) {
   InSequence seq;
 
   expect_construct(mock_managed_lock);
-  MockLeaderWatcher leader_watcher(m_threads, m_local_io_ctx, &listener);
+  MockLeaderWatcher leader_watcher(m_mock_threads, m_local_io_ctx, &listener);
 
   // Init
   expect_is_leader(mock_managed_lock, false, false);
index 5cded6859b0856f0547f97d8f26e9c586e749424..7de87f3d15c1fa8bc412156eba2bed23edc63f0f 100644 (file)
@@ -264,13 +264,13 @@ void ImageReplayer<I>::RemoteJournalerListener::handle_update(
 }
 
 template <typename I>
-ImageReplayer<I>::ImageReplayer(Threads *threads,
-                             shared_ptr<ImageDeleter> image_deleter,
-                             ImageSyncThrottlerRef<I> image_sync_throttler,
-                             RadosRef local,
-                             const std::string &local_mirror_uuid,
-                             int64_t local_pool_id,
-                             const std::string &global_image_id) :
+ImageReplayer<I>::ImageReplayer(Threads<librbd::ImageCtx> *threads,
+                                shared_ptr<ImageDeleter> image_deleter,
+                                ImageSyncThrottlerRef<I> image_sync_throttler,
+                                RadosRef local,
+                                const std::string &local_mirror_uuid,
+                                int64_t local_pool_id,
+                                const std::string &global_image_id) :
   m_threads(threads),
   m_image_deleter(image_deleter),
   m_image_sync_throttler(image_sync_throttler),
index da3d107a5f31312c8238450e08a43e5a47e82747..07ed7f8ada4cb37b0a8d2b1fb03b7f3bebf19e5f 100644 (file)
@@ -46,7 +46,7 @@ namespace journal { template <typename> class Replay; }
 namespace rbd {
 namespace mirror {
 
-struct Threads;
+template <typename> struct Threads;
 
 namespace image_replayer { template <typename> class BootstrapRequest; }
 namespace image_replayer { template <typename> class EventPreprocessor; }
@@ -69,7 +69,8 @@ public:
     STATE_STOPPED,
   };
 
-  ImageReplayer(Threads *threads, std::shared_ptr<ImageDeleter> image_deleter,
+  ImageReplayer(Threads<librbd::ImageCtx> *threads,
+                std::shared_ptr<ImageDeleter> image_deleter,
                 ImageSyncThrottlerRef<ImageCtxT> image_sync_throttler,
                 RadosRef local, const std::string &local_mirror_uuid,
                 int64_t local_pool_id, const std::string &global_image_id);
@@ -269,7 +270,7 @@ private:
     ImageReplayer<ImageCtxT> *replayer;
   };
 
-  Threads *m_threads;
+  Threads<librbd::ImageCtx> *m_threads;
   std::shared_ptr<ImageDeleter> m_image_deleter;
   ImageSyncThrottlerRef<ImageCtxT> m_image_sync_throttler;
 
index 432c593a12525117d69e2e6894e062073ac85244..0adc4f3428e7d3b6a0269263aca89e2527afaf92 100644 (file)
@@ -25,7 +25,7 @@ using librbd::util::create_context_callback;
 using librbd::util::create_rados_callback;
 
 template <typename I>
-Instances<I>::Instances(Threads *threads, librados::IoCtx &ioctx) :
+Instances<I>::Instances(Threads<I> *threads, librados::IoCtx &ioctx) :
   m_threads(threads), m_ioctx(ioctx),
   m_cct(reinterpret_cast<CephContext *>(ioctx.cct())),
   m_lock("rbd::mirror::Instances " + ioctx.get_pool_name()) {
index e8a4c25200d0f8e1f5bd7aa760d04e4e69d11813..2aa4bcf721db3e8cb4768e7ffda72be3cc2219c7 100644 (file)
@@ -18,19 +18,20 @@ namespace librbd { class ImageCtx; }
 namespace rbd {
 namespace mirror {
 
-struct Threads;
+template <typename> struct Threads;
 
 template <typename ImageCtxT = librbd::ImageCtx>
 class Instances {
 public:
-  static Instances *create(Threads *threads, librados::IoCtx &ioctx) {
+  static Instances *create(Threads<ImageCtxT> *threads,
+                           librados::IoCtx &ioctx) {
     return new Instances(threads, ioctx);
   }
   void destroy() {
     delete this;
   }
 
-  Instances(Threads *threads, librados::IoCtx &ioctx);
+  Instances(Threads<ImageCtxT> *threads, librados::IoCtx &ioctx);
   virtual ~Instances();
 
   void init(Context *on_finish);
@@ -81,7 +82,7 @@ private:
     }
   };
 
-  Threads *m_threads;
+  Threads<ImageCtxT> *m_threads;
   librados::IoCtx &m_ioctx;
   CephContext *m_cct;
 
index 8376bbe6fe0ea42c522132997e77fffd4ec28a0c..10b0430bef4bd852a69c3dda52d131f1d0e82678 100644 (file)
@@ -25,7 +25,7 @@ using librbd::util::create_context_callback;
 using librbd::util::create_rados_callback;
 
 template <typename I>
-LeaderWatcher<I>::LeaderWatcher(Threads *threads, librados::IoCtx &io_ctx,
+LeaderWatcher<I>::LeaderWatcher(Threads<I> *threads, librados::IoCtx &io_ctx,
                                 Listener *listener)
   : Watcher(io_ctx, threads->work_queue, RBD_MIRROR_LEADER),
     m_threads(threads), m_listener(listener),
index a3a662a8f6405a29c703b2cb184679fc3f0b9513..c1fbd013452d7a6809ccd36ef1e3c03226879683 100644 (file)
@@ -21,7 +21,7 @@ namespace librbd { class ImageCtx; }
 namespace rbd {
 namespace mirror {
 
-struct Threads;
+template <typename> struct Threads;
 
 template <typename ImageCtxT = librbd::ImageCtx>
 class LeaderWatcher : protected librbd::Watcher {
@@ -34,7 +34,8 @@ public:
     virtual void pre_release_handler(Context *on_finish) = 0;
   };
 
-  LeaderWatcher(Threads *threads, librados::IoCtx &io_ctx, Listener *listener);
+  LeaderWatcher(Threads<ImageCtxT> *threads, librados::IoCtx &io_ctx,
+                Listener *listener);
   ~LeaderWatcher() override;
 
   int init();
@@ -176,7 +177,7 @@ private:
     }
   };
 
-  Threads *m_threads;
+  Threads<ImageCtxT> *m_threads;
   Listener *m_listener;
 
   Mutex m_lock;
index 66d2f2e9c08fa37e478de71bdcfb034c51accdea..0424edd91ddb200604a2225a3abd95efbe5af423 100644 (file)
@@ -7,6 +7,7 @@
 #include "common/admin_socket.h"
 #include "common/debug.h"
 #include "common/errno.h"
+#include "librbd/ImageCtx.h"
 #include "Mirror.h"
 #include "Threads.h"
 #include "ImageSync.h"
@@ -203,8 +204,8 @@ Mirror::Mirror(CephContext *cct, const std::vector<const char*> &args) :
   m_local(new librados::Rados()),
   m_asok_hook(new MirrorAdminSocketHook(cct, this))
 {
-  cct->lookup_or_create_singleton_object<Threads>(m_threads,
-                                                  "rbd_mirror::threads");
+  cct->lookup_or_create_singleton_object<Threads<librbd::ImageCtx> >(
+    m_threads, "rbd_mirror::threads");
 }
 
 Mirror::~Mirror()
index 7c39fe9d6de09c43b9e7f572f490c828f8ff687f..59e0575358b385e1494a6504faf2a8da5247d511 100644 (file)
 #include "ImageDeleter.h"
 #include "types.h"
 
+namespace librbd { struct ImageCtx; }
+
 namespace rbd {
 namespace mirror {
 
-struct Threads;
+template <typename> struct Threads;
 class MirrorAdminSocketHook;
 
 /**
@@ -55,7 +57,7 @@ private:
 
   CephContext *m_cct;
   std::vector<const char*> m_args;
-  Threads *m_threads = nullptr;
+  Threads<librbd::ImageCtx> *m_threads = nullptr;
   Mutex m_lock;
   Cond m_cond;
   RadosRef m_local;
index fd798fa887c2921afad7f78aff6f8a0a8b5c4627..4e6af299cc2d7be587bb94403488c0aaea645c49 100644 (file)
@@ -206,7 +206,8 @@ private:
   Commands commands;
 };
 
-Replayer::Replayer(Threads *threads, std::shared_ptr<ImageDeleter> image_deleter,
+Replayer::Replayer(Threads<librbd::ImageCtx> *threads,
+                   std::shared_ptr<ImageDeleter> image_deleter,
                    ImageSyncThrottlerRef<> image_sync_throttler,
                    int64_t local_pool_id, const peer_t &peer,
                    const std::vector<const char*> &args) :
index 286b70dddbfee4eebd0f26ce82d02f9e34588979..abcc35694af0c3c6c7a3a674c8f11db619b3ad4b 100644 (file)
@@ -27,7 +27,7 @@ namespace librbd { class ImageCtx; }
 namespace rbd {
 namespace mirror {
 
-struct Threads;
+template <typename> struct Threads;
 class ReplayerAdminSocketHook;
 template <typename> class InstanceWatcher;
 
@@ -36,7 +36,8 @@ template <typename> class InstanceWatcher;
  */
 class Replayer {
 public:
-  Replayer(Threads *threads, std::shared_ptr<ImageDeleter> image_deleter,
+  Replayer(Threads<librbd::ImageCtx> *threads,
+           std::shared_ptr<ImageDeleter> image_deleter,
            ImageSyncThrottlerRef<> image_sync_throttler,
            int64_t local_pool_id, const peer_t &peer,
            const std::vector<const char*> &args);
@@ -70,7 +71,7 @@ private:
   void handle_post_acquire_leader(Context *on_finish);
   void handle_pre_release_leader(Context *on_finish);
 
-  Threads *m_threads;
+  Threads<librbd::ImageCtx> *m_threads;
   std::shared_ptr<ImageDeleter> m_image_deleter;
   ImageSyncThrottlerRef<> m_image_sync_throttler;
   mutable Mutex m_lock;
index 8fa7d6d9a0034db34e6b79183820b64602e1ea1b..8c22440a006718c6d9c56afdc93282f1c05c2b9d 100644 (file)
@@ -4,11 +4,13 @@
 #include "tools/rbd_mirror/Threads.h"
 #include "common/Timer.h"
 #include "common/WorkQueue.h"
+#include "librbd/ImageCtx.h"
 
 namespace rbd {
 namespace mirror {
 
-Threads::Threads(CephContext *cct) : timer_lock("Threads::timer_lock") {
+template <typename I>
+Threads<I>::Threads(CephContext *cct) : timer_lock("Threads::timer_lock") {
   thread_pool = new ThreadPool(cct, "Journaler::thread_pool", "tp_journal",
                                cct->_conf->rbd_op_threads, "rbd_op_threads");
   thread_pool->start();
@@ -20,7 +22,8 @@ Threads::Threads(CephContext *cct) : timer_lock("Threads::timer_lock") {
   timer->init();
 }
 
-Threads::~Threads() {
+template <typename I>
+Threads<I>::~Threads() {
   {
     Mutex::Locker timer_locker(timer_lock);
     timer->shutdown();
@@ -36,3 +39,5 @@ Threads::~Threads() {
 
 } // namespace mirror
 } // namespace rbd
+
+template class rbd::mirror::Threads<librbd::ImageCtx>;
index ba952836ad290a1d2f06b77bfbb1fca1eb9ed199..f52e8837d35a1e0ddf2a1b1786d92e00e9a5a3b6 100644 (file)
@@ -11,9 +11,12 @@ class ContextWQ;
 class SafeTimer;
 class ThreadPool;
 
+namespace librbd { struct ImageCtx; }
+
 namespace rbd {
 namespace mirror {
 
+template <typename ImageCtxT = librbd::ImageCtx>
 struct Threads {
   ThreadPool *thread_pool = nullptr;
   ContextWQ *work_queue = nullptr;
@@ -31,4 +34,6 @@ struct Threads {
 } // namespace mirror
 } // namespace rbd
 
+extern template class rbd::mirror::Threads<librbd::ImageCtx>;
+
 #endif // CEPH_RBD_MIRROR_THREADS_H