From: Yehuda Sadeh Date: Fri, 10 Aug 2018 00:04:48 +0000 (-0700) Subject: rgw: basic rgw services registry X-Git-Tag: v14.1.0~965^2~45 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=add60c82eaa1d9e8b247ae927fae4f9a132c9172;p=ceph.git rgw: basic rgw services registry Signed-off-by: Yehuda Sadeh --- diff --git a/src/rgw/CMakeLists.txt b/src/rgw/CMakeLists.txt index 939282733c22..fa3a4d3c5a1c 100644 --- a/src/rgw/CMakeLists.txt +++ b/src/rgw/CMakeLists.txt @@ -39,6 +39,8 @@ function(gperf_generate input output) endfunction() set(librgw_common_srcs + services/svc_rados.cc + rgw_service.cc rgw_acl.cc rgw_acl_s3.cc rgw_acl_swift.cc @@ -117,6 +119,8 @@ set(librgw_common_srcs add_library(rgw_common OBJECT ${librgw_common_srcs}) +target_include_directories(rgw_common SYSTEM PUBLIC "services") + if(WITH_LTTNG) # rgw/rgw_op.cc includes "tracing/rgw_op.h" # rgw/rgw_rados.cc includes "tracing/rgw_rados.h" diff --git a/src/rgw/rgw_service.cc b/src/rgw/rgw_service.cc new file mode 100644 index 000000000000..5a4da5903b04 --- /dev/null +++ b/src/rgw/rgw_service.cc @@ -0,0 +1,61 @@ +#include "rgw_service.h" + +#include "services/svc_rados.h" + + + +RGWServiceInstance::~RGWServiceInstance() +{ + if (svc) { + svc->svc_registry->remove_instance(this); + } +} + +void RGWServiceRegistry::register_all(CephContext *cct) +{ + services["rados"] = make_shared(cct); +} + +bool RGWServiceRegistry::find(const string& name, RGWServiceRef *svc) +{ + auto iter = services.find(name); + if (iter == services.end()) { + return false; + } + + *svc = iter->second; + return true; +} + +int RGWServiceRegistry::instantiate(RGWServiceRegistryRef& registry, RGWServiceRef& svc, JSONFormattable& conf) { + auto self_ref = shared_from_this(); + RGWServiceInstanceRef instance_ref; + int r = svc->create_instance(conf, &instance_ref); + if (r < 0) { + return r; + } + instance_ref->svc = svc; + instance_ref->svc_id = ++max_registry_id; + + r = instance_ref->init(conf); + if (r < 0) { + return r; + } + + if (instance_ref->svc_instance.empty()) { + char buf[32]; + snprintf(buf, sizeof(buf), "%lld", (long long)instance_ref->svc_id); + instance_ref->svc_instance = buf; + } + + instance_info& iinfo = instances[instance_ref->svc_id]; + iinfo.id = instance_ref->svc_id; + iinfo.title = instance_ref->get_title(); + iinfo.conf = conf; + + return 0; +} + +void RGWServiceRegistry::remove_instance(RGWServiceInstance *instance) { + instances.erase(instance->svc_id); +} diff --git a/src/rgw/rgw_service.h b/src/rgw/rgw_service.h new file mode 100644 index 000000000000..04022b358453 --- /dev/null +++ b/src/rgw/rgw_service.h @@ -0,0 +1,90 @@ +#ifndef CEPH_RGW_SERVICE_H +#define CEPH_RGW_SERVICE_H + + +#include +#include +#include + +#include "rgw/rgw_common.h" + + +class CephContext; +class JSONFormattable; +class RGWServiceInstance; +class RGWServiceRegistry; + +using RGWServiceInstanceRef = std::shared_ptr; +using RGWServiceRegistryRef = std::shared_ptr; + +class RGWService +{ + friend class RGWServiceRegistry; + friend class RGWServiceInstance; + +protected: + RGWServiceRegistryRef svc_registry; + CephContext *cct; + std::string svc_type; + +public: + RGWService(CephContext *_cct, const std::string& _svc_type) : cct(_cct), + svc_type(_svc_type) {} + virtual ~RGWService() = default; + + const std::string& type() { + return svc_type; + } + virtual std::vector deps() = 0; + virtual int create_instance(JSONFormattable& conf, RGWServiceInstanceRef *instance) = 0; +}; + + +using RGWServiceRef = std::shared_ptr; + + +class RGWServiceInstance +{ + friend class RGWServiceRegistry; +protected: + CephContext *cct; + std::shared_ptr svc; + string svc_instance; + uint64_t svc_id{0}; + +public: + RGWServiceInstance(RGWService *svc, CephContext *_cct) : cct(_cct) {} + + virtual ~RGWServiceInstance(); + virtual int init(JSONFormattable& conf) = 0; + + string get_title() { + return svc->type() + ":" + svc_instance; + } +}; + +class RGWServiceRegistry : std::enable_shared_from_this { + map services; + + struct instance_info { + uint64_t id; + string title; + JSONFormattable conf; + RGWServiceInstanceRef ref; + }; + map instances; /* registry_id -> instance */ + + std::atomic max_registry_id; + + void register_all(CephContext *cct); +public: + RGWServiceRegistry(CephContext *cct) { + register_all(cct); + } + bool find(const string& name, RGWServiceRef *svc); + + int instantiate(RGWServiceRegistryRef& registry, RGWServiceRef& svc, JSONFormattable& conf); + void remove_instance(RGWServiceInstance *instance); +}; + +#endif diff --git a/src/rgw/services/svc_rados.cc b/src/rgw/services/svc_rados.cc new file mode 100644 index 000000000000..fed1e20fbe52 --- /dev/null +++ b/src/rgw/services/svc_rados.cc @@ -0,0 +1 @@ +#include "svc_rados.h" diff --git a/src/rgw/services/svc_rados.h b/src/rgw/services/svc_rados.h new file mode 100644 index 000000000000..a53efdda0786 --- /dev/null +++ b/src/rgw/services/svc_rados.h @@ -0,0 +1,18 @@ +#ifndef CEPH_RGW_SERVICES_ZONE_H +#define CEPH_RGW_SERVICES_ZONE_H + + +#include "rgw/rgw_service.h" + + +class RGWS_RADOS : public RGWService +{ +public: + RGWS_RADOS(CephContext *cct) : RGWService(cct, "rados") {} + + std::vector deps(); + int create_instance(JSONFormattable& conf, RGWServiceInstanceRef *instance); +}; + + +#endif