common/async: add RadosLockClient for with_lease()

Signed-off-by: Casey Bodley <cbodley@redhat.com>
This commit is contained in:
Casey Bodley 2024-10-14 10:42:16 -04:00
parent ae62f7f7b9
commit 1aa7f81fb8
3 changed files with 84 additions and 1 deletions

View File

@ -0,0 +1,82 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#pragma once
#include <boost/asio/any_io_executor.hpp>
#include "librados/librados_asio.h"
#include "librados/redirect_version.h"
#include "cls/lock/cls_lock_client.h"
#include "lock_client.h"
namespace ceph::async {
/// A LockClient based on librados and cls_lock.
/// \see with_lease
class RadosLockClient : public LockClient {
public:
using executor_type = boost::asio::any_io_executor;
executor_type get_executor() const { return ex; }
RadosLockClient(executor_type ex,
librados::IoCtx ioctx,
std::string oid,
rados::cls::lock::Lock lock,
bool ephemeral)
: ex(std::move(ex)),
ioctx(std::move(ioctx)),
oid(std::move(oid)),
lock(std::move(lock)),
ephemeral(ephemeral)
{}
private:
executor_type ex;
librados::IoCtx ioctx;
std::string oid;
rados::cls::lock::Lock lock;
bool ephemeral = false;
void acquire(ceph::timespan dur, Handler h) override {
librados::ObjectWriteOperation op;
lock.set_duration(dur);
if (ephemeral) {
lock.lock_exclusive_ephemeral(&op);
} else {
lock.lock_exclusive(&op);
}
librados::async_operate(ex, ioctx, oid, std::move(op), 0, nullptr,
librados::redirect_version(std::move(h)));
}
void renew(ceph::timespan dur, Handler h) override {
librados::ObjectWriteOperation op;
op.assert_exists();
lock.set_must_renew(true);
lock.set_duration(dur);
if (ephemeral) {
lock.lock_exclusive_ephemeral(&op);
} else {
lock.lock_exclusive(&op);
}
librados::async_operate(ex, ioctx, oid, std::move(op), 0, nullptr,
librados::redirect_version(std::move(h)));
}
void release(Handler h) override {
librados::ObjectWriteOperation op;
op.assert_exists();
lock.unlock(&op);
librados::async_operate(ex, ioctx, oid, std::move(op), 0, nullptr,
librados::redirect_version(std::move(h)));
}
};
} // namespace ceph::async

View File

@ -407,7 +407,7 @@ target_link_libraries(unittest_async_spawn_throttle ceph-common Boost::boost Boo
add_executable(unittest_async_lease test_async_lease.cc)
add_ceph_unittest(unittest_async_lease)
target_link_libraries(unittest_async_lease ceph-common Boost::system Boost::context)
target_link_libraries(unittest_async_lease ceph-common Boost::system Boost::context librados)
add_executable(unittest_async_yield_waiter test_async_yield_waiter.cc)
add_ceph_unittest(unittest_async_yield_waiter)

View File

@ -12,6 +12,7 @@
//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING
#include "common/async/lease.h"
#include "common/async/lock_rados.h"
#include <optional>
#include <utility>