diff --git a/src/core/store.c b/src/core/store.c
index 9c83218f1efccbde39512f6831383d5f99c5801e..7fc11e178373c4fe6637e159da754c97e4e43c8d 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
#include <stdlib.h>
#include <string.h>
-#include <math.h>
#include <pthread.h>
/*
* private types
*/
-struct sdb_store {
+struct sdb_memstore {
sdb_object_t super;
/* hosts are the top-level entries and
/* internal representation of a to-be-stored object */
typedef struct {
- sdb_store_obj_t *parent;
+ sdb_memstore_obj_t *parent;
sdb_avltree_t *parent_tree;
int type;
const char *name;
sdb_time_t last_update;
+ const char * const *backends;
+ size_t backends_num;
} store_obj_t;
-#define STORE_OBJ_INIT { NULL, NULL, 0, NULL, 0 }
+#define STORE_OBJ_INIT { NULL, NULL, 0, NULL, 0, NULL, 0 }
static sdb_type_t host_type;
static sdb_type_t service_type;
store_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
{
int err;
- if (! (SDB_STORE(obj)->hosts = sdb_avltree_create()))
+ if (! (SDB_MEMSTORE(obj)->hosts = sdb_avltree_create()))
return -1;
- if ((err = pthread_rwlock_init(&SDB_STORE(obj)->host_lock,
+ if ((err = pthread_rwlock_init(&SDB_MEMSTORE(obj)->host_lock,
/* attr = */ NULL))) {
char errbuf[128];
sdb_log(SDB_LOG_ERR, "store: Failed to initialize lock: %s",
store_destroy(sdb_object_t *obj)
{
int err;
- if ((err = pthread_rwlock_destroy(&SDB_STORE(obj)->host_lock))) {
+ if ((err = pthread_rwlock_destroy(&SDB_MEMSTORE(obj)->host_lock))) {
char errbuf[128];
sdb_log(SDB_LOG_ERR, "store: Failed to destroy lock: %s",
sdb_strerror(err, errbuf, sizeof(errbuf)));
return;
}
- sdb_avltree_destroy(SDB_STORE(obj)->hosts);
- SDB_STORE(obj)->hosts = NULL;
+ sdb_avltree_destroy(SDB_MEMSTORE(obj)->hosts);
+ SDB_MEMSTORE(obj)->hosts = NULL;
} /* store_destroy */
static int
store_obj_init(sdb_object_t *obj, va_list ap)
{
- sdb_store_obj_t *sobj = STORE_OBJ(obj);
+ sdb_memstore_obj_t *sobj = STORE_OBJ(obj);
sobj->type = va_arg(ap, int);
static void
store_obj_destroy(sdb_object_t *obj)
{
- sdb_store_obj_t *sobj = STORE_OBJ(obj);
+ sdb_memstore_obj_t *sobj = STORE_OBJ(obj);
size_t i;
for (i = 0; i < sobj->backends_num; ++i)
static int
metric_init(sdb_object_t *obj, va_list ap)
{
- sdb_metric_t *sobj = METRIC(obj);
+ metric_t *sobj = METRIC(obj);
int ret;
/* this will consume the first argument (type) of ap */
static void
metric_destroy(sdb_object_t *obj)
{
- sdb_metric_t *sobj = METRIC(obj);
+ metric_t *sobj = METRIC(obj);
assert(obj);
store_obj_destroy(obj);
} /* attr_destroy */
static sdb_type_t store_type = {
- /* size = */ sizeof(sdb_store_t),
+ /* size = */ sizeof(sdb_memstore_t),
/* init = */ store_init,
/* destroy = */ store_destroy,
};
};
static sdb_type_t metric_type = {
- /* size = */ sizeof(sdb_metric_t),
+ /* size = */ sizeof(metric_t),
/* init = */ metric_init,
/* destroy = */ metric_destroy
};
*/
static int
-record_backend(sdb_store_obj_t *obj)
+record_backends(sdb_memstore_obj_t *obj,
+ const char * const *backends, size_t backends_num)
{
- const sdb_plugin_info_t *info;
char **tmp;
size_t i;
- info = sdb_plugin_current();
- if (! info)
- return 0;
+ for (i = 0; i < backends_num; i++) {
+ bool found = 0;
+ size_t j;
- for (i = 0; i < obj->backends_num; ++i)
- if (!strcasecmp(obj->backends[i], info->plugin_name))
- return 0;
+ for (j = 0; j < obj->backends_num; ++j) {
+ if (!strcasecmp(obj->backends[j], backends[i])) {
+ found = 1;
+ break;
+ }
+ }
+ if (found)
+ continue;
- tmp = realloc(obj->backends,
- (obj->backends_num + 1) * sizeof(*obj->backends));
- if (! tmp)
- return -1;
+ tmp = realloc(obj->backends,
+ (obj->backends_num + 1) * sizeof(*obj->backends));
+ if (! tmp)
+ return -1;
- obj->backends = tmp;
- obj->backends[obj->backends_num] = strdup(info->plugin_name);
- if (! obj->backends[obj->backends_num])
- return -1;
+ obj->backends = tmp;
+ obj->backends[obj->backends_num] = strdup(backends[i]);
+ if (! obj->backends[obj->backends_num])
+ return -1;
- ++obj->backends_num;
+ ++obj->backends_num;
+ }
return 0;
-} /* record_backend */
+} /* record_backends */
static int
-store_obj(store_obj_t *obj, sdb_store_obj_t **updated_obj)
+store_obj(store_obj_t *obj, sdb_memstore_obj_t **updated_obj)
{
- sdb_store_obj_t *old, *new;
+ sdb_memstore_obj_t *old, *new;
int status = 0;
assert(obj->parent_tree);
if (updated_obj)
*updated_obj = new;
- if (record_backend(new))
+ if (record_backends(new, obj->backends, obj->backends_num))
return -1;
return status;
} /* store_obj */
static int
-store_metric_store(sdb_metric_t *metric, sdb_store_metric_t *m)
+store_metric_store(metric_t *metric, sdb_store_metric_t *m)
{
char *type = metric->store.type;
char *id = metric->store.id;
return host->services;
} /* get_host_children */
-/*
- * ts_tojson serializes a time-series to JSON.
- *
- * The function never returns an error. Rather, an error message will be part
- * of the serialized data.
- */
-static void
-ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
-{
- char start_str[64];
- char end_str[64];
-
- size_t i;
-
- /* TODO: make time format configurable */
- if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
- snprintf(start_str, sizeof(start_str), "<error>");
- start_str[sizeof(start_str) - 1] = '\0';
- if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
- snprintf(end_str, sizeof(end_str), "<error>");
- end_str[sizeof(end_str) - 1] = '\0';
-
- sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
- start_str, end_str);
-
- for (i = 0; i < ts->data_names_len; ++i) {
- size_t j;
- sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
-
- for (j = 0; j < ts->data_len; ++j) {
- char time_str[64];
-
- if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
- snprintf(time_str, sizeof(time_str), "<error>");
- time_str[sizeof(time_str) - 1] = '\0';
-
- /* Some GNU libc versions may print '-nan' which we dont' want */
- if (isnan(ts->data[i][j].value))
- sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
- "\"value\": \"nan\"}", time_str);
- else
- sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
- "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
-
- if (j < ts->data_len - 1)
- sdb_strbuf_append(buf, ",");
- }
-
- if (i < ts->data_names_len - 1)
- sdb_strbuf_append(buf, "],");
- else
- sdb_strbuf_append(buf, "]");
- }
- sdb_strbuf_append(buf, "}}");
-} /* ts_tojson */
-
/*
* store writer API
*/
static int
store_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
{
- sdb_store_t *st = SDB_STORE(user_data);
+ sdb_memstore_t *st = SDB_MEMSTORE(user_data);
store_obj_t obj = STORE_OBJ_INIT;
- sdb_store_obj_t *new = NULL;
+ sdb_memstore_obj_t *new = NULL;
const char *hostname;
host_t *host;
obj.type = SDB_ATTRIBUTE;
obj.name = attr->key;
obj.last_update = attr->last_update;
+ obj.backends = attr->backends;
+ obj.backends_num = attr->backends_num;
if (! status)
status = store_obj(&obj, &new);
static int
store_host(sdb_store_host_t *host, sdb_object_t *user_data)
{
- sdb_store_t *st = SDB_STORE(user_data);
- store_obj_t obj = { NULL, st->hosts, SDB_HOST, NULL, 0 };
+ sdb_memstore_t *st = SDB_MEMSTORE(user_data);
+ store_obj_t obj = { NULL, st->hosts, SDB_HOST, NULL, 0, NULL, 0 };
int status = 0;
if ((! host) || (! host->name))
obj.name = host->name;
obj.last_update = host->last_update;
+ obj.backends = host->backends;
+ obj.backends_num = host->backends_num;
pthread_rwlock_wrlock(&st->host_lock);
status = store_obj(&obj, NULL);
pthread_rwlock_unlock(&st->host_lock);
static int
store_service(sdb_store_service_t *service, sdb_object_t *user_data)
{
- sdb_store_t *st = SDB_STORE(user_data);
+ sdb_memstore_t *st = SDB_MEMSTORE(user_data);
store_obj_t obj = STORE_OBJ_INIT;
host_t *host;
obj.name = service->name;
obj.last_update = service->last_update;
+ obj.backends = service->backends;
+ obj.backends_num = service->backends_num;
if (! status)
status = store_obj(&obj, NULL);
static int
store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
{
- sdb_store_t *st = SDB_STORE(user_data);
+ sdb_memstore_t *st = SDB_MEMSTORE(user_data);
store_obj_t obj = STORE_OBJ_INIT;
- sdb_store_obj_t *new = NULL;
+ sdb_memstore_obj_t *new = NULL;
host_t *host;
int status = 0;
obj.name = metric->name;
obj.last_update = metric->last_update;
+ obj.backends = metric->backends;
+ obj.backends_num = metric->backends_num;
if (! status)
status = store_obj(&obj, &new);
sdb_object_deref(SDB_OBJ(host));
return status;
} /* store_metric */
-sdb_store_writer_t sdb_store_writer = {
+sdb_store_writer_t sdb_memstore_writer = {
store_host, store_service, store_metric, store_attribute,
};
sdb_strbuf_t __attribute__((unused)) *errbuf,
sdb_object_t __attribute__((unused)) *user_data)
{
- return SDB_OBJ(sdb_store_query_prepare(ast));
+ return SDB_OBJ(sdb_memstore_query_prepare(ast));
} /* prepare_query */
static int
execute_query(sdb_object_t *q,
- sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
+ sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf,
sdb_object_t *user_data)
{
- return sdb_store_query_execute(SDB_STORE(user_data),
- QUERY(q), buf, errbuf);
+ return sdb_memstore_query_execute(SDB_MEMSTORE(user_data),
+ QUERY(q), w, wd, errbuf);
} /* execute_query */
-sdb_store_reader_t sdb_store_reader = {
+sdb_store_reader_t sdb_memstore_reader = {
prepare_query, execute_query,
};
* public API
*/
-sdb_store_t *
-sdb_store_create(void)
+sdb_memstore_t *
+sdb_memstore_create(void)
{
- return SDB_STORE(sdb_object_create("store", store_type));
-} /* sdb_store_create */
+ return SDB_MEMSTORE(sdb_object_create("store", store_type));
+} /* sdb_memstore_create */
int
-sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
+sdb_memstore_host(sdb_memstore_t *store, const char *name, sdb_time_t last_update)
{
sdb_store_host_t host = {
name, last_update, 0, NULL, 0,
};
return store_host(&host, SDB_OBJ(store));
-} /* sdb_store_host */
+} /* sdb_memstore_host */
int
-sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
+sdb_memstore_service(sdb_memstore_t *store, const char *hostname, const char *name,
sdb_time_t last_update)
{
sdb_store_service_t service = {
hostname, name, last_update, 0, NULL, 0,
};
return store_service(&service, SDB_OBJ(store));
-} /* sdb_store_service */
+} /* sdb_memstore_service */
int
-sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
+sdb_memstore_metric(sdb_memstore_t *store, const char *hostname, const char *name,
sdb_metric_store_t *metric_store, sdb_time_t last_update)
{
sdb_store_metric_t metric = {
metric.store.id = metric_store->id;
}
return store_metric(&metric, SDB_OBJ(store));
-} /* sdb_store_metric */
+} /* sdb_memstore_metric */
int
-sdb_store_attribute(sdb_store_t *store, const char *hostname,
+sdb_memstore_attribute(sdb_memstore_t *store, const char *hostname,
const char *key, const sdb_data_t *value, sdb_time_t last_update)
{
sdb_store_attribute_t attr = {
attr.value = *value;
}
return store_attribute(&attr, SDB_OBJ(store));
-} /* sdb_store_attribute */
+} /* sdb_memstore_attribute */
int
-sdb_store_service_attr(sdb_store_t *store, const char *hostname,
+sdb_memstore_service_attr(sdb_memstore_t *store, const char *hostname,
const char *service, const char *key, const sdb_data_t *value,
sdb_time_t last_update)
{
attr.value = *value;
}
return store_attribute(&attr, SDB_OBJ(store));
-} /* sdb_store_service_attr */
+} /* sdb_memstore_service_attr */
int
-sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
+sdb_memstore_metric_attr(sdb_memstore_t *store, const char *hostname,
const char *metric, const char *key, const sdb_data_t *value,
sdb_time_t last_update)
{
attr.value = *value;
}
return store_attribute(&attr, SDB_OBJ(store));
-} /* sdb_store_metric_attr */
+} /* sdb_memstore_metric_attr */
-sdb_store_obj_t *
-sdb_store_get_host(sdb_store_t *store, const char *name)
+sdb_memstore_obj_t *
+sdb_memstore_get_host(sdb_memstore_t *store, const char *name)
{
host_t *host;
return NULL;
return STORE_OBJ(host);
-} /* sdb_store_get_host */
+} /* sdb_memstore_get_host */
-sdb_store_obj_t *
-sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
+sdb_memstore_obj_t *
+sdb_memstore_get_child(sdb_memstore_obj_t *host, int type, const char *name)
{
sdb_avltree_t *children;
if (! children)
return NULL;
return STORE_OBJ(sdb_avltree_lookup(children, name));
-} /* sdb_store_get_child */
+} /* sdb_memstore_get_child */
int
-sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
+sdb_memstore_get_field(sdb_memstore_obj_t *obj, int field, sdb_data_t *res)
{
sdb_data_t tmp;
else
sdb_data_free_datum(&tmp);
return 0;
-} /* sdb_store_get_field */
+} /* sdb_memstore_get_field */
int
-sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
- sdb_store_matcher_t *filter)
+sdb_memstore_get_attr(sdb_memstore_obj_t *obj, const char *name, sdb_data_t *res,
+ sdb_memstore_matcher_t *filter)
{
sdb_avltree_t *tree = NULL;
- sdb_store_obj_t *attr;
+ sdb_memstore_obj_t *attr;
if ((! obj) || (! name))
return -1;
attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
if (! attr)
return -1;
- if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
+ if (filter && (! sdb_memstore_matcher_matches(filter, attr, NULL))) {
sdb_object_deref(SDB_OBJ(attr));
return -1;
}
sdb_data_copy(res, &ATTR(attr)->value);
sdb_object_deref(SDB_OBJ(attr));
return 0;
-} /* sdb_store_get_attr */
-
-/* TODO: sdb_store_fetch_timeseries should move into the plugin module */
-
-int
-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;
- host_t *host;
- sdb_metric_t *m;
-
- sdb_timeseries_t *ts;
-
- int status = 0;
-
- if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf))
- return -1;
-
- pthread_rwlock_rdlock(&store->host_lock);
- host = HOST(sdb_avltree_lookup(store->hosts, hostname));
- 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(&store->host_lock);
- return -1;
- }
-
- m = METRIC(sdb_avltree_lookup(metrics, 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(&store->host_lock);
- return -1;
- }
-
- if ((! m->store.type) || (! m->store.id)) {
- sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
- "- no data-store configured for the stored metric",
- hostname, metric);
- sdb_object_deref(SDB_OBJ(m));
- pthread_rwlock_unlock(&store->host_lock);
- return -1;
- }
-
- {
- char type[strlen(m->store.type) + 1];
- char id[strlen(m->store.id) + 1];
-
- strncpy(type, m->store.type, sizeof(type));
- strncpy(id, m->store.id, sizeof(id));
- pthread_rwlock_unlock(&store->host_lock);
-
- ts = sdb_plugin_fetch_timeseries(type, id, opts);
- if (! ts) {
- sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
- "- %s fetcher callback returned no data for '%s'",
- hostname, metric, type, id);
- status = -1;
- }
- }
-
- ts_tojson(ts, buf);
- sdb_object_deref(SDB_OBJ(m));
- sdb_timeseries_destroy(ts);
- return status;
-} /* sdb_store_fetch_timeseries */
+} /* sdb_memstore_get_attr */
int
-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_memstore_scan(sdb_memstore_t *store, int type,
+ sdb_memstore_matcher_t *m, sdb_memstore_matcher_t *filter,
+ sdb_memstore_lookup_cb cb, void *user_data)
{
sdb_avltree_iter_t *host_iter = NULL;
int status = 0;
/* has_next returns false if the iterator is NULL */
while (sdb_avltree_iter_has_next(host_iter)) {
- sdb_store_obj_t *host;
+ sdb_memstore_obj_t *host;
sdb_avltree_iter_t *iter = NULL;
host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
assert(host);
- if (! sdb_store_matcher_matches(filter, host, NULL))
+ if (! sdb_memstore_matcher_matches(filter, host, NULL))
continue;
if (type == SDB_SERVICE)
if (iter) {
while (sdb_avltree_iter_has_next(iter)) {
- sdb_store_obj_t *obj;
+ sdb_memstore_obj_t *obj;
obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
assert(obj);
- if (sdb_store_matcher_matches(m, obj, filter)) {
+ if (sdb_memstore_matcher_matches(m, obj, filter)) {
if (cb(obj, filter, user_data)) {
sdb_log(SDB_LOG_ERR, "store: Callback returned "
"an error while scanning");
}
}
}
- else if (sdb_store_matcher_matches(m, host, filter)) {
+ else if (sdb_memstore_matcher_matches(m, host, filter)) {
if (cb(host, filter, user_data)) {
sdb_log(SDB_LOG_ERR, "store: Callback returned "
"an error while scanning");
sdb_avltree_iter_destroy(host_iter);
pthread_rwlock_unlock(&store->host_lock);
return status;
-} /* sdb_store_scan */
+} /* sdb_memstore_scan */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */