]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librads, libceph: store CephContext
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 23 May 2011 21:02:15 +0000 (14:02 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 24 May 2011 00:28:32 +0000 (17:28 -0700)
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 <colin.mccabe@dreamhost.com>
src/include/rados/librados.h
src/include/rados/librados.hpp
src/libceph.cc
src/librados.cc
src/rados.cc
src/rados_sync.cc
src/rbd.cc
src/rgw/rgw_rados.cc
src/rgw/rgw_rados.h
src/test/test_mutate.cc

index fdf14e9885080198fdbc0f7e87addfaedb5bfa6b..1d26fdef62456c8609494da422b63391d1c9ccfa 100644 (file)
@@ -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);
index 5a9b79da8ed68695a3cc6eda9e29a3be4b809786..e5e2061edf3698f9ec5aecfdcddc348f96a57083 100644 (file)
@@ -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;
index c184158fdacccf93973771643400754d420d6a5d..e8ff17ee91acaebb2c62c68594e25a38e9f4026f 100644 (file)
@@ -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;
index 697d279779d5c1d47e1912e026caa8e4ed9780c9..d3290d30acce6ea63186bf12d6ce89b18361a770 100644 (file)
@@ -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<std::string> conf_files;
   get_str_list(path, conf_files);
   std::deque<std::string> 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)
index caf51485bc1edf5699ed3fd1a810808abb11eef5..e262c6a74248c7f850df95fe4d9dc67ac5ae4910 100644 (file)
@@ -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;
index 6d0261b5477ac6ab2e54fb6eae61af710ee36316..950571067d14718a4b1aceb24e8da2bbaa36c351 100644 (file)
@@ -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);
   }
index 595bea77a4b707125cc329a701713d5f846cd183..98fd811eb2a87ea40c1feb8aa3f05342a56db720 100644 (file)
@@ -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);
   }
index 335df494d3c8c89388cd34a30a5a9f6afa9f0618..525665984a3c09cb4f31172c29552118174f1c5f 100644 (file)
@@ -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;
 
index ffb06e61292d400fbfa3aae7a211081adea738e3..790c7c3b7b98341a5fa851db6b03febfa3b6f30d 100644 (file)
@@ -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);
   /** 
index 5d555292e04b9ba400470f56d3b1e11c61fec4a1..b1b8f924ae9f9665ff9964f52dda1845ad20de38 100644 (file)
@@ -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;
   }