From: Colin Patrick McCabe Date: Mon, 23 May 2011 21:02:15 +0000 (-0700) Subject: librads, libceph: store CephContext X-Git-Tag: v0.29~42^2~2^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9a660ac9105c3a27ab23d854c9a8eb4451451fa1;p=ceph.git librads, libceph: store CephContext Don't use the global g_ceph_context. Instead, store the CephContext in the structures provided by the library user. Signed-off-by: Colin McCabe --- diff --git a/src/include/rados/librados.h b/src/include/rados/librados.h index fdf14e98850..1d26fdef624 100644 --- a/src/include/rados/librados.h +++ b/src/include/rados/librados.h @@ -26,7 +26,7 @@ extern "C" { #define LIBRADOS_SUPPORTS_WATCH 1 -struct md_config_t; +struct CephContext; typedef void *rados_t; typedef void *rados_ioctx_t; @@ -57,7 +57,7 @@ void rados_version(int *major, int *minor, int *extra); int rados_create(rados_t *cluster, const char * const id); /* initialize rados with an existing configuration. */ -int rados_create_with_config(rados_t *cluster, struct md_config_t *conf); +int rados_create_with_context(rados_t *cluster, struct CephContext *cct_); /* Connect to the cluster */ int rados_connect(rados_t cluster); diff --git a/src/include/rados/librados.hpp b/src/include/rados/librados.hpp index 5a9b79da8ed..e5e2061edf3 100644 --- a/src/include/rados/librados.hpp +++ b/src/include/rados/librados.hpp @@ -11,7 +11,7 @@ #include "librados.h" -struct md_config_t; +class CephContext; namespace librados { @@ -250,7 +250,7 @@ namespace librados ~Rados(); int init(const char * const id); - int init_with_config(md_config_t * conf); + int init_with_context(CephContext *cct_); int connect(); void shutdown(); int conf_read_file(const char * const path) const; diff --git a/src/libceph.cc b/src/libceph.cc index c184158fdac..e8ff17ee91a 100644 --- a/src/libceph.cc +++ b/src/libceph.cc @@ -184,6 +184,10 @@ public: return cwd.c_str(); } + CephContext *get_ceph_context() const { + return cct; + } + private: uint64_t msgr_nonce; bool mounted; @@ -280,7 +284,7 @@ extern "C" int ceph_mount(struct ceph_mount_info *cmount, const char *root) { std::string mount_root; - keyring_init(&g_ceph_context); + keyring_init(cmount->get_ceph_context()); if (root) mount_root = root; diff --git a/src/librados.cc b/src/librados.cc index 697d279779d..d3290d30acc 100644 --- a/src/librados.cc +++ b/src/librados.cc @@ -89,10 +89,7 @@ struct librados::IoCtxImpl { object_locator_t oloc; IoCtxImpl() {} - IoCtxImpl(RadosClient *c, int pid, const char *pool_name_, snapid_t s = CEPH_NOSNAP) : - ref_cnt(0), client(c), poolid(pid), - pool_name(pool_name_), snap_seq(s), assert_ver(0), - notify_timeout(g_conf.client_notify_timeout), oloc(pid) {} + IoCtxImpl(RadosClient *c, int pid, const char *pool_name_, snapid_t s); void dup(const IoCtxImpl& rhs) { // Copy everything except the ref count @@ -320,6 +317,10 @@ struct librados::ObjListCtx { class librados::RadosClient : public Dispatcher { +public: + CephContext *cct; + md_config_t *conf; +private: enum { DISCONNECTED, CONNECTING, @@ -353,8 +354,8 @@ class librados::RadosClient : public Dispatcher SafeTimer timer; public: - RadosClient() : state(DISCONNECTED), - messenger(NULL), objecter(NULL), + RadosClient(CephContext *cct_) : cct(cct_), conf(cct_->_conf), + state(DISCONNECTED), messenger(NULL), objecter(NULL), lock("radosclient"), timer(lock), max_watch_cookie(0) { } @@ -627,6 +628,13 @@ public: } }; +librados::IoCtxImpl:: +IoCtxImpl(RadosClient *c, int pid, const char *pool_name_, snapid_t s) + : ref_cnt(0), client(c), poolid(pid), + pool_name(pool_name_), snap_seq(s), assert_ver(0), + notify_timeout(c->conf->client_notify_timeout), oloc(pid) +{} + int librados::RadosClient:: connect() { @@ -676,9 +684,9 @@ connect() dout(1) << "calling monclient init" << dendl; monclient.init(); - err = monclient.authenticate(g_conf.client_mount_timeout); + err = monclient.authenticate(conf->client_mount_timeout); if (err) { - dout(0) << g_conf.name << " authentication error " << strerror(-err) << dendl; + dout(0) << conf->name << " authentication error " << strerror(-err) << dendl; shutdown(); goto out; } @@ -2630,15 +2638,15 @@ init(const char * const id) } int librados::Rados:: -init_with_config(md_config_t *conf) +init_with_context(CephContext *cct_) { - return rados_create_with_config((rados_t *)&client, conf); + return rados_create_with_context((rados_t *)&client, cct_); } int librados::Rados:: connect() { - int ret = keyring_init(&g_ceph_context); + int ret = keyring_init(client->cct); if (ret) return ret; return client->connect(); @@ -2670,7 +2678,8 @@ int librados::Rados:: conf_get(const char *option, std::string &val) { char *str; - int ret = g_conf.get_val(option, &str, -1); + md_config_t *conf = client->cct->_conf; + int ret = conf->get_val(option, &str, -1); if (ret) return ret; val = str; @@ -2799,6 +2808,8 @@ static int rados_initialized = 0; extern "C" int rados_create(rados_t *pcluster, const char * const id) { rados_init_mutex.Lock(); + CephContext *cct_; + // FIXME: get rid of this mutex and the reference to the global if (!rados_initialized) { CephInitParameters iparams(CEPH_ENTITY_TYPE_CLIENT, CEPH_CONF_FILE_DEFAULT); iparams.conf_file = ""; @@ -2806,16 +2817,17 @@ extern "C" int rados_create(rados_t *pcluster, const char * const id) iparams.name.set(CEPH_ENTITY_TYPE_CLIENT, id); } - // TODO: store this conf pointer in the RadosClient and use it as our - // configuration - CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0); - cct->_conf->parse_env(); // environment variables override - cct->_conf->apply_changes(); + cct_ = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0); + cct_->_conf->parse_env(); // environment variables override + cct_->_conf->apply_changes(); ++rados_initialized; } + else { + cct_ = &g_ceph_context; + } rados_init_mutex.Unlock(); - librados::RadosClient *radosp = new librados::RadosClient; + librados::RadosClient *radosp = new librados::RadosClient(cct_); *pcluster = (void *)radosp; return 0; } @@ -2824,27 +2836,27 @@ extern "C" int rados_create(rados_t *pcluster, const char * const id) * already called common_init and want to use that particular configuration for * their cluster. */ -extern "C" int rados_create_with_config(rados_t *pcluster, md_config_t *conf) +extern "C" int rados_create_with_context(rados_t *pcluster, CephContext *cct_) { rados_init_mutex.Lock(); if (!rados_initialized) { ++rados_initialized; - /* This is a no-op now. g_conf is still global and we can't actually do - * anything useful with the provided conf pointer. + /* This is a no-op now. g_ceph_context is still global in libcommon and we + * can't actually do anything useful with the provided conf pointer. */ } rados_init_mutex.Unlock(); - librados::RadosClient *radosp = new librados::RadosClient; + librados::RadosClient *radosp = new librados::RadosClient(cct_); *pcluster = (void *)radosp; return 0; } extern "C" int rados_connect(rados_t cluster) { - int ret = keyring_init(&g_ceph_context); + librados::RadosClient *radosp = (librados::RadosClient *)cluster; + int ret = keyring_init(radosp->cct); if (ret) return ret; - librados::RadosClient *radosp = (librados::RadosClient *)cluster; return radosp->connect(); } @@ -2873,25 +2885,29 @@ extern "C" int rados_conf_read_file(rados_t cluster, const char *path) if (!path) path = CEPH_CONF_FILE_DEFAULT; + librados::RadosClient *client = (librados::RadosClient *)cluster; + md_config_t *conf = client->cct->_conf; std::list conf_files; get_str_list(path, conf_files); std::deque parse_errors; - int ret = g_conf.parse_config_files(conf_files, &parse_errors); + int ret = conf->parse_config_files(conf_files, &parse_errors); if (ret) return ret; - g_conf.parse_env(); // environment variables override + conf->parse_env(); // environment variables override - g_conf.apply_changes(); + conf->apply_changes(); complain_about_parse_errors(&parse_errors); return 0; } extern "C" int rados_conf_set(rados_t cluster, const char *option, const char *value) { - int ret = g_conf.set_val(option, value); + librados::RadosClient *client = (librados::RadosClient *)cluster; + md_config_t *conf = client->cct->_conf; + int ret = conf->set_val(option, value); if (ret) return ret; - g_conf.apply_changes(); + conf->apply_changes(); return 0; } @@ -2912,7 +2928,9 @@ extern "C" int rados_cluster_stat(rados_t cluster, rados_cluster_stat_t *result) extern "C" int rados_conf_get(rados_t cluster, const char *option, char *buf, size_t len) { char *tmp = buf; - return g_conf.get_val(option, &tmp, len); + librados::RadosClient *client = (librados::RadosClient *)cluster; + md_config_t *conf = client->cct->_conf; + return conf->get_val(option, &tmp, len); } extern "C" int rados_pool_lookup(rados_t cluster, const char *name) diff --git a/src/rados.cc b/src/rados.cc index caf51485bc1..e262c6a7424 100644 --- a/src/rados.cc +++ b/src/rados.cc @@ -203,7 +203,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, // open rados Rados rados; - ret = rados.init_with_config(&g_conf); + ret = rados.init_with_context(&g_ceph_context); if (ret) { cerr << "couldn't initialize rados! error " << ret << std::endl; return ret; diff --git a/src/rados_sync.cc b/src/rados_sync.cc index 6d0261b5477..950571067d1 100644 --- a/src/rados_sync.cc +++ b/src/rados_sync.cc @@ -985,7 +985,7 @@ int rados_tool_sync(const std::map < std::string, std::string > &opts, // open rados Rados rados; - if (rados.init_with_config(&g_conf) < 0) { + if (rados.init_with_context(&g_ceph_context) < 0) { cerr << "rados" << ": failed to initialize Rados!" << std::endl; exit(1); } diff --git a/src/rbd.cc b/src/rbd.cc index 595bea77a4b..98fd811eb2a 100644 --- a/src/rbd.cc +++ b/src/rbd.cc @@ -942,7 +942,7 @@ int main(int argc, const char **argv) } bool talk_to_cluster = (opt_cmd != OPT_MAP && opt_cmd != OPT_UNMAP); - if (talk_to_cluster && rados.init_with_config(&g_conf) < 0) { + if (talk_to_cluster && rados.init_with_context(&g_ceph_context) < 0) { cerr << "error: couldn't initialize rados!" << std::endl; exit(1); } diff --git a/src/rgw/rgw_rados.cc b/src/rgw/rgw_rados.cc index 335df494d3c..525665984a3 100644 --- a/src/rgw/rgw_rados.cc +++ b/src/rgw/rgw_rados.cc @@ -23,14 +23,14 @@ static librados::IoCtx root_pool_ctx; * Initialize the RADOS instance and prepare to do other ops * Returns 0 on success, -ERR# on failure. */ -int RGWRados::initialize(md_config_t *conf) +int RGWRados::initialize(CephContext *cct) { int ret; rados = new Rados(); if (!rados) return -ENOMEM; - ret = rados->init_with_config(conf); + ret = rados->init_with_context(cct); if (ret < 0) return ret; diff --git a/src/rgw/rgw_rados.h b/src/rgw/rgw_rados.h index ffb06e61292..790c7c3b7b9 100644 --- a/src/rgw/rgw_rados.h +++ b/src/rgw/rgw_rados.h @@ -21,7 +21,7 @@ class RGWRados : public RGWAccess public: /** Initialize the RADOS instance and prepare to do other ops */ - virtual int initialize(md_config_t *conf); + virtual int initialize(CephContext *cct); /** set up a bucket listing. id is ignored, handle is filled in. */ virtual int list_buckets_init(std::string& id, RGWAccessHandle *handle); /** diff --git a/src/test/test_mutate.cc b/src/test/test_mutate.cc index 5d555292e04..b1b8f924ae9 100644 --- a/src/test/test_mutate.cc +++ b/src/test/test_mutate.cc @@ -67,7 +67,7 @@ int main(int argc, const char **argv) } Rados rados; - if (rados.init_with_config(&g_conf) < 0) { + if (rados.init_with_context(&g_ceph_context) < 0) { cerr << "couldn't initialize rados!" << std::endl; return 1; }