]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
librbd: new attach parent state machine
authorJason Dillaman <dillaman@redhat.com>
Tue, 4 Sep 2018 16:54:46 +0000 (12:54 -0400)
committerJason Dillaman <dillaman@redhat.com>
Wed, 19 Sep 2018 12:05:29 +0000 (08:05 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/librbd/CMakeLists.txt
src/librbd/image/AttachParentRequest.cc [new file with mode: 0644]
src/librbd/image/AttachParentRequest.h [new file with mode: 0644]
src/test/librbd/CMakeLists.txt
src/test/librbd/image/test_mock_AttachParentRequest.cc [new file with mode: 0644]

index e587be7e2a0ca5369b50ec62c5197e0072665bf0..a0744a5cd087e596d76c5e47ea12126a52175f4f 100644 (file)
@@ -46,6 +46,7 @@ set(librbd_internal_srcs
   exclusive_lock/PostAcquireRequest.cc
   exclusive_lock/PreReleaseRequest.cc
   exclusive_lock/StandardPolicy.cc
+  image/AttachParentRequest.cc
   image/CloneRequest.cc
   image/CloseRequest.cc
   image/CreateRequest.cc
diff --git a/src/librbd/image/AttachParentRequest.cc b/src/librbd/image/AttachParentRequest.cc
new file mode 100644 (file)
index 0000000..239f105
--- /dev/null
@@ -0,0 +1,96 @@
+// -*- mode:C++; tab-width:8; c-basic-offattach:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/image/AttachParentRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "common/WorkQueue.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/Utils.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::image::AttachParentRequest: " << this \
+                           << " " << __func__ << ": "
+
+namespace librbd {
+namespace image {
+
+using util::create_context_callback;
+using util::create_rados_callback;
+
+template <typename I>
+void AttachParentRequest<I>::send() {
+  attach_parent();
+}
+
+template <typename I>
+void AttachParentRequest<I>::attach_parent() {
+  auto cct = m_image_ctx.cct;
+  ldout(cct, 5) << dendl;
+
+  librados::ObjectWriteOperation op;
+  if (!m_legacy_parent) {
+    librbd::cls_client::parent_attach(&op, m_parent_image_spec,
+                                      m_parent_overlap);
+  } else {
+    librbd::cls_client::set_parent(&op,
+                                   {m_parent_image_spec.pool_id,
+                                    m_parent_image_spec.image_id,
+                                    m_parent_image_spec.snap_id},
+                                   m_parent_overlap);
+  }
+
+  auto aio_comp = create_rados_callback<
+    AttachParentRequest<I>,
+    &AttachParentRequest<I>::handle_attach_parent>(this);
+  int r = m_image_ctx.md_ctx.aio_operate(m_image_ctx.header_oid, aio_comp, &op);
+  ceph_assert(r == 0);
+  aio_comp->release();
+}
+
+template <typename I>
+void AttachParentRequest<I>::handle_attach_parent(int r) {
+  auto cct = m_image_ctx.cct;
+  ldout(cct, 5) << dendl;
+
+  if (!m_legacy_parent && r == -EOPNOTSUPP) {
+    if (m_parent_image_spec.pool_namespace ==
+          m_image_ctx.md_ctx.get_namespace()) {
+      m_parent_image_spec.pool_namespace = "";
+    }
+    if (m_parent_image_spec.pool_namespace.empty()) {
+      ldout(cct, 10) << "retrying using legacy parent method" << dendl;
+      m_legacy_parent = true;
+      attach_parent();
+      return;
+    }
+
+    // namespaces require newer OSDs
+    r = -EXDEV;
+  }
+
+  if (r < 0) {
+    lderr(cct) << "attach parent encountered an error: " << cpp_strerror(r)
+               << dendl;
+    finish(r);
+    return;
+  }
+
+  finish(0);
+}
+
+template <typename I>
+void AttachParentRequest<I>::finish(int r) {
+  auto cct = m_image_ctx.cct;
+  ldout(cct, 5) << "r=" << r << dendl;
+
+  m_on_finish->complete(r);
+  delete this;
+}
+
+} // namespace image
+} // namespace librbd
+
+template class librbd::image::AttachParentRequest<librbd::ImageCtx>;
diff --git a/src/librbd/image/AttachParentRequest.h b/src/librbd/image/AttachParentRequest.h
new file mode 100644 (file)
index 0000000..de4f8ed
--- /dev/null
@@ -0,0 +1,74 @@
+// -*- mode:C++; tab-width:8; c-basic-offattach:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_IMAGE_ATTACH_PARENT_REQUEST_H
+#define CEPH_LIBRBD_IMAGE_ATTACH_PARENT_REQUEST_H
+
+#include "include/int_types.h"
+#include "include/buffer.h"
+#include "include/rados/librados.hpp"
+#include "librbd/Types.h"
+
+class Context;
+
+namespace librbd {
+
+class ImageCtx;
+
+namespace image {
+
+template <typename ImageCtxT = ImageCtx>
+class AttachParentRequest {
+public:
+  static AttachParentRequest* create(ImageCtxT& image_ctx,
+                                     const cls::rbd::ParentImageSpec& pspec,
+                                     uint64_t parent_overlap,
+                                     Context* on_finish) {
+    return new AttachParentRequest(image_ctx, pspec, parent_overlap, on_finish);
+  }
+
+  AttachParentRequest(ImageCtxT& image_ctx,
+                      const cls::rbd::ParentImageSpec& pspec,
+                      uint64_t parent_overlap, Context* on_finish)
+    : m_image_ctx(image_ctx), m_parent_image_spec(pspec),
+      m_parent_overlap(parent_overlap), m_on_finish(on_finish) {
+  }
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |    * * * * * *
+   *    |    *         * -EOPNOTSUPP
+   *    v    v         *
+   * ATTACH_PARENT * * *
+   *    |
+   *    v
+   * <finish>
+   *
+   * @endverbatim
+   */
+
+  ImageCtxT& m_image_ctx;
+  cls::rbd::ParentImageSpec m_parent_image_spec;
+  uint64_t m_parent_overlap;
+  Context* m_on_finish;
+
+  bool m_legacy_parent = false;
+
+  void attach_parent();
+  void handle_attach_parent(int r);
+
+  void finish(int r);
+
+};
+
+} // namespace image
+} // namespace librbd
+
+extern template class librbd::image::AttachParentRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_IMAGE_ATTACH_PARENT_REQUEST_H
index 15c83beaac97a0968728cc637fd6ac156c3dd869..5bcb57a1bbd571bb5617f8012fb125495b1c9134 100644 (file)
@@ -54,6 +54,7 @@ set(unittest_librbd_srcs
   exclusive_lock/test_mock_PreAcquireRequest.cc
   exclusive_lock/test_mock_PostAcquireRequest.cc
   exclusive_lock/test_mock_PreReleaseRequest.cc
+  image/test_mock_AttachParentRequest.cc
   image/test_mock_CloneRequest.cc
   image/test_mock_DetachChildRequest.cc
   image/test_mock_DetachParentRequest.cc
diff --git a/src/test/librbd/image/test_mock_AttachParentRequest.cc b/src/test/librbd/image/test_mock_AttachParentRequest.cc
new file mode 100644 (file)
index 0000000..071901a
--- /dev/null
@@ -0,0 +1,155 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "test/librbd/mock/MockContextWQ.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "librbd/image/AttachParentRequest.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+  MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+  }
+};
+
+} // anonymous namespace
+} // namespace librbd
+
+// template definitions
+#include "librbd/image/AttachParentRequest.cc"
+
+namespace librbd {
+namespace image {
+
+using ::testing::_;
+using ::testing::InSequence;
+using ::testing::Return;
+using ::testing::StrEq;
+
+class TestMockImageAttachParentRequest : public TestMockFixture {
+public:
+  typedef AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;
+
+  void SetUp() override {
+    TestMockFixture::SetUp();
+
+    ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
+  }
+
+  void expect_parent_attach(MockImageCtx &mock_image_ctx, int r) {
+    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+                exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
+                     StrEq("parent_attach"), _, _, _))
+      .WillOnce(Return(r));
+  }
+
+  void expect_set_parent(MockImageCtx &mock_image_ctx, int r) {
+    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+                exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
+                     StrEq("set_parent"), _, _, _))
+      .WillOnce(Return(r));
+  }
+
+  librbd::ImageCtx *image_ctx;
+};
+
+TEST_F(TestMockImageAttachParentRequest, ParentAttachSuccess) {
+  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+  MockTestImageCtx mock_image_ctx(*image_ctx);
+
+  InSequence seq;
+  expect_parent_attach(mock_image_ctx, 0);
+
+  cls::rbd::ParentImageSpec parent_image_spec{
+    1, "ns", "image id", 123};
+
+  C_SaferCond ctx;
+  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
+                                             234, &ctx);
+  req->send();
+  ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockImageAttachParentRequest, SetParentSuccess) {
+  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+  MockTestImageCtx mock_image_ctx(*image_ctx);
+
+  InSequence seq;
+  expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
+  expect_set_parent(mock_image_ctx, 0);
+
+  cls::rbd::ParentImageSpec parent_image_spec{
+    1, "", "image id", 123};
+
+  C_SaferCond ctx;
+  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
+                                             234, &ctx);
+  req->send();
+  ASSERT_EQ(0, ctx.wait());
+}
+
+TEST_F(TestMockImageAttachParentRequest, ParentAttachError) {
+  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+  MockTestImageCtx mock_image_ctx(*image_ctx);
+
+  InSequence seq;
+  expect_parent_attach(mock_image_ctx, -EPERM);
+
+  cls::rbd::ParentImageSpec parent_image_spec{
+    1, "", "image id", 123};
+
+  C_SaferCond ctx;
+  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
+                                             234, &ctx);
+  req->send();
+  ASSERT_EQ(-EPERM, ctx.wait());
+}
+
+TEST_F(TestMockImageAttachParentRequest, SetParentError) {
+  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+  MockTestImageCtx mock_image_ctx(*image_ctx);
+
+  InSequence seq;
+  expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
+  expect_set_parent(mock_image_ctx, -EINVAL);
+
+  cls::rbd::ParentImageSpec parent_image_spec{
+    1, "", "image id", 123};
+
+  C_SaferCond ctx;
+  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
+                                             234, &ctx);
+  req->send();
+  ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockImageAttachParentRequest, NamespaceUnsupported) {
+  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
+
+  MockTestImageCtx mock_image_ctx(*image_ctx);
+
+  InSequence seq;
+  expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
+
+  cls::rbd::ParentImageSpec parent_image_spec{
+    1, "ns", "image id", 123};
+
+  C_SaferCond ctx;
+  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
+                                             234, &ctx);
+  req->send();
+  ASSERT_EQ(-EXDEV, ctx.wait());
+}
+
+} // namespace image
+} // namespace librbd