From 5f730868525ac370ecc0b55fcbc44f52ec11ebbd Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 23 Feb 2011 04:43:12 -0800 Subject: [PATCH] Splt rados_init into rados_create + rados_connect Splt rados_init into rados_create and rados_connect. The pattern will be for users to call create, set configuration, and then connect. Rename rados_release to rados_destroy, to be more symmetrical with rados_create. You can't reconnect after calling destroy. Don't create the messenger inside the RadosClient constructor. Instead, wait until RadosClient::connect(). Rename rados_conf_apply to rados_reopen_log. Add comment about SIGHUP. Signed-off-by: Colin McCabe --- src/include/rados/librados.h | 17 +++++++++++---- src/librados.cc | 41 ++++++++++++++++-------------------- src/testrados.c | 7 ++++-- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/include/rados/librados.h b/src/include/rados/librados.h index a240881ea40a4..33cd30bfa5718 100644 --- a/src/include/rados/librados.h +++ b/src/include/rados/librados.h @@ -51,8 +51,13 @@ struct rados_statfs_t { void rados_version(int *major, int *minor, int *extra); /* initialization */ -int rados_init(rados_t *cluster); -void rados_release(rados_t cluster); +int rados_create(rados_t *cluster); + +/* Connect to the cluster */ +int rados_connect(rados_t cluster); + +/* destroy the cluster instance */ +void rados_destroy(rados_t cluster); /* Config * @@ -66,8 +71,12 @@ int rados_conf_read_file(rados_t cluster, const char *path); * Returns 0 on success, error code otherwise. */ int rados_conf_set(rados_t cluster, const char *option, const char *value); -/* Applies any configuration changes */ -int rados_conf_apply(void); +/* Reopens the log file. + * You must do this after changing the logging configuration. + * It is also good practice to call this from your SIGHUP signal handler, so that users can send you + * a SIGHUP to reopen the log. + */ +int rados_reopen_log(void); /* Returns a configuration value as a string. * If len is positive, that is the maximum number of bytes we'll write into the diff --git a/src/librados.cc b/src/librados.cc index 569e09b81c049..3fb7dae02a621 100644 --- a/src/librados.cc +++ b/src/librados.cc @@ -79,13 +79,13 @@ class RadosClient : public Dispatcher SafeTimer timer; public: - RadosClient() : messenger(NULL), lock("radosclient"), timer(lock), - max_watch_cookie(0) { - messenger = new SimpleMessenger(); + RadosClient() : messenger(NULL), objecter(NULL), + lock("radosclient"), timer(lock), max_watch_cookie(0) + { } ~RadosClient(); - int init(); + int connect(); void shutdown(); struct PoolCtx { @@ -484,14 +484,14 @@ public: } }; -int RadosClient::init() +int RadosClient::connect() { // get monmap int ret = monclient.build_initial_monmap(); if (ret < 0) return ret; - assert_warn(messenger); + messenger = new SimpleMessenger(); if (!messenger) return -ENOMEM; @@ -1827,8 +1827,7 @@ int Rados::initialize(int argc, const char *argv[]) common_set_defaults(false); common_init(args, "librados", STARTUP_FLAG_INIT_KEYS); - client = new RadosClient(); - return client->init(); + return 0; } void Rados::shutdown() @@ -2354,10 +2353,8 @@ void Rados::set_notify_timeout(pool_t pool, uint32_t timeout) static Mutex rados_init_mutex("rados_init"); static int rados_initialized = 0; -extern "C" int rados_init(rados_t *pcluster) +extern "C" int rados_create(rados_t *pcluster) { - int ret = 0; - rados_init_mutex.Lock(); if (!rados_initialized) { // parse environment @@ -2370,18 +2367,18 @@ extern "C" int rados_init(rados_t *pcluster) ++rados_initialized; } rados_init_mutex.Unlock(); - RadosClient *radosp = new RadosClient; - ret = radosp->init(); - if (ret < 0) { - delete radosp; - } else { - *pcluster = (void *)radosp; - } - return ret; + *pcluster = (void *)radosp; + return 0; } -extern "C" void rados_release(rados_t cluster) +extern "C" int rados_connect(rados_t cluster) +{ + RadosClient *radosp = (RadosClient *)cluster; + return radosp->connect(); +} + +extern "C" void rados_destroy(rados_t cluster) { RadosClient *radosp = (RadosClient *)cluster; radosp->shutdown(); @@ -2421,9 +2418,8 @@ extern "C" int rados_conf_set(rados_t cluster, const char *option, const char *v return g_conf.set_val(option, value); } -extern "C" int rados_conf_apply(void) +extern "C" int rados_reopen_log(void) { - // Simulate SIGHUP after a configuration change. sighup_handler(SIGHUP); return 0; } @@ -2433,7 +2429,6 @@ extern "C" int rados_conf_get(rados_t cluster, const char *option, char **buf, i return g_conf.get_val(option, buf, len); } - extern "C" int rados_lookup_pool(rados_t cluster, const char *name) { RadosClient *radosp = (RadosClient *)cluster; diff --git a/src/testrados.c b/src/testrados.c index 5259b24e3ee84..1e7dfe3850e37 100644 --- a/src/testrados.c +++ b/src/testrados.c @@ -28,7 +28,10 @@ int main(int argc, const char **argv) exit(1); } - rados_conf_parse_argv(cl, argc, argv); + if (rados_connect(cl)) { + printf("error connecting\n"); + exit(1); + } /* create a pool */ r = rados_create_pool("foo"); @@ -128,7 +131,7 @@ int main(int argc, const char **argv) printf("rados_delete_pool = %d\n", r); r = rados_close_pool(pool); - rados_deinitialize(); + rados_destroy(cl); return 0; } -- 2.39.5