From 5eb9fab9a904e0b9c10189a62e73b543dda62300 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sat, 8 Aug 2015 09:34:56 +0200 Subject: [PATCH] store: Drop the global (default) store. Instead, register a store from sysdbd and make sure to re-register it on reconfigure. Previously, the store plugin would have been dropped in that case. --- src/core/store.c | 28 ---------------------------- src/include/core/store.h | 19 ------------------- src/tools/sysdbd/main.c | 31 +++++++++++++++++++++++++++++-- t/unit/frontend/query_test.c | 19 +++++++++++++++++-- 4 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index e24d2ef..bea4d2b 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -59,8 +59,6 @@ struct sdb_store { pthread_rwlock_t host_lock; }; -sdb_store_t *global_store = NULL; - /* * private types */ @@ -867,32 +865,6 @@ sdb_store_create(void) return SDB_STORE(sdb_object_create("store", store_type)); } /* sdb_store_create */ -int -sdb_store_init(void) -{ - if (global_store) - return 0; - - global_store = SDB_STORE(sdb_object_create("store", store_type)); - if (! global_store) { - sdb_log(SDB_LOG_ERR, "store: Failed to allocate store"); - return -1; - } - if (sdb_plugin_register_writer("memstore", - &sdb_store_writer, SDB_OBJ(global_store))) - return -1; - return sdb_plugin_register_reader("memstore", - &sdb_store_reader, SDB_OBJ(global_store)); -} /* sdb_store_init */ - -void -sdb_store_clear(void) -{ - if (! global_store) - return; - sdb_avltree_clear(global_store->hosts); -} /* sdb_store_clear */ - int sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update) { diff --git a/src/include/core/store.h b/src/include/core/store.h index aa53e8e..16a094a 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -284,25 +284,6 @@ extern sdb_store_reader_t sdb_store_reader; sdb_store_t * sdb_store_create(void); -/* - * sdb_store_init: - * Initialize the store sub-system. This function has to be called before - * doing any other store operations. - * - * Returns: - * - 0 on success - * - a negative value else - */ -int -sdb_store_init(void); - -/* - * sdb_store_clear: - * Clear the entire store and remove all stored objects. - */ -void -sdb_store_clear(void); - /* * sdb_store_host, sdb_store_service, sdb_store_metric, sdb_store_attribute, * sdb_store_metric_attr: diff --git a/src/tools/sysdbd/main.c b/src/tools/sysdbd/main.c index cc3525e..ce54313 100644 --- a/src/tools/sysdbd/main.c +++ b/src/tools/sysdbd/main.c @@ -181,6 +181,31 @@ daemonize(void) return 0; } /* daemonize */ +static int +store_init(void) +{ + sdb_store_t *store = sdb_store_create(); + + if (! store) { + sdb_log(SDB_LOG_ERR, "store: Failed to allocate store"); + return -1; + } + if (sdb_plugin_register_writer("memstore", + &sdb_store_writer, SDB_OBJ(store))) { + sdb_object_deref(SDB_OBJ(store)); + return -1; + } + if (sdb_plugin_register_reader("memstore", + &sdb_store_reader, SDB_OBJ(store))) { + sdb_object_deref(SDB_OBJ(store)); + return -1; + } + + /* Pass ownership to the plugins */ + sdb_object_deref(SDB_OBJ(store)); + return 0; +} /* store_init */ + static int configure(void) { @@ -194,6 +219,10 @@ configure(void) "\tCheck other error messages for details."); return 1; } + if (store_init()) { + sdb_log(SDB_LOG_ERR, "Failed to initialize the store"); + return 1; + } if (! listen_addresses) { listen_addresses = default_listen_addresses; @@ -370,8 +399,6 @@ main(int argc, char **argv) if (sdb_ssl_init()) exit(1); - if (sdb_store_init()) - exit(1); sdb_plugin_init_all(); plugin_main_loop.default_interval = SECS_TO_SDB_TIME(60); diff --git a/t/unit/frontend/query_test.c b/t/unit/frontend/query_test.c index 7fb73a4..5330999 100644 --- a/t/unit/frontend/query_test.c +++ b/t/unit/frontend/query_test.c @@ -43,10 +43,19 @@ static void populate(void) { + sdb_store_t *store; sdb_data_t datum; - sdb_store_init(); + /* the frontend accesses the store via the plugin API */ + store = sdb_store_create(); + ck_assert(store != NULL); + ck_assert(sdb_plugin_register_writer("test-writer", + &sdb_store_writer, SDB_OBJ(store)) == 0); + ck_assert(sdb_plugin_register_reader("test-reader", + &sdb_store_reader, SDB_OBJ(store)) == 0); + sdb_object_deref(SDB_OBJ(store)); + /* populate the store */ sdb_plugin_store_host("h1", 1 * SDB_INTERVAL_SECOND); sdb_plugin_store_host("h2", 3 * SDB_INTERVAL_SECOND); @@ -78,6 +87,12 @@ populate(void) &datum, 1 * SDB_INTERVAL_SECOND); } /* populate */ +static void +turndown(void) +{ + sdb_plugin_unregister_all(); +} /* turndown */ + #define HOST_H1 \ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \ "\"update_interval\": \"0s\", \"backends\": [], " \ @@ -732,7 +747,7 @@ END_TEST TEST_MAIN("frontend::query") { TCase *tc = tcase_create("core"); - tcase_add_checked_fixture(tc, populate, sdb_store_clear); + tcase_add_checked_fixture(tc, populate, turndown); TC_ADD_LOOP_TEST(tc, query); ADD_TCASE(tc); } -- 2.30.2