From f59f0d49551fdfc0620cc3481909189441ce8edf Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Mon, 17 Oct 2016 22:44:55 -0400 Subject: [PATCH] librbd: move journal tag decode helpers to shared location Signed-off-by: Jason Dillaman --- src/librbd/CMakeLists.txt | 7 +- src/librbd/Journal.cc | 123 ++---------------------------------- src/librbd/journal/Utils.cc | 85 +++++++++++++++++++++++++ src/librbd/journal/Utils.h | 81 ++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 120 deletions(-) create mode 100644 src/librbd/journal/Utils.cc create mode 100644 src/librbd/journal/Utils.h diff --git a/src/librbd/CMakeLists.txt b/src/librbd/CMakeLists.txt index 68f3f461c7e1a..7f2d863c18fa7 100644 --- a/src/librbd/CMakeLists.txt +++ b/src/librbd/CMakeLists.txt @@ -1,6 +1,6 @@ add_library(rbd_types STATIC journal/Types.cc - mirroring_watcher/Types.cc + mirroring_watcher/Types.cc WatchNotifyTypes.cc) set(librbd_internal_srcs @@ -22,9 +22,9 @@ set(librbd_internal_srcs Journal.cc LibrbdAdminSocketHook.cc LibrbdWriteback.cc - MirroringWatcher.cc + MirroringWatcher.cc ObjectMap.cc - ObjectWatcher.cc + ObjectWatcher.cc Operations.cc Utils.cc cache/ImageWriteback.cc @@ -47,6 +47,7 @@ set(librbd_internal_srcs journal/CreateRequest.cc journal/Replay.cc journal/StandardPolicy.cc + journal/Utils.cc mirror/DisableRequest.cc mirror/EnableRequest.cc object_map/CreateRequest.cc diff --git a/src/librbd/Journal.cc b/src/librbd/Journal.cc index a0ebe8f5a8f74..999c26b9a9510 100644 --- a/src/librbd/Journal.cc +++ b/src/librbd/Journal.cc @@ -12,6 +12,7 @@ #include "journal/Policy.h" #include "journal/ReplayEntry.h" #include "journal/Settings.h" +#include "journal/Utils.h" #include "common/errno.h" #include "common/Timer.h" #include "common/WorkQueue.h" @@ -28,121 +29,12 @@ namespace librbd { -namespace { - -struct C_DecodeTag : public Context { - CephContext *cct; - Mutex *lock; - uint64_t *tag_tid; - journal::TagData *tag_data; - Context *on_finish; - - cls::journal::Tag tag; - - C_DecodeTag(CephContext *cct, Mutex *lock, uint64_t *tag_tid, - journal::TagData *tag_data, Context *on_finish) - : cct(cct), lock(lock), tag_tid(tag_tid), tag_data(tag_data), - on_finish(on_finish) { - } - - virtual void complete(int r) override { - on_finish->complete(process(r)); - Context::complete(0); - } - virtual void finish(int r) override { - } - - int process(int r) { - if (r < 0) { - lderr(cct) << this << " " << __func__ << ": " - << "failed to allocate tag: " << cpp_strerror(r) << dendl; - return r; - } - - Mutex::Locker locker(*lock); - *tag_tid = tag.tid; - - bufferlist::iterator data_it = tag.data.begin(); - r = decode(&data_it, tag_data); - if (r < 0) { - lderr(cct) << this << " " << __func__ << ": " - << "failed to decode allocated tag" << dendl; - return r; - } - - ldout(cct, 20) << this << " " << __func__ << ": " - << "allocated journal tag: " - << "tid=" << tag.tid << ", " - << "data=" << *tag_data << dendl; - return 0; - } - - static int decode(bufferlist::iterator *it, - journal::TagData *tag_data) { - try { - ::decode(*tag_data, *it); - } catch (const buffer::error &err) { - return -EBADMSG; - } - return 0; - } - -}; - -struct C_DecodeTags : public Context { - CephContext *cct; - Mutex *lock; - uint64_t *tag_tid; - journal::TagData *tag_data; - Context *on_finish; - - ::journal::Journaler::Tags tags; - - C_DecodeTags(CephContext *cct, Mutex *lock, uint64_t *tag_tid, - journal::TagData *tag_data, Context *on_finish) - : cct(cct), lock(lock), tag_tid(tag_tid), tag_data(tag_data), - on_finish(on_finish) { - } - - virtual void complete(int r) { - on_finish->complete(process(r)); - Context::complete(0); - } - virtual void finish(int r) override { - } - - int process(int r) { - if (r < 0) { - lderr(cct) << this << " " << __func__ << ": " - << "failed to retrieve journal tags: " << cpp_strerror(r) - << dendl; - return r; - } - - if (tags.empty()) { - lderr(cct) << this << " " << __func__ << ": " - << "no journal tags retrieved" << dendl; - return -ENOENT; - } - - Mutex::Locker locker(*lock); - *tag_tid = tags.back().tid; - - bufferlist::iterator data_it = tags.back().data.begin(); - r = C_DecodeTag::decode(&data_it, tag_data); - if (r < 0) { - lderr(cct) << this << " " << __func__ << ": " - << "failed to decode journal tag" << dendl; - return r; - } +using util::create_async_context_callback; +using util::create_context_callback; +using journal::util::C_DecodeTag; +using journal::util::C_DecodeTags; - ldout(cct, 20) << this << " " << __func__ << ": " - << "most recent journal tag: " - << "tid=" << *tag_tid << ", " - << "data=" << *tag_data << dendl; - return 0; - } -}; +namespace { // TODO: once journaler is 100% async, remove separate threads and // reuse ImageCtx's thread pool @@ -391,9 +283,6 @@ int allocate_journaler_tag(CephContext *cct, J *journaler, } // anonymous namespace -using util::create_async_context_callback; -using util::create_context_callback; - // client id for local image template const std::string Journal::IMAGE_CLIENT_ID(""); diff --git a/src/librbd/journal/Utils.cc b/src/librbd/journal/Utils.cc new file mode 100644 index 0000000000000..e1888f0f74f7e --- /dev/null +++ b/src/librbd/journal/Utils.cc @@ -0,0 +1,85 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "librbd/journal/Utils.h" +#include "common/dout.h" +#include "common/errno.h" +#include "librbd/journal/Types.h" + +#define dout_subsys ceph_subsys_rbd +#undef dout_prefix +#define dout_prefix *_dout << "librbd::journal::" + +namespace librbd { +namespace journal { +namespace util { + +int C_DecodeTag::decode(bufferlist::iterator *it, TagData *tag_data) { + try { + ::decode(*tag_data, *it); + } catch (const buffer::error &err) { + return -EBADMSG; + } + return 0; +} + +int C_DecodeTag::process(int r) { + if (r < 0) { + lderr(cct) << "C_DecodeTag: " << this << " " << __func__ << ": " + << "failed to allocate tag: " << cpp_strerror(r) + << dendl; + return r; + } + + Mutex::Locker locker(*lock); + *tag_tid = tag.tid; + + bufferlist::iterator data_it = tag.data.begin(); + r = decode(&data_it, tag_data); + if (r < 0) { + lderr(cct) << "C_DecodeTag: " << this << " " << __func__ << ": " + << "failed to decode allocated tag" << dendl; + return r; + } + + ldout(cct, 20) << "C_DecodeTag: " << this << " " << __func__ << ": " + << "allocated journal tag: " + << "tid=" << tag.tid << ", " + << "data=" << *tag_data << dendl; + return 0; +} + +int C_DecodeTags::process(int r) { + if (r < 0) { + lderr(cct) << "C_DecodeTags: " << this << " " << __func__ << ": " + << "failed to retrieve journal tags: " << cpp_strerror(r) + << dendl; + return r; + } + + if (tags.empty()) { + lderr(cct) << "C_DecodeTags: " << this << " " << __func__ << ": " + << "no journal tags retrieved" << dendl; + return -ENOENT; + } + + Mutex::Locker locker(*lock); + *tag_tid = tags.back().tid; + bufferlist::iterator data_it = tags.back().data.begin(); + r = C_DecodeTag::decode(&data_it, tag_data); + if (r < 0) { + lderr(cct) << "C_DecodeTags: " << this << " " << __func__ << ": " + << "failed to decode journal tag" << dendl; + return r; + } + + ldout(cct, 20) << "C_DecodeTags: " << this << " " << __func__ << ": " + << "most recent journal tag: " + << "tid=" << *tag_tid << ", " + << "data=" << *tag_data << dendl; + return 0; +} + +} // namespace util +} // namespace journal +} // namespace librbd diff --git a/src/librbd/journal/Utils.h b/src/librbd/journal/Utils.h new file mode 100644 index 0000000000000..7ce4852697969 --- /dev/null +++ b/src/librbd/journal/Utils.h @@ -0,0 +1,81 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#ifndef CEPH_LIBRBD_JOURNAL_UTILS_H +#define CEPH_LIBRBD_JOURNAL_UTILS_H + +#include "include/int_types.h" +#include "include/Context.h" +#include "cls/journal/cls_journal_types.h" +#include + +struct CephContext; +struct Mutex; + +namespace librbd { +namespace journal { + +struct TagData; + +namespace util { + +struct C_DecodeTag : public Context { + CephContext *cct; + Mutex *lock; + uint64_t *tag_tid; + TagData *tag_data; + Context *on_finish; + + cls::journal::Tag tag; + + C_DecodeTag(CephContext *cct, Mutex *lock, uint64_t *tag_tid, + TagData *tag_data, Context *on_finish) + : cct(cct), lock(lock), tag_tid(tag_tid), tag_data(tag_data), + on_finish(on_finish) { + } + + virtual void complete(int r) override { + on_finish->complete(process(r)); + Context::complete(0); + } + virtual void finish(int r) override { + } + + int process(int r); + + static int decode(bufferlist::iterator *it, TagData *tag_data); + +}; + +struct C_DecodeTags : public Context { + typedef std::list Tags; + + CephContext *cct; + Mutex *lock; + uint64_t *tag_tid; + TagData *tag_data; + Context *on_finish; + + Tags tags; + + C_DecodeTags(CephContext *cct, Mutex *lock, uint64_t *tag_tid, + TagData *tag_data, Context *on_finish) + : cct(cct), lock(lock), tag_tid(tag_tid), tag_data(tag_data), + on_finish(on_finish) { + } + + virtual void complete(int r) { + on_finish->complete(process(r)); + Context::complete(0); + } + virtual void finish(int r) override { + } + + int process(int r); +}; + +} // namespace util +} // namespace journal +} // namespace librbd + +#endif // CEPH_LIBRBD_JOURNAL_UTILS_H -- 2.39.5