@cryptotaxi247 / netdata-1 / commits / 0b534374b

fix loading contexts (#19404)

Costa Tsaousis committed Jan 15, 2025 at 11:25 UTC 0b534374b33b64af867154f601f3058258497a6f
3 files changed +46 -24
src/database/contexts/contexts-loading.c
+24 -9
@@ -16,12 +16,22 @@ void load_instance_labels_on_demand(nd_uuid_t *uuid, void *data) {
16
17 static void rrdinstance_load_dimension_callback(SQL_DIMENSION_DATA *sd, void *data) {
18 RRDHOST *host = data;
19 +
20 + UUIDMAP_ID id = uuidmap_create(sd->dim_id);
21 + time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
22 + get_metric_retention_by_id(host, id, &min_first_time_t, &max_last_time_t);
23 + if((!min_first_time_t || min_first_time_t == LONG_MAX) && !max_last_time_t) {
24 + uuidmap_free(id);
25 + return;
26 + }
27 +
28 RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item(host->rrdctx.contexts, sd->context);
29 if(!rca) {
30 ignored_metrics++;
31 // nd_log(NDLS_DAEMON, NDLP_ERR,
32 // "RRDCONTEXT: context '%s' is not found in host '%s' - not loading dimensions",
33 // sd->context, rrdhost_hostname(host));
34 + uuidmap_free(id);
35 return;
36 }
37 RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
@@ -33,12 +43,13 @@ static void rrdinstance_load_dimension_callback(SQL_DIMENSION_DATA *sd, void *da
43 // nd_log(NDLS_DAEMON, NDLP_ERR,
44 // "RRDCONTEXT: instance '%s' of context '%s' is not found in host '%s' - not loading dimensions",
45 // sd->chart_id, sd->context, rrdhost_hostname(host));
46 + uuidmap_free(id);
47 return;
48 }
49 RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
50
51 RRDMETRIC trm = {
41 - .uuid = uuidmap_create(sd->dim_id),
52 + .uuid = id,
53 .id = string_strdupz(sd->id),
54 .name = string_strdupz(sd->name),
55 .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomic
@@ -55,14 +66,18 @@ static void rrdinstance_load_dimension_callback(SQL_DIMENSION_DATA *sd, void *da
66 static void rrdinstance_load_instance_callback(SQL_CHART_DATA *sc, void *data) {
67 RRDHOST *host = data;
68
58 - RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item(host->rrdctx.contexts, sc->context);
59 - if(!rca) {
60 - ignored_instances++;
61 -// nd_log(NDLS_DAEMON, NDLP_ERR,
62 -// "RRDCONTEXT: context '%s' is not found in host '%s' - not loadings instances",
63 -// sc->context, rrdhost_hostname(host));
64 - return;
65 - }
69 + RRDCONTEXT tc = {
70 + .id = string_strdupz(sc->context),
71 + .title = string_strdupz(sc->title),
72 + .units = string_strdupz(sc->units),
73 + .family = string_strdupz(sc->family),
74 + .priority = sc->priority,
75 + .chart_type = sc->chart_type,
76 + .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomics
77 + .rrdhost = host,
78 + };
79 +
80 + RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_set_and_acquire_item(host->rrdctx.contexts, string2str(tc.id), &tc, sizeof(tc));
81 RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
82
83 RRDINSTANCE tri = {
src/database/contexts/internal.h
+2
@@ -471,4 +471,6 @@ void rrdcontext_update_from_collected_rrdinstance(RRDINSTANCE *ri);
471
472 void rrdcontext_garbage_collect_single_host(RRDHOST *host, bool worker_jobs);
473
474 +void get_metric_retention_by_id(RRDHOST *host, UUIDMAP_ID id, time_t *min_first_time_t, time_t *max_last_time_t);
475 +
476 #endif //NETDATA_RRDCONTEXT_INTERNAL_H
src/database/contexts/worker.c
+20 -15
@@ -137,6 +137,24 @@ static void rrdcontext_recalculate_retention_all_hosts(void) {
137 // ----------------------------------------------------------------------------
138 // garbage collector
139
140 +void get_metric_retention_by_id(RRDHOST *host, UUIDMAP_ID id, time_t *min_first_time_t, time_t *max_last_time_t) {
141 + *min_first_time_t = LONG_MAX;
142 + *max_last_time_t = 0;
143 +
144 + for (size_t tier = 0; tier < nd_profile.storage_tiers; tier++) {
145 + STORAGE_ENGINE *eng = host->db[tier].eng;
146 +
147 + time_t first_time_t = 0, last_time_t = 0;
148 + if (eng->api.metric_retention_by_id(host->db[tier].si, id, &first_time_t, &last_time_t)) {
149 + if (first_time_t > 0 && first_time_t < *min_first_time_t)
150 + *min_first_time_t = first_time_t;
151 +
152 + if (last_time_t > *max_last_time_t)
153 + *max_last_time_t = last_time_t;
154 + }
155 + }
156 +}
157 +
158 bool rrdmetric_update_retention(RRDMETRIC *rm) {
159 time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
160
@@ -144,21 +162,8 @@ bool rrdmetric_update_retention(RRDMETRIC *rm) {
162 min_first_time_t = rrddim_first_entry_s(rm->rrddim);
163 max_last_time_t = rrddim_last_entry_s(rm->rrddim);
164 }
147 - else {
148 - RRDHOST *rrdhost = rm->ri->rc->rrdhost;
149 - for (size_t tier = 0; tier < nd_profile.storage_tiers; tier++) {
150 - STORAGE_ENGINE *eng = rrdhost->db[tier].eng;
151 -
152 - time_t first_time_t = 0, last_time_t = 0;
153 - if (eng->api.metric_retention_by_id(rrdhost->db[tier].si, rm->uuid, &first_time_t, &last_time_t)) {
154 - if (first_time_t > 0 && first_time_t < min_first_time_t)
155 - min_first_time_t = first_time_t;
156 -
157 - if (last_time_t > max_last_time_t)
158 - max_last_time_t = last_time_t;
159 - }
160 - }
161 - }
165 + else
166 + get_metric_retention_by_id(rm->ri->rc->rrdhost, rm->uuid, &min_first_time_t, &max_last_time_t);
167
168 if((min_first_time_t == LONG_MAX || min_first_time_t == 0) && max_last_time_t == 0)
169 return false;