Code

store: Drop the global (default) store.
authorSebastian Harl <sh@tokkee.org>
Sat, 8 Aug 2015 07:34:56 +0000 (09:34 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 8 Aug 2015 07:57:04 +0000 (09:57 +0200)
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
src/include/core/store.h
src/tools/sysdbd/main.c
t/unit/frontend/query_test.c

index e24d2ef0a5356bf0a2badc8aa5fe468f94983a4f..bea4d2b578c421ed7706791e2aee0db95524de96 100644 (file)
@@ -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)
 {
index aa53e8eadec81721c19d0c84adefe7634a146bb3..16a094adbf8e347b9278d94142a3ff4d0242632b 100644 (file)
@@ -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:
index cc3525e30ef4374e3c904797cccd176de2e94043..ce54313542df26cd254847d601f01fc14413d74a 100644 (file)
@@ -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);
 
index 7fb73a453f33dc7bf84a8989557f8c9470f93b10..5330999c2d091e9828b00dc9d0c01fb1509b810a 100644 (file)
 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);
 }