summaryrefslogtreecommitdiffstats
path: root/database/rrdsetvar.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-30 18:47:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-30 18:47:05 +0000
commit97e01009d69b8fbebfebf68f51e3d126d0ed43fc (patch)
tree02e8b836c3a9d89806f3e67d4a5fe9f52dbb0061 /database/rrdsetvar.c
parentReleasing debian version 1.36.1-1. (diff)
downloadnetdata-97e01009d69b8fbebfebf68f51e3d126d0ed43fc.tar.xz
netdata-97e01009d69b8fbebfebf68f51e3d126d0ed43fc.zip
Merging upstream version 1.37.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'database/rrdsetvar.c')
-rw-r--r--database/rrdsetvar.c317
1 files changed, 210 insertions, 107 deletions
diff --git a/database/rrdsetvar.c b/database/rrdsetvar.c
index e520764a2..22cf8a1f0 100644
--- a/database/rrdsetvar.c
+++ b/database/rrdsetvar.c
@@ -1,190 +1,293 @@
// SPDX-License-Identifier: GPL-3.0-or-later
-#define NETDATA_HEALTH_INTERNALS
#include "rrd.h"
-// ----------------------------------------------------------------------------
-// RRDSETVAR management
-// CHART VARIABLES
+typedef struct rrdsetvar {
+ STRING *name; // variable name
+ void *value; // we need this to maintain the allocation for custom chart variables
-static inline void rrdsetvar_free_variables(RRDSETVAR *rs) {
- RRDSET *st = rs->rrdset;
+ const RRDVAR_ACQUIRED *rrdvar_local;
+ const RRDVAR_ACQUIRED *rrdvar_family_chart_id;
+ const RRDVAR_ACQUIRED *rrdvar_family_chart_name;
+ const RRDVAR_ACQUIRED *rrdvar_host_chart_id;
+ const RRDVAR_ACQUIRED *rrdvar_host_chart_name;
+
+ RRDVAR_FLAGS flags:24;
+ RRDVAR_TYPE type:8;
+} RRDSETVAR;
+
+// should only be called while the rrdsetvar dict is write locked
+// otherwise, 2+ threads may be setting the same variables at the same time
+static inline void rrdsetvar_free_rrdvars_unsafe(RRDSET *st, RRDSETVAR *rs) {
RRDHOST *host = st->rrdhost;
// ------------------------------------------------------------------------
// CHART
- rrdvar_free(host, &st->rrdvar_root_index, rs->var_local);
- rs->var_local = NULL;
+
+ if(st->rrdvars) {
+ rrdvar_release_and_del(st->rrdvars, rs->rrdvar_local);
+ rs->rrdvar_local = NULL;
+ }
// ------------------------------------------------------------------------
// FAMILY
- rrdvar_free(host, &st->rrdfamily->rrdvar_root_index, rs->var_family);
- rs->var_family = NULL;
- rrdvar_free(host, &st->rrdfamily->rrdvar_root_index, rs->var_family_name);
- rs->var_family_name = NULL;
+ if(st->rrdfamily) {
+ rrdvar_release_and_del(rrdfamily_rrdvars_dict(st->rrdfamily), rs->rrdvar_family_chart_id);
+ rs->rrdvar_family_chart_id = NULL;
+
+ rrdvar_release_and_del(rrdfamily_rrdvars_dict(st->rrdfamily), rs->rrdvar_family_chart_name);
+ rs->rrdvar_family_chart_name = NULL;
+ }
// ------------------------------------------------------------------------
// HOST
- rrdvar_free(host, &host->rrdvar_root_index, rs->var_host);
- rs->var_host = NULL;
- rrdvar_free(host, &host->rrdvar_root_index, rs->var_host_name);
- rs->var_host_name = NULL;
+ if(host->rrdvars && host->health_enabled) {
+ rrdvar_release_and_del(host->rrdvars, rs->rrdvar_host_chart_id);
+ rs->rrdvar_host_chart_id = NULL;
- // ------------------------------------------------------------------------
- // KEYS
- freez(rs->key_fullid);
- rs->key_fullid = NULL;
-
- freez(rs->key_fullname);
- rs->key_fullname = NULL;
+ rrdvar_release_and_del(host->rrdvars, rs->rrdvar_host_chart_name);
+ rs->rrdvar_host_chart_name = NULL;
+ }
}
-static inline void rrdsetvar_create_variables(RRDSETVAR *rs) {
- RRDSET *st = rs->rrdset;
+// should only be called while the rrdsetvar dict is write locked
+// otherwise, 2+ threads may be setting the same variables at the same time
+static inline void rrdsetvar_update_rrdvars_unsafe(RRDSET *st, RRDSETVAR *rs) {
RRDHOST *host = st->rrdhost;
- RRDVAR_OPTIONS options = rs->options;
- if(rs->options & RRDVAR_OPTION_ALLOCATED)
- options &= ~ RRDVAR_OPTION_ALLOCATED;
+ RRDVAR_FLAGS options = rs->flags;
+ options &= ~RRDVAR_OPTIONS_REMOVED_WHEN_PROPAGATING_TO_RRDVAR;
// ------------------------------------------------------------------------
// free the old ones (if any)
- rrdsetvar_free_variables(rs);
+ rrdsetvar_free_rrdvars_unsafe(st, rs);
// ------------------------------------------------------------------------
// KEYS
char buffer[RRDVAR_MAX_LENGTH + 1];
- snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rs->variable);
- rs->key_fullid = strdupz(buffer);
+ snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", rrdset_id(st), string2str(rs->name));
+ STRING *key_chart_id = string_strdupz(buffer);
- snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
- rs->key_fullname = strdupz(buffer);
+ snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", rrdset_name(st), string2str(rs->name));
+ STRING *key_chart_name = string_strdupz(buffer);
// ------------------------------------------------------------------------
// CHART
- rs->var_local = rrdvar_create_and_index("local", &st->rrdvar_root_index, rs->variable, rs->type, options, rs->value);
+
+ if(st->rrdvars) {
+ rs->rrdvar_local = rrdvar_add_and_acquire("local", st->rrdvars, rs->name, rs->type, options, rs->value);
+ }
// ------------------------------------------------------------------------
// FAMILY
- rs->var_family = rrdvar_create_and_index("family", &st->rrdfamily->rrdvar_root_index, rs->key_fullid, rs->type, options, rs->value);
- rs->var_family_name = rrdvar_create_and_index("family", &st->rrdfamily->rrdvar_root_index, rs->key_fullname, rs->type, options, rs->value);
+
+ if(st->rrdfamily) {
+ rs->rrdvar_family_chart_id = rrdvar_add_and_acquire("family", rrdfamily_rrdvars_dict(st->rrdfamily), key_chart_id, rs->type, options, rs->value);
+ rs->rrdvar_family_chart_name = rrdvar_add_and_acquire("family", rrdfamily_rrdvars_dict(st->rrdfamily), key_chart_name, rs->type, options, rs->value);
+ }
// ------------------------------------------------------------------------
// HOST
- rs->var_host = rrdvar_create_and_index("host", &host->rrdvar_root_index, rs->key_fullid, rs->type, options, rs->value);
- rs->var_host_name = rrdvar_create_and_index("host", &host->rrdvar_root_index, rs->key_fullname, rs->type, options, rs->value);
-}
-RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, RRDVAR_TYPE type, void *value, RRDVAR_OPTIONS options) {
- debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
- RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
+ if(host->rrdvars && host->health_enabled) {
+ rs->rrdvar_host_chart_id = rrdvar_add_and_acquire("host", host->rrdvars, key_chart_id, rs->type, options, rs->value);
+ rs->rrdvar_host_chart_name = rrdvar_add_and_acquire("host", host->rrdvars, key_chart_name, rs->type, options, rs->value);
+ }
- rs->variable = strdupz(variable);
- rs->hash = simple_hash(rs->variable);
- rs->type = type;
- rs->value = value;
- rs->options = options;
- rs->rrdset = st;
+ // free the keys
+ string_freez(key_chart_id);
+ string_freez(key_chart_name);
+}
- rs->next = st->variables;
- st->variables = rs;
+static void rrdsetvar_free_value_unsafe(RRDSETVAR *rs) {
+ if(rs->flags & RRDVAR_FLAG_ALLOCATED) {
+ void *old = rs->value;
+ rs->value = NULL;
+ rs->flags &= ~RRDVAR_FLAG_ALLOCATED;
+ freez(old);
+ }
+}
- rrdsetvar_create_variables(rs);
+static void rrdsetvar_set_value_unsafe(RRDSETVAR *rs, void *new_value) {
+ rrdsetvar_free_value_unsafe(rs);
- return rs;
+ if(new_value)
+ rs->value = new_value;
+ else {
+ NETDATA_DOUBLE *n = mallocz(sizeof(NETDATA_DOUBLE));
+ *n = NAN;
+ rs->value = n;
+ rs->flags |= RRDVAR_FLAG_ALLOCATED;
+ }
}
-void rrdsetvar_rename_all(RRDSET *st) {
- debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
+struct rrdsetvar_constructor {
+ RRDSET *rrdset;
+ const char *name;
+ void *value;
+ RRDVAR_FLAGS flags :16;
+ RRDVAR_TYPE type:8;
+};
- RRDSETVAR *rs;
- for(rs = st->variables; rs ; rs = rs->next)
- rrdsetvar_create_variables(rs);
+static void rrdsetvar_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdsetvar, void *constructor_data) {
+ RRDSETVAR *rs = rrdsetvar;
+ struct rrdsetvar_constructor *ctr = constructor_data;
+
+ ctr->flags &= ~RRDVAR_OPTIONS_REMOVED_ON_NEW_OBJECTS;
- rrdsetcalc_link_matching(st);
+ rs->name = string_strdupz(ctr->name);
+ rs->type = ctr->type;
+ rs->flags = ctr->flags;
+ rrdsetvar_set_value_unsafe(rs, ctr->value);
+
+ // create the rrdvariables while we are having a write lock to the dictionary
+ rrdsetvar_update_rrdvars_unsafe(ctr->rrdset, rs);
}
-void rrdsetvar_free(RRDSETVAR *rs) {
- RRDSET *st = rs->rrdset;
- debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
+static bool rrdsetvar_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdsetvar, void *new_rrdsetvar __maybe_unused, void *constructor_data) {
+ RRDSETVAR *rs = rrdsetvar;
+ struct rrdsetvar_constructor *ctr = constructor_data;
- if(st->variables == rs) {
- st->variables = rs->next;
- }
- else {
- RRDSETVAR *t;
- for (t = st->variables; t && t->next != rs; t = t->next);
- if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->key_fullname, st->id);
- else t->next = rs->next;
+ ctr->flags &= ~RRDVAR_OPTIONS_REMOVED_ON_NEW_OBJECTS;
+
+ RRDVAR_FLAGS options = rs->flags;
+ options &= ~RRDVAR_OPTIONS_REMOVED_ON_NEW_OBJECTS;
+
+ if(((ctr->value == NULL && rs->value != NULL && rs->flags & RRDVAR_FLAG_ALLOCATED) || (rs->value == ctr->value))
+ && ctr->flags == options && rs->type == ctr->type) {
+ // don't reset it - everything is the same, or as it should...
+ return false;
}
- rrdsetvar_free_variables(rs);
+ internal_error(true, "RRDSETVAR: resetting variable '%s' of chart '%s' of host '%s', options from 0x%x to 0x%x, type from %d to %d",
+ string2str(rs->name), rrdset_id(ctr->rrdset), rrdhost_hostname(ctr->rrdset->rrdhost),
+ options, ctr->flags, rs->type, ctr->type);
- freez(rs->variable);
+ rrdsetvar_free_value_unsafe(rs); // we are going to change the options, so free it before setting it
+ rs->flags = ctr->flags;
+ rs->type = ctr->type;
+ rrdsetvar_set_value_unsafe(rs, ctr->value);
- if(rs->options & RRDVAR_OPTION_ALLOCATED)
- freez(rs->value);
+ // recreate the rrdvariables while we are having a write lock to the dictionary
+ rrdsetvar_update_rrdvars_unsafe(ctr->rrdset, rs);
+ return true;
+}
+
+static void rrdsetvar_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdsetvar, void *rrdset __maybe_unused) {
+ RRDSET *st = rrdset;
+ RRDSETVAR *rs = rrdsetvar;
- freez(rs);
+ rrdsetvar_free_rrdvars_unsafe(st, rs);
+ rrdsetvar_free_value_unsafe(rs);
+ string_freez(rs->name);
+ rs->name = NULL;
}
-// --------------------------------------------------------------------------------------------------------------------
-// custom chart variables
+void rrdsetvar_index_init(RRDSET *st) {
+ if(!st->rrdsetvar_root_index) {
+ st->rrdsetvar_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
-RRDSETVAR *rrdsetvar_custom_chart_variable_create(RRDSET *st, const char *name) {
- RRDHOST *host = st->rrdhost;
+ dictionary_register_insert_callback(st->rrdsetvar_root_index, rrdsetvar_insert_callback, NULL);
+ dictionary_register_conflict_callback(st->rrdsetvar_root_index, rrdsetvar_conflict_callback, NULL);
+ dictionary_register_delete_callback(st->rrdsetvar_root_index, rrdsetvar_delete_callback, st);
+ }
+}
- char *n = strdupz(name);
- rrdvar_fix_name(n);
- uint32_t hash = simple_hash(n);
+void rrdsetvar_index_destroy(RRDSET *st) {
+ dictionary_destroy(st->rrdsetvar_root_index);
+ st->rrdsetvar_root_index = NULL;
+}
- rrdset_wrlock(st);
+const RRDSETVAR_ACQUIRED *rrdsetvar_add_and_acquire(RRDSET *st, const char *name, RRDVAR_TYPE type, void *value, RRDVAR_FLAGS flags) {
+ struct rrdsetvar_constructor tmp = {
+ .name = name,
+ .type = type,
+ .value = value,
+ .flags = flags,
+ .rrdset = st,
+ };
+
+ const RRDSETVAR_ACQUIRED *rsa = (const RRDSETVAR_ACQUIRED *)dictionary_set_and_acquire_item_advanced(st->rrdsetvar_root_index, name, -1, NULL, sizeof(RRDSETVAR), &tmp);
+ return rsa;
+}
+
+void rrdsetvar_add_and_leave_released(RRDSET *st, const char *name, RRDVAR_TYPE type, void *value, RRDVAR_FLAGS flags) {
+ const RRDSETVAR_ACQUIRED *rsa = rrdsetvar_add_and_acquire(st, name, type, value, flags);
+ dictionary_acquired_item_release(st->rrdsetvar_root_index, (const DICTIONARY_ITEM *)rsa);
+}
+
+void rrdsetvar_rename_all(RRDSET *st) {
+ debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", rrdset_id(st), rrdset_name(st));
- // find it
RRDSETVAR *rs;
- for(rs = st->variables; rs ; rs = rs->next) {
- if(hash == rs->hash && strcmp(n, rs->variable) == 0) {
- rrdset_unlock(st);
- if(rs->options & RRDVAR_OPTION_CUSTOM_CHART_VAR) {
- freez(n);
- return rs;
- }
- else {
- error("RRDSETVAR: custom variable '%s' on chart '%s' of host '%s', conflicts with an internal chart variable", n, st->id, host->hostname);
- freez(n);
- return NULL;
- }
- }
+ dfe_start_write(st->rrdsetvar_root_index, rs) {
+ // should only be called while the rrdsetvar dict is write locked
+ rrdsetvar_update_rrdvars_unsafe(st, rs);
}
+ dfe_done(rs);
- // not found, allocate one
+ rrdcalc_link_matching_alerts_to_rrdset(st);
+}
- NETDATA_DOUBLE *v = mallocz(sizeof(NETDATA_DOUBLE));
- *v = NAN;
+void rrdsetvar_release_and_delete_all(RRDSET *st) {
+ RRDSETVAR *rs;
+ dfe_start_write(st->rrdsetvar_root_index, rs) {
+ dictionary_del_advanced(st->rrdsetvar_root_index, string2str(rs->name), (ssize_t)string_strlen(rs->name) + 1);
+ }
+ dfe_done(rs);
+}
- rs = rrdsetvar_create(st, n, RRDVAR_TYPE_CALCULATED, v, RRDVAR_OPTION_ALLOCATED|RRDVAR_OPTION_CUSTOM_CHART_VAR);
- rrdset_unlock(st);
+void rrdsetvar_release(DICTIONARY *dict, const RRDSETVAR_ACQUIRED *rsa) {
+ dictionary_acquired_item_release(dict, (const DICTIONARY_ITEM *)rsa);
+}
+
+// --------------------------------------------------------------------------------------------------------------------
+// custom chart variables
- freez(n);
+const RRDSETVAR_ACQUIRED *rrdsetvar_custom_chart_variable_add_and_acquire(RRDSET *st, const char *name) {
+ STRING *name_string = rrdvar_name_to_string(name);
+ const RRDSETVAR_ACQUIRED *rs = rrdsetvar_add_and_acquire(st, string2str(name_string), RRDVAR_TYPE_CALCULATED, NULL, RRDVAR_FLAG_CUSTOM_CHART_VAR);
+ string_freez(name_string);
return rs;
}
-void rrdsetvar_custom_chart_variable_set(RRDSETVAR *rs, NETDATA_DOUBLE value) {
- if(rs->type != RRDVAR_TYPE_CALCULATED || !(rs->options & RRDVAR_OPTION_CUSTOM_CHART_VAR) || !(rs->options & RRDVAR_OPTION_ALLOCATED)) {
+void rrdsetvar_custom_chart_variable_set(RRDSET *st, const RRDSETVAR_ACQUIRED *rsa, NETDATA_DOUBLE value) {
+ if(!rsa) return;
+
+ RRDSETVAR *rs = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rsa);
+
+ if(rs->type != RRDVAR_TYPE_CALCULATED || !(rs->flags & RRDVAR_FLAG_CUSTOM_CHART_VAR) || !(rs->flags & RRDVAR_FLAG_ALLOCATED)) {
error("RRDSETVAR: requested to set variable '%s' of chart '%s' on host '%s' to value " NETDATA_DOUBLE_FORMAT
- " but the variable is not a custom chart one.", rs->variable, rs->rrdset->id, rs->rrdset->rrdhost->hostname, value);
+ " but the variable is not a custom chart one (it has options 0x%x, value pointer %p). Ignoring request.", string2str(rs->name), rrdset_id(st), rrdhost_hostname(st->rrdhost), value, (uint32_t)rs->flags, rs->value);
}
else {
NETDATA_DOUBLE *v = rs->value;
if(*v != value) {
*v = value;
+ rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_SEND_VARIABLES);
+ }
+ }
+}
+
+void rrdsetvar_print_to_streaming_custom_chart_variables(RRDSET *st, BUFFER *wb) {
+ rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND_VARIABLES);
- // mark the chart to be sent upstream
- rrdset_flag_clear(rs->rrdset, RRDSET_FLAG_UPSTREAM_EXPOSED);
+ // send the chart local custom variables
+ RRDSETVAR *rs;
+ dfe_start_read(st->rrdsetvar_root_index, rs) {
+ if(unlikely(rs->type == RRDVAR_TYPE_CALCULATED && rs->flags & RRDVAR_FLAG_CUSTOM_CHART_VAR)) {
+ NETDATA_DOUBLE *value = (NETDATA_DOUBLE *) rs->value;
+
+ buffer_sprintf(wb
+ , "VARIABLE CHART %s = " NETDATA_DOUBLE_FORMAT "\n"
+ , string2str(rs->name)
+ , *value
+ );
}
}
+ dfe_done(rs);
}