mirror of
https://github.com/ceph/ceph
synced 2025-01-14 15:04:30 +00:00
librbd: helper state machine for detaching children for v1/v2 clones
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
This commit is contained in:
parent
ffdf44358e
commit
b84319f3a4
@ -46,6 +46,7 @@ set(librbd_internal_srcs
|
||||
image/CloneRequest.cc
|
||||
image/CloseRequest.cc
|
||||
image/CreateRequest.cc
|
||||
image/DetachChildRequest.cc
|
||||
image/OpenRequest.cc
|
||||
image/RefreshParentRequest.cc
|
||||
image/RefreshRequest.cc
|
||||
|
136
src/librbd/image/DetachChildRequest.cc
Normal file
136
src/librbd/image/DetachChildRequest.cc
Normal file
@ -0,0 +1,136 @@
|
||||
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
||||
// vim: ts=8 sw=2 smarttab
|
||||
|
||||
#include "librbd/image/DetachChildRequest.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"
|
||||
#include <string>
|
||||
|
||||
#define dout_subsys ceph_subsys_rbd
|
||||
#undef dout_prefix
|
||||
#define dout_prefix *_dout << "librbd::image::DetachChildRequest: " << this \
|
||||
<< " " << __func__ << ": "
|
||||
|
||||
namespace librbd {
|
||||
namespace image {
|
||||
|
||||
using util::create_context_callback;
|
||||
using util::create_rados_callback;
|
||||
|
||||
template <typename I>
|
||||
void DetachChildRequest<I>::send() {
|
||||
{
|
||||
RWLock::RLocker snap_locker(m_image_ctx.snap_lock);
|
||||
RWLock::RLocker parent_locker(m_image_ctx.parent_lock);
|
||||
|
||||
// use oldest snapshot or HEAD for parent spec
|
||||
if (!m_image_ctx.snap_info.empty()) {
|
||||
m_parent_spec = m_image_ctx.snap_info.begin()->second.parent.spec;
|
||||
} else {
|
||||
m_parent_spec = m_image_ctx.parent_md.spec;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_parent_spec.pool_id == -1) {
|
||||
// ignore potential race with parent disappearing
|
||||
m_image_ctx.op_work_queue->queue(create_context_callback<
|
||||
DetachChildRequest<I>,
|
||||
&DetachChildRequest<I>::finish>(this), 0);
|
||||
return;
|
||||
} else if (!m_image_ctx.test_op_features(RBD_OPERATION_FEATURE_CLONE_CHILD)) {
|
||||
clone_v1_remove_child();
|
||||
return;
|
||||
}
|
||||
|
||||
clone_v2_child_detach();
|
||||
}
|
||||
|
||||
template <typename I>
|
||||
void DetachChildRequest<I>::clone_v2_child_detach() {
|
||||
auto cct = m_image_ctx.cct;
|
||||
ldout(cct, 5) << dendl;
|
||||
|
||||
librados::ObjectWriteOperation op;
|
||||
cls_client::child_detach(&op, m_parent_spec.snap_id,
|
||||
{m_image_ctx.md_ctx.get_id(), m_image_ctx.id});
|
||||
|
||||
librados::Rados rados(m_image_ctx.md_ctx);
|
||||
int r = rados.ioctx_create2(m_parent_spec.pool_id, m_parent_io_ctx);
|
||||
assert(r == 0);
|
||||
|
||||
auto aio_comp = create_rados_callback<
|
||||
DetachChildRequest<I>,
|
||||
&DetachChildRequest<I>::handle_clone_v2_child_detach>(this);
|
||||
r = m_parent_io_ctx.aio_operate(util::header_name(m_parent_spec.image_id),
|
||||
aio_comp, &op);
|
||||
assert(r == 0);
|
||||
aio_comp->release();
|
||||
}
|
||||
|
||||
template <typename I>
|
||||
void DetachChildRequest<I>::handle_clone_v2_child_detach(int r) {
|
||||
auto cct = m_image_ctx.cct;
|
||||
ldout(cct, 5) << "r=" << r << dendl;
|
||||
|
||||
if (r == -ENOENT) {
|
||||
r = 0;
|
||||
} else if (r < 0) {
|
||||
lderr(cct) << "error detaching child from parent: " << cpp_strerror(r)
|
||||
<< dendl;
|
||||
finish(r);
|
||||
return;
|
||||
}
|
||||
|
||||
finish(0);
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
void DetachChildRequest<I>::clone_v1_remove_child() {
|
||||
auto cct = m_image_ctx.cct;
|
||||
ldout(cct, 5) << dendl;
|
||||
|
||||
librados::ObjectWriteOperation op;
|
||||
librbd::cls_client::remove_child(&op, m_parent_spec, m_image_ctx.id);
|
||||
|
||||
auto aio_comp = create_rados_callback<
|
||||
DetachChildRequest<I>,
|
||||
&DetachChildRequest<I>::handle_clone_v1_remove_child>(this);
|
||||
int r = m_image_ctx.md_ctx.aio_operate(RBD_CHILDREN, aio_comp, &op);
|
||||
assert(r == 0);
|
||||
aio_comp->release();
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
void DetachChildRequest<I>::handle_clone_v1_remove_child(int r) {
|
||||
auto cct = m_image_ctx.cct;
|
||||
ldout(cct, 5) << "r=" << r << dendl;
|
||||
|
||||
if (r == -ENOENT) {
|
||||
r = 0;
|
||||
} else if (r < 0) {
|
||||
lderr(cct) << "failed to remove child from children list: "
|
||||
<< cpp_strerror(r) << dendl;
|
||||
finish(r);
|
||||
return;
|
||||
}
|
||||
|
||||
finish(0);
|
||||
}
|
||||
|
||||
template <typename I>
|
||||
void DetachChildRequest<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::DetachChildRequest<librbd::ImageCtx>;
|
74
src/librbd/image/DetachChildRequest.h
Normal file
74
src/librbd/image/DetachChildRequest.h
Normal file
@ -0,0 +1,74 @@
|
||||
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
||||
// vim: ts=8 sw=2 smarttab
|
||||
|
||||
#ifndef CEPH_LIBRBD_IMAGE_DETACH_CHILD_REQUEST_H
|
||||
#define CEPH_LIBRBD_IMAGE_DETACH_CHILD_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 DetachChildRequest {
|
||||
public:
|
||||
static DetachChildRequest* create(ImageCtxT& image_ctx, Context* on_finish) {
|
||||
return new DetachChildRequest(image_ctx, on_finish);
|
||||
}
|
||||
|
||||
DetachChildRequest(ImageCtxT& image_ctx, Context* on_finish)
|
||||
: m_image_ctx(image_ctx), m_on_finish(on_finish) {
|
||||
}
|
||||
|
||||
void send();
|
||||
|
||||
private:
|
||||
/**
|
||||
* @verbatim
|
||||
*
|
||||
* <start>
|
||||
* |
|
||||
* v (skip if v1)
|
||||
* CHILD_DETACH
|
||||
* |
|
||||
* v (skip if v2)
|
||||
* REMOVE_CHILD
|
||||
* |
|
||||
* v
|
||||
* <finish>
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
|
||||
ImageCtxT& m_image_ctx;
|
||||
Context* m_on_finish;
|
||||
|
||||
librados::IoCtx m_parent_io_ctx;
|
||||
ParentSpec m_parent_spec;
|
||||
|
||||
ceph::bufferlist m_out_bl;
|
||||
|
||||
void clone_v2_child_detach();
|
||||
void handle_clone_v2_child_detach(int r);
|
||||
|
||||
void clone_v1_remove_child();
|
||||
void handle_clone_v1_remove_child(int r);
|
||||
|
||||
void finish(int r);
|
||||
|
||||
};
|
||||
|
||||
} // namespace image
|
||||
} // namespace librbd
|
||||
|
||||
extern template class librbd::image::DetachChildRequest<librbd::ImageCtx>;
|
||||
|
||||
#endif // CEPH_LIBRBD_IMAGE_DETACH_CHILD_REQUEST_H
|
@ -44,6 +44,7 @@ set(unittest_librbd_srcs
|
||||
exclusive_lock/test_mock_PostAcquireRequest.cc
|
||||
exclusive_lock/test_mock_PreReleaseRequest.cc
|
||||
image/test_mock_CloneRequest.cc
|
||||
image/test_mock_DetachChildRequest.cc
|
||||
image/test_mock_RefreshRequest.cc
|
||||
image/test_mock_RemoveRequest.cc
|
||||
io/test_mock_ImageRequest.cc
|
||||
|
160
src/test/librbd/image/test_mock_DetachChildRequest.cc
Normal file
160
src/test/librbd/image/test_mock_DetachChildRequest.cc
Normal file
@ -0,0 +1,160 @@
|
||||
// -*- 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 "test/librados_test_stub/MockTestMemRadosClient.h"
|
||||
#include "librbd/image/DetachChildRequest.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/DetachChildRequest.cc"
|
||||
|
||||
namespace librbd {
|
||||
namespace image {
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::DoAll;
|
||||
using ::testing::InSequence;
|
||||
using ::testing::Return;
|
||||
using ::testing::StrEq;
|
||||
|
||||
class TestMockImageDetachChildRequest : public TestMockFixture {
|
||||
public:
|
||||
typedef DetachChildRequest<MockTestImageCtx> MockDetachChildRequest;
|
||||
|
||||
void SetUp() override {
|
||||
TestMockFixture::SetUp();
|
||||
|
||||
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
|
||||
}
|
||||
|
||||
void expect_test_op_features(MockTestImageCtx& mock_image_ctx, bool enabled) {
|
||||
EXPECT_CALL(mock_image_ctx,
|
||||
test_op_features(RBD_OPERATION_FEATURE_CLONE_CHILD))
|
||||
.WillOnce(Return(enabled));
|
||||
}
|
||||
|
||||
void expect_child_detach(MockImageCtx &mock_image_ctx, int r) {
|
||||
auto& parent_spec = mock_image_ctx.parent_md.spec;
|
||||
|
||||
bufferlist bl;
|
||||
encode(parent_spec.snap_id, bl);
|
||||
encode(cls::rbd::ChildImageSpec{mock_image_ctx.md_ctx.get_id(),
|
||||
mock_image_ctx.id}, bl);
|
||||
|
||||
auto& io_ctx_impl = get_mock_io_ctx(mock_image_ctx.md_ctx);
|
||||
auto rados_client = io_ctx_impl.get_mock_rados_client();
|
||||
|
||||
EXPECT_CALL(*rados_client, create_ioctx(_, _))
|
||||
.WillOnce(DoAll(GetReference(&io_ctx_impl),
|
||||
Return(&io_ctx_impl)));
|
||||
|
||||
EXPECT_CALL(io_ctx_impl,
|
||||
exec(util::header_name(parent_spec.image_id),
|
||||
_, StrEq("rbd"), StrEq("child_detach"), ContentsEqual(bl),
|
||||
_, _))
|
||||
.WillOnce(Return(r));
|
||||
}
|
||||
|
||||
void expect_remove_child(MockImageCtx &mock_image_ctx, int r) {
|
||||
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
|
||||
exec(RBD_CHILDREN, _, StrEq("rbd"), StrEq("remove_child"), _,
|
||||
_, _))
|
||||
.WillOnce(Return(r));
|
||||
}
|
||||
|
||||
librbd::ImageCtx *image_ctx;
|
||||
};
|
||||
|
||||
TEST_F(TestMockImageDetachChildRequest, SuccessV1) {
|
||||
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
|
||||
|
||||
MockTestImageCtx mock_image_ctx(*image_ctx);
|
||||
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "parent id", 234};
|
||||
|
||||
InSequence seq;
|
||||
expect_test_op_features(mock_image_ctx, false);
|
||||
expect_remove_child(mock_image_ctx, 0);
|
||||
|
||||
C_SaferCond ctx;
|
||||
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
|
||||
req->send();
|
||||
ASSERT_EQ(0, ctx.wait());
|
||||
}
|
||||
|
||||
TEST_F(TestMockImageDetachChildRequest, SuccessV2) {
|
||||
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
|
||||
|
||||
MockTestImageCtx mock_image_ctx(*image_ctx);
|
||||
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "parent id", 234};
|
||||
|
||||
InSequence seq;
|
||||
expect_test_op_features(mock_image_ctx, true);
|
||||
expect_child_detach(mock_image_ctx, 0);
|
||||
|
||||
C_SaferCond ctx;
|
||||
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
|
||||
req->send();
|
||||
ASSERT_EQ(0, ctx.wait());
|
||||
}
|
||||
|
||||
TEST_F(TestMockImageDetachChildRequest, ParentDNE) {
|
||||
MockTestImageCtx mock_image_ctx(*image_ctx);
|
||||
expect_op_work_queue(mock_image_ctx);
|
||||
|
||||
C_SaferCond ctx;
|
||||
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
|
||||
req->send();
|
||||
ASSERT_EQ(0, ctx.wait());
|
||||
}
|
||||
|
||||
TEST_F(TestMockImageDetachChildRequest, ChildDetachError) {
|
||||
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
|
||||
|
||||
MockTestImageCtx mock_image_ctx(*image_ctx);
|
||||
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "parent id", 234};
|
||||
|
||||
InSequence seq;
|
||||
expect_test_op_features(mock_image_ctx, true);
|
||||
expect_child_detach(mock_image_ctx, -EPERM);
|
||||
|
||||
C_SaferCond ctx;
|
||||
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
|
||||
req->send();
|
||||
ASSERT_EQ(-EPERM, ctx.wait());
|
||||
}
|
||||
|
||||
TEST_F(TestMockImageDetachChildRequest, RemoveChildError) {
|
||||
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
|
||||
|
||||
MockTestImageCtx mock_image_ctx(*image_ctx);
|
||||
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "parent id", 234};
|
||||
|
||||
InSequence seq;
|
||||
expect_test_op_features(mock_image_ctx, false);
|
||||
expect_remove_child(mock_image_ctx, -EINVAL);
|
||||
|
||||
C_SaferCond ctx;
|
||||
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
|
||||
req->send();
|
||||
ASSERT_EQ(-EINVAL, ctx.wait());
|
||||
}
|
||||
|
||||
} // namespace image
|
||||
} // namespace librbd
|
@ -198,6 +198,8 @@ struct MockImageCtx {
|
||||
MOCK_CONST_METHOD2(test_features, bool(uint64_t test_features,
|
||||
const RWLock &in_snap_lock));
|
||||
|
||||
MOCK_CONST_METHOD1(test_op_features, bool(uint64_t op_features));
|
||||
|
||||
MOCK_METHOD1(cancel_async_requests, void(Context*));
|
||||
|
||||
MOCK_METHOD0(create_exclusive_lock, MockExclusiveLock*());
|
||||
|
Loading…
Reference in New Issue
Block a user