]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: async request to list watchers
authorMykola Golub <to.my.trociny@gmail.com>
Thu, 14 Dec 2017 16:52:12 +0000 (18:52 +0200)
committerJason Dillaman <dillaman@redhat.com>
Tue, 14 Aug 2018 22:29:45 +0000 (18:29 -0400)
Signed-off-by: Mykola Golub <mgolub@suse.com>
src/librbd/CMakeLists.txt
src/librbd/image/ListWatchersRequest.cc [new file with mode: 0644]
src/librbd/image/ListWatchersRequest.h [new file with mode: 0644]
src/test/librbd/CMakeLists.txt
src/test/librbd/image/test_mock_ListWatchersRequest.cc [new file with mode: 0644]

index b9c08d4624fa77f2369ea9bd2761edc08b221362..2cd5779f7da3259bf4033f4ee91ed9d2e4197b88 100644 (file)
@@ -49,6 +49,7 @@ set(librbd_internal_srcs
   image/CloseRequest.cc
   image/CreateRequest.cc
   image/DetachChildRequest.cc
+  image/ListWatchersRequest.cc
   image/OpenRequest.cc
   image/RefreshParentRequest.cc
   image/RefreshRequest.cc
diff --git a/src/librbd/image/ListWatchersRequest.cc b/src/librbd/image/ListWatchersRequest.cc
new file mode 100644 (file)
index 0000000..21e8bfa
--- /dev/null
@@ -0,0 +1,184 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "ListWatchersRequest.h"
+#include "common/RWLock.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageWatcher.h"
+#include "librbd/Utils.h"
+
+#include <algorithm>
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::image::ListWatchersRequest: " << this \
+                           << " " << __func__ << ": "
+
+namespace librbd {
+namespace image {
+
+using librados::IoCtx;
+using util::create_rados_callback;
+
+template<typename I>
+ListWatchersRequest<I>::ListWatchersRequest(I &image_ctx, int flags,
+                                            std::list<obj_watch_t> *watchers,
+                                            Context *on_finish)
+  : m_image_ctx(image_ctx), m_flags(flags), m_watchers(watchers),
+    m_on_finish(on_finish), m_cct(m_image_ctx.cct) {
+}
+
+template<typename I>
+void ListWatchersRequest<I>::send() {
+  ldout(m_cct, 20) << dendl;
+
+  list_image_watchers();
+}
+
+template<typename I>
+void ListWatchersRequest<I>::list_image_watchers() {
+  ldout(m_cct, 20) << dendl;
+
+  librados::ObjectReadOperation op;
+  op.list_watchers(&m_object_watchers, &m_ret_val);
+
+  using klass = ListWatchersRequest<I>;
+  librados::AioCompletion *rados_completion =
+    create_rados_callback<klass, &klass::handle_list_image_watchers>(this);
+
+  int r = m_image_ctx.md_ctx.aio_operate(m_image_ctx.header_oid,
+                                         rados_completion, &op, &m_out_bl);
+  assert(r == 0);
+  rados_completion->release();
+}
+
+template<typename I>
+void ListWatchersRequest<I>::handle_list_image_watchers(int r) {
+  ldout(m_cct, 20) << "r=" << r << dendl;
+
+  if (r == 0 && m_ret_val < 0) {
+    r = m_ret_val;
+  }
+  if (r < 0) {
+    lderr(m_cct) << "error listing image watchers: " << cpp_strerror(r)
+                 << dendl;
+    finish(r);
+    return;
+  }
+
+  get_mirror_image();
+}
+
+template<typename I>
+void ListWatchersRequest<I>::get_mirror_image() {
+  ldout(m_cct, 20) << dendl;
+  if ((m_object_watchers.empty()) ||
+      (m_flags & LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES) == 0 ||
+      (m_image_ctx.features & RBD_FEATURE_JOURNALING) == 0) {
+    finish(0);
+    return;
+  }
+
+  librados::ObjectReadOperation op;
+  cls_client::mirror_image_get_start(&op, m_image_ctx.id);
+
+  using klass = ListWatchersRequest<I>;
+  librados::AioCompletion *comp =
+    create_rados_callback<klass, &klass::handle_get_mirror_image>(this);
+  m_out_bl.clear();
+  int r = m_image_ctx.md_ctx.aio_operate(RBD_MIRRORING, comp, &op, &m_out_bl);
+  assert(r == 0);
+  comp->release();
+}
+
+template<typename I>
+void ListWatchersRequest<I>::handle_get_mirror_image(int r) {
+  ldout(m_cct, 20) << "r=" << r << dendl;
+
+  if (r == -ENOENT) {
+    finish(0);
+    return;
+  } else if (r < 0) {
+    ldout(m_cct, 1) << "error retrieving mirror image: " << cpp_strerror(r)
+                    << dendl;
+  }
+
+  list_mirror_watchers();
+}
+
+template<typename I>
+void ListWatchersRequest<I>::list_mirror_watchers() {
+  ldout(m_cct, 20) << dendl;
+
+  librados::ObjectReadOperation op;
+  op.list_watchers(&m_mirror_watchers, &m_ret_val);
+
+  using klass = ListWatchersRequest<I>;
+  librados::AioCompletion *rados_completion =
+    create_rados_callback<klass, &klass::handle_list_mirror_watchers>(this);
+  m_out_bl.clear();
+  int r = m_image_ctx.md_ctx.aio_operate(RBD_MIRRORING, rados_completion,
+                                         &op, &m_out_bl);
+  assert(r == 0);
+  rados_completion->release();
+}
+
+template<typename I>
+void ListWatchersRequest<I>::handle_list_mirror_watchers(int r) {
+  ldout(m_cct, 20) << "r=" << r << dendl;
+
+  if (r == 0 && m_ret_val < 0) {
+    r = m_ret_val;
+  }
+  if (r < 0 && r != -ENOENT) {
+    ldout(m_cct, 1) << "error listing mirror watchers: " << cpp_strerror(r)
+                    << dendl;
+  }
+  finish(0);
+}
+
+template<typename I>
+void ListWatchersRequest<I>::finish(int r) {
+  ldout(m_cct, 20) << "r=" << r << dendl;
+
+  if (r == 0) {
+    m_watchers->clear();
+
+    if (m_object_watchers.size() > 0) {
+      RWLock::RLocker owner_locker(m_image_ctx.owner_lock);
+      uint64_t watch_handle = m_image_ctx.image_watcher != nullptr ?
+        m_image_ctx.image_watcher->get_watch_handle() : 0;
+
+      for (auto &w : m_object_watchers) {
+        if ((m_flags & LIST_WATCHERS_FILTER_OUT_MY_INSTANCE) != 0) {
+          if (w.cookie == watch_handle) {
+            continue;
+          }
+        }
+        if ((m_flags & LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES) != 0) {
+          auto it = std::find_if(m_mirror_watchers.begin(),
+                                 m_mirror_watchers.end(),
+                                 [w] (obj_watch_t &watcher) {
+                                   return (strncmp(w.addr, watcher.addr,
+                                                   sizeof(w.addr)) == 0);
+                                 });
+          if (it != m_mirror_watchers.end()) {
+            continue;
+          }
+        }
+        m_watchers->push_back(w);
+      }
+    }
+  }
+
+  m_on_finish->complete(r);
+  delete this;
+}
+
+} // namespace image
+} // namespace librbd
+
+template class librbd::image::ListWatchersRequest<librbd::ImageCtx>;
diff --git a/src/librbd/image/ListWatchersRequest.h b/src/librbd/image/ListWatchersRequest.h
new file mode 100644 (file)
index 0000000..6600981
--- /dev/null
@@ -0,0 +1,87 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_IMAGE_LIST_WATCHERS_REQUEST_H
+#define CEPH_LIBRBD_IMAGE_LIST_WATCHERS_REQUEST_H
+
+#include "include/rados/rados_types.hpp"
+
+#include <list>
+
+class Context;
+
+namespace librbd {
+
+class ImageCtx;
+
+namespace image {
+
+enum {
+  LIST_WATCHERS_FILTER_OUT_MY_INSTANCE = 1 << 0,
+  LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES = 1 << 1,
+};
+
+template<typename ImageCtxT = ImageCtx>
+class ListWatchersRequest {
+public:
+  static ListWatchersRequest *create(ImageCtxT &image_ctx, int flags,
+                                     std::list<obj_watch_t> *watchers,
+                                     Context *on_finish) {
+    return new ListWatchersRequest(image_ctx, flags, watchers, on_finish);
+  }
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |
+   *    v
+   * LIST_IMAGE_WATCHERS
+   *    |
+   *    v
+   * GET_MIRROR_IMAGE (skip if not needed)
+   *    |
+   *    v
+   * LIST_MIRROR_WATCHERS (skip if not needed)
+   *    |
+   *    v
+   * <finish>
+   *
+   * @endverbatim
+   */
+
+  ListWatchersRequest(ImageCtxT &image_ctx, int flags, std::list<obj_watch_t> *watchers,
+                      Context *on_finish);
+
+  ImageCtxT& m_image_ctx;
+  int m_flags;
+  std::list<obj_watch_t> *m_watchers;
+  Context *m_on_finish;
+
+  CephContext *m_cct;
+  int m_ret_val;
+  bufferlist m_out_bl;
+  std::list<obj_watch_t> m_object_watchers;
+  std::list<obj_watch_t> m_mirror_watchers;
+
+  void list_image_watchers();
+  void handle_list_image_watchers(int r);
+
+  void get_mirror_image();
+  void handle_get_mirror_image(int r);
+
+  void list_mirror_watchers();
+  void handle_list_mirror_watchers(int r);
+
+  void finish(int r);
+};
+
+} // namespace image
+} // namespace librbd
+
+extern template class librbd::image::ListWatchersRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_IMAGE_LIST_WATCHERS_REQUEST_H
index 35b9d69802be65126b3c1be19e00c1218dc65db9..ced4f678e3ce612cf6f5c18fb9111ebfe6498cf0 100644 (file)
@@ -46,6 +46,7 @@ set(unittest_librbd_srcs
   exclusive_lock/test_mock_PreReleaseRequest.cc
   image/test_mock_CloneRequest.cc
   image/test_mock_DetachChildRequest.cc
+  image/test_mock_ListWatchersRequest.cc
   image/test_mock_RefreshRequest.cc
   image/test_mock_RemoveRequest.cc
   io/test_mock_ImageRequest.cc
diff --git a/src/test/librbd/image/test_mock_ListWatchersRequest.cc b/src/test/librbd/image/test_mock_ListWatchersRequest.cc
new file mode 100644 (file)
index 0000000..8090379
--- /dev/null
@@ -0,0 +1,220 @@
+// -*- 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 "librbd/image/ListWatchersRequest.h"
+#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
+#include "test/librados_test_stub/MockTestMemRadosClient.h"
+#include "test/librbd/mock/MockContextWQ.h"
+#include "test/librbd/mock/MockImageCtx.h"
+#include "test/librbd/test_support.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace librbd {
+
+namespace {
+
+struct MockTestImageCtx : public librbd::MockImageCtx {
+  MockTestImageCtx(librbd::ImageCtx &image_ctx)
+    : librbd::MockImageCtx(image_ctx) {
+  }
+};
+
+} // anonymous namespace
+} // namespace librbd
+
+// template definitions
+#include "librbd/image/ListWatchersRequest.cc"
+template class librbd::image::ListWatchersRequest<librbd::MockImageCtx>;
+
+namespace librbd {
+
+namespace image {
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::InSequence;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::StrEq;
+
+class TestMockListWatchersRequest : public TestMockFixture {
+public:
+  typedef ListWatchersRequest<MockImageCtx> MockListWatchersRequest;
+
+  obj_watch_t watcher(const std::string &address, uint64_t watch_handle) {
+    obj_watch_t w;
+    strcpy(w.addr, address.c_str());
+    w.watcher_id = 0;
+    w.cookie = watch_handle;
+    w.timeout_seconds = 0;
+
+    return w;
+  }
+
+  void expect_list_watchers(MockTestImageCtx &mock_image_ctx,
+                            const std::string oid,
+                            const std::list<obj_watch_t> &watchers, int r) {
+    auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+                               list_watchers(oid, _));
+    if (r < 0) {
+      expect.WillOnce(Return(r));
+    } else {
+      expect.WillOnce(DoAll(SetArgPointee<1>(watchers), Return(0)));
+    }
+  }
+
+  void expect_list_image_watchers(MockTestImageCtx &mock_image_ctx,
+                                  const std::list<obj_watch_t> &watchers,
+                                  int r) {
+    expect_list_watchers(mock_image_ctx, mock_image_ctx.header_oid,
+                         watchers, r);
+  }
+
+  void expect_list_mirror_watchers(MockTestImageCtx &mock_image_ctx,
+                                   const std::list<obj_watch_t> &watchers,
+                                   int r) {
+    expect_list_watchers(mock_image_ctx, RBD_MIRRORING, watchers, r);
+  }
+
+  void expect_mirror_image_get(MockImageCtx &mock_image_ctx, int r) {
+    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
+                exec(RBD_MIRRORING, _, StrEq("rbd"), StrEq("mirror_image_get"),
+                     _, _, _))
+      .WillOnce(Return(r));
+  }
+
+  void expect_get_watch_handle(MockImageWatcher &mock_watcher,
+                               uint64_t watch_handle) {
+    EXPECT_CALL(mock_watcher, get_watch_handle())
+      .WillOnce(Return(watch_handle));
+  }
+};
+
+TEST_F(TestMockListWatchersRequest, NoImageWatchers) {
+  librbd::ImageCtx *ictx;
+  ASSERT_EQ(0, open_image(m_image_name, &ictx));
+
+  MockTestImageCtx mock_image_ctx(*ictx);
+  MockImageWatcher mock_watcher;
+
+  InSequence seq;
+  expect_list_image_watchers(mock_image_ctx, {}, 0);
+
+  std::list<obj_watch_t> watchers;
+  C_SaferCond ctx;
+  auto req = MockListWatchersRequest::create(mock_image_ctx, 0, &watchers,
+                                             &ctx);
+  req->send();
+
+  ASSERT_EQ(0, ctx.wait());
+  ASSERT_TRUE(watchers.empty());
+}
+
+TEST_F(TestMockListWatchersRequest, Error) {
+  librbd::ImageCtx *ictx;
+  ASSERT_EQ(0, open_image(m_image_name, &ictx));
+
+  MockTestImageCtx mock_image_ctx(*ictx);
+  MockImageWatcher mock_watcher;
+
+  InSequence seq;
+  expect_list_image_watchers(mock_image_ctx, {}, -EINVAL);
+
+  std::list<obj_watch_t> watchers;
+  C_SaferCond ctx;
+  auto req = MockListWatchersRequest::create(mock_image_ctx, 0, &watchers,
+                                             &ctx);
+  req->send();
+
+  ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockListWatchersRequest, Success) {
+  librbd::ImageCtx *ictx;
+  ASSERT_EQ(0, open_image(m_image_name, &ictx));
+
+  MockTestImageCtx mock_image_ctx(*ictx);
+  MockImageWatcher mock_watcher;
+
+  InSequence seq;
+  expect_list_image_watchers(mock_image_ctx,
+                             {watcher("a", 123), watcher("b", 456)}, 0);
+  expect_get_watch_handle(*mock_image_ctx.image_watcher, 123);
+
+  std::list<obj_watch_t> watchers;
+  C_SaferCond ctx;
+  auto req = MockListWatchersRequest::create(mock_image_ctx, 0, &watchers,
+                                             &ctx);
+  req->send();
+
+  ASSERT_EQ(0, ctx.wait());
+  ASSERT_EQ(2U, watchers.size());
+
+  auto w = watchers.begin();
+  ASSERT_STREQ("a", w->addr);
+  ASSERT_EQ(123, w->cookie);
+
+  w++;
+  ASSERT_STREQ("b", w->addr);
+  ASSERT_EQ(456, w->cookie);
+}
+
+TEST_F(TestMockListWatchersRequest, FilterOutMyInstance) {
+  librbd::ImageCtx *ictx;
+  ASSERT_EQ(0, open_image(m_image_name, &ictx));
+
+  MockTestImageCtx mock_image_ctx(*ictx);
+  MockImageWatcher mock_watcher;
+
+  InSequence seq;
+  expect_list_image_watchers(mock_image_ctx,
+                             {watcher("a", 123), watcher("b", 456)}, 0);
+  expect_get_watch_handle(*mock_image_ctx.image_watcher, 123);
+
+  std::list<obj_watch_t> watchers;
+  C_SaferCond ctx;
+  auto req = MockListWatchersRequest::create(
+      mock_image_ctx, LIST_WATCHERS_FILTER_OUT_MY_INSTANCE, &watchers, &ctx);
+  req->send();
+
+  ASSERT_EQ(0, ctx.wait());
+  ASSERT_EQ(1U, watchers.size());
+
+  ASSERT_STREQ("b", watchers.begin()->addr);
+  ASSERT_EQ(456, watchers.begin()->cookie);
+}
+
+TEST_F(TestMockListWatchersRequest, FilterOutMirrorInstance) {
+  REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
+
+  librbd::ImageCtx *ictx;
+  ASSERT_EQ(0, open_image(m_image_name, &ictx));
+
+  MockTestImageCtx mock_image_ctx(*ictx);
+  MockImageWatcher mock_watcher;
+
+  InSequence seq;
+  expect_list_image_watchers(mock_image_ctx,
+                             {watcher("a", 123), watcher("b", 456)}, 0);
+  expect_mirror_image_get(mock_image_ctx, 0);
+  expect_list_mirror_watchers(mock_image_ctx, {watcher("b", 789)}, 0);
+  expect_get_watch_handle(*mock_image_ctx.image_watcher, 123);
+
+  std::list<obj_watch_t> watchers;
+  C_SaferCond ctx;
+  auto req = MockListWatchersRequest::create(
+      mock_image_ctx, LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES, &watchers,
+      &ctx);
+  req->send();
+
+  ASSERT_EQ(0, ctx.wait());
+  ASSERT_EQ(1U, watchers.size());
+
+  ASSERT_STREQ("a", watchers.begin()->addr);
+  ASSERT_EQ(123, watchers.begin()->cookie);
+}
+
+} // namespace image
+} // namespace librbd