diff options
author | Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com> | 2022-06-16 09:53:35 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-16 16:53:35 +0300 |
commit | 100a12c6cc01222b1518e5e50d2147f592d8a111 (patch) | |
tree | 1aa8f4a5f94f05dd10a07634e60544a523ba75fd /database/storage_engine.c | |
parent | 131e5f5f6e5822a1fd8107c228f1a9a49f08e847 (diff) |
Configurable storage engine for Netdata agents: step 3 (#12892)
* storage engine: add host context API
Add a new API to allow storage engines to manage host contexts.
* Replace single global context with per-engine global context
* Context is full managed by storage engines: a storage engine
can use no context, a global engine context, per host contexts,
or a mix of these.
* Currently, only dbengine uses contexts.
Following the current logic, legacy hosts use their own context,
while non-legacy hosts share the global context.
* storage engine: use empty function instead of null for context ops
* rrdhost: don't check return value for void call
* rrdhost: create context with host
* storage engine: move rrddim ops to rrddim_mem.{c,h}
* storage engine: don't use NULL for end-of-list marker
* storage engine: fallback to default engine
Diffstat (limited to 'database/storage_engine.c')
-rw-r--r-- | database/storage_engine.c | 194 |
1 files changed, 147 insertions, 47 deletions
diff --git a/database/storage_engine.c b/database/storage_engine.c index 36f01de166..0bfb19cb66 100644 --- a/database/storage_engine.c +++ b/database/storage_engine.c @@ -5,68 +5,144 @@ #ifdef ENABLE_DBENGINE #include "engine/rrdengineapi.h" #endif - -#define im_collect_ops { \ - .init = rrddim_collect_init,\ - .store_metric = rrddim_collect_store_metric,\ - .finalize = rrddim_collect_finalize\ -} - -#define im_query_ops { \ - .init = rrddim_query_init, \ - .next_metric = rrddim_query_next_metric, \ - .is_finished = rrddim_query_is_finished, \ - .finalize = rrddim_query_finalize, \ - .latest_time = rrddim_query_latest_time, \ - .oldest_time = rrddim_query_oldest_time \ -} +#include "libnetdata/libnetdata.h" static STORAGE_ENGINE engines[] = { { .id = RRD_MEMORY_MODE_NONE, .name = RRD_MEMORY_MODE_NONE_NAME, .api = { - .collect_ops = im_collect_ops, - .query_ops = im_query_ops - } + .engine_ops = { + .create = rrddim_storage_engine_instance_new, + .exit = rrddim_storage_engine_instance_exit, + .destroy = rrddim_storage_engine_instance_destroy + }, + .collect_ops = { + .init = rrddim_collect_init, + .store_metric = rrddim_collect_store_metric, + .finalize = rrddim_collect_finalize + }, + .query_ops = { + .init = rrddim_query_init, + .next_metric = rrddim_query_next_metric, + .is_finished = rrddim_query_is_finished, + .finalize = rrddim_query_finalize, + .latest_time = rrddim_query_latest_time, + .oldest_time = rrddim_query_oldest_time + } + }, + .context = NULL }, { .id = RRD_MEMORY_MODE_RAM, .name = RRD_MEMORY_MODE_RAM_NAME, .api = { - .collect_ops = im_collect_ops, - .query_ops = im_query_ops - } + .engine_ops = { + .create = rrddim_storage_engine_instance_new, + .exit = rrddim_storage_engine_instance_exit, + .destroy = rrddim_storage_engine_instance_destroy + }, + .collect_ops = { + .init = rrddim_collect_init, + .store_metric = rrddim_collect_store_metric, + .finalize = rrddim_collect_finalize + }, + .query_ops = { + .init = rrddim_query_init, + .next_metric = rrddim_query_next_metric, + .is_finished = rrddim_query_is_finished, + .finalize = rrddim_query_finalize, + .latest_time = rrddim_query_latest_time, + .oldest_time = rrddim_query_oldest_time + } + }, + .context = NULL }, { .id = RRD_MEMORY_MODE_MAP, .name = RRD_MEMORY_MODE_MAP_NAME, .api = { - .collect_ops = im_collect_ops, - .query_ops = im_query_ops - } + .engine_ops = { + .create = rrddim_storage_engine_instance_new, + .exit = rrddim_storage_engine_instance_exit, + .destroy = rrddim_storage_engine_instance_destroy + }, + .collect_ops = { + .init = rrddim_collect_init, + .store_metric = rrddim_collect_store_metric, + .finalize = rrddim_collect_finalize + }, + .query_ops = { + .init = rrddim_query_init, + .next_metric = rrddim_query_next_metric, + .is_finished = rrddim_query_is_finished, + .finalize = rrddim_query_finalize, + .latest_time = rrddim_query_latest_time, + .oldest_time = rrddim_query_oldest_time + } + }, + .context = NULL }, { .id = RRD_MEMORY_MODE_SAVE, .name = RRD_MEMORY_MODE_SAVE_NAME, .api = { - .collect_ops = im_collect_ops, - .query_ops = im_query_ops - } + .engine_ops = { + .create = rrddim_storage_engine_instance_new, + .exit = rrddim_storage_engine_instance_exit, + .destroy = rrddim_storage_engine_instance_destroy + }, + .collect_ops = { + .init = rrddim_collect_init, + .store_metric = rrddim_collect_store_metric, + .finalize = rrddim_collect_finalize + }, + .query_ops = { + .init = rrddim_query_init, + .next_metric = rrddim_query_next_metric, + .is_finished = rrddim_query_is_finished, + .finalize = rrddim_query_finalize, + .latest_time = rrddim_query_latest_time, + .oldest_time = rrddim_query_oldest_time + } + }, + .context = NULL }, { .id = RRD_MEMORY_MODE_ALLOC, .name = RRD_MEMORY_MODE_ALLOC_NAME, .api = { - .collect_ops = im_collect_ops, - .query_ops = im_query_ops - } + .engine_ops = { + .create = rrddim_storage_engine_instance_new, + .exit = rrddim_storage_engine_instance_exit, + .destroy = rrddim_storage_engine_instance_destroy + }, + .collect_ops = { + .init = rrddim_collect_init, + .store_metric = rrddim_collect_store_metric, + .finalize = rrddim_collect_finalize + }, + .query_ops = { + .init = rrddim_query_init, + .next_metric = rrddim_query_next_metric, + .is_finished = rrddim_query_is_finished, + .finalize = rrddim_query_finalize, + .latest_time = rrddim_query_latest_time, + .oldest_time = rrddim_query_oldest_time + } + }, + .context = NULL }, #ifdef ENABLE_DBENGINE { .id = RRD_MEMORY_MODE_DBENGINE, .name = RRD_MEMORY_MODE_DBENGINE_NAME, .api = { + .engine_ops = { + .create = rrdeng_init, + .exit = rrdeng_prepare_exit, + .destroy = rrdeng_exit + }, .collect_ops = { .init = rrdeng_store_metric_init, .store_metric = rrdeng_store_metric_next, @@ -80,28 +156,34 @@ static STORAGE_ENGINE engines[] = { .latest_time = rrdeng_metric_latest_time, .oldest_time = rrdeng_metric_oldest_time } - } - }, + }, + .context = NULL + } #endif - { .id = RRD_MEMORY_MODE_NONE, .name = NULL } }; +const size_t engine_count = sizeof(engines) / sizeof(engines[0]); + STORAGE_ENGINE* storage_engine_find(const char* name) { - for (STORAGE_ENGINE* it = engines; it->name; it++) { - if (strcmp(it->name, name) == 0) - return it; - } - return NULL; + for (size_t i = 0; i != engine_count; i++) + if (strcmp(engines[i].name, name) == 0) + return &engines[i]; + + error("No storage engine found for memory mode %s.", name); + return storage_engine_get(default_rrd_memory_mode); } STORAGE_ENGINE* storage_engine_get(RRD_MEMORY_MODE mmode) { - for (STORAGE_ENGINE* it = engines; it->name; it++) { - if (it->id == mmode) - return it; - } - return NULL; + for (size_t i = 0; i != engine_count; i++) + if (engines[i].id == mmode) + return &engines[i]; + STORAGE_ENGINE* eng = mmode == default_rrd_memory_mode + ? &engines[0] // default engine not available, use NONE + : storage_engine_get(default_rrd_memory_mode); + error("No storage engine for memory mode %u, will use %s (%u) instead.", mmode, eng->name, eng->id); + return eng; } STORAGE_ENGINE* storage_engine_foreach_init() @@ -112,9 +194,27 @@ STORAGE_ENGINE* storage_engine_foreach_init() STORAGE_ENGINE* storage_engine_foreach_next(STORAGE_ENGINE* it) { - if (!it || !it->name) - return NULL; - it++; - return it->name ? it : NULL; + return (it >= &engines[engine_count]) ? NULL : it; +} + +STORAGE_ENGINE_INSTANCE* storage_engine_new(STORAGE_ENGINE* eng, RRDHOST *host) +{ + STORAGE_ENGINE_INSTANCE* instance = host->rrdeng_ctx; + if (!instance && eng) { + instance = eng->api.engine_ops.create(eng, host); + if (instance) { + instance->engine = eng; + } + } + return instance; +} + +void storage_engine_delete(STORAGE_ENGINE_INSTANCE* instance) { + if (instance) { + STORAGE_ENGINE* eng = instance->engine; + if (eng) { + eng->api.engine_ops.destroy(instance); + } + } } |