From: Prasanna Kumar Kalever Date: Tue, 11 Mar 2025 09:47:41 +0000 (+0530) Subject: rbd-mirror: fix MirrorStatusWatcher X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=11026a33daab672519b57cd3fe8df6956b96a50d;p=ceph.git rbd-mirror: fix MirrorStatusWatcher * moved the internal functions scope to private * use m_on_start_finish to save the init time Context and use later, instead of passing it in various functions Signed-off-by: Prasanna Kumar Kalever --- diff --git a/src/tools/rbd_mirror/MirrorStatusWatcher.cc b/src/tools/rbd_mirror/MirrorStatusWatcher.cc index 225ed1bf3bf3a..2f3902b695068 100644 --- a/src/tools/rbd_mirror/MirrorStatusWatcher.cc +++ b/src/tools/rbd_mirror/MirrorStatusWatcher.cc @@ -21,11 +21,14 @@ using librbd::util::create_rados_callback; template MirrorStatusWatcher::MirrorStatusWatcher(librados::IoCtx &io_ctx, librbd::asio::ContextWQ *work_queue) - : Watcher(io_ctx, work_queue, RBD_MIRRORING) { + : Watcher(io_ctx, work_queue, RBD_MIRRORING), + m_lock(ceph::make_mutex("rbd::mirror::MirrorStatusWatcher " + + stringify(io_ctx.get_id()))) { } template MirrorStatusWatcher::~MirrorStatusWatcher() { + ceph_assert(m_on_start_finish == nullptr); } template @@ -42,18 +45,24 @@ void MirrorStatusWatcher::init(Context *on_finish) { register_watch(on_finish); }); - remove_down_image_status(on_finish); + { + std::lock_guard locker{m_lock}; + ceph_assert(m_on_start_finish == nullptr); + std::swap(m_on_start_finish, on_finish); + } + + remove_down_image_status(); } template -void MirrorStatusWatcher::remove_down_image_status(Context *on_finish) { +void MirrorStatusWatcher::remove_down_image_status() { dout(20) << dendl; librados::ObjectWriteOperation op; librbd::cls_client::mirror_image_status_remove_down(&op); auto comp = create_rados_callback( - new LambdaContext([this, on_finish](int r) { - handle_remove_down_image_status(r, on_finish); + new LambdaContext([this](int r) { + handle_remove_down_image_status(r); })); int r = m_ioctx.aio_operate(RBD_MIRRORING, comp, &op); @@ -62,22 +71,21 @@ void MirrorStatusWatcher::remove_down_image_status(Context *on_finish) { } template -void MirrorStatusWatcher::handle_remove_down_image_status(int r, - Context *on_finish) { +void MirrorStatusWatcher::handle_remove_down_image_status(int r) { dout(20) << "r=" << r << dendl; - remove_down_group_status(on_finish); + remove_down_group_status(); } template -void MirrorStatusWatcher::remove_down_group_status(Context *on_finish) { +void MirrorStatusWatcher::remove_down_group_status() { dout(20) << dendl; librados::ObjectWriteOperation op; librbd::cls_client::mirror_group_status_remove_down(&op); auto comp = create_rados_callback( - new LambdaContext([this, on_finish](int r) { - handle_remove_down_group_status(r, on_finish); + new LambdaContext([this](int r) { + handle_remove_down_group_status(r); })); int r = m_ioctx.aio_operate(RBD_MIRRORING, comp, &op); @@ -86,11 +94,18 @@ void MirrorStatusWatcher::remove_down_group_status(Context *on_finish) { } template -void MirrorStatusWatcher::handle_remove_down_group_status(int r, - Context *on_finish) { +void MirrorStatusWatcher::handle_remove_down_group_status(int r) { dout(20) << "r=" << r << dendl; - on_finish->complete(r); + Context *on_finish = nullptr; + { + std::lock_guard locker{m_lock}; + std::swap(m_on_start_finish, on_finish); + } + + if (on_finish) { + on_finish->complete(0); + } } template diff --git a/src/tools/rbd_mirror/MirrorStatusWatcher.h b/src/tools/rbd_mirror/MirrorStatusWatcher.h index 609e661e2bdbf..9ba71dd46f327 100644 --- a/src/tools/rbd_mirror/MirrorStatusWatcher.h +++ b/src/tools/rbd_mirror/MirrorStatusWatcher.h @@ -30,12 +30,17 @@ public: ~MirrorStatusWatcher() override; void init(Context *on_finish); - void remove_down_image_status(Context *on_finish); - void handle_remove_down_image_status(int r, Context *on_finish); - void remove_down_group_status(Context *on_finish); - void handle_remove_down_group_status(int r, Context *on_finish); void shut_down(Context *on_finish); +private: + mutable ceph::mutex m_lock; + Context* m_on_start_finish = nullptr; + + void remove_down_image_status(); + void handle_remove_down_image_status(int r); + void remove_down_group_status(); + void handle_remove_down_group_status(int r); + protected: void handle_notify(uint64_t notify_id, uint64_t handle, uint64_t notifier_id, bufferlist &bl) override;