From 736b758b135ca438f068f38353c26b751d6ecf2a Mon Sep 17 00:00:00 2001 From: Yuan Zhou Date: Thu, 20 Dec 2018 18:30:19 +0800 Subject: [PATCH] tools: create local utils for object cache daemon Signed-off-by: Yuan Zhou --- .../immutable_object_cache/CMakeLists.txt | 5 +- .../immutable_object_cache/ObjectCacheFile.cc | 1 - .../ObjectCacheStore.cc | 8 +- src/tools/immutable_object_cache/Utils.cc | 18 +++ src/tools/immutable_object_cache/Utils.h | 113 ++++++++++++++++++ 5 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 src/tools/immutable_object_cache/Utils.cc create mode 100644 src/tools/immutable_object_cache/Utils.h diff --git a/src/tools/immutable_object_cache/CMakeLists.txt b/src/tools/immutable_object_cache/CMakeLists.txt index cf0decf6962..4358b90216a 100644 --- a/src/tools/immutable_object_cache/CMakeLists.txt +++ b/src/tools/immutable_object_cache/CMakeLists.txt @@ -5,7 +5,9 @@ set(ceph_immutable_object_cache_files CacheServer.cc CacheClient.cc CacheSession.cc - SimplePolicy.cc) + SimplePolicy.cc + Utils.cc + ) add_library(ceph_immutable_object_cache_lib STATIC ${ceph_immutable_object_cache_files}) add_executable(ceph-immutable-object-cache @@ -15,6 +17,7 @@ add_executable(ceph-immutable-object-cache CacheServer.cc CacheSession.cc SimplePolicy.cc + Utils.cc main.cc) target_link_libraries(ceph-immutable-object-cache librados-cxx diff --git a/src/tools/immutable_object_cache/ObjectCacheFile.cc b/src/tools/immutable_object_cache/ObjectCacheFile.cc index 538a59edc53..750a2249e0c 100644 --- a/src/tools/immutable_object_cache/ObjectCacheFile.cc +++ b/src/tools/immutable_object_cache/ObjectCacheFile.cc @@ -5,7 +5,6 @@ #include "include/Context.h" #include "common/dout.h" #include "common/WorkQueue.h" -#include "librbd/ImageCtx.h" #include #include #include diff --git a/src/tools/immutable_object_cache/ObjectCacheStore.cc b/src/tools/immutable_object_cache/ObjectCacheStore.cc index 83ad6ad53b0..7894266bdfd 100644 --- a/src/tools/immutable_object_cache/ObjectCacheStore.cc +++ b/src/tools/immutable_object_cache/ObjectCacheStore.cc @@ -1,9 +1,9 @@ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab +#include "include/Context.h" #include "ObjectCacheStore.h" -#include "librbd/Utils.h" - +#include "Utils.h" #include #define dout_context g_ceph_context @@ -217,10 +217,10 @@ int ObjectCacheStore::promote_object(librados::IoCtx* ioctx, std::string object_ on_finish->complete(ret); }); - librados::AioCompletion* read_completion = librbd::util::create_rados_callback(ctx); + librados::AioCompletion* read_completion = create_rados_callback(ctx); int ret = ioctx->aio_read(object_name, read_completion, read_buf, read_len, 0); if(ret < 0) { - lderr(m_cct) << "fail to read from rados" << dendl; + lderr(m_cct) << "failed to read from rados" << dendl; } read_completion->release(); diff --git a/src/tools/immutable_object_cache/Utils.cc b/src/tools/immutable_object_cache/Utils.cc new file mode 100644 index 00000000000..28b9b0b1d10 --- /dev/null +++ b/src/tools/immutable_object_cache/Utils.cc @@ -0,0 +1,18 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "Utils.h" + +#define dout_subsys ceph_subsys_immutable_obj_cache +#undef dout_prefix +#define dout_prefix *_dout << "ceph::cache::Utils: " << __func__ << ": " + +namespace ceph { +namespace immutable_obj_cache { + +librados::AioCompletion *create_rados_callback(Context *on_finish) { + return create_rados_callback(on_finish); +} + +} // namespace immutable_obj_cache +} // namespace ceph diff --git a/src/tools/immutable_object_cache/Utils.h b/src/tools/immutable_object_cache/Utils.h new file mode 100644 index 00000000000..d3fb8d92cd4 --- /dev/null +++ b/src/tools/immutable_object_cache/Utils.h @@ -0,0 +1,113 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#ifndef CEPH_CACHE_UTILS_H +#define CEPH_CACHE_UTILS_H + +#include "include/rados/librados.hpp" +#include "include/Context.h" + + +namespace ceph { +namespace immutable_obj_cache { +namespace detail { + +template +void rados_callback(rados_completion_t c, void *arg) { + reinterpret_cast(arg)->complete(rados_aio_get_return_value(c)); +} + +template +void rados_callback(rados_completion_t c, void *arg) { + T *obj = reinterpret_cast(arg); + int r = rados_aio_get_return_value(c); + (obj->*MF)(r); +} + +template +void rados_state_callback(rados_completion_t c, void *arg) { + T *obj = reinterpret_cast(arg); + int r = rados_aio_get_return_value(c); + Context *on_finish = (obj->*MF)(&r); + if (on_finish != nullptr) { + on_finish->complete(r); + if (destroy) { + delete obj; + } + } +} + +template +class C_CallbackAdapter : public Context { + T *obj; +public: + C_CallbackAdapter(T *obj) : obj(obj) { + } + +protected: + void finish(int r) override { + (obj->*MF)(r); + } +}; + +template +class C_StateCallbackAdapter : public Context { + T *obj; +public: + C_StateCallbackAdapter(T *obj) : obj(obj){ + } + +protected: + void complete(int r) override { + Context *on_finish = (obj->*MF)(&r); + if (on_finish != nullptr) { + on_finish->complete(r); + if (destroy) { + delete obj; + } + } + Context::complete(r); + } + void finish(int r) override { + } +}; + +template +struct C_AsyncCallback : public Context { + WQ *op_work_queue; + Context *on_finish; + + C_AsyncCallback(WQ *op_work_queue, Context *on_finish) + : op_work_queue(op_work_queue), on_finish(on_finish) { + } + void finish(int r) override { + op_work_queue->queue(on_finish, r); + } +}; + +} // namespace detail + + +librados::AioCompletion *create_rados_callback(Context *on_finish); + +template +librados::AioCompletion *create_rados_callback(T *obj) { + return librados::Rados::aio_create_completion( + obj, &detail::rados_callback, nullptr); +} + +template +librados::AioCompletion *create_rados_callback(T *obj) { + return librados::Rados::aio_create_completion( + obj, &detail::rados_callback, nullptr); +} + +template +librados::AioCompletion *create_rados_callback(T *obj) { + return librados::Rados::aio_create_completion( + obj, &detail::rados_state_callback, nullptr); +} + +} // namespace immutable_obj_cache +} // namespace ceph +#endif -- 2.39.5