From: Jason Dillaman Date: Fri, 21 Jul 2017 00:17:23 +0000 (-0400) Subject: rbd-mirror: helper state machine for querying remote peer uuid / image id X-Git-Tag: v13.0.0~218^2~12 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b3dba1c876ea9ffd1b27b69db4b2f90604f2738f;p=ceph.git rbd-mirror: helper state machine for querying remote peer uuid / image id Signed-off-by: Jason Dillaman --- diff --git a/src/test/rbd_mirror/CMakeLists.txt b/src/test/rbd_mirror/CMakeLists.txt index eb7b7674d60f..47029cd9034c 100644 --- a/src/test/rbd_mirror/CMakeLists.txt +++ b/src/test/rbd_mirror/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(unittest_rbd_mirror image_replayer/test_mock_EventPreprocessor.cc image_replayer/test_mock_GetMirrorImageIdRequest.cc image_replayer/test_mock_PrepareLocalImageRequest.cc + image_replayer/test_mock_PrepareRemoteImageRequest.cc image_sync/test_mock_ImageCopyRequest.cc image_sync/test_mock_ObjectCopyRequest.cc image_sync/test_mock_SnapshotCopyRequest.cc diff --git a/src/test/rbd_mirror/image_replayer/test_mock_PrepareRemoteImageRequest.cc b/src/test/rbd_mirror/image_replayer/test_mock_PrepareRemoteImageRequest.cc new file mode 100644 index 000000000000..793811816e62 --- /dev/null +++ b/src/test/rbd_mirror/image_replayer/test_mock_PrepareRemoteImageRequest.cc @@ -0,0 +1,163 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "test/rbd_mirror/test_mock_fixture.h" +#include "cls/rbd/cls_rbd_types.h" +#include "librbd/journal/TypeTraits.h" +#include "tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.h" +#include "tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.h" +#include "test/librados_test_stub/MockTestMemIoCtxImpl.h" +#include "test/librbd/mock/MockImageCtx.h" + +namespace librbd { + +namespace { + +struct MockTestImageCtx : public librbd::MockImageCtx { + MockTestImageCtx(librbd::ImageCtx &image_ctx) + : librbd::MockImageCtx(image_ctx) { + } +}; + +} // anonymous namespace +} // namespace librbd + +namespace rbd { +namespace mirror { +namespace image_replayer { + +template <> +struct GetMirrorImageIdRequest { + static GetMirrorImageIdRequest* s_instance; + std::string* image_id = nullptr; + Context* on_finish = nullptr; + + static GetMirrorImageIdRequest* create(librados::IoCtx& io_ctx, + const std::string& global_image_id, + std::string* image_id, + Context* on_finish) { + assert(s_instance != nullptr); + s_instance->image_id = image_id; + s_instance->on_finish = on_finish; + return s_instance; + } + + GetMirrorImageIdRequest() { + s_instance = this; + } + + MOCK_METHOD0(send, void()); +}; + +GetMirrorImageIdRequest* GetMirrorImageIdRequest::s_instance = nullptr; + +} // namespace image_replayer +} // namespace mirror +} // namespace rbd + +// template definitions +#include "tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.cc" + +namespace rbd { +namespace mirror { +namespace image_replayer { + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InSequence; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::StrEq; +using ::testing::WithArg; + +class TestMockImageReplayerPrepareRemoteImageRequest : public TestMockFixture { +public: + typedef PrepareRemoteImageRequest MockPrepareRemoteImageRequest; + typedef GetMirrorImageIdRequest MockGetMirrorImageIdRequest; + + void expect_get_mirror_image_id(MockGetMirrorImageIdRequest& mock_get_mirror_image_id_request, + const std::string& image_id, int r) { + EXPECT_CALL(mock_get_mirror_image_id_request, send()) + .WillOnce(Invoke([&mock_get_mirror_image_id_request, image_id, r]() { + *mock_get_mirror_image_id_request.image_id = image_id; + mock_get_mirror_image_id_request.on_finish->complete(r); + })); + } + + void expect_mirror_uuid_get(librados::IoCtx &io_ctx, + const std::string &mirror_uuid, int r) { + bufferlist bl; + ::encode(mirror_uuid, bl); + + EXPECT_CALL(get_mock_io_ctx(io_ctx), + exec(RBD_MIRRORING, _, StrEq("rbd"), StrEq("mirror_uuid_get"), _, _, _)) + .WillOnce(DoAll(WithArg<5>(Invoke([bl](bufferlist *out_bl) { + *out_bl = bl; + })), + Return(r))); + } +}; + +TEST_F(TestMockImageReplayerPrepareRemoteImageRequest, Success) { + InSequence seq; + expect_mirror_uuid_get(m_remote_io_ctx, "remote mirror uuid", 0); + MockGetMirrorImageIdRequest mock_get_mirror_image_id_request; + expect_get_mirror_image_id(mock_get_mirror_image_id_request, + "remote image id", 0); + + std::string remote_mirror_uuid; + std::string remote_image_id; + C_SaferCond ctx; + auto req = MockPrepareRemoteImageRequest::create(m_remote_io_ctx, + "global image id", + &remote_mirror_uuid, + &remote_image_id, + &ctx); + req->send(); + + ASSERT_EQ(0, ctx.wait()); + ASSERT_EQ(std::string("remote mirror uuid"), remote_mirror_uuid); + ASSERT_EQ(std::string("remote image id"), remote_image_id); +} + +TEST_F(TestMockImageReplayerPrepareRemoteImageRequest, MirrorUuidError) { + InSequence seq; + expect_mirror_uuid_get(m_remote_io_ctx, "", -EINVAL); + + std::string remote_mirror_uuid; + std::string remote_image_id; + C_SaferCond ctx; + auto req = MockPrepareRemoteImageRequest::create(m_remote_io_ctx, + "global image id", + &remote_mirror_uuid, + &remote_image_id, + &ctx); + req->send(); + + ASSERT_EQ(-EINVAL, ctx.wait()); + ASSERT_EQ(std::string(""), remote_mirror_uuid); +} + +TEST_F(TestMockImageReplayerPrepareRemoteImageRequest, MirrorImageIdError) { + InSequence seq; + expect_mirror_uuid_get(m_remote_io_ctx, "remote mirror uuid", 0); + MockGetMirrorImageIdRequest mock_get_mirror_image_id_request; + expect_get_mirror_image_id(mock_get_mirror_image_id_request, "", -EINVAL); + + std::string remote_mirror_uuid; + std::string remote_image_id; + C_SaferCond ctx; + auto req = MockPrepareRemoteImageRequest::create(m_remote_io_ctx, + "global image id", + &remote_mirror_uuid, + &remote_image_id, + &ctx); + req->send(); + + ASSERT_EQ(-EINVAL, ctx.wait()); + ASSERT_EQ(std::string("remote mirror uuid"), remote_mirror_uuid); +} + +} // namespace image_replayer +} // namespace mirror +} // namespace rbd diff --git a/src/tools/rbd_mirror/CMakeLists.txt b/src/tools/rbd_mirror/CMakeLists.txt index 27b7e26431c6..5538fc1945a2 100644 --- a/src/tools/rbd_mirror/CMakeLists.txt +++ b/src/tools/rbd_mirror/CMakeLists.txt @@ -28,6 +28,7 @@ set(rbd_mirror_internal image_replayer/OpenImageRequest.cc image_replayer/OpenLocalImageRequest.cc image_replayer/PrepareLocalImageRequest.cc + image_replayer/PrepareRemoteImageRequest.cc image_replayer/ReplayStatusFormatter.cc image_sync/ImageCopyRequest.cc image_sync/ObjectCopyRequest.cc diff --git a/src/tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.cc b/src/tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.cc new file mode 100644 index 000000000000..11979d09959f --- /dev/null +++ b/src/tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.cc @@ -0,0 +1,104 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.h" +#include "include/rados/librados.hpp" +#include "cls/rbd/cls_rbd_client.h" +#include "common/errno.h" +#include "librbd/ImageCtx.h" +#include "librbd/Utils.h" +#include "tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.h" + +#define dout_context g_ceph_context +#define dout_subsys ceph_subsys_rbd_mirror +#undef dout_prefix +#define dout_prefix *_dout << "rbd::mirror::image_replayer::" \ + << "PrepareRemoteImageRequest: " << this << " " \ + << __func__ << ": " + +namespace rbd { +namespace mirror { +namespace image_replayer { + +using librbd::util::create_context_callback; +using librbd::util::create_rados_callback; + +template +void PrepareRemoteImageRequest::send() { + get_remote_mirror_uuid(); +} + +template +void PrepareRemoteImageRequest::get_remote_mirror_uuid() { + dout(20) << dendl; + + librados::ObjectReadOperation op; + librbd::cls_client::mirror_uuid_get_start(&op); + + librados::AioCompletion *aio_comp = create_rados_callback< + PrepareRemoteImageRequest, + &PrepareRemoteImageRequest::handle_get_remote_mirror_uuid>(this); + int r = m_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl); + assert(r == 0); + aio_comp->release(); +} + +template +void PrepareRemoteImageRequest::handle_get_remote_mirror_uuid(int r) { + if (r >= 0) { + bufferlist::iterator it = m_out_bl.begin(); + r = librbd::cls_client::mirror_uuid_get_finish(&it, m_remote_mirror_uuid); + if (r >= 0 && m_remote_mirror_uuid->empty()) { + r = -ENOENT; + } + } + + dout(20) << "r=" << r << dendl; + if (r < 0) { + derr << "failed to retrieve remote mirror uuid: " << cpp_strerror(r) + << dendl; + finish(r); + return; + } + + get_remote_image_id(); +} + +template +void PrepareRemoteImageRequest::get_remote_image_id() { + dout(20) << dendl; + + Context *ctx = create_context_callback< + PrepareRemoteImageRequest, + &PrepareRemoteImageRequest::handle_get_remote_image_id>(this); + auto req = GetMirrorImageIdRequest::create(m_io_ctx, m_global_image_id, + m_remote_image_id, ctx); + req->send(); +} + +template +void PrepareRemoteImageRequest::handle_get_remote_image_id(int r) { + dout(20) << "r=" << r << ", " + << "remote_image_id=" << *m_remote_image_id << dendl; + + if (r < 0) { + finish(r); + return; + } + + finish(0); +} + +template +void PrepareRemoteImageRequest::finish(int r) { + dout(20) << "r=" << r << dendl; + + m_on_finish->complete(r); + delete this; +} + +} // namespace image_replayer +} // namespace mirror +} // namespace rbd + +template class rbd::mirror::image_replayer::PrepareRemoteImageRequest; diff --git a/src/tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.h b/src/tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.h new file mode 100644 index 000000000000..9943fd742a7e --- /dev/null +++ b/src/tools/rbd_mirror/image_replayer/PrepareRemoteImageRequest.h @@ -0,0 +1,88 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#ifndef RBD_MIRROR_IMAGE_REPLAYER_PREPARE_REMOTE_IMAGE_REQUEST_H +#define RBD_MIRROR_IMAGE_REPLAYER_PREPARE_REMOTE_IMAGE_REQUEST_H + +#include "include/buffer.h" +#include + +namespace librados { struct IoCtx; } +namespace librbd { struct ImageCtx; } + +struct Context; +struct ContextWQ; + +namespace rbd { +namespace mirror { +namespace image_replayer { + +template +class PrepareRemoteImageRequest { +public: + static PrepareRemoteImageRequest *create(librados::IoCtx &io_ctx, + const std::string &global_image_id, + std::string *remote_mirror_uuid, + std::string *remote_image_id, + Context *on_finish) { + return new PrepareRemoteImageRequest(io_ctx, global_image_id, + remote_mirror_uuid, remote_image_id, + on_finish); + } + + PrepareRemoteImageRequest(librados::IoCtx &io_ctx, + const std::string &global_image_id, + std::string *remote_mirror_uuid, + std::string *remote_image_id, + Context *on_finish) + : m_io_ctx(io_ctx), m_global_image_id(global_image_id), + m_remote_mirror_uuid(remote_mirror_uuid), + m_remote_image_id(remote_image_id), + m_on_finish(on_finish) { + } + + void send(); + +private: + /** + * @verbatim + * + * + * | + * v + * GET_REMOTE_MIRROR_UUID + * | + * v + * GET_REMOTE_IMAGE_ID + * | + * v + * + + * @endverbatim + */ + + librados::IoCtx &m_io_ctx; + std::string m_global_image_id; + std::string *m_remote_mirror_uuid; + std::string *m_remote_image_id; + Context *m_on_finish; + + bufferlist m_out_bl; + + void get_remote_mirror_uuid(); + void handle_get_remote_mirror_uuid(int r); + + void get_remote_image_id(); + void handle_get_remote_image_id(int r); + + void finish(int r); + +}; + +} // namespace image_replayer +} // namespace mirror +} // namespace rbd + +extern template class rbd::mirror::image_replayer::PrepareRemoteImageRequest; + +#endif // RBD_MIRROR_IMAGE_REPLAYER_PREPARE_REMOTE_IMAGE_REQUEST_H