From 7a26dfab6de4a331de3eb80345457d08e5dc46da Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Mon, 28 Nov 2016 16:02:07 -0500 Subject: [PATCH] rbd-mirror: templatize Threads helper class for mock tests Signed-off-by: Jason Dillaman --- src/test/rbd_mirror/test_ImageReplayer.cc | 4 +- src/test/rbd_mirror/test_fixture.cc | 2 +- src/test/rbd_mirror/test_fixture.h | 4 +- .../rbd_mirror/test_mock_LeaderWatcher.cc | 46 +++++++++++++++---- src/tools/rbd_mirror/ImageReplayer.cc | 14 +++--- src/tools/rbd_mirror/ImageReplayer.h | 7 +-- src/tools/rbd_mirror/Instances.cc | 2 +- src/tools/rbd_mirror/Instances.h | 9 ++-- src/tools/rbd_mirror/LeaderWatcher.cc | 2 +- src/tools/rbd_mirror/LeaderWatcher.h | 7 +-- src/tools/rbd_mirror/Mirror.cc | 5 +- src/tools/rbd_mirror/Mirror.h | 6 ++- src/tools/rbd_mirror/Replayer.cc | 3 +- src/tools/rbd_mirror/Replayer.h | 7 +-- src/tools/rbd_mirror/Threads.cc | 9 +++- src/tools/rbd_mirror/Threads.h | 5 ++ 16 files changed, 88 insertions(+), 44 deletions(-) diff --git a/src/test/rbd_mirror/test_ImageReplayer.cc b/src/test/rbd_mirror/test_ImageReplayer.cc index 35139384149..14a511a1456 100644 --- a/src/test/rbd_mirror/test_ImageReplayer.cc +++ b/src/test/rbd_mirror/test_ImageReplayer.cc @@ -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( + m_threads = new rbd::mirror::Threads<>(reinterpret_cast( 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 m_image_deleter; std::shared_ptr m_local_cluster; librados::Rados m_remote_cluster; diff --git a/src/test/rbd_mirror/test_fixture.cc b/src/test/rbd_mirror/test_fixture.cc index 0d6fc4d5a9b..563194f7669 100644 --- a/src/test/rbd_mirror/test_fixture.cc +++ b/src/test/rbd_mirror/test_fixture.cc @@ -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( + m_threads = new rbd::mirror::Threads<>(reinterpret_cast( m_local_io_ctx.cct())); } diff --git a/src/test/rbd_mirror/test_fixture.h b/src/test/rbd_mirror/test_fixture.h index 41526dbdc22..5cc99e454cb 100644 --- a/src/test/rbd_mirror/test_fixture.h +++ b/src/test/rbd_mirror/test_fixture.h @@ -17,7 +17,7 @@ class RBD; namespace rbd { namespace mirror { -class Threads; +template class Threads; class TestFixture : public ::testing::Test { public: @@ -37,7 +37,7 @@ public: std::set m_image_ctxs; - Threads *m_threads = nullptr; + Threads *m_threads = nullptr; int create_image(librbd::RBD &rbd, librados::IoCtx &ioctx, diff --git a/src/test/rbd_mirror/test_mock_LeaderWatcher.cc b/src/test/rbd_mirror/test_mock_LeaderWatcher.cc index 7adc2737b79..0796045834b 100644 --- a/src/test/rbd_mirror/test_mock_LeaderWatcher.cc +++ b/src/test/rbd_mirror/test_mock_LeaderWatcher.cc @@ -163,6 +163,18 @@ struct ManagedLock { namespace rbd { namespace mirror { +template <> +struct Threads { + Mutex &timer_lock; + SafeTimer *timer; + ContextWQ *work_queue; + + Threads(Threads *threads) + : timer_lock(threads->timer_lock), timer(threads->timer), + work_queue(threads->work_queue) { + } +}; + template <> struct MirrorStatusWatcher { static MirrorStatusWatcher* s_instance; @@ -194,7 +206,8 @@ template <> struct Instances { static Instances* s_instance; - static Instances *create(Threads *threads, librados::IoCtx &ioctx) { + static Instances *create(Threads *threads, + librados::IoCtx &ioctx) { assert(s_instance != nullptr); return s_instance; } @@ -260,6 +273,17 @@ public: typedef MirrorStatusWatcher MockMirrorStatusWatcher; typedef Instances MockInstances; typedef LeaderWatcher MockLeaderWatcher; + typedef Threads 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); diff --git a/src/tools/rbd_mirror/ImageReplayer.cc b/src/tools/rbd_mirror/ImageReplayer.cc index 5cded6859b0..7de87f3d15c 100644 --- a/src/tools/rbd_mirror/ImageReplayer.cc +++ b/src/tools/rbd_mirror/ImageReplayer.cc @@ -264,13 +264,13 @@ void ImageReplayer::RemoteJournalerListener::handle_update( } template -ImageReplayer::ImageReplayer(Threads *threads, - shared_ptr image_deleter, - ImageSyncThrottlerRef image_sync_throttler, - RadosRef local, - const std::string &local_mirror_uuid, - int64_t local_pool_id, - const std::string &global_image_id) : +ImageReplayer::ImageReplayer(Threads *threads, + shared_ptr image_deleter, + ImageSyncThrottlerRef 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), diff --git a/src/tools/rbd_mirror/ImageReplayer.h b/src/tools/rbd_mirror/ImageReplayer.h index da3d107a5f3..07ed7f8ada4 100644 --- a/src/tools/rbd_mirror/ImageReplayer.h +++ b/src/tools/rbd_mirror/ImageReplayer.h @@ -46,7 +46,7 @@ namespace journal { template class Replay; } namespace rbd { namespace mirror { -struct Threads; +template struct Threads; namespace image_replayer { template class BootstrapRequest; } namespace image_replayer { template class EventPreprocessor; } @@ -69,7 +69,8 @@ public: STATE_STOPPED, }; - ImageReplayer(Threads *threads, std::shared_ptr image_deleter, + ImageReplayer(Threads *threads, + std::shared_ptr image_deleter, ImageSyncThrottlerRef 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 *replayer; }; - Threads *m_threads; + Threads *m_threads; std::shared_ptr m_image_deleter; ImageSyncThrottlerRef m_image_sync_throttler; diff --git a/src/tools/rbd_mirror/Instances.cc b/src/tools/rbd_mirror/Instances.cc index 432c593a125..0adc4f3428e 100644 --- a/src/tools/rbd_mirror/Instances.cc +++ b/src/tools/rbd_mirror/Instances.cc @@ -25,7 +25,7 @@ using librbd::util::create_context_callback; using librbd::util::create_rados_callback; template -Instances::Instances(Threads *threads, librados::IoCtx &ioctx) : +Instances::Instances(Threads *threads, librados::IoCtx &ioctx) : m_threads(threads), m_ioctx(ioctx), m_cct(reinterpret_cast(ioctx.cct())), m_lock("rbd::mirror::Instances " + ioctx.get_pool_name()) { diff --git a/src/tools/rbd_mirror/Instances.h b/src/tools/rbd_mirror/Instances.h index e8a4c25200d..2aa4bcf721d 100644 --- a/src/tools/rbd_mirror/Instances.h +++ b/src/tools/rbd_mirror/Instances.h @@ -18,19 +18,20 @@ namespace librbd { class ImageCtx; } namespace rbd { namespace mirror { -struct Threads; +template struct Threads; template class Instances { public: - static Instances *create(Threads *threads, librados::IoCtx &ioctx) { + static Instances *create(Threads *threads, + librados::IoCtx &ioctx) { return new Instances(threads, ioctx); } void destroy() { delete this; } - Instances(Threads *threads, librados::IoCtx &ioctx); + Instances(Threads *threads, librados::IoCtx &ioctx); virtual ~Instances(); void init(Context *on_finish); @@ -81,7 +82,7 @@ private: } }; - Threads *m_threads; + Threads *m_threads; librados::IoCtx &m_ioctx; CephContext *m_cct; diff --git a/src/tools/rbd_mirror/LeaderWatcher.cc b/src/tools/rbd_mirror/LeaderWatcher.cc index 8376bbe6fe0..10b0430bef4 100644 --- a/src/tools/rbd_mirror/LeaderWatcher.cc +++ b/src/tools/rbd_mirror/LeaderWatcher.cc @@ -25,7 +25,7 @@ using librbd::util::create_context_callback; using librbd::util::create_rados_callback; template -LeaderWatcher::LeaderWatcher(Threads *threads, librados::IoCtx &io_ctx, +LeaderWatcher::LeaderWatcher(Threads *threads, librados::IoCtx &io_ctx, Listener *listener) : Watcher(io_ctx, threads->work_queue, RBD_MIRROR_LEADER), m_threads(threads), m_listener(listener), diff --git a/src/tools/rbd_mirror/LeaderWatcher.h b/src/tools/rbd_mirror/LeaderWatcher.h index a3a662a8f64..c1fbd013452 100644 --- a/src/tools/rbd_mirror/LeaderWatcher.h +++ b/src/tools/rbd_mirror/LeaderWatcher.h @@ -21,7 +21,7 @@ namespace librbd { class ImageCtx; } namespace rbd { namespace mirror { -struct Threads; +template struct Threads; template 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 *threads, librados::IoCtx &io_ctx, + Listener *listener); ~LeaderWatcher() override; int init(); @@ -176,7 +177,7 @@ private: } }; - Threads *m_threads; + Threads *m_threads; Listener *m_listener; Mutex m_lock; diff --git a/src/tools/rbd_mirror/Mirror.cc b/src/tools/rbd_mirror/Mirror.cc index 66d2f2e9c08..0424edd91dd 100644 --- a/src/tools/rbd_mirror/Mirror.cc +++ b/src/tools/rbd_mirror/Mirror.cc @@ -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 &args) : m_local(new librados::Rados()), m_asok_hook(new MirrorAdminSocketHook(cct, this)) { - cct->lookup_or_create_singleton_object(m_threads, - "rbd_mirror::threads"); + cct->lookup_or_create_singleton_object >( + m_threads, "rbd_mirror::threads"); } Mirror::~Mirror() diff --git a/src/tools/rbd_mirror/Mirror.h b/src/tools/rbd_mirror/Mirror.h index 7c39fe9d6de..59e0575358b 100644 --- a/src/tools/rbd_mirror/Mirror.h +++ b/src/tools/rbd_mirror/Mirror.h @@ -17,10 +17,12 @@ #include "ImageDeleter.h" #include "types.h" +namespace librbd { struct ImageCtx; } + namespace rbd { namespace mirror { -struct Threads; +template struct Threads; class MirrorAdminSocketHook; /** @@ -55,7 +57,7 @@ private: CephContext *m_cct; std::vector m_args; - Threads *m_threads = nullptr; + Threads *m_threads = nullptr; Mutex m_lock; Cond m_cond; RadosRef m_local; diff --git a/src/tools/rbd_mirror/Replayer.cc b/src/tools/rbd_mirror/Replayer.cc index fd798fa887c..4e6af299cc2 100644 --- a/src/tools/rbd_mirror/Replayer.cc +++ b/src/tools/rbd_mirror/Replayer.cc @@ -206,7 +206,8 @@ private: Commands commands; }; -Replayer::Replayer(Threads *threads, std::shared_ptr image_deleter, +Replayer::Replayer(Threads *threads, + std::shared_ptr image_deleter, ImageSyncThrottlerRef<> image_sync_throttler, int64_t local_pool_id, const peer_t &peer, const std::vector &args) : diff --git a/src/tools/rbd_mirror/Replayer.h b/src/tools/rbd_mirror/Replayer.h index 286b70dddbf..abcc35694af 100644 --- a/src/tools/rbd_mirror/Replayer.h +++ b/src/tools/rbd_mirror/Replayer.h @@ -27,7 +27,7 @@ namespace librbd { class ImageCtx; } namespace rbd { namespace mirror { -struct Threads; +template struct Threads; class ReplayerAdminSocketHook; template class InstanceWatcher; @@ -36,7 +36,8 @@ template class InstanceWatcher; */ class Replayer { public: - Replayer(Threads *threads, std::shared_ptr image_deleter, + Replayer(Threads *threads, + std::shared_ptr image_deleter, ImageSyncThrottlerRef<> image_sync_throttler, int64_t local_pool_id, const peer_t &peer, const std::vector &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 *m_threads; std::shared_ptr m_image_deleter; ImageSyncThrottlerRef<> m_image_sync_throttler; mutable Mutex m_lock; diff --git a/src/tools/rbd_mirror/Threads.cc b/src/tools/rbd_mirror/Threads.cc index 8fa7d6d9a00..8c22440a006 100644 --- a/src/tools/rbd_mirror/Threads.cc +++ b/src/tools/rbd_mirror/Threads.cc @@ -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 +Threads::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 +Threads::~Threads() { { Mutex::Locker timer_locker(timer_lock); timer->shutdown(); @@ -36,3 +39,5 @@ Threads::~Threads() { } // namespace mirror } // namespace rbd + +template class rbd::mirror::Threads; diff --git a/src/tools/rbd_mirror/Threads.h b/src/tools/rbd_mirror/Threads.h index ba952836ad2..f52e8837d35 100644 --- a/src/tools/rbd_mirror/Threads.h +++ b/src/tools/rbd_mirror/Threads.h @@ -11,9 +11,12 @@ class ContextWQ; class SafeTimer; class ThreadPool; +namespace librbd { struct ImageCtx; } + namespace rbd { namespace mirror { +template 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; + #endif // CEPH_RBD_MIRROR_THREADS_H -- 2.39.5