librbd: removed ManagedLock dependency from ancillary classes

Signed-off-by: Jason Dillaman <dillaman@redhat.com>
This commit is contained in:
Jason Dillaman 2017-01-17 13:42:43 -05:00
parent 611b7120fe
commit 2a86f48c89
18 changed files with 105 additions and 88 deletions

View File

@ -37,6 +37,7 @@ set(librbd_internal_srcs
managed_lock/GetLockerRequest.cc
managed_lock/ReleaseRequest.cc
managed_lock/ReacquireRequest.cc
managed_lock/Utils.cc
exclusive_lock/AutomaticPolicy.cc
exclusive_lock/PreAcquireRequest.cc
exclusive_lock/PostAcquireRequest.cc

View File

@ -8,6 +8,7 @@
#include "librbd/managed_lock/ReleaseRequest.h"
#include "librbd/managed_lock/ReacquireRequest.h"
#include "librbd/managed_lock/Types.h"
#include "librbd/managed_lock/Utils.h"
#include "librbd/Watcher.h"
#include "librbd/ImageCtx.h"
#include "cls/lock/cls_lock_client.h"
@ -15,7 +16,6 @@
#include "common/errno.h"
#include "common/WorkQueue.h"
#include "librbd/Utils.h"
#include <sstream>
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
@ -28,8 +28,6 @@ using namespace managed_lock;
namespace {
const std::string WATCHER_LOCK_COOKIE_PREFIX = "auto";
template <typename R>
struct C_SendLockRequest : public Context {
R* request;
@ -42,15 +40,17 @@ struct C_SendLockRequest : public Context {
} // anonymous namespace
template <typename I>
const std::string ManagedLock<I>::WATCHER_LOCK_TAG("internal");
using librbd::util::create_context_callback;
using librbd::util::unique_lock_name;
using managed_lock::util::decode_lock_cookie;
using managed_lock::util::encode_lock_cookie;
template <typename I>
ManagedLock<I>::ManagedLock(librados::IoCtx &ioctx, ContextWQ *work_queue,
const string& oid, Watcher *watcher, Mode mode,
bool blacklist_on_break_lock,
uint32_t blacklist_expire_seconds)
: m_lock(util::unique_lock_name("librbd::ManagedLock<I>::m_lock", this)),
: m_lock(unique_lock_name("librbd::ManagedLock<I>::m_lock", this)),
m_state(STATE_UNLOCKED),
m_ioctx(ioctx), m_cct(reinterpret_cast<CephContext *>(ioctx.cct())),
m_work_queue(work_queue),
@ -253,25 +253,6 @@ void ManagedLock<I>::post_release_lock_handler(bool shutting_down, int r,
on_finish->complete(r);
}
template <typename I>
bool ManagedLock<I>::decode_lock_cookie(const std::string &tag,
uint64_t *handle) {
std::string prefix;
std::istringstream ss(tag);
if (!(ss >> prefix >> *handle) || prefix != WATCHER_LOCK_COOKIE_PREFIX) {
return false;
}
return true;
}
template <typename I>
string ManagedLock<I>::encode_lock_cookie(uint64_t watch_handle) {
assert(watch_handle != 0);
std::ostringstream ss;
ss << WATCHER_LOCK_COOKIE_PREFIX << " " << watch_handle;
return ss.str();
}
template <typename I>
bool ManagedLock<I>::is_transition_state() const {
switch (m_state) {
@ -402,10 +383,10 @@ void ManagedLock<I>::send_acquire_lock() {
m_state = STATE_WAITING_FOR_REGISTER;
return;
}
m_cookie = ManagedLock<I>::encode_lock_cookie(watch_handle);
m_cookie = encode_lock_cookie(watch_handle);
m_work_queue->queue(new FunctionContext([this](int r) {
pre_acquire_lock_handler(util::create_context_callback<
pre_acquire_lock_handler(create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_pre_acquire_lock>(this));
}));
}
@ -423,7 +404,7 @@ void ManagedLock<I>::handle_pre_acquire_lock(int r) {
AcquireRequest<I>* req = AcquireRequest<I>::create(
m_ioctx, m_watcher, m_work_queue, m_oid, m_cookie, m_mode == EXCLUSIVE,
m_blacklist_on_break_lock, m_blacklist_expire_seconds,
util::create_context_callback<
create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_acquire_lock>(this));
m_work_queue->queue(new C_SendLockRequest<AcquireRequest<I>>(req), 0);
}
@ -444,7 +425,7 @@ void ManagedLock<I>::handle_acquire_lock(int r) {
m_post_next_state = (r < 0 ? STATE_UNLOCKED : STATE_LOCKED);
m_work_queue->queue(new FunctionContext([this, r](int ret) {
post_acquire_lock_handler(r, util::create_context_callback<
post_acquire_lock_handler(r, create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_post_acquire_lock>(this));
}));
}
@ -496,7 +477,7 @@ void ManagedLock<I>::send_reacquire_lock() {
return;
}
m_new_cookie = ManagedLock<I>::encode_lock_cookie(watch_handle);
m_new_cookie = encode_lock_cookie(watch_handle);
if (m_cookie == m_new_cookie) {
ldout(m_cct, 10) << ": skipping reacquire since cookie still valid"
<< dendl;
@ -510,7 +491,7 @@ void ManagedLock<I>::send_reacquire_lock() {
using managed_lock::ReacquireRequest;
ReacquireRequest<I>* req = ReacquireRequest<I>::create(m_ioctx, m_oid,
m_cookie, m_new_cookie, m_mode == EXCLUSIVE,
util::create_context_callback<
create_context_callback<
ManagedLock, &ManagedLock<I>::handle_reacquire_lock>(this));
m_work_queue->queue(new C_SendLockRequest<ReacquireRequest<I>>(req));
}
@ -574,7 +555,7 @@ void ManagedLock<I>::send_release_lock() {
m_state = STATE_PRE_RELEASING;
m_work_queue->queue(new FunctionContext([this](int r) {
pre_release_lock_handler(false, util::create_context_callback<
pre_release_lock_handler(false, create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_pre_release_lock>(this));
}));
}
@ -591,7 +572,7 @@ void ManagedLock<I>::handle_pre_release_lock(int r) {
using managed_lock::ReleaseRequest;
ReleaseRequest<I>* req = ReleaseRequest<I>::create(m_ioctx, m_watcher,
m_work_queue, m_oid, m_cookie,
util::create_context_callback<
create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_release_lock>(this));
m_work_queue->queue(new C_SendLockRequest<ReleaseRequest<I>>(req), 0);
}
@ -610,7 +591,7 @@ void ManagedLock<I>::handle_release_lock(int r) {
m_post_next_state = r < 0 ? STATE_LOCKED : STATE_UNLOCKED;
m_work_queue->queue(new FunctionContext([this, r](int ret) {
post_release_lock_handler(false, r, util::create_context_callback<
post_release_lock_handler(false, r, create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_post_release_lock>(this));
}));
}
@ -630,7 +611,7 @@ void ManagedLock<I>::send_shutdown() {
if (m_state == STATE_UNLOCKED) {
m_state = STATE_SHUTTING_DOWN;
m_work_queue->queue(new FunctionContext([this](int r) {
shutdown_handler(r, util::create_context_callback<
shutdown_handler(r, create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_shutdown>(this));
}));
return;
@ -659,7 +640,7 @@ void ManagedLock<I>::send_shutdown_release() {
Mutex::Locker locker(m_lock);
m_work_queue->queue(new FunctionContext([this](int r) {
pre_release_lock_handler(true, util::create_context_callback<
pre_release_lock_handler(true, create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_shutdown_pre_release>(this));
}));
}
@ -678,7 +659,7 @@ void ManagedLock<I>::handle_shutdown_pre_release(int r) {
ReleaseRequest<I>* req = ReleaseRequest<I>::create(m_ioctx, m_watcher,
m_work_queue, m_oid, cookie,
new FunctionContext([this](int r) {
post_release_lock_handler(true, r, util::create_context_callback<
post_release_lock_handler(true, r, create_context_callback<
ManagedLock<I>, &ManagedLock<I>::handle_shutdown_post_release>(this));
}));
req->send();

View File

@ -30,8 +30,6 @@ private:
typedef typename TypeTraits::Watcher Watcher;
public:
static const std::string WATCHER_LOCK_TAG;
static ManagedLock *create(librados::IoCtx& ioctx, ContextWQ *work_queue,
const std::string& oid, Watcher *watcher,
managed_lock::Mode mode,
@ -67,8 +65,6 @@ public:
return m_state == STATE_LOCKED;
}
static bool decode_lock_cookie(const std::string &tag, uint64_t *handle);
protected:
/**
@ -174,8 +170,6 @@ private:
ActionsContexts m_actions_contexts;
static std::string encode_lock_cookie(uint64_t watch_handle);
bool is_lock_owner(Mutex &lock) const;
bool is_transition_state() const;

View File

@ -9,7 +9,6 @@
#include "common/WorkQueue.h"
#include "include/stringify.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ManagedLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"

View File

@ -14,9 +14,6 @@ class Context;
namespace librbd {
template <typename> class Journal;
template <typename> class ManagedLock;
namespace exclusive_lock {
template <typename ImageCtxT = ImageCtx>

View File

@ -6,7 +6,6 @@
#include "common/errno.h"
#include "librbd/AioImageRequestWQ.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ManagedLock.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/Journal.h"

View File

@ -12,8 +12,6 @@ class Context;
namespace librbd {
struct ImageCtx;
template <typename> class ManagedLock;
template <typename> class Journal;
namespace exclusive_lock {

View File

@ -3,18 +3,17 @@
#include "librbd/managed_lock/AcquireRequest.h"
#include "librbd/Watcher.h"
#include "librbd/ManagedLock.h"
#include "cls/lock/cls_lock_client.h"
#include "cls/lock/cls_lock_types.h"
#include "common/dout.h"
#include "common/errno.h"
#include "common/WorkQueue.h"
#include "include/stringify.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"
#include "librbd/managed_lock/BreakRequest.h"
#include "librbd/managed_lock/GetLockerRequest.h"
#include "librbd/ImageCtx.h"
#include "librbd/managed_lock/Utils.h"
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
@ -25,10 +24,10 @@ using std::string;
namespace librbd {
using util::detail::C_AsyncCallback;
using util::create_context_callback;
using util::create_rados_safe_callback;
using util::create_rados_ack_callback;
using librbd::util::detail::C_AsyncCallback;
using librbd::util::create_context_callback;
using librbd::util::create_rados_safe_callback;
using librbd::util::create_rados_ack_callback;
namespace managed_lock {
@ -110,7 +109,7 @@ void AcquireRequest<I>::send_lock() {
librados::ObjectWriteOperation op;
rados::cls::lock::lock(&op, RBD_LOCK_NAME,
m_exclusive ? LOCK_EXCLUSIVE : LOCK_SHARED, m_cookie,
ManagedLock<I>::WATCHER_LOCK_TAG, "", utime_t(), 0);
util::get_watcher_lock_tag(), "", utime_t(), 0);
using klass = AcquireRequest;
librados::AioCompletion *rados_completion =

View File

@ -8,9 +8,9 @@
#include "common/errno.h"
#include "include/stringify.h"
#include "librbd/ImageCtx.h"
#include "librbd/ManagedLock.h"
#include "librbd/Utils.h"
#include "librbd/managed_lock/Types.h"
#include "librbd/managed_lock/Utils.h"
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
@ -20,7 +20,7 @@
namespace librbd {
namespace managed_lock {
using util::create_rados_ack_callback;
using librbd::util::create_rados_ack_callback;
template <typename I>
GetLockerRequest<I>::GetLockerRequest(librados::IoCtx& ioctx,
@ -78,7 +78,7 @@ void GetLockerRequest<I>::handle_get_lockers(int r) {
return;
}
if (lock_tag != ManagedLock<>::WATCHER_LOCK_TAG) {
if (lock_tag != util::get_watcher_lock_tag()) {
ldout(m_cct, 5) <<"locked by external mechanism: tag=" << lock_tag << dendl;
finish(-EBUSY);
return;
@ -96,8 +96,7 @@ void GetLockerRequest<I>::handle_get_lockers(int r) {
std::map<rados::cls::lock::locker_id_t,
rados::cls::lock::locker_info_t>::iterator iter = lockers.begin();
if (!ManagedLock<>::decode_lock_cookie(iter->first.cookie,
&m_locker->handle)) {
if (!util::decode_lock_cookie(iter->first.cookie, &m_locker->handle)) {
ldout(m_cct, 5) << "locked by external mechanism: "
<< "cookie=" << iter->first.cookie << dendl;
finish(-EBUSY);

View File

@ -3,14 +3,13 @@
#include "librbd/managed_lock/ReacquireRequest.h"
#include "librbd/Watcher.h"
#include "librbd/ManagedLock.h"
#include "cls/lock/cls_lock_client.h"
#include "cls/lock/cls_lock_types.h"
#include "common/dout.h"
#include "common/errno.h"
#include "librbd/Utils.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"
#include "librbd/managed_lock/Utils.h"
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
@ -49,7 +48,7 @@ void ReacquireRequest<I>::set_cookie() {
librados::ObjectWriteOperation op;
rados::cls::lock::set_cookie(&op, RBD_LOCK_NAME,
m_exclusive ? LOCK_EXCLUSIVE : LOCK_SHARED,
m_old_cookie, ManagedLock<I>::WATCHER_LOCK_TAG,
m_old_cookie, util::get_watcher_lock_tag(),
m_new_cookie);
librados::AioCompletion *rados_completion = create_rados_safe_callback<

View File

@ -0,0 +1,43 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/assert.h"
#include "librbd/managed_lock/Utils.h"
#include <sstream>
namespace librbd {
namespace managed_lock {
namespace util {
namespace {
const std::string WATCHER_LOCK_COOKIE_PREFIX = "auto";
const std::string WATCHER_LOCK_TAG("internal");
} // anonymous namespace
const std::string &get_watcher_lock_tag() {
return WATCHER_LOCK_TAG;
}
bool decode_lock_cookie(const std::string &tag, uint64_t *handle) {
std::string prefix;
std::istringstream ss(tag);
if (!(ss >> prefix >> *handle) || prefix != WATCHER_LOCK_COOKIE_PREFIX) {
return false;
}
return true;
}
std::string encode_lock_cookie(uint64_t watch_handle) {
assert(watch_handle != 0);
std::ostringstream ss;
ss << WATCHER_LOCK_COOKIE_PREFIX << " " << watch_handle;
return ss.str();
}
} // namespace util
} // namespace managed_lock
} // namespace librbd

View File

@ -0,0 +1,23 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_LIBRBD_MANAGED_LOCK_UTILS_H
#define CEPH_LIBRBD_MANAGED_LOCK_UTILS_H
#include "include/int_types.h"
#include <string>
namespace librbd {
namespace managed_lock {
namespace util {
const std::string &get_watcher_lock_tag();
bool decode_lock_cookie(const std::string &tag, uint64_t *handle);
std::string encode_lock_cookie(uint64_t watch_handle);
} // namespace util
} // namespace managed_lock
} // namespace librbd
#endif // CEPH_LIBRBD_MANAGED_LOCK_UTILS_H

View File

@ -18,8 +18,6 @@ template class librbd::exclusive_lock::PreReleaseRequest<librbd::MockImageCtx>;
namespace librbd {
using librbd::ManagedLock;
namespace exclusive_lock {
namespace {

View File

@ -81,9 +81,6 @@ GetLockerRequest<librbd::MockImageCtx> *GetLockerRequest<librbd::MockImageCtx>::
#include "librbd/managed_lock/AcquireRequest.cc"
template class librbd::managed_lock::AcquireRequest<librbd::MockImageCtx>;
#include "librbd/ManagedLock.cc"
template class librbd::ManagedLock<librbd::MockImageCtx>;
namespace {
MATCHER_P(IsLockType, exclusive, "") {
@ -116,7 +113,6 @@ public:
typedef AcquireRequest<MockImageCtx> MockAcquireRequest;
typedef BreakRequest<MockImageCtx> MockBreakRequest;
typedef GetLockerRequest<MockImageCtx> MockGetLockerRequest;
typedef ManagedLock<MockImageCtx> MockManagedLock;
void expect_lock(MockImageCtx &mock_image_ctx, int r,
bool exclusive = true) {

View File

@ -7,7 +7,6 @@
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "cls/lock/cls_lock_ops.h"
#include "librbd/ManagedLock.h"
#include "librbd/managed_lock/BreakRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

View File

@ -7,9 +7,9 @@
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "cls/lock/cls_lock_ops.h"
#include "librbd/ManagedLock.h"
#include "librbd/managed_lock/GetLockerRequest.h"
#include "librbd/managed_lock/Types.h"
#include "librbd/managed_lock/Utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
@ -89,7 +89,7 @@ TEST_F(TestMockManagedLockGetLockerRequest, SuccessExclusive) {
InSequence seq;
expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
"auto 123", ManagedLock<>::WATCHER_LOCK_TAG,
"auto 123", util::get_watcher_lock_tag(),
LOCK_EXCLUSIVE);
C_SaferCond ctx;
@ -116,7 +116,7 @@ TEST_F(TestMockManagedLockGetLockerRequest, SuccessShared) {
InSequence seq;
expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
"auto 123", ManagedLock<>::WATCHER_LOCK_TAG,
"auto 123", util::get_watcher_lock_tag(),
LOCK_SHARED);
C_SaferCond ctx;
@ -206,7 +206,7 @@ TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoIncompatibleShared) {
InSequence seq;
expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
"auto 123", ManagedLock<>::WATCHER_LOCK_TAG,
"auto 123", util::get_watcher_lock_tag(),
LOCK_SHARED);
C_SaferCond ctx;
@ -228,7 +228,7 @@ TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoIncompatibleExclusive) {
InSequence seq;
expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
"auto 123", ManagedLock<>::WATCHER_LOCK_TAG,
"auto 123", util::get_watcher_lock_tag(),
LOCK_EXCLUSIVE);
C_SaferCond ctx;
@ -250,7 +250,7 @@ TEST_F(TestMockManagedLockGetLockerRequest, GetLockInfoExternalCookie) {
InSequence seq;
expect_get_lock_info(mock_image_ctx, 0, entity_name_t::CLIENT(1), "1.2.3.4",
"external cookie", ManagedLock<>::WATCHER_LOCK_TAG,
"external cookie", util::get_watcher_lock_tag(),
LOCK_EXCLUSIVE);
C_SaferCond ctx;

View File

@ -16,9 +16,6 @@
#include "librbd/managed_lock/ReacquireRequest.cc"
template class librbd::managed_lock::ReacquireRequest<librbd::MockImageCtx>;
#include "librbd/ManagedLock.cc"
template class librbd::ManagedLock<librbd::MockImageCtx>;
namespace {
MATCHER_P(IsLockType, exclusive, "") {
@ -44,7 +41,6 @@ using ::testing::StrEq;
class TestMockManagedLockReacquireRequest : public TestMockFixture {
public:
typedef ReacquireRequest<MockImageCtx> MockReacquireRequest;
typedef ManagedLock<MockImageCtx> MockManagedLock;
void expect_set_cookie(MockImageCtx &mock_image_ctx, int r,
bool exclusive = true) {

View File

@ -23,9 +23,6 @@ struct Traits<MockImageCtx> {
#include "librbd/managed_lock/ReleaseRequest.cc"
template class librbd::managed_lock::ReleaseRequest<librbd::MockImageCtx>;
#include "librbd/ManagedLock.cc"
template class librbd::ManagedLock<librbd::MockImageCtx>;
namespace librbd {
namespace managed_lock {
@ -40,7 +37,6 @@ static const std::string TEST_COOKIE("auto 123");
class TestMockManagedLockReleaseRequest : public TestMockFixture {
public:
typedef ReleaseRequest<MockImageCtx> MockReleaseRequest;
typedef ManagedLock<MockImageCtx> MockManagedLock;
void expect_unlock(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),