]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: helper state machines for updating/removing mirror images
authorJason Dillaman <dillaman@redhat.com>
Mon, 27 Jan 2020 21:45:54 +0000 (16:45 -0500)
committerJason Dillaman <dillaman@redhat.com>
Thu, 30 Jan 2020 15:26:36 +0000 (10:26 -0500)
When the mirror image state is updated or removed, we need to send
notifications to peer listeners like rbd-mirror. These two new
state machines allow re-use of that logic to avoid the duplication.

Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/librbd/CMakeLists.txt
src/librbd/mirror/ImageRemoveRequest.cc [new file with mode: 0644]
src/librbd/mirror/ImageRemoveRequest.h [new file with mode: 0644]
src/librbd/mirror/ImageStateUpdateRequest.cc [new file with mode: 0644]
src/librbd/mirror/ImageStateUpdateRequest.h [new file with mode: 0644]

index 7376c2ffcd0b557d692fa71e1e9886464e807f76..60920aa39d3e6161d88ea3bb15fee7b5fc8933c2 100644 (file)
@@ -103,6 +103,8 @@ set(librbd_internal_srcs
   mirror/EnableRequest.cc
   mirror/GetInfoRequest.cc
   mirror/GetStatusRequest.cc
+  mirror/ImageRemoveRequest.cc
+  mirror/ImageStateUpdateRequest.cc
   mirror/PromoteRequest.cc
   mirror/snapshot/CreateNonPrimaryRequest.cc
   mirror/snapshot/CreatePrimaryRequest.cc
diff --git a/src/librbd/mirror/ImageRemoveRequest.cc b/src/librbd/mirror/ImageRemoveRequest.cc
new file mode 100644 (file)
index 0000000..1aa265d
--- /dev/null
@@ -0,0 +1,98 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/mirror/ImageRemoveRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/MirroringWatcher.h"
+#include "librbd/Utils.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::mirror::ImageRemoveRequest: " \
+                           << this << " " << __func__ << ": "
+
+namespace librbd {
+namespace mirror {
+
+using util::create_rados_callback;
+
+template <typename I>
+ImageRemoveRequest<I>::ImageRemoveRequest(
+    librados::IoCtx& io_ctx, const std::string& global_image_id,
+    const std::string& image_id, Context* on_finish)
+  : m_io_ctx(io_ctx), m_global_image_id(global_image_id), m_image_id(image_id),
+    m_on_finish(on_finish), m_cct(static_cast<CephContext*>(m_io_ctx.cct())) {
+}
+
+template <typename I>
+void ImageRemoveRequest<I>::send() {
+  remove_mirror_image();
+}
+
+template <typename I>
+void ImageRemoveRequest<I>::remove_mirror_image() {
+  ldout(m_cct, 10) << dendl;
+
+  librados::ObjectWriteOperation op;
+  cls_client::mirror_image_remove(&op, m_image_id);
+
+  auto comp = create_rados_callback<
+    ImageRemoveRequest<I>,
+    &ImageRemoveRequest<I>::handle_remove_mirror_image>(this);
+  int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op);
+  ceph_assert(r == 0);
+  comp->release();
+}
+
+template <typename I>
+void ImageRemoveRequest<I>::handle_remove_mirror_image(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  if (r < 0 && r != -ENOENT) {
+    lderr(m_cct) << "failed to remove mirroring image: " << cpp_strerror(r)
+                 << dendl;
+    finish(r);
+    return;
+  }
+
+  notify_mirroring_watcher();
+}
+
+template <typename I>
+void ImageRemoveRequest<I>::notify_mirroring_watcher() {
+  ldout(m_cct, 10) << dendl;
+
+  auto ctx = util::create_context_callback<
+    ImageRemoveRequest<I>,
+    &ImageRemoveRequest<I>::handle_notify_mirroring_watcher>(this);
+  MirroringWatcher<I>::notify_image_updated(
+    m_io_ctx, cls::rbd::MIRROR_IMAGE_STATE_DISABLED,
+    m_image_id, m_global_image_id, ctx);
+}
+
+template <typename I>
+void ImageRemoveRequest<I>::handle_notify_mirroring_watcher(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  if (r < 0) {
+    lderr(m_cct) << "failed to notify mirror image update: " << cpp_strerror(r)
+                 << dendl;
+  }
+
+  finish(0);
+}
+
+template <typename I>
+void ImageRemoveRequest<I>::finish(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  m_on_finish->complete(r);
+  delete this;
+}
+
+} // namespace mirror
+} // namespace librbd
+
+template class librbd::mirror::ImageRemoveRequest<librbd::ImageCtx>;
diff --git a/src/librbd/mirror/ImageRemoveRequest.h b/src/librbd/mirror/ImageRemoveRequest.h
new file mode 100644 (file)
index 0000000..c04f9fa
--- /dev/null
@@ -0,0 +1,77 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_MIRROR_IMAGE_REMOVE_REQUEST_H
+#define CEPH_LIBRBD_MIRROR_IMAGE_REMOVE_REQUEST_H
+
+#include "include/rados/librados.hpp"
+#include "common/ceph_mutex.h"
+#include "cls/rbd/cls_rbd_types.h"
+
+#include <string>
+
+class Context;
+
+namespace librbd {
+
+class ImageCtx;
+
+namespace mirror {
+
+template <typename ImageCtxT = ImageCtx>
+class ImageRemoveRequest {
+public:
+  static ImageRemoveRequest *create(librados::IoCtx& io_ctx,
+                                    const std::string& global_image_id,
+                                    const std::string& image_id,
+                                    Context* on_finish) {
+    return new ImageRemoveRequest(io_ctx, global_image_id, image_id, on_finish);
+  }
+
+  ImageRemoveRequest(librados::IoCtx& io_ctx,
+                     const std::string& global_image_id,
+                     const std::string& image_id,
+                     Context* on_finish);
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |
+   * REMOVE_MIRROR_IMAGE
+   *    |
+   *    v
+   * NOTIFY_MIRRORING_WATCHER
+   *    |
+   *    v
+   * <finish>
+   *
+   * @endverbatim
+   */
+
+  librados::IoCtx& m_io_ctx;
+  std::string m_global_image_id;
+  std::string m_image_id;
+  Context* m_on_finish;
+
+  CephContext* m_cct;
+
+  void remove_mirror_image();
+  void handle_remove_mirror_image(int r);
+
+  void notify_mirroring_watcher();
+  void handle_notify_mirroring_watcher(int r);
+
+  void finish(int r);
+
+};
+
+} // namespace mirror
+} // namespace librbd
+
+extern template class librbd::mirror::ImageRemoveRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_MIRROR_IMAGE_REMOVE_REQUEST_H
diff --git a/src/librbd/mirror/ImageStateUpdateRequest.cc b/src/librbd/mirror/ImageStateUpdateRequest.cc
new file mode 100644 (file)
index 0000000..98e9871
--- /dev/null
@@ -0,0 +1,151 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/mirror/ImageStateUpdateRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/MirroringWatcher.h"
+#include "librbd/Utils.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::mirror::ImageStateUpdateRequest: " \
+                           << this << " " << __func__ << ": "
+
+namespace librbd {
+namespace mirror {
+
+using util::create_rados_callback;
+
+template <typename I>
+ImageStateUpdateRequest<I>::ImageStateUpdateRequest(
+    librados::IoCtx& io_ctx,
+    const std::string& image_id,
+    cls::rbd::MirrorImageState mirror_image_state,
+    const cls::rbd::MirrorImage& mirror_image,
+    Context* on_finish)
+  : m_io_ctx(io_ctx), m_image_id(image_id),
+    m_mirror_image_state(mirror_image_state), m_mirror_image(mirror_image),
+    m_on_finish(on_finish), m_cct(static_cast<CephContext*>(m_io_ctx.cct())) {
+  ceph_assert(m_mirror_image_state != cls::rbd::MIRROR_IMAGE_STATE_DISABLED);
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::send() {
+  get_mirror_image();
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::get_mirror_image() {
+  if (!m_mirror_image.global_image_id.empty()) {
+    set_mirror_image();
+    return;
+  }
+
+  ldout(m_cct, 10) << dendl;
+  librados::ObjectReadOperation op;
+  cls_client::mirror_image_get_start(&op, m_image_id);
+
+  auto comp = create_rados_callback<
+    ImageStateUpdateRequest<I>,
+    &ImageStateUpdateRequest<I>::handle_get_mirror_image>(this);
+  int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op, &m_out_bl);
+  ceph_assert(r == 0);
+  comp->release();
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::handle_get_mirror_image(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  if (r == 0) {
+    auto iter = m_out_bl.cbegin();
+    r = cls_client::mirror_image_get_finish(&iter, &m_mirror_image);
+  }
+
+  if (r == -ENOENT) {
+    ldout(m_cct, 20) << "mirroring is disabled" << dendl;
+    finish(0);
+    return;
+  } else if (r < 0) {
+    lderr(m_cct) << "failed to retrieve mirroring state: " << cpp_strerror(r)
+                 << dendl;
+    finish(r);
+    return;
+  }
+
+  set_mirror_image();
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::set_mirror_image() {
+  if (m_mirror_image.state == m_mirror_image_state) {
+    finish(0);
+    return;
+  }
+
+  ldout(m_cct, 10) << dendl;
+  m_mirror_image.state = m_mirror_image_state;
+
+  librados::ObjectWriteOperation op;
+  cls_client::mirror_image_set(&op, m_image_id, m_mirror_image);
+
+  auto comp = create_rados_callback<
+    ImageStateUpdateRequest<I>,
+    &ImageStateUpdateRequest<I>::handle_set_mirror_image>(this);
+  int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op);
+  ceph_assert(r == 0);
+  comp->release();
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::handle_set_mirror_image(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  if (r < 0) {
+    lderr(m_cct) << "failed to disable mirroring image: " << cpp_strerror(r)
+                 << dendl;
+    finish(r);
+    return;
+  }
+
+  notify_mirroring_watcher();
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::notify_mirroring_watcher() {
+  ldout(m_cct, 10) << dendl;
+
+  auto ctx = util::create_context_callback<
+    ImageStateUpdateRequest<I>,
+    &ImageStateUpdateRequest<I>::handle_notify_mirroring_watcher>(this);
+  MirroringWatcher<I>::notify_image_updated(
+    m_io_ctx, m_mirror_image_state, m_image_id, m_mirror_image.global_image_id,
+    ctx);
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::handle_notify_mirroring_watcher(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  if (r < 0) {
+    lderr(m_cct) << "failed to notify mirror image update: " << cpp_strerror(r)
+                 << dendl;
+  }
+
+  finish(0);
+}
+
+template <typename I>
+void ImageStateUpdateRequest<I>::finish(int r) {
+  ldout(m_cct, 10) << "r=" << r << dendl;
+
+  m_on_finish->complete(r);
+  delete this;
+}
+
+} // namespace mirror
+} // namespace librbd
+
+template class librbd::mirror::ImageStateUpdateRequest<librbd::ImageCtx>;
diff --git a/src/librbd/mirror/ImageStateUpdateRequest.h b/src/librbd/mirror/ImageStateUpdateRequest.h
new file mode 100644 (file)
index 0000000..9e0affe
--- /dev/null
@@ -0,0 +1,92 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_MIRROR_IMAGE_STATE_UPDATE_REQUEST_H
+#define CEPH_LIBRBD_MIRROR_IMAGE_STATE_UPDATE_REQUEST_H
+
+#include "include/rados/librados.hpp"
+#include "common/ceph_mutex.h"
+#include "cls/rbd/cls_rbd_types.h"
+#include "librbd/mirror/Types.h"
+
+#include <string>
+
+class Context;
+
+namespace librbd {
+
+class ImageCtx;
+
+namespace mirror {
+
+template <typename ImageCtxT = ImageCtx>
+class ImageStateUpdateRequest {
+public:
+  static ImageStateUpdateRequest *create(
+      librados::IoCtx& io_ctx,
+      const std::string& image_id,
+      cls::rbd::MirrorImageState mirror_image_state,
+      const cls::rbd::MirrorImage& mirror_image,
+      Context* on_finish) {
+    return new ImageStateUpdateRequest(
+      io_ctx, image_id, mirror_image_state, mirror_image, on_finish);
+  }
+
+  ImageStateUpdateRequest(
+      librados::IoCtx& io_ctx,
+      const std::string& image_id,
+      cls::rbd::MirrorImageState mirror_image_state,
+      const cls::rbd::MirrorImage& mirror_image,
+      Context* on_finish);
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |
+   *    v (skip if provided)
+   * GET_MIRROR_IMAGE
+   *    |
+   *    v
+   * SET_MIRROR_IMAGE
+   *    |
+   *    v
+   * NOTIFY_MIRRORING_WATCHER
+   *    |
+   *    v
+   * <finish>
+   *
+   * @endverbatim
+   */
+
+  librados::IoCtx& m_io_ctx;
+  std::string m_image_id;
+  cls::rbd::MirrorImageState m_mirror_image_state;
+  cls::rbd::MirrorImage m_mirror_image;
+  Context* m_on_finish;
+
+  CephContext* m_cct;
+  bufferlist m_out_bl;
+
+  void get_mirror_image();
+  void handle_get_mirror_image(int r);
+
+  void set_mirror_image();
+  void handle_set_mirror_image(int r);
+
+  void notify_mirroring_watcher();
+  void handle_notify_mirroring_watcher(int r);
+
+  void finish(int r);
+
+};
+
+} // namespace mirror
+} // namespace librbd
+
+extern template class librbd::mirror::ImageStateUpdateRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_MIRROR_IMAGE_STATE_UPDATE_REQUEST_H