From 873627f033f749c6dc48293de1183ca4f6961926 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 7 Aug 2015 18:26:51 +0200 Subject: [PATCH] store: All store functions now accept a store object. That is, instead of operating on a global, shared instance. --- src/core/store.c | 39 +++---- src/core/store_exec.c | 34 +++--- src/include/core/store.h | 29 +++--- t/unit/core/store_expr_test.c | 28 +++-- t/unit/core/store_json_test.c | 48 +++++---- t/unit/core/store_lookup_test.c | 43 +++++--- t/unit/core/store_test.c | 176 +++++++++++++++++--------------- 7 files changed, 223 insertions(+), 174 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index 0ab57d9..e24d2ef 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -847,9 +847,10 @@ prepare_query(sdb_ast_node_t *ast, static int execute_query(sdb_object_t *q, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, - sdb_object_t __attribute__((unused)) *user_data) + sdb_object_t *user_data) { - return sdb_store_query_execute(QUERY(q), buf, errbuf); + return sdb_store_query_execute(SDB_STORE(user_data), + QUERY(q), buf, errbuf); } /* execute_query */ sdb_store_reader_t sdb_store_reader = { @@ -938,14 +939,14 @@ sdb_store_metric_attr(sdb_store_t *store, const char *hostname, } /* sdb_store_metric_attr */ sdb_store_obj_t * -sdb_store_get_host(const char *name) +sdb_store_get_host(sdb_store_t *store, const char *name) { sdb_host_t *host; - if ((! global_store) || (! name)) + if ((! store) || (! name)) return NULL; - host = lookup_host(global_store, name, /* canonicalize = */ 0); + host = lookup_host(store, name, /* canonicalize = */ 0); if (! host) return NULL; @@ -1059,7 +1060,8 @@ sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res, /* TODO: sdb_store_fetch_timeseries should move into the plugin module */ int -sdb_store_fetch_timeseries(const char *hostname, const char *metric, +sdb_store_fetch_timeseries(sdb_store_t *store, + const char *hostname, const char *metric, sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf) { sdb_avltree_t *metrics; @@ -1070,17 +1072,17 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, int status = 0; - if ((! global_store) || (! hostname) || (! metric) || (! opts) || (! buf)) + if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf)) return -1; - pthread_rwlock_rdlock(&global_store->host_lock); - host = lookup_host(global_store, hostname, /* canonicalize = */ 1); + pthread_rwlock_rdlock(&store->host_lock); + host = lookup_host(store, hostname, /* canonicalize = */ 1); metrics = get_host_children(host, SDB_METRIC); sdb_object_deref(SDB_OBJ(host)); if (! metrics) { sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' " "- host '%s' not found", hostname, metric, hostname); - pthread_rwlock_unlock(&global_store->host_lock); + pthread_rwlock_unlock(&store->host_lock); return -1; } @@ -1088,7 +1090,7 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, if (! m) { sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' " "- metric '%s' not found", hostname, metric, metric); - pthread_rwlock_unlock(&global_store->host_lock); + pthread_rwlock_unlock(&store->host_lock); return -1; } @@ -1097,7 +1099,7 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, "- no data-store configured for the stored metric", hostname, metric); sdb_object_deref(SDB_OBJ(m)); - pthread_rwlock_unlock(&global_store->host_lock); + pthread_rwlock_unlock(&store->host_lock); return -1; } @@ -1107,7 +1109,7 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, strncpy(type, m->store.type, sizeof(type)); strncpy(id, m->store.id, sizeof(id)); - pthread_rwlock_unlock(&global_store->host_lock); + pthread_rwlock_unlock(&store->host_lock); ts = sdb_plugin_fetch_timeseries(type, id, opts); if (! ts) { @@ -1125,13 +1127,14 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric, } /* sdb_store_fetch_timeseries */ int -sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter, +sdb_store_scan(sdb_store_t *store, int type, + sdb_store_matcher_t *m, sdb_store_matcher_t *filter, sdb_store_lookup_cb cb, void *user_data) { sdb_avltree_iter_t *host_iter = NULL; int status = 0; - if ((! global_store) || (! cb)) + if ((! store) || (! cb)) return -1; if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) { @@ -1139,8 +1142,8 @@ sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter, return -1; } - pthread_rwlock_rdlock(&global_store->host_lock); - host_iter = sdb_avltree_get_iter(global_store->hosts); + pthread_rwlock_rdlock(&store->host_lock); + host_iter = sdb_avltree_get_iter(store->hosts); if (! host_iter) status = -1; @@ -1190,7 +1193,7 @@ sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter, } sdb_avltree_iter_destroy(host_iter); - pthread_rwlock_unlock(&global_store->host_lock); + pthread_rwlock_unlock(&store->host_lock); return status; } /* sdb_store_scan */ diff --git a/src/core/store_exec.c b/src/core/store_exec.c index 705fbe1..5daaa14 100644 --- a/src/core/store_exec.c +++ b/src/core/store_exec.c @@ -71,8 +71,9 @@ sstrlen(const char *s) */ static int -exec_fetch(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, - const char *hostname, const char *name, sdb_store_matcher_t *filter) +exec_fetch(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, + int type, const char *hostname, const char *name, + sdb_store_matcher_t *filter) { uint32_t res_type = htonl(SDB_CONNECTION_FETCH); @@ -92,7 +93,7 @@ exec_fetch(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, if (type == SDB_HOST) hostname = name; - host = sdb_store_get_host(hostname); + host = sdb_store_get_host(store, hostname); if ((! host) || (filter && (! sdb_store_matcher_matches(filter, host, NULL)))) { sdb_strbuf_sprintf(errbuf, "Failed to fetch %s %s: " @@ -151,8 +152,8 @@ exec_fetch(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, } /* exec_fetch */ static int -exec_list(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, - sdb_store_matcher_t *filter) +exec_list(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, + int type, sdb_store_matcher_t *filter) { uint32_t res_type = htonl(SDB_CONNECTION_LIST); sdb_store_json_formatter_t *f; @@ -169,7 +170,7 @@ exec_list(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, } sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) { + if (sdb_store_scan(store, type, /* m = */ NULL, filter, list_tojson, f)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " "store to JSON"); sdb_strbuf_sprintf(errbuf, "Out of memory"); @@ -184,8 +185,8 @@ exec_list(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, } /* exec_list */ static int -exec_lookup(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, - sdb_store_matcher_t *m, sdb_store_matcher_t *filter) +exec_lookup(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, + int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter) { uint32_t res_type = htonl(SDB_CONNECTION_LOOKUP); sdb_store_json_formatter_t *f; @@ -203,7 +204,7 @@ exec_lookup(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_scan(type, m, filter, lookup_tojson, f)) { + if (sdb_store_scan(store, type, m, filter, lookup_tojson, f)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss", SDB_STORE_TYPE_TO_NAME(type)); sdb_strbuf_sprintf(errbuf, "Failed to lookup %ss", @@ -304,14 +305,14 @@ exec_store(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, sdb_ast_store_t *st) } /* exec_store */ static int -exec_timeseries(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, +exec_timeseries(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, const char *hostname, const char *metric, sdb_timeseries_opts_t *opts) { uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES); sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) { + if (sdb_store_fetch_timeseries(store, hostname, metric, opts, buf)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series"); sdb_strbuf_sprintf(errbuf, "Failed to fetch time-series"); return -1; @@ -325,7 +326,7 @@ exec_timeseries(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, */ int -sdb_store_query_execute(sdb_store_query_t *q, +sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *q, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) { sdb_timeseries_opts_t ts_opts; @@ -341,16 +342,16 @@ sdb_store_query_execute(sdb_store_query_t *q, ast = q->ast; switch (ast->type) { case SDB_AST_TYPE_FETCH: - return exec_fetch(buf, errbuf, SDB_AST_FETCH(ast)->obj_type, + return exec_fetch(store, buf, errbuf, SDB_AST_FETCH(ast)->obj_type, SDB_AST_FETCH(ast)->hostname, SDB_AST_FETCH(ast)->name, q->filter); case SDB_AST_TYPE_LIST: - return exec_list(buf, errbuf, SDB_AST_LIST(ast)->obj_type, + return exec_list(store, buf, errbuf, SDB_AST_LIST(ast)->obj_type, q->filter); case SDB_AST_TYPE_LOOKUP: - return exec_lookup(buf, errbuf, SDB_AST_LOOKUP(ast)->obj_type, + return exec_lookup(store, buf, errbuf, SDB_AST_LOOKUP(ast)->obj_type, q->matcher, q->filter); case SDB_AST_TYPE_STORE: @@ -364,7 +365,8 @@ sdb_store_query_execute(sdb_store_query_t *q, case SDB_AST_TYPE_TIMESERIES: ts_opts.start = SDB_AST_TIMESERIES(ast)->start; ts_opts.end = SDB_AST_TIMESERIES(ast)->end; - return exec_timeseries(buf, errbuf, SDB_AST_TIMESERIES(ast)->hostname, + return exec_timeseries(store, buf, errbuf, + SDB_AST_TIMESERIES(ast)->hostname, SDB_AST_TIMESERIES(ast)->metric, &ts_opts); default: diff --git a/src/include/core/store.h b/src/include/core/store.h index 1cb0e5a..aa53e8e 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -330,25 +330,27 @@ sdb_store_metric_attr(sdb_store_t *store, const char *hostname, /* * sdb_store_get_host: - * Query the store for a host by its (canonicalized) name. + * Query the specified store for a host by its (canonicalized) name. * * The function increments the ref count of the host object. The caller needs * to deref it when no longer using it. */ sdb_store_obj_t * -sdb_store_get_host(const char *name); +sdb_store_get_host(sdb_store_t *store, const char *name); /* * sdb_store_fetch_timeseries: * Fetch the time-series described by the specified host's metric and - * serialize it as JSON into the provided string buffer. + * serialize it as JSON into the provided string buffer. The host data is + * retrieved from the specified store. * * Returns: * - 0 on success * - a negative value else */ int -sdb_store_fetch_timeseries(const char *hostname, const char *metric, +sdb_store_fetch_timeseries(sdb_store_t *store, + const char *hostname, const char *metric, sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf); /* @@ -440,15 +442,15 @@ sdb_store_query_prepare_matcher(sdb_ast_node_t *ast); /* * sdb_store_query_execute: - * Execute a previously prepared query. The query result will be written to - * 'buf' and any errors to 'errbuf'. + * Execute a previously prepared query in the specified store. The query + * result will be written to 'buf' and any errors to 'errbuf'. * * Returns: * - the result type (to be used by the server reply) * - a negative value on error */ int -sdb_store_query_execute(sdb_store_query_t *m, +sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *m, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf); /* @@ -697,18 +699,19 @@ typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, /* * sdb_store_scan: - * Look up objects of the specified type in the store. The specified callback - * function is called for each object in the store matching 'm'. The function - * performs a full scan of all objects stored in the database. If specified, - * the filter will be used to preselect objects for further evaluation. See - * the description of 'sdb_store_matcher_matches' for details. + * Look up objects of the specified type in the specified store. The specified + * callback function is called for each object in the store matching 'm'. The + * function performs a full scan of all objects stored in the database. If + * specified, the filter will be used to preselect objects for further + * evaluation. See the description of 'sdb_store_matcher_matches' for details. * * Returns: * - 0 on success * - a negative value else */ int -sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter, +sdb_store_scan(sdb_store_t *store, int type, + sdb_store_matcher_t *m, sdb_store_matcher_t *filter, sdb_store_lookup_cb cb, void *user_data); /* diff --git a/t/unit/core/store_expr_test.c b/t/unit/core/store_expr_test.c index 153e52f..7a918ce 100644 --- a/t/unit/core/store_expr_test.c +++ b/t/unit/core/store_expr_test.c @@ -37,6 +37,8 @@ #include +static sdb_store_t *store; + static void populate(void) { @@ -93,46 +95,54 @@ populate(void) size_t i; - sdb_store_init(); + store = sdb_store_create(); + ck_assert(store != NULL); for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) { - int status = sdb_plugin_store_host(hosts[i], 1); + int status = sdb_store_host(store, hosts[i], 1); ck_assert(status == 0); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) { - int status = sdb_plugin_store_metric(metrics[i].host, + int status = sdb_store_metric(store, metrics[i].host, metrics[i].metric, /* store */ NULL, 1); ck_assert(status == 0); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) { - int status = sdb_plugin_store_service(services[i].host, + int status = sdb_store_service(store, services[i].host, services[i].service, 1); ck_assert(status == 0); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) { - int status = sdb_plugin_store_attribute(attrs[i].host, + int status = sdb_store_attribute(store, attrs[i].host, attrs[i].name, &attrs[i].value, 1); ck_assert(status == 0); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(svc_attrs); ++i) { - int status = sdb_plugin_store_service_attribute(svc_attrs[i].host, + int status = sdb_store_service_attr(store, svc_attrs[i].host, svc_attrs[i].service, svc_attrs[i].name, &svc_attrs[i].value, 1); ck_assert(status == 0); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(metric_attrs); ++i) { - int status = sdb_plugin_store_metric_attribute(metric_attrs[i].host, + int status = sdb_store_metric_attr(store, metric_attrs[i].host, metric_attrs[i].metric, metric_attrs[i].name, &metric_attrs[i].value, 1); ck_assert(status == 0); } } /* populate */ +static void +turndown(void) +{ + sdb_object_deref(SDB_OBJ(store)); + store = NULL; +} /* turndown */ + #define NAME { SDB_TYPE_INTEGER, { .integer = SDB_FIELD_NAME } } #define LAST_UPDATE { SDB_TYPE_INTEGER, { .integer = SDB_FIELD_LAST_UPDATE } } #define AGE { SDB_TYPE_INTEGER, { .integer = SDB_FIELD_AGE } } @@ -555,7 +565,7 @@ START_TEST(test_expr_iter) size_t i; if (expr_iter_data[_i].host) { - obj = sdb_store_get_host(expr_iter_data[_i].host); + obj = sdb_store_get_host(store, expr_iter_data[_i].host); ck_assert(obj != NULL); if (expr_iter_data[_i].child) { @@ -637,7 +647,7 @@ END_TEST TEST_MAIN("core::store_expr") { 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, expr_iter); ADD_TCASE(tc); } diff --git a/t/unit/core/store_json_test.c b/t/unit/core/store_json_test.c index 6be3c14..d887c43 100644 --- a/t/unit/core/store_json_test.c +++ b/t/unit/core/store_json_test.c @@ -40,54 +40,64 @@ #undef SDB_INTERVAL_SECOND #define SDB_INTERVAL_SECOND 1000000000L +static sdb_store_t *store; + static void populate(void) { sdb_data_t datum; - sdb_store_init(); + store = sdb_store_create(); + ck_assert(store != NULL); - sdb_plugin_store_host("h1", 1 * SDB_INTERVAL_SECOND); - sdb_plugin_store_host("h2", 3 * SDB_INTERVAL_SECOND); + sdb_store_host(store, "h1", 1 * SDB_INTERVAL_SECOND); + sdb_store_host(store, "h2", 3 * SDB_INTERVAL_SECOND); datum.type = SDB_TYPE_STRING; datum.data.string = "v1"; - sdb_plugin_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND); + sdb_store_attribute(store, "h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND); datum.data.string = "v2"; - sdb_plugin_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND); + sdb_store_attribute(store, "h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND); datum.data.string = "v3"; - sdb_plugin_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); + sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); /* make sure that older updates don't overwrite existing values */ datum.data.string = "fail"; - sdb_plugin_store_attribute("h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND); - sdb_plugin_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); + sdb_store_attribute(store, "h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND); + sdb_store_attribute(store, "h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); - sdb_plugin_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND); - sdb_plugin_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND); - sdb_plugin_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND); + sdb_store_metric(store, "h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND); + sdb_store_metric(store, "h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND); + sdb_store_metric(store, "h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND); - sdb_plugin_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND); - sdb_plugin_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND); + sdb_store_service(store, "h2", "s1", 1 * SDB_INTERVAL_SECOND); + sdb_store_service(store, "h2", "s2", 2 * SDB_INTERVAL_SECOND); datum.type = SDB_TYPE_INTEGER; datum.data.integer = 42; - sdb_plugin_store_metric_attribute("h1", "m1", "k3", + sdb_store_metric_attr(store, "h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); datum.data.integer = 123; - sdb_plugin_store_service_attribute("h2", "s2", "k1", + sdb_store_service_attr(store, "h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND); datum.data.integer = 4711; - sdb_plugin_store_service_attribute("h2", "s2", "k2", + sdb_store_service_attr(store, "h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND); /* don't overwrite k1 */ datum.data.integer = 666; - sdb_plugin_store_service_attribute("h2", "s2", "k1", + sdb_store_service_attr(store, "h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND); } /* populate */ +static void +turndown(void) +{ + sdb_object_deref(SDB_OBJ(store)); + store = NULL; +} /* turndown */ + static int scan_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t __attribute__((unused)) *filter, @@ -477,7 +487,7 @@ START_TEST(test_store_tojson) store_tojson_data[_i].type, SDB_WANT_ARRAY); ck_assert(f != NULL); - status = sdb_store_scan(store_tojson_data[_i].type, + status = sdb_store_scan(store, store_tojson_data[_i].type, /* m = */ NULL, filter, store_tojson_data[_i].f, f); fail_unless(status == 0, "sdb_store_scan(HOST, ..., tojson) = %d; expected: 0", @@ -495,8 +505,8 @@ END_TEST TEST_MAIN("core::store_json") { TCase *tc = tcase_create("core"); + tcase_add_unchecked_fixture(tc, populate, turndown); TC_ADD_LOOP_TEST(tc, store_tojson); - tcase_add_unchecked_fixture(tc, populate, sdb_store_clear); ADD_TCASE(tc); } TEST_MAIN_END diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 49f2268..9f48683 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -38,6 +38,8 @@ #include #include +static sdb_store_t *store; + static void populate(void) { @@ -74,40 +76,48 @@ populate(void) size_t i; - sdb_store_init(); + store = sdb_store_create(); + ck_assert(store != NULL); for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) { - int status = sdb_plugin_store_host(hosts[i], 1); + int status = sdb_store_host(store, hosts[i], 1); fail_unless(status == 0, - "sdb_plugin_store_host(%s, 1) = %d; expected: 0", + "sdb_store_host(%s, 1) = %d; expected: 0", hosts[i], status); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) { - int status = sdb_plugin_store_metric(metrics[i].host, + int status = sdb_store_metric(store, metrics[i].host, metrics[i].metric, /* store */ NULL, 1); fail_unless(status == 0, - "sdb_plugin_store_metric(%s, %s, NULL, 1) = %d; expected: 0", + "sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0", metrics[i].host, metrics[i].metric, status); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) { - int status = sdb_plugin_store_service(services[i].host, + int status = sdb_store_service(store, services[i].host, services[i].service, 1); fail_unless(status == 0, - "sdb_plugin_store_service(%s, %s, 1) = %d; expected: 0", + "sdb_store_service(%s, %s, 1) = %d; expected: 0", services[i].host, services[i].service, status); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) { - int status = sdb_plugin_store_attribute(attrs[i].host, + int status = sdb_store_attribute(store, attrs[i].host, attrs[i].name, &attrs[i].value, 1); fail_unless(status == 0, - "sdb_plugin_store_attribute(%s, %s, , 1) = %d; expected: 0", + "sdb_store_attribute(%s, %s, , 1) = %d; expected: 0", attrs[i].host, attrs[i].name, status); } } /* populate */ +static void +turndown(void) +{ + sdb_object_deref(SDB_OBJ(store)); + store = NULL; +} /* turndown */ + struct { int type; char *name; @@ -157,7 +167,7 @@ START_TEST(test_cmp_name) sdb_store_matcher_t *m, *n; int status; - host = sdb_store_get_host("a"); + host = sdb_store_get_host(store, "a"); fail_unless(host != NULL, "sdb_store_get_host(a) = NULL; expected: "); @@ -268,7 +278,7 @@ START_TEST(test_cmp_attr) const char *op_str[] = { "<", "<=", "=", ">=", ">" }; ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str)); - host = sdb_store_get_host("a"); + host = sdb_store_get_host(store, "a"); fail_unless(host != NULL, "sdb_store_get_host(a) = NULL; expected: "); @@ -376,7 +386,7 @@ START_TEST(test_cmp_obj) ck_assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str)); - host = sdb_store_get_host(cmp_obj_data[_i].host); + host = sdb_store_get_host(store, cmp_obj_data[_i].host); fail_unless(host != NULL, "sdb_store_get_host(%s) = NULL; expected: ", cmp_obj_data[_i].host); @@ -450,7 +460,7 @@ START_TEST(test_store_match_op) int status; size_t i; - obj = sdb_store_get_host("a"); + obj = sdb_store_get_host(store, "a"); status = sdb_store_matcher_matches(always, obj, /* filter */ NULL); fail_unless(status == 1, @@ -623,7 +633,8 @@ START_TEST(test_scan) int check, n; n = 0; - check = sdb_store_scan(SDB_HOST, /* matcher */ NULL, /* filter */ NULL, + check = sdb_store_scan(store, SDB_HOST, + /* matcher */ NULL, /* filter */ NULL, scan_cb, &n); fail_unless(check == 0, "sdb_store_scan() = %d; expected: 0", check); @@ -649,7 +660,7 @@ START_TEST(test_scan) } n = 0; - sdb_store_scan(SDB_HOST, m, filter, scan_cb, &n); + sdb_store_scan(store, SDB_HOST, m, filter, scan_cb, &n); fail_unless(n == scan_data[_i].expected, "sdb_store_scan(HOST, matcher{%s}, filter{%s}) " "found %d hosts; expected: %d", scan_data[_i].query, @@ -664,7 +675,7 @@ END_TEST TEST_MAIN("core::store_lookup") { 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, cmp_name); TC_ADD_LOOP_TEST(tc, cmp_attr); TC_ADD_LOOP_TEST(tc, cmp_obj); diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index 6d528de..1d524b6 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -38,10 +38,13 @@ #include #include +static sdb_store_t *store; + static void init(void) { - sdb_store_init(); + store = sdb_store_create(); + ck_assert(store != NULL); } static void @@ -49,43 +52,50 @@ populate(void) { sdb_data_t datum; - sdb_plugin_store_host("h1", 1); - sdb_plugin_store_host("h2", 3); + sdb_store_host(store, "h1", 1); + sdb_store_host(store, "h2", 3); datum.type = SDB_TYPE_STRING; datum.data.string = "v1"; - sdb_plugin_store_attribute("h1", "k1", &datum, 1); + sdb_store_attribute(store, "h1", "k1", &datum, 1); datum.data.string = "v2"; - sdb_plugin_store_attribute("h1", "k2", &datum, 2); + sdb_store_attribute(store, "h1", "k2", &datum, 2); datum.data.string = "v3"; - sdb_plugin_store_attribute("h1", "k3", &datum, 2); + sdb_store_attribute(store, "h1", "k3", &datum, 2); /* make sure that older updates don't overwrite existing values */ datum.data.string = "fail"; - sdb_plugin_store_attribute("h1", "k2", &datum, 1); - sdb_plugin_store_attribute("h1", "k3", &datum, 2); + sdb_store_attribute(store, "h1", "k2", &datum, 1); + sdb_store_attribute(store, "h1", "k3", &datum, 2); - sdb_plugin_store_metric("h1", "m1", /* store */ NULL, 2); - sdb_plugin_store_metric("h1", "m2", /* store */ NULL, 1); - sdb_plugin_store_metric("h2", "m1", /* store */ NULL, 1); + sdb_store_metric(store, "h1", "m1", /* store */ NULL, 2); + sdb_store_metric(store, "h1", "m2", /* store */ NULL, 1); + sdb_store_metric(store, "h2", "m1", /* store */ NULL, 1); - sdb_plugin_store_service("h2", "s1", 1); - sdb_plugin_store_service("h2", "s2", 2); + sdb_store_service(store, "h2", "s1", 1); + sdb_store_service(store, "h2", "s2", 2); datum.type = SDB_TYPE_INTEGER; datum.data.integer = 42; - sdb_plugin_store_metric_attribute("h1", "m1", "k3", &datum, 2); + sdb_store_metric_attr(store, "h1", "m1", "k3", &datum, 2); datum.data.integer = 123; - sdb_plugin_store_service_attribute("h2", "s2", "k1", &datum, 2); + sdb_store_service_attr(store, "h2", "s2", "k1", &datum, 2); datum.data.integer = 4711; - sdb_plugin_store_service_attribute("h2", "s2", "k2", &datum, 1); + sdb_store_service_attr(store, "h2", "s2", "k2", &datum, 1); /* don't overwrite k1 */ datum.data.integer = 666; - sdb_plugin_store_service_attribute("h2", "s2", "k1", &datum, 2); + sdb_store_service_attr(store, "h2", "s2", "k1", &datum, 2); } /* populate */ +static void +turndown(void) +{ + sdb_object_deref(SDB_OBJ(store)); + store = NULL; +} /* turndown */ + START_TEST(test_store_host) { struct { @@ -117,10 +127,10 @@ START_TEST(test_store_host) for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { int status; - status = sdb_plugin_store_host(golden_data[i].name, + status = sdb_store_host(store, golden_data[i].name, golden_data[i].last_update); fail_unless(status == golden_data[i].expected, - "sdb_plugin_store_host(%s, %d) = %d; expected: %d", + "sdb_store_host(%s, %d) = %d; expected: %d", golden_data[i].name, (int)golden_data[i].last_update, status, golden_data[i].expected); } @@ -128,7 +138,7 @@ START_TEST(test_store_host) for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) { sdb_store_obj_t *have; - have = sdb_store_get_host(golden_hosts[i].name); + have = sdb_store_get_host(store, golden_hosts[i].name); fail_unless((have != NULL) == golden_hosts[i].have, "sdb_store_get_host(%s) = %p; expected: %s", golden_hosts[i].name, have, @@ -145,9 +155,9 @@ START_TEST(test_store_get_host) size_t i; for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_hosts); ++i) { - int status = sdb_plugin_store_host(golden_hosts[i], 1); + int status = sdb_store_host(store, golden_hosts[i], 1); fail_unless(status >= 0, - "sdb_plugin_store_host(%s) = %d; expected: >=0", + "sdb_store_host(%s) = %d; expected: >=0", golden_hosts[i], status); } @@ -155,7 +165,7 @@ START_TEST(test_store_get_host) sdb_store_obj_t *sobj1, *sobj2; int ref_cnt; - sobj1 = sdb_store_get_host(golden_hosts[i]); + sobj1 = sdb_store_get_host(store, golden_hosts[i]); fail_unless(sobj1 != NULL, "sdb_store_get_host(%s) = NULL; expected: ", golden_hosts[i]); @@ -165,7 +175,7 @@ START_TEST(test_store_get_host) "sdb_store_get_host(%s) did not increment ref count: " "got: %d; expected: >1", golden_hosts[i], ref_cnt); - sobj2 = sdb_store_get_host(golden_hosts[i]); + sobj2 = sdb_store_get_host(store, golden_hosts[i]); fail_unless(sobj2 != NULL, "sdb_store_get_host(%s) = NULL; expected: ", golden_hosts[i]); @@ -184,7 +194,7 @@ START_TEST(test_store_get_host) for (i = 0; i < SDB_STATIC_ARRAY_LEN(unknown_hosts); ++i) { sdb_store_obj_t *sobj; - sobj = sdb_store_get_host(unknown_hosts[i]); + sobj = sdb_store_get_host(store, unknown_hosts[i]); fail_unless(!sobj, "sdb_store_get_host(%s) = ; expected: NULL", unknown_hosts[i], sobj ? SDB_OBJ(sobj)->name : "NULL"); } @@ -212,8 +222,8 @@ START_TEST(test_store_attr) size_t i; - sdb_plugin_store_host("l", 1); - sdb_plugin_store_host("m", 1); + sdb_store_host(store, "l", 1); + sdb_store_host(store, "m", 1); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { sdb_data_t datum; int status; @@ -222,11 +232,11 @@ START_TEST(test_store_attr) datum.type = SDB_TYPE_STRING; datum.data.string = golden_data[i].value; - status = sdb_plugin_store_attribute(golden_data[i].host, + status = sdb_store_attribute(store, golden_data[i].host, golden_data[i].key, &datum, golden_data[i].last_update); fail_unless(status == golden_data[i].expected, - "sdb_plugin_store_attribute(%s, %s, %s, %d) = %d; expected: %d", + "sdb_store_attribute(%s, %s, %s, %d) = %d; expected: %d", golden_data[i].host, golden_data[i].key, golden_data[i].value, golden_data[i].last_update, status, golden_data[i].expected); } @@ -265,16 +275,16 @@ START_TEST(test_store_metric) size_t i; - sdb_plugin_store_host("m", 1); - sdb_plugin_store_host("l", 1); + sdb_store_host(store, "m", 1); + sdb_store_host(store, "l", 1); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { int status; - status = sdb_plugin_store_metric(golden_data[i].host, + status = sdb_store_metric(store, golden_data[i].host, golden_data[i].metric, golden_data[i].store, golden_data[i].last_update); fail_unless(status == golden_data[i].expected, - "sdb_plugin_store_metric(%s, %s, %p, %d) = %d; expected: %d", + "sdb_store_metric(%s, %s, %p, %d) = %d; expected: %d", golden_data[i].host, golden_data[i].metric, golden_data[i].store, golden_data[i].last_update, status, golden_data[i].expected); @@ -309,20 +319,20 @@ START_TEST(test_store_metric_attr) size_t i; - sdb_plugin_store_host("m", 1); - sdb_plugin_store_host("l", 1); - sdb_plugin_store_metric("m", "m1", NULL, 1); - sdb_plugin_store_metric("l", "m1", NULL, 1); - sdb_plugin_store_metric("l", "m2", NULL, 1); + sdb_store_host(store, "m", 1); + sdb_store_host(store, "l", 1); + sdb_store_metric(store, "m", "m1", NULL, 1); + sdb_store_metric(store, "l", "m1", NULL, 1); + sdb_store_metric(store, "l", "m2", NULL, 1); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { int status; - status = sdb_plugin_store_metric_attribute(golden_data[i].host, + status = sdb_store_metric_attr(store, golden_data[i].host, golden_data[i].metric, golden_data[i].attr, &golden_data[i].value, golden_data[i].last_update); fail_unless(status == golden_data[i].expected, - "sdb_plugin_store_metric_attribute(%s, %s, %s, %d, %d) = %d; " + "sdb_store_metric_attr(%s, %s, %s, %d, %d) = %d; " "expected: %d", golden_data[i].host, golden_data[i].metric, golden_data[i].attr, golden_data[i].value.data.integer, golden_data[i].last_update, status, golden_data[i].expected); @@ -350,15 +360,15 @@ START_TEST(test_store_service) size_t i; - sdb_plugin_store_host("m", 1); - sdb_plugin_store_host("l", 1); + sdb_store_host(store, "m", 1); + sdb_store_host(store, "l", 1); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { int status; - status = sdb_plugin_store_service(golden_data[i].host, + status = sdb_store_service(store, golden_data[i].host, golden_data[i].svc, golden_data[i].last_update); fail_unless(status == golden_data[i].expected, - "sdb_plugin_store_service(%s, %s, %d) = %d; expected: %d", + "sdb_store_service(%s, %s, %d) = %d; expected: %d", golden_data[i].host, golden_data[i].svc, golden_data[i].last_update, status, golden_data[i].expected); } @@ -392,20 +402,20 @@ START_TEST(test_store_service_attr) size_t i; - sdb_plugin_store_host("m", 1); - sdb_plugin_store_host("l", 1); - sdb_plugin_store_service("m", "s1", 1); - sdb_plugin_store_service("l", "s1", 1); - sdb_plugin_store_service("l", "s2", 1); + sdb_store_host(store, "m", 1); + sdb_store_host(store, "l", 1); + sdb_store_service(store, "m", "s1", 1); + sdb_store_service(store, "l", "s1", 1); + sdb_store_service(store, "l", "s2", 1); for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { int status; - status = sdb_plugin_store_service_attribute(golden_data[i].host, + status = sdb_store_service_attr(store, golden_data[i].host, golden_data[i].svc, golden_data[i].attr, &golden_data[i].value, golden_data[i].last_update); fail_unless(status == golden_data[i].expected, - "sdb_plugin_store_service_attribute(%s, %s, %s, %d, %d) = %d; " + "sdb_store_service_attr(%s, %s, %s, %d, %d) = %d; " "expected: %d", golden_data[i].host, golden_data[i].svc, golden_data[i].attr, golden_data[i].value.data.integer, golden_data[i].last_update, status, golden_data[i].expected); @@ -467,13 +477,13 @@ START_TEST(test_get_field) sdb_time_t now = sdb_gettime(); int check; - sdb_plugin_store_host("host", 10); - sdb_plugin_store_host("host", 20); - sdb_plugin_store_attribute("host", "attr", &get_field_data[_i].value, 10); - sdb_plugin_store_attribute("host", "attr", &get_field_data[_i].value, 20); + sdb_store_host(store, "host", 10); + sdb_store_host(store, "host", 20); + sdb_store_attribute(store, "host", "attr", &get_field_data[_i].value, 10); + sdb_store_attribute(store, "host", "attr", &get_field_data[_i].value, 20); if (get_field_data[_i].hostname) { - obj = sdb_store_get_host(get_field_data[_i].hostname); + obj = sdb_store_get_host(store, get_field_data[_i].hostname); ck_assert(obj != NULL); if (get_field_data[_i].attr) { @@ -566,7 +576,7 @@ START_TEST(test_get_child) sdb_store_obj_t *obj; const char *expected_name = golden_data[i].host; - obj = sdb_store_get_host(golden_data[i].host); + obj = sdb_store_get_host(store, golden_data[i].host); if (golden_data[i].expected && (golden_data[i].type == SDB_HOST)) fail_unless(obj == NULL, "sdb_store_get_host(%s) = %p; expected: NULL", @@ -624,51 +634,51 @@ START_TEST(test_interval) sdb_store_obj_t *host; /* 10 us interval */ - sdb_plugin_store_host("host", 10); - sdb_plugin_store_host("host", 20); - sdb_plugin_store_host("host", 30); - sdb_plugin_store_host("host", 40); + sdb_store_host(store, "host", 10); + sdb_store_host(store, "host", 20); + sdb_store_host(store, "host", 30); + sdb_store_host(store, "host", 40); - host = sdb_store_get_host("host"); + host = sdb_store_get_host(store, "host"); fail_unless(host != NULL, "INTERNAL ERROR: store doesn't have host after adding it"); fail_unless(host->interval == 10, - "sdb_plugin_store_host() did not calculate interval correctly: " + "sdb_store_host() did not calculate interval correctly: " "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 10); /* multiple updates for the same timestamp don't modify the interval */ - sdb_plugin_store_host("host", 40); - sdb_plugin_store_host("host", 40); - sdb_plugin_store_host("host", 40); - sdb_plugin_store_host("host", 40); + sdb_store_host(store, "host", 40); + sdb_store_host(store, "host", 40); + sdb_store_host(store, "host", 40); + sdb_store_host(store, "host", 40); fail_unless(host->interval == 10, - "sdb_plugin_store_host() changed interval when doing multiple updates " + "sdb_store_host() changed interval when doing multiple updates " "using the same timestamp; got: %"PRIsdbTIME"; " "expected: %"PRIsdbTIME, host->interval, 10); /* multiple updates using an timestamp don't modify the interval */ - sdb_plugin_store_host("host", 20); - sdb_plugin_store_host("host", 20); - sdb_plugin_store_host("host", 20); - sdb_plugin_store_host("host", 20); + sdb_store_host(store, "host", 20); + sdb_store_host(store, "host", 20); + sdb_store_host(store, "host", 20); + sdb_store_host(store, "host", 20); fail_unless(host->interval == 10, - "sdb_plugin_store_host() changed interval when doing multiple updates " + "sdb_store_host() changed interval when doing multiple updates " "using an old timestamp; got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 10); /* new interval: 20 us */ - sdb_plugin_store_host("host", 60); + sdb_store_host(store, "host", 60); fail_unless(host->interval == 11, - "sdb_plugin_store_host() did not calculate interval correctly: " + "sdb_store_host() did not calculate interval correctly: " "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11); /* new interval: 40 us */ - sdb_plugin_store_host("host", 100); + sdb_store_host(store, "host", 100); fail_unless(host->interval == 13, - "sdb_plugin_store_host() did not calculate interval correctly: " + "sdb_store_host() did not calculate interval correctly: " "got: %"PRIsdbTIME"; expected: %"PRIsdbTIME, host->interval, 11); sdb_object_deref(SDB_OBJ(host)); @@ -719,7 +729,7 @@ START_TEST(test_scan) int check; /* empty store */ - check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL, + check = sdb_store_scan(store, SDB_HOST, /* m, filter = */ NULL, NULL, scan_count, &i); fail_unless(check == 0, "sdb_store_scan(HOST), empty store = %d; expected: 0", check); @@ -729,7 +739,7 @@ START_TEST(test_scan) populate(); - check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL, + check = sdb_store_scan(store, SDB_HOST, /* m, filter = */ NULL, NULL, scan_count, &i); fail_unless(check == 0, "sdb_store_scan(HOST) = %d; expected: 0", check); @@ -738,7 +748,7 @@ START_TEST(test_scan) "expected: 1", (int)i); i = 0; - check = sdb_store_scan(SDB_HOST, /* m, filter = */ NULL, NULL, + check = sdb_store_scan(store, SDB_HOST, /* m, filter = */ NULL, NULL, scan_error, &i); fail_unless(check == -1, "sdb_store_scan(HOST), error callback = %d; expected: -1", check); @@ -747,7 +757,7 @@ START_TEST(test_scan) "(callback returned error); expected: 1", (int)i); i = 0; - check = sdb_store_scan(SDB_SERVICE, /* m, filter = */ NULL, NULL, + check = sdb_store_scan(store, SDB_SERVICE, /* m, filter = */ NULL, NULL, scan_count, &i); fail_unless(check == 0, "sdb_store_scan(SERVICE) = %d; expected: 0", check); @@ -756,7 +766,7 @@ START_TEST(test_scan) "expected: 2", (int)i); i = 0; - check = sdb_store_scan(SDB_METRIC, /* m, filter = */ NULL, NULL, + check = sdb_store_scan(store, SDB_METRIC, /* m, filter = */ NULL, NULL, scan_count, &i); fail_unless(check == 0, "sdb_store_scan(METRIC) = %d; expected: 0", check); @@ -769,6 +779,7 @@ END_TEST TEST_MAIN("core::store") { TCase *tc = tcase_create("core"); + tcase_add_unchecked_fixture(tc, init, turndown); tcase_add_test(tc, test_store_host); tcase_add_test(tc, test_store_get_host); tcase_add_test(tc, test_store_attr); @@ -780,7 +791,6 @@ TEST_MAIN("core::store") tcase_add_test(tc, test_get_child); tcase_add_test(tc, test_interval); tcase_add_test(tc, test_scan); - tcase_add_unchecked_fixture(tc, init, sdb_store_clear); ADD_TCASE(tc); } TEST_MAIN_END -- 2.30.2