// SPDX-License-Identifier: GPL-3.0-or-later
#include "internal.h"
static uint64_t rrdcontext_get_next_version(RRDCONTEXT *rc);
static bool check_if_cloud_version_changed_unsafe(RRDCONTEXT *rc, bool sending __maybe_unused);
static void rrdcontext_delete_from_sql_unsafe(RRDCONTEXT *rc);
static void rrdcontext_dequeue_from_post_processing(RRDCONTEXT *rc);
static void rrdcontext_post_process_updates(RRDCONTEXT *rc, bool force, RRD_FLAGS reason, bool worker_jobs);
static void rrdcontext_garbage_collect_single_host(RRDHOST *host, bool worker_jobs);
static void rrdcontext_garbage_collect_for_all_hosts(void);
extern usec_t rrdcontext_next_db_rotation_ut;
// ----------------------------------------------------------------------------
// load from SQL
static void rrdinstance_load_clabel(SQL_CLABEL_DATA *sld, void *data) {
RRDINSTANCE *ri = data;
rrdlabels_add(ri->rrdlabels, sld->label_key, sld->label_value, sld->label_source);
}
static void rrdinstance_load_dimension(SQL_DIMENSION_DATA *sd, void *data) {
RRDINSTANCE *ri = data;
RRDMETRIC trm = {
.id = string_strdupz(sd->id),
.name = string_strdupz(sd->name),
.flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomic
};
if(sd->hidden) trm.flags |= RRD_FLAG_HIDDEN;
uuid_copy(trm.uuid, sd->dim_id);
dictionary_set(ri->rrdmetrics, string2str(trm.id), &trm, sizeof(trm));
}
static void rrdinstance_load_chart_callback(SQL_CHART_DATA *sc, void *data) {
RRDHOST *host = data;
RRDCONTEXT tc = {
.id = string_strdupz(sc->context),
.title = string_strdupz(sc->title),
.units = string_strdupz(sc->units),
.family = string_strdupz(sc->family),
.priority = sc->priority,
.chart_type = sc->chart_type,
.flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomics
.rrdhost = host,
};
RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_set_and_acquire_item(host->rrdctx.contexts, string2str(tc.id), &tc, sizeof(tc));
RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
RRDINSTANCE tri = {
.id = string_strdupz(sc->id),
.name = string_strdupz(sc->name),
.title = string_strdupz(sc->title),
.units = string_strdupz(sc->units),
.family = string_strdupz(sc->family),
.chart_type = sc->chart_type,
.priority = sc->priority,
.update_every_s = sc->update_every,
.flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomics
};
uuid_copy(tri.uuid, sc