1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+#include "rrdcontext.h"
4
+#include "sqlite/sqlite_context.h"
5
+#include "aclk/schema-wrappers/context.h"
6
+#include "aclk/aclk_contexts_api.h"
7
+#include "aclk/aclk_api.h"
8
+
9
+int rrdcontext_enabled = CONFIG_BOOLEAN_NO;
10
+
11
+#define MESSAGES_PER_BUNDLE_TO_SEND_TO_HUB_PER_HOST 5000
12
+#define FULL_RETENTION_SCAN_DELAY_AFTER_DB_ROTATION_SECS 120
13
+#define RRDCONTEXT_WORKER_THREAD_HEARTBEAT_SECS 1
14
+#define RRDCONTEXT_MINIMUM_ALLOWED_PRIORITY 10
15
+
16
+// #define LOG_TRANSITIONS 1
17
+// #define LOG_RRDINSTANCES 1
18
+
19
+typedef enum {
20
+ RRD_FLAG_NONE = 0,
21
+ RRD_FLAG_DELETED = (1 << 0), // this is a deleted object (metrics, instances, contexts)
22
+ RRD_FLAG_COLLECTED = (1 << 1), // this object is currently being collected
23
+ RRD_FLAG_UPDATED = (1 << 2), // this object has updates to propagate
24
+ RRD_FLAG_ARCHIVED = (1 << 3), // this object is not currently being collected
25
+ RRD_FLAG_OWN_LABELS = (1 << 4), // this instance has its own labels - not linked to an RRDSET
26
+ RRD_FLAG_LIVE_RETENTION = (1 << 5), // we have got live retention from the database
27
+ RRD_FLAG_QUEUED = (1 << 6), // this context is currently queued to be dispatched to hub
28
+ RRD_FLAG_DONT_PROCESS = (1 << 7), // don't process updates for this object
29
+
30
+ RRD_FLAG_UPDATE_REASON_LOAD_SQL = (1 << 10), // this object has just been loaded from SQL
31
+ RRD_FLAG_UPDATE_REASON_NEW_OBJECT = (1 << 11), // this object has just been created
32
+ RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT = (1 << 12), // we received an update on this object
33
+ RRD_FLAG_UPDATE_REASON_CHANGED_LINKING = (1 << 13), // an instance or a metric switched RRDSET or RRDDIM
34
+ RRD_FLAG_UPDATE_REASON_CHANGED_UUID = (1 << 14), // an instance or a metric changed UUID
35
+ RRD_FLAG_UPDATE_REASON_CHANGED_NAME = (1 << 15), // an instance or a metric changed name
36
+ RRD_FLAG_UPDATE_REASON_CHANGED_UNITS = (1 << 16), // this context or instance changed units
37
+ RRD_FLAG_UPDATE_REASON_CHANGED_TITLE = (1 << 17), // this context or instance changed title
38
+ RRD_FLAG_UPDATE_REASON_CHANGED_FAMILY = (1 << 18), // the context or the instance changed family
39
+ RRD_FLAG_UPDATE_REASON_CHANGED_CHART_TYPE = (1 << 19), // this context or instance changed chart type
40
+ RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY = (1 << 20), // this context or instance changed its priority
41
+ RRD_FLAG_UPDATE_REASON_CHANGED_UPDATE_EVERY = (1 << 21), // the instance or the metric changed update frequency
42
+ RRD_FLAG_UPDATE_REASON_ZERO_RETENTION = (1 << 22), // this object has not retention
43
+ RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T = (1 << 23), // this object changed its oldest time in the db
44
+ RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T = (1 << 24), // this object change its latest time in the db
45
+ RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED = (1 << 25), // this object has stopped being collected
46
+ RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED = (1 << 26), // this object has started being collected
47
+ RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD = (1 << 27), // this context belongs to a host that just disconnected
48
+ RRD_FLAG_UPDATE_REASON_DB_ROTATION = (1 << 28), // this context changed because of a db rotation
49
+ RRD_FLAG_UPDATE_REASON_UNUSED = (1 << 29), // this context is not used anymore
50
+} RRD_FLAGS;
51
+
52
+#define RRD_FLAG_ALL_UPDATE_REASONS ( \
53
+ RRD_FLAG_UPDATE_REASON_LOAD_SQL \
54
+ |RRD_FLAG_UPDATE_REASON_NEW_OBJECT \
55
+ |RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT \
56
+ |RRD_FLAG_UPDATE_REASON_CHANGED_LINKING \
57
+ |RRD_FLAG_UPDATE_REASON_CHANGED_UUID \
58
+ |RRD_FLAG_UPDATE_REASON_CHANGED_NAME \
59
+ |RRD_FLAG_UPDATE_REASON_CHANGED_UNITS \
60
+ |RRD_FLAG_UPDATE_REASON_CHANGED_TITLE \
61
+ |RRD_FLAG_UPDATE_REASON_CHANGED_FAMILY \
62
+ |RRD_FLAG_UPDATE_REASON_CHANGED_CHART_TYPE \
63
+ |RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY \
64
+ |RRD_FLAG_UPDATE_REASON_CHANGED_UPDATE_EVERY \
65
+ |RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
66
+ |RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T \
67
+ |RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T \
68
+ |RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED \
69
+ |RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED \
70
+ |RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD \
71
+ |RRD_FLAG_UPDATE_REASON_DB_ROTATION \
72
+ |RRD_FLAG_UPDATE_REASON_UNUSED \
73
+)
74
+
75
+#define RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS ( \
76
+ RRD_FLAG_ARCHIVED \
77
+ |RRD_FLAG_DONT_PROCESS \
78
+ |RRD_FLAG_ALL_UPDATE_REASONS \
79
+ )
80
+
81
+#define RRD_FLAGS_PREVENTING_DELETIONS ( \
82
+ RRD_FLAG_QUEUED \
83
+ |RRD_FLAG_COLLECTED \
84
+ |RRD_FLAG_UPDATE_REASON_LOAD_SQL \
85
+ |RRD_FLAG_UPDATE_REASON_NEW_OBJECT \
86
+ |RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT \
87
+ |RRD_FLAG_UPDATE_REASON_CHANGED_LINKING \
88
+)
89
+
90
+#define rrd_flag_set_updated(obj, reason) (obj)->flags |= (RRD_FLAG_UPDATED | (reason))
91
+#define rrd_flag_unset_updated(obj) (obj)->flags &= ~(RRD_FLAG_UPDATED | RRD_FLAG_ALL_UPDATE_REASONS)
92
+
93
+#define rrd_flag_set_collected(obj) do { \
94
+ if(likely( !((obj)->flags & RRD_FLAG_COLLECTED))) \
95
+ (obj)->flags |= (RRD_FLAG_COLLECTED | RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED | RRD_FLAG_UPDATED); \
96
+ if(likely( ((obj)->flags & (RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED)))) \
97
+ (obj)->flags &= ~(RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED); \
98
+ if(unlikely(((obj)->flags & (RRD_FLAG_DELETED | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION)))) \
99
+ (obj)->flags &= ~(RRD_FLAG_DELETED | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION); \
100
+ if(unlikely(((obj)->flags & RRD_FLAG_DONT_PROCESS))) \
101
+ (obj)->flags &= ~RRD_FLAG_DONT_PROCESS; \
102
+} while(0)
103
+
104
+#define rrd_flag_set_archived(obj) do { \
105
+ if(likely( !((obj)->flags & RRD_FLAG_ARCHIVED))) \
106
+ (obj)->flags |= (RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED | RRD_FLAG_UPDATED); \
107
+ if(likely( ((obj)->flags & (RRD_FLAG_COLLECTED | RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED)))) \
108
+ (obj)->flags &= ~(RRD_FLAG_COLLECTED | RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED); \
109
+ if(unlikely(((obj)->flags & (RRD_FLAG_DELETED | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION)))) \
110
+ (obj)->flags &= ~(RRD_FLAG_DELETED | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION); \
111
+} while(0)
112
+
113
+#define rrd_flag_set_deleted(obj, reason) do { \
114
+ if(likely( !((obj)->flags & RRD_FLAG_DELETED))) \
115
+ (obj)->flags |= (RRD_FLAG_DELETED | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION | RRD_FLAG_UPDATED | (reason)); \
116
+ if(unlikely(((obj)->flags & RRD_FLAG_ARCHIVED))) \
117
+ (obj)->flags &= ~RRD_FLAG_ARCHIVED; \
118
+ if(likely( ((obj)->flags & RRD_FLAG_COLLECTED))) \
119
+ (obj)->flags &= ~RRD_FLAG_COLLECTED; \
120
+} while(0)
121
+
122
+
123
+#define rrd_flag_is_collected(obj) ((obj)->flags & RRD_FLAG_COLLECTED)
124
+#define rrd_flag_is_archived(obj) ((obj)->flags & RRD_FLAG_ARCHIVED)
125
+
126
+static struct rrdcontext_reason {
127
+ RRD_FLAGS flag;
128
+ const char *name;
129
+ usec_t delay_ut;
130
+} rrdcontext_reasons[] = {
131
+ // context related
132
+ { RRD_FLAG_UPDATE_REASON_NEW_OBJECT, "object created", 60 * USEC_PER_SEC },
133
+ { RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT, "object updated", 60 * USEC_PER_SEC },
134
+ { RRD_FLAG_UPDATE_REASON_LOAD_SQL, "loaded from sql", 60 * USEC_PER_SEC },
135
+ { RRD_FLAG_UPDATE_REASON_CHANGED_TITLE, "changed title", 30 * USEC_PER_SEC },
136
+ { RRD_FLAG_UPDATE_REASON_CHANGED_UNITS, "changed units", 30 * USEC_PER_SEC },
137
+ { RRD_FLAG_UPDATE_REASON_CHANGED_FAMILY, "changed family", 30 * USEC_PER_SEC },
138
+ { RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY, "changed priority", 30 * USEC_PER_SEC },
139
+ { RRD_FLAG_UPDATE_REASON_ZERO_RETENTION, "has no retention", 60 * USEC_PER_SEC },
140
+ { RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T, "updated first_time_t", 30 * USEC_PER_SEC },
141
+ { RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T, "updated last_time_t", 60 * USEC_PER_SEC },
142
+ { RRD_FLAG_UPDATE_REASON_CHANGED_CHART_TYPE, "changed chart type", 30 * USEC_PER_SEC },
143
+ { RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED, "stopped collected", 60 * USEC_PER_SEC },
144
+ { RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED, "started collected", 0 * USEC_PER_SEC },
145
+ { RRD_FLAG_UPDATE_REASON_UNUSED, "unused", 0 * USEC_PER_SEC },
146
+
147
+ // not context related
148
+ { RRD_FLAG_UPDATE_REASON_CHANGED_UUID, "changed uuid", 60 * USEC_PER_SEC },
149
+ { RRD_FLAG_UPDATE_REASON_CHANGED_UPDATE_EVERY, "changed updated every",60 * USEC_PER_SEC },
150
+ { RRD_FLAG_UPDATE_REASON_CHANGED_LINKING, "changed rrd link", 60 * USEC_PER_SEC },
151
+ { RRD_FLAG_UPDATE_REASON_CHANGED_NAME, "changed name", 60 * USEC_PER_SEC },
152
+ { RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD, "child disconnected", 30 * USEC_PER_SEC },
153
+ { RRD_FLAG_UPDATE_REASON_DB_ROTATION, "db rotation", 60 * USEC_PER_SEC },
154
+
155
+ // terminator
156
+ { 0, NULL, 0 },
157
+};
158
+
159
+
160
+typedef struct rrdmetric {
161
+ uuid_t uuid;
162
+
163
+ STRING *id;
164
+ STRING *name;
165
+
166
+ RRDDIM *rrddim;
167
+
168
+ time_t first_time_t;
169
+ time_t last_time_t;
170
+ RRD_FLAGS flags;
171
+
172
+ struct rrdinstance *ri;
173
+
174
+ usec_t created_ut; // the time this object was created
175
+} RRDMETRIC;
176
+
177
+typedef struct rrdinstance {
178
+ uuid_t uuid;
179
+
180
+ STRING *id;
181
+ STRING *name;
182
+ STRING *title;
183
+ STRING *units;
184
+ STRING *family;
185
+ uint32_t priority;
186
+ RRDSET_TYPE chart_type;
187
+
188
+ RRD_FLAGS flags; // flags related to this instance
189
+ time_t first_time_t;
190
+ time_t last_time_t;
191
+
192
+ int update_every; // data collection frequency
193
+ RRDSET *rrdset; // pointer to RRDSET when collected, or NULL
194
+
195
+ DICTIONARY *rrdlabels; // linked to RRDSET->state->chart_labels or own version
196
+
197
+ struct rrdcontext *rc;
198
+ DICTIONARY *rrdmetrics;
199
+} RRDINSTANCE;
200
+
201
+typedef struct rrdcontext {
202
+ uint64_t version;
203
+
204
+ STRING *id;
205
+ STRING *title;
206
+ STRING *units;
207
+ STRING *family;
208
+ uint32_t priority;
209
+ RRDSET_TYPE chart_type;
210
+
211
+ RRD_FLAGS flags;
212
+ time_t first_time_t;
213
+ time_t last_time_t;
214
+
215
+ VERSIONED_CONTEXT_DATA hub;
216
+
217
+ DICTIONARY *rrdinstances;
218
+ RRDHOST *rrdhost;
219
+
220
+ struct {
221
+ RRD_FLAGS queued_flags; // the last flags that triggered the queueing
222
+ usec_t queued_ut; // the last time this was queued
223
+ usec_t delay_calc_ut; // the last time we calculated the scheduled_dispatched_ut
224
+ usec_t scheduled_dispatch_ut; // the time it was/is scheduled to be sent
225
+ usec_t dequeued_ut; // the last time we sent (or deduped) this context
226
+ } queue;
227
+
228
+ netdata_mutex_t mutex;
229
+} RRDCONTEXT;
230
+
231
+// ----------------------------------------------------------------------------
232
+// helper one-liners for RRDMETRIC
233
+
234
+static inline RRDMETRIC *rrdmetric_acquired_value(RRDMETRIC_ACQUIRED *rma) {
235
+ return dictionary_acquired_item_value((DICTIONARY_ITEM *)rma);
236
+}
237
+
238
+static inline void rrdmetric_release(RRDMETRIC_ACQUIRED *rma) {
239
+ RRDMETRIC *rm = rrdmetric_acquired_value(rma);
240
+ dictionary_acquired_item_release(rm->ri->rrdmetrics, (DICTIONARY_ITEM *)rma);
241
+}
242
+
243
+// ----------------------------------------------------------------------------
244
+// helper one-liners for RRDINSTANCE
245
+
246
+static inline RRDINSTANCE_ACQUIRED *rrdinstance_dup(RRDINSTANCE_ACQUIRED *ria) {
247
+ return (RRDINSTANCE_ACQUIRED *)dictionary_acquired_item_dup((DICTIONARY_ITEM *)ria);
248
+}
249
+
250
+static inline RRDINSTANCE *rrdinstance_acquired_value(RRDINSTANCE_ACQUIRED *ria) {
251
+ return dictionary_acquired_item_value((DICTIONARY_ITEM *)ria);
252
+}
253
+
254
+static inline const char *rrdinstance_acquired_name(RRDINSTANCE_ACQUIRED *ria) {
255
+ return dictionary_acquired_item_name((DICTIONARY_ITEM *)ria);
256
+}
257
+
258
+static inline void rrdinstance_release(RRDINSTANCE_ACQUIRED *ria) {
259
+ RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
260
+ dictionary_acquired_item_release(ri->rc->rrdinstances, (DICTIONARY_ITEM *)ria);
261
+}
262
+
263
+// ----------------------------------------------------------------------------
264
+// helper one-liners for RRDCONTEXT
265
+
266
+static inline RRDCONTEXT_ACQUIRED *rrdcontext_dup(RRDCONTEXT_ACQUIRED *rca) {
267
+ return (RRDCONTEXT_ACQUIRED *)dictionary_acquired_item_dup((DICTIONARY_ITEM *)rca);
268
+}
269
+
270
+static inline const char *rrdcontext_acquired_name(RRDCONTEXT_ACQUIRED *rca) {
271
+ return dictionary_acquired_item_name((DICTIONARY_ITEM *)rca);
272
+}
273
+
274
+static inline RRDCONTEXT *rrdcontext_acquired_value(RRDCONTEXT_ACQUIRED *rca) {
275
+ return dictionary_acquired_item_value((DICTIONARY_ITEM *)rca);
276
+}
277
+
278
+static inline RRDCONTEXT_ACQUIRED *rrdcontext_acquire(RRDHOST *host, const char *name) {
279
+ return (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item((DICTIONARY *)host->rrdctx, name);
280
+}
281
+
282
+static inline void rrdcontext_release(RRDCONTEXT_ACQUIRED *rca) {
283
+ RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
284
+ dictionary_acquired_item_release((DICTIONARY *)rc->rrdhost->rrdctx, (DICTIONARY_ITEM *)rca);
285
+}
286
+
287
+static void rrdcontext_recalculate_context_retention(RRDCONTEXT *rc, RRD_FLAGS reason, int job_id);
288
+static void rrdcontext_recalculate_host_retention(RRDHOST *host, RRD_FLAGS reason, int job_id);
289
+
290
+#define rrdcontext_version_hash(host) rrdcontext_version_hash_with_callback(host, NULL, false, NULL)
291
+static uint64_t rrdcontext_version_hash_with_callback(RRDHOST *host, void (*callback)(RRDCONTEXT *, bool, void *), bool snapshot, void *bundle);
292
+
293
+void rrdcontext_delete_from_sql_unsafe(RRDCONTEXT *rc);
294
+
295
+#define rrdcontext_lock(rc) netdata_mutex_lock(&((rc)->mutex))
296
+#define rrdcontext_unlock(rc) netdata_mutex_unlock(&((rc)->mutex))
297
+
298
+// ----------------------------------------------------------------------------
299
+// Updates triggers
300
+
301
+static void rrdmetric_trigger_updates(RRDMETRIC *rm, bool force, bool escalate);
302
+static void rrdinstance_trigger_updates(RRDINSTANCE *ri, bool force, bool escalate);
303
+static void rrdcontext_trigger_updates(RRDCONTEXT *rc, bool force);
304
+
305
+// ----------------------------------------------------------------------------
306
+// visualizing flags
307
+
308
+static void rrd_flags_to_buffer(RRD_FLAGS flags, BUFFER *wb) {
309
+ if(flags & RRD_FLAG_QUEUED)
310
+ buffer_strcat(wb, "QUEUED ");
311
+
312
+ if(flags & RRD_FLAG_DELETED)
313
+ buffer_strcat(wb, "DELETED ");
314
+
315
+ if(flags & RRD_FLAG_COLLECTED)
316
+ buffer_strcat(wb, "COLLECTED ");
317
+
318
+ if(flags & RRD_FLAG_UPDATED)
319
+ buffer_strcat(wb, "UPDATED ");
320
+
321
+ if(flags & RRD_FLAG_ARCHIVED)
322
+ buffer_strcat(wb, "ARCHIVED ");
323
+
324
+ if(flags & RRD_FLAG_OWN_LABELS)
325
+ buffer_strcat(wb, "OWN_LABELS ");
326
+
327
+ if(flags & RRD_FLAG_LIVE_RETENTION)
328
+ buffer_strcat(wb, "LIVE_RETENTION ");
329
+
330
+ if(flags & RRD_FLAG_DONT_PROCESS)
331
+ buffer_strcat(wb, "DONT_PROCESS ");
332
+}
333
+
334
+static void rrd_reasons_to_buffer(RRD_FLAGS flags, BUFFER *wb) {
335
+ for(int i = 0, added = 0; rrdcontext_reasons[i].name ; i++) {
336
+ if (flags & rrdcontext_reasons[i].flag) {
337
+ if (added)
338
+ buffer_strcat(wb, ", ");
339
+ buffer_strcat(wb, rrdcontext_reasons[i].name);
340
+ added++;
341
+ }
342
+ }
343
+}
344
+
345
+// ----------------------------------------------------------------------------
346
+// logging of all data collected
347
+
348
+#ifdef LOG_TRANSITIONS
349
+static void log_transition(STRING *metric, STRING *instance, STRING *context, RRD_FLAGS flags, const char *msg) {
350
+ BUFFER *wb = buffer_create(1000);
351
+
352
+ buffer_sprintf(wb, "RRD TRANSITION: context '%s'", string2str(context));
353
+
354
+ if(instance)
355
+ buffer_sprintf(wb, ", instance '%s'", string2str(instance));
356
+
357
+ if(metric)
358
+ buffer_sprintf(wb, ", metric '%s'", string2str(metric));
359
+
360
+ buffer_sprintf(wb, ", triggered by %s: ", msg);
361
+
362
+ rrd_flags_to_buffer(flags, wb);
363
+
364
+ buffer_strcat(wb, ", reasons: ");
365
+
366
+ rrd_reasons_to_buffer(flags, wb);
367
+
368
+ internal_error(true, "%s", buffer_tostring(wb));
369
+ buffer_free(wb);
370
+}
371
+#else
372
+#define log_transition(metric, instance, context, flags, msg) debug_dummy()
373
+#endif
374
+
375
+#ifdef LOG_RRDINSTANCES
376
+static void rrdinstance_log(RRDINSTANCE *ri, const char *msg) {
377
+ char uuid[UUID_STR_LEN];
378
+
379
+ uuid_unparse(ri->uuid, uuid);
380
+
381
+ BUFFER *wb = buffer_create(1000);
382
+
383
+ buffer_sprintf(wb,
384
+ "RRDINSTANCE: %s id '%s' (host '%s'), uuid '%s', name '%s', context '%s', title '%s', units '%s', family '%s', priority %zu, chart type '%s', update every %d, rrdset '%s', flags %s%s%s%s%s%s%s%s, first_time_t %ld, last_time_t %ld",
385
+ msg,
386
+ string2str(ri->id),
387
+ ri->rc->rrdhost->hostname,
388
+ uuid,
389
+ string2str(ri->name),
390
+ string2str(ri->rc->id),
391
+ string2str(ri->title),
392
+ string2str(ri->units),
393
+ string2str(ri->family),
394
+ ri->priority,
395
+ rrdset_type_name(ri->chart_type),
396
+ ri->update_every,
397
+ ri->rrdset?ri->rrdset->id:"NONE",
398
+ ri->flags & RRD_FLAG_DELETED ?"DELETED ":"",
399
+ ri->flags & RRD_FLAG_UPDATED ?"UPDATED ":"",
400
+ rrd_flag_is_collected(ri) ?"COLLECTED ":"",
401
+ rrd_flag_is_archived(ri) ?"ARCHIVED ":"",
402
+ ri->flags & RRD_FLAG_OWNLABELS ?"OWNLABELS ":"",
403
+ ri->flags & RRD_FLAG_LIVE_RETENTION ?"LIVE ":"",
404
+ ri->flags & RRD_FLAG_QUEUED ?"QUEUED ":"",
405
+ ri->flags & RRD_FLAG_DONT_TRIGGER ?"BLOCKED ":"",
406
+ ri->first_time_t,
407
+ ri->last_time_t
408
+ );
409
+
410
+ buffer_strcat(wb, ", update reasons: { ");
411
+ for(int i = 0, added = 0; rrdcontext_reasons[i].name ;i++)
412
+ if(ri->flags & rrdcontext_reasons[i].flag) {
413
+ if(added) buffer_strcat(wb, ", ");
414
+ buffer_strcat(wb, rrdcontext_reasons[i].name);
415
+ added++;
416
+ }
417
+ buffer_strcat(wb, " }");
418
+
419
+ buffer_strcat(wb, ", labels: { ");
420
+ if(ri->rrdlabels) {
421
+ if(!rrdlabels_to_buffer(ri->rrdlabels, wb, "", "=", "'", ", ", NULL, NULL, NULL, NULL))
422
+ buffer_strcat(wb, "EMPTY }");
423
+ else
424
+ buffer_strcat(wb, " }");
425
+ }
426
+ else
427
+ buffer_strcat(wb, "NONE }");
428
+
429
+ buffer_strcat(wb, ", metrics: { ");
430
+ if(ri->rrdmetrics) {
431
+ RRDMETRIC *v;
432
+ int i = 0;
433
+ dfe_start_read((DICTIONARY *)ri->rrdmetrics, v) {
434
+ buffer_sprintf(wb, "%s%s", i?",":"", v_name);
435
+ i++;
436
+ }
437
+ dfe_done(v);
438
+
439
+ if(!i)
440
+ buffer_strcat(wb, "EMPTY }");
441
+ else
442
+ buffer_strcat(wb, " }");
443
+ }
444
+ else
445
+ buffer_strcat(wb, "NONE }");
446
+
447
+ internal_error(true, "%s", buffer_tostring(wb));
448
+ buffer_free(wb);
449
+}
450
+#else
451
+#define rrdinstance_log(ir, msg) debug_dummy()
452
+#endif
453
+
454
+// ----------------------------------------------------------------------------
455
+// RRDMETRIC
456
+
457
+static void rrdmetric_free(RRDMETRIC *rm) {
458
+ string_freez(rm->id);
459
+ string_freez(rm->name);
460
+
461
+ rm->id = NULL;
462
+ rm->name = NULL;
463
+ rm->ri = NULL;
464
+}
465
+
466
+static void rrdmetric_update_retention(RRDMETRIC *rm) {
467
+ time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
468
+
469
+ if(rm->rrddim) {
470
+ min_first_time_t = rrddim_first_entry_t(rm->rrddim);
471
+ max_last_time_t = rrddim_last_entry_t(rm->rrddim);
472
+ }
473
+#ifdef ENABLE_DBENGINE
474
+ else {
475
+ RRDHOST *rrdhost = rm->ri->rc->rrdhost;
476
+ for (int tier = 0; tier < storage_tiers; tier++) {
477
+ if(!rrdhost->storage_instance[tier]) continue;
478
+
479
+ time_t first_time_t, last_time_t;
480
+ if (rrdeng_metric_retention_by_uuid(rrdhost->storage_instance[tier], &rm->uuid, &first_time_t, &last_time_t) == 0) {
481
+ if (first_time_t < min_first_time_t)
482
+ min_first_time_t = first_time_t;
483
+
484
+ if (last_time_t > max_last_time_t)
485
+ max_last_time_t = last_time_t;
486
+ }
487
+ }
488
+ }
489
+#endif
490
+
491
+ if(min_first_time_t == LONG_MAX)
492
+ min_first_time_t = 0;
493
+
494
+ if(min_first_time_t > max_last_time_t) {
495
+ internal_error(true, "RRDMETRIC: retention of '%s' is flipped", string2str(rm->id));
496
+ time_t tmp = min_first_time_t;
497
+ min_first_time_t = max_last_time_t;
498
+ max_last_time_t = tmp;
499
+ }
500
+
501
+ // check if retention changed
502
+
503
+ if (min_first_time_t != rm->first_time_t) {
504
+ rm->first_time_t = min_first_time_t;
505
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
506
+ }
507
+
508
+ if (max_last_time_t != rm->last_time_t) {
509
+ rm->last_time_t = max_last_time_t;
510
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
511
+ }
512
+
513
+ if(unlikely(!rm->first_time_t && !rm->last_time_t))
514
+ rrd_flag_set_deleted(rm, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
515
+
516
+ rm->flags |= RRD_FLAG_LIVE_RETENTION;
517
+}
518
+
519
+// called when this rrdmetric is inserted to the rrdmetrics dictionary of a rrdinstance
520
+static void rrdmetric_insert_callback(const char *id __maybe_unused, void *value, void *data) {
521
+ RRDMETRIC *rm = value;
522
+
523
+ // link it to its parent
524
+ rm->ri = data;
525
+
526
+ // remove flags that we need to figure out at runtime
527
+ rm->flags = rm->flags & RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS;
528
+
529
+ rm->created_ut = now_realtime_usec();
530
+
531
+ // signal the react callback to do the job
532
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_NEW_OBJECT);
533
+}
534
+
535
+// called when this rrdmetric is deleted from the rrdmetrics dictionary of a rrdinstance
536
+static void rrdmetric_delete_callback(const char *id __maybe_unused, void *value, void *data __maybe_unused) {
537
+ RRDMETRIC *rm = value;
538
+
539
+ internal_error(rm->rrddim, "RRDMETRIC: '%s' is freed but there is a RRDDIM linked to it.", string2str(rm->id));
540
+
541
+ // free the resources
542
+ rrdmetric_free(rm);
543
+}
544
+
545
+// called when the same rrdmetric is inserted again to the rrdmetrics dictionary of a rrdinstance
546
+static void rrdmetric_conflict_callback(const char *id __maybe_unused, void *oldv, void *newv, void *data __maybe_unused) {
547
+ RRDMETRIC *rm = oldv;
548
+ RRDMETRIC *rm_new = newv;
549
+
550
+ internal_error(rm->id != rm_new->id,
551
+ "RRDMETRIC: '%s' cannot change id to '%s'",
552
+ string2str(rm->id), string2str(rm_new->id));
553
+
554
+ if(uuid_compare(rm->uuid, rm_new->uuid) != 0) {
555
+ char uuid1[UUID_STR_LEN], uuid2[UUID_STR_LEN];
556
+ uuid_unparse(rm->uuid, uuid1);
557
+ uuid_unparse(rm_new->uuid, uuid2);
558
+ internal_error(true, "RRDMETRIC: '%s' of instance '%s' changed uuid from '%s' to '%s'", string2str(rm->id), string2str(rm->ri->id), uuid1, uuid2);
559
+ uuid_copy(rm->uuid, rm_new->uuid);
560
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_UUID);
561
+ }
562
+
563
+ if(rm->rrddim && rm_new->rrddim && rm->rrddim != rm_new->rrddim) {
564
+ rm->rrddim = rm_new->rrddim;
565
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_LINKING);
566
+ }
567
+
568
+ if(rm->rrddim && uuid_compare(rm->uuid, rm->rrddim->metric_uuid) != 0) {
569
+ char uuid1[UUID_STR_LEN], uuid2[UUID_STR_LEN];
570
+ uuid_unparse(rm->uuid, uuid1);
571
+ uuid_unparse(rm_new->uuid, uuid2);
572
+ internal_error(true, "RRDMETRIC: '%s' is linked to RRDDIM '%s' but they have different UUIDs. RRDMETRIC has '%s', RRDDIM has '%s'", string2str(rm->id), rm->rrddim->id, uuid1, uuid2);
573
+ }
574
+
575
+ if(rm->rrddim != rm_new->rrddim)
576
+ rm->rrddim = rm_new->rrddim;
577
+
578
+ if(rm->name != rm_new->name) {
579
+ STRING *old = rm->name;
580
+ rm->name = string_dup(rm_new->name);
581
+ string_freez(old);
582
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_NAME);
583
+ }
584
+
585
+ if(!rm->first_time_t || (rm_new->first_time_t && rm_new->first_time_t < rm->first_time_t)) {
586
+ rm->first_time_t = rm_new->first_time_t;
587
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
588
+ }
589
+
590
+ if(!rm->last_time_t || (rm_new->last_time_t && rm_new->last_time_t > rm->last_time_t)) {
591
+ rm->last_time_t = rm_new->last_time_t;
592
+ rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
593
+ }
594
+
595
+ rm->flags |= (rm_new->flags & RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS);
596
+
597
+ if(rrd_flag_is_collected(rm) && rrd_flag_is_archived(rm))
598
+ rrd_flag_set_collected(rm);
599
+
600
+ if(rm->flags & RRD_FLAG_UPDATED)
601
+ rm->flags |= RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT;
602
+
603
+ rrdmetric_free(rm_new);
604
+
605
+ // the react callback will continue from here
606
+}
607
+
608
+static void rrdmetric_react_callback(const char *id __maybe_unused, void *value, void *data __maybe_unused) {
609
+ RRDMETRIC *rm = value;
610
+
611
+ rrdmetric_trigger_updates(rm, false, true);
612
+}
613
+
614
+static void rrdmetrics_create(RRDINSTANCE *ri) {
615
+ if(unlikely(!ri)) return;
616
+ if(likely(ri->rrdmetrics)) return;
617
+
618
+ ri->rrdmetrics = dictionary_create(DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
619
+ dictionary_register_insert_callback(ri->rrdmetrics, rrdmetric_insert_callback, (void *)ri);
620
+ dictionary_register_delete_callback(ri->rrdmetrics, rrdmetric_delete_callback, (void *)ri);
621
+ dictionary_register_conflict_callback(ri->rrdmetrics, rrdmetric_conflict_callback, (void *)ri);
622
+ dictionary_register_react_callback(ri->rrdmetrics, rrdmetric_react_callback, (void *)ri);
623
+}
624
+
625
+static void rrdmetrics_destroy(RRDINSTANCE *ri) {
626
+ if(unlikely(!ri || !ri->rrdmetrics)) return;
627
+ dictionary_destroy(ri->rrdmetrics);
628
+ ri->rrdmetrics = NULL;
629
+}
630
+
631
+static inline bool rrdmetric_should_be_deleted(RRDMETRIC *rm) {
632
+ if(likely(!(rm->flags & RRD_FLAG_DELETED)))
633
+ return false;
634
+
635
+ if(likely(!(rm->flags & RRD_FLAG_LIVE_RETENTION)))
636
+ return false;
637
+
638
+ if(unlikely(rm->flags & RRD_FLAGS_PREVENTING_DELETIONS))
639
+ return false;
640
+
641
+ if(likely(rm->rrddim))
642
+ return false;
643
+
644
+ if((now_realtime_usec() - rm->created_ut) < 600 * USEC_PER_SEC)
645
+ return false;
646
+
647
+ rrdmetric_update_retention(rm);
648
+ if(rm->first_time_t || rm->last_time_t)
649
+ return false;
650
+
651
+ return true;
652
+}
653
+
654
+static void rrdmetric_trigger_updates(RRDMETRIC *rm, bool force, bool escalate) {
655
+ if(likely(!force && !(rm->flags & RRD_FLAG_UPDATED))) return;
656
+
657
+ if(unlikely(rrd_flag_is_collected(rm) && !rm->rrddim))
658
+ rrd_flag_set_archived(rm);
659
+
660
+ if(unlikely((rm->flags & RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD) && rrd_flag_is_collected(rm)))
661
+ rrd_flag_set_archived(rm);
662
+
663
+ rrdmetric_update_retention(rm);
664
+
665
+ if(unlikely(escalate && rm->flags & RRD_FLAG_UPDATED && !(rm->ri->flags & RRD_FLAG_DONT_PROCESS))) {
666
+ log_transition(rm->id, rm->ri->id, rm->ri->rc->id, rm->flags, "RRDMETRIC");
667
+ rrdinstance_trigger_updates(rm->ri, true, true);
668
+ }
669
+}
670
+
671
+static inline void rrdmetric_from_rrddim(RRDDIM *rd) {
672
+ if(unlikely(!rd->rrdset))
673
+ fatal("RRDMETRIC: rrddim '%s' does not have a rrdset.", rd->id);
674
+
675
+ if(unlikely(!rd->rrdset->rrdhost))
676
+ fatal("RRDMETRIC: rrdset '%s' does not have a rrdhost", rd->rrdset->id);
677
+
678
+ if(unlikely(!rd->rrdset->rrdinstance))
679
+ fatal("RRDMETRIC: rrdset '%s' does not have a rrdinstance", rd->rrdset->id);
680
+
681
+ RRDINSTANCE *ri = rrdinstance_acquired_value(rd->rrdset->rrdinstance);
682
+
683
+ RRDMETRIC trm = {
684
+ .id = string_strdupz(rd->id),
685
+ .name = string_strdupz(rd->name),
686
+ .flags = RRD_FLAG_NONE,
687
+ .rrddim = rd,
688
+ };
689
+ uuid_copy(trm.uuid, rd->metric_uuid);
690
+
691
+ RRDMETRIC_ACQUIRED *rma = (RRDMETRIC_ACQUIRED *)dictionary_set_and_acquire_item(ri->rrdmetrics, string2str(trm.id), &trm, sizeof(trm));
692
+
693
+ if(rd->rrdmetric)
694
+ rrdmetric_release(rd->rrdmetric);
695
+
696
+ rd->rrdmetric = rma;
697
+}
698
+
699
+#define rrddim_get_rrdmetric(rd) rrddim_get_rrdmetric_with_trace(rd, __FUNCTION__)
700
+static inline RRDMETRIC *rrddim_get_rrdmetric_with_trace(RRDDIM *rd, const char *function) {
701
+ if(unlikely(!rd->rrdmetric))
702
+ fatal("RRDMETRIC: RRDDIM '%s' is not linked to an RRDMETRIC at %s()", rd->id, function);
703
+
704
+ RRDMETRIC *rm = rrdmetric_acquired_value(rd->rrdmetric);
705
+
706
+ if(unlikely(rm->rrddim != rd))
707
+ fatal("RRDMETRIC: '%s' is not linked to RRDDIM '%s' at %s()", string2str(rm->id), rd->id, function);
708
+
709
+ return rm;
710
+}
711
+
712
+static inline void rrdmetric_rrddim_is_freed(RRDDIM *rd) {
713
+ RRDMETRIC *rm = rrddim_get_rrdmetric(rd);
714
+
715
+ if(unlikely(rrd_flag_is_collected(rm)))
716
+ rrd_flag_set_archived(rm);
717
+
718
+ rm->rrddim = NULL;
719
+ rrdmetric_trigger_updates(rm, false, true);
720
+ rrdmetric_release(rd->rrdmetric);
721
+ rd->rrdmetric = NULL;
722
+}
723
+
724
+static inline void rrdmetric_updated_rrddim_flags(RRDDIM *rd) {
725
+ RRDMETRIC *rm = rrddim_get_rrdmetric(rd);
726
+
727
+ if(unlikely(rd->flags & (RRDDIM_FLAG_ARCHIVED | RRDDIM_FLAG_OBSOLETE))) {
728
+ if(unlikely(rrd_flag_is_collected(rm)))
729
+ rrd_flag_set_archived(rm);
730
+ }
731
+
732
+ rrdmetric_trigger_updates(rm, false, true);
733
+}
734
+
735
+static inline void rrdmetric_collected_rrddim(RRDDIM *rd) {
736
+ RRDMETRIC *rm = rrddim_get_rrdmetric(rd);
737
+
738
+ if(unlikely(!rrd_flag_is_collected(rm)))
739
+ rrd_flag_set_collected(rm);
740
+
741
+ rrdmetric_trigger_updates(rm, false, true);
742
+}
743
+
744
+// ----------------------------------------------------------------------------
745
+// RRDINSTANCE
746
+
747
+static void rrdinstance_free(RRDINSTANCE *ri) {
748
+
749
+ if(ri->flags & RRD_FLAG_OWN_LABELS)
750
+ dictionary_destroy(ri->rrdlabels);
751
+
752
+ rrdmetrics_destroy(ri);
753
+ string_freez(ri->id);
754
+ string_freez(ri->name);
755
+ string_freez(ri->title);
756
+ string_freez(ri->units);
757
+ string_freez(ri->family);
758
+
759
+ ri->id = NULL;
760
+ ri->name = NULL;
761
+ ri->title = NULL;
762
+ ri->units = NULL;
763
+ ri->family = NULL;
764
+ ri->rc = NULL;
765
+ ri->rrdlabels = NULL;
766
+ ri->rrdmetrics = NULL;
767
+ ri->rrdset = NULL;
768
+}
769
+
770
+static void rrdinstance_insert_callback(const char *id __maybe_unused, void *value, void *data) {
771
+ RRDINSTANCE *ri = value;
772
+
773
+ // link it to its parent
774
+ ri->rc = data;
775
+
776
+ ri->flags = ri->flags & RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS;
777
+
778
+ if(!ri->name)
779
+ ri->name = string_dup(ri->id);
780
+
781
+ if(ri->rrdset && ri->rrdset->state) {
782
+ ri->rrdlabels = ri->rrdset->state->chart_labels;
783
+ if(ri->flags & RRD_FLAG_OWN_LABELS)
784
+ ri->flags &= ~RRD_FLAG_OWN_LABELS;
785
+ }
786
+ else {
787
+ ri->rrdlabels = rrdlabels_create();
788
+ ri->flags |= RRD_FLAG_OWN_LABELS;
789
+ }
790
+
791
+ rrdmetrics_create(ri);
792
+ rrdinstance_log(ri, "INSERT");
793
+
794
+ // signal the react callback to do the job
795
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_NEW_OBJECT);
796
+}
797
+
798
+static void rrdinstance_delete_callback(const char *id, void *value, void *data) {
799
+ (void)id;
800
+ RRDCONTEXT *rc = data; (void)rc;
801
+ RRDINSTANCE *ri = (RRDINSTANCE *)value;
802
+
803
+ rrdinstance_log(ri, "DELETE");
804
+
805
+ internal_error(ri->rrdset, "RRDINSTANCE: '%s' is freed but there is a RRDSET linked to it.", string2str(ri->id));
806
+
807
+ rrdinstance_free(ri);
808
+}
809
+
810
+static void rrdinstance_conflict_callback(const char *id __maybe_unused, void *oldv, void *newv, void *data __maybe_unused) {
811
+ RRDINSTANCE *ri = (RRDINSTANCE *)oldv;
812
+ RRDINSTANCE *ri_new = (RRDINSTANCE *)newv;
813
+
814
+ internal_error(ri->id != ri_new->id,
815
+ "RRDINSTANCE: '%s' cannot change id to '%s'",
816
+ string2str(ri->id), string2str(ri_new->id));
817
+
818
+ if(uuid_compare(ri->uuid, ri_new->uuid) != 0) {
819
+ uuid_copy(ri->uuid, ri_new->uuid);
820
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_UUID);
821
+ }
822
+
823
+ if(ri->rrdset && ri_new->rrdset && ri->rrdset != ri_new->rrdset) {
824
+ ri->rrdset = ri_new->rrdset;
825
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LINKING);
826
+ }
827
+
828
+ if(ri->rrdset && ri->rrdset->chart_uuid && uuid_compare(ri->uuid, *ri->rrdset->chart_uuid) != 0) {
829
+ char uuid1[UUID_STR_LEN], uuid2[UUID_STR_LEN];
830
+ uuid_unparse(ri->uuid, uuid1);
831
+ uuid_unparse(*ri->rrdset->chart_uuid, uuid2);
832
+ internal_error(true, "RRDINSTANCE: '%s' is linked to RRDSET '%s' but they have different UUIDs. RRDINSTANCE has '%s', RRDSET has '%s'", string2str(ri->id), ri->rrdset->id, uuid1, uuid2);
833
+ }
834
+
835
+ if(ri->name != ri_new->name) {
836
+ STRING *old = ri->name;
837
+ ri->name = string_dup(ri_new->name);
838
+ string_freez(old);
839
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_NAME);
840
+ }
841
+
842
+ if(ri->title != ri_new->title) {
843
+ STRING *old = ri->title;
844
+ ri->title = string_dup(ri_new->title);
845
+ string_freez(old);
846
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_TITLE);
847
+ }
848
+
849
+ if(ri->units != ri_new->units) {
850
+ STRING *old = ri->units;
851
+ ri->units = string_dup(ri_new->units);
852
+ string_freez(old);
853
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_UNITS);
854
+ }
855
+
856
+ if(ri->family != ri_new->family) {
857
+ STRING *old = ri->family;
858
+ ri->family = string_dup(ri_new->family);
859
+ string_freez(old);
860
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FAMILY);
861
+ }
862
+
863
+ if(ri->chart_type != ri_new->chart_type) {
864
+ ri->chart_type = ri_new->chart_type;
865
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_CHART_TYPE);
866
+ }
867
+
868
+ if(ri->priority != ri_new->priority) {
869
+ ri->priority = ri_new->priority;
870
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY);
871
+ }
872
+
873
+ if(ri->update_every != ri_new->update_every) {
874
+ ri->update_every = ri_new->update_every;
875
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_UPDATE_EVERY);
876
+ }
877
+
878
+ if(ri->rrdset != ri_new->rrdset) {
879
+ ri->rrdset = ri_new->rrdset;
880
+
881
+ if(ri->flags & RRD_FLAG_OWN_LABELS) {
882
+ DICTIONARY *old = ri->rrdlabels;
883
+ ri->rrdlabels = ri->rrdset->state->chart_labels;
884
+ ri->flags &= ~RRD_FLAG_OWN_LABELS;
885
+ rrdlabels_destroy(old);
886
+ }
887
+ }
888
+
889
+ ri->flags |= (ri_new->flags & RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS);
890
+
891
+ if(rrd_flag_is_collected(ri) && rrd_flag_is_archived(ri))
892
+ rrd_flag_set_collected(ri);
893
+
894
+ if(ri->flags & RRD_FLAG_UPDATED)
895
+ ri->flags |= RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT;
896
+
897
+ rrdinstance_log(ri, "CONFLICT");
898
+
899
+ // free the new one
900
+ rrdinstance_free(ri_new);
901
+
902
+ // the react callback will continue from here
903
+}
904
+
905
+static void rrdinstance_react_callback(const char *id __maybe_unused, void *value, void *data __maybe_unused) {
906
+ RRDINSTANCE *ri = value;
907
+
908
+ rrdinstance_trigger_updates(ri, false, true);
909
+}
910
+
911
+void rrdinstances_create(RRDCONTEXT *rc) {
912
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
913
+ return;
914
+
915
+ if(unlikely(!rc || rc->rrdinstances)) return;
916
+
917
+ rc->rrdinstances = dictionary_create(DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
918
+ dictionary_register_insert_callback(rc->rrdinstances, rrdinstance_insert_callback, (void *)rc);
919
+ dictionary_register_delete_callback(rc->rrdinstances, rrdinstance_delete_callback, (void *)rc);
920
+ dictionary_register_conflict_callback(rc->rrdinstances, rrdinstance_conflict_callback, (void *)rc);
921
+ dictionary_register_react_callback(rc->rrdinstances, rrdinstance_react_callback, (void *)rc);
922
+}
923
+
924
+void rrdinstances_destroy(RRDCONTEXT *rc) {
925
+ if(unlikely(!rc || !rc->rrdinstances)) return;
926
+
927
+ dictionary_destroy(rc->rrdinstances);
928
+ rc->rrdinstances = NULL;
929
+}
930
+
931
+static inline bool rrdinstance_should_be_deleted(RRDINSTANCE *ri) {
932
+ if(likely(!(ri->flags & RRD_FLAG_DELETED)))
933
+ return false;
934
+
935
+ if(likely(!(ri->flags & RRD_FLAG_LIVE_RETENTION)))
936
+ return false;
937
+
938
+ if(unlikely(ri->flags & RRD_FLAGS_PREVENTING_DELETIONS))
939
+ return false;
940
+
941
+ if(likely(ri->rrdset))
942
+ return false;
943
+
944
+ if(unlikely(dictionary_stats_referenced_items(ri->rrdmetrics) != 0))
945
+ return false;
946
+
947
+ if(unlikely(dictionary_stats_entries(ri->rrdmetrics) != 0))
948
+ return false;
949
+
950
+ if(ri->first_time_t || ri->last_time_t)
951
+ return false;
952
+
953
+ return true;
954
+}
955
+
956
+static void rrdinstance_trigger_updates(RRDINSTANCE *ri, bool force, bool escalate) {
957
+ if(unlikely(ri->flags & RRD_FLAG_DONT_PROCESS)) return;
958
+ if(unlikely(!force && !(ri->flags & RRD_FLAG_UPDATED))) return;
959
+
960
+ if(likely(ri->rrdset)) {
961
+ if(unlikely(ri->rrdset->priority != ri->priority)) {
962
+ ri->priority = ri->rrdset->priority;
963
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY);
964
+ }
965
+ if(unlikely(ri->rrdset->update_every != ri->update_every)) {
966
+ ri->update_every = ri->rrdset->update_every;
967
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_UPDATE_EVERY);
968
+ }
969
+ }
970
+ else if(unlikely(rrd_flag_is_collected(ri))) {
971
+ rrd_flag_set_archived(ri);
972
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LINKING);
973
+ }
974
+
975
+ time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
976
+ size_t metrics_active = 0, metrics_deleted = 0;
977
+ bool live_retention = true, currently_collected = false;
978
+ {
979
+ RRDMETRIC *rm;
980
+ dfe_start_read((DICTIONARY *)ri->rrdmetrics, rm) {
981
+ if(!(rm->flags & RRD_FLAG_LIVE_RETENTION))
982
+ live_retention = false;
983
+
984
+ if (unlikely((rrdmetric_should_be_deleted(rm)))) {
985
+ metrics_deleted++;
986
+ rrd_flag_unset_updated(rm);
987
+ continue;
988
+ }
989
+
990
+ if(rm->flags & RRD_FLAG_COLLECTED)
991
+ currently_collected = true;
992
+
993
+ metrics_active++;
994
+
995
+ if (rm->first_time_t && rm->first_time_t < min_first_time_t)
996
+ min_first_time_t = rm->first_time_t;
997
+
998
+ if (rm->last_time_t && rm->last_time_t > max_last_time_t)
999
+ max_last_time_t = rm->last_time_t;
1000
+
1001
+ rrd_flag_unset_updated(rm);
1002
+ }
1003
+ dfe_done(rm);
1004
+ }
1005
+
1006
+ if(live_retention && !(ri->flags & RRD_FLAG_LIVE_RETENTION))
1007
+ ri->flags |= RRD_FLAG_LIVE_RETENTION;
1008
+ else if(!live_retention && (ri->flags & RRD_FLAG_LIVE_RETENTION))
1009
+ ri->flags &= ~RRD_FLAG_LIVE_RETENTION;
1010
+
1011
+ if(unlikely(!metrics_active)) {
1012
+ // no metrics available
1013
+
1014
+ if(ri->first_time_t) {
1015
+ ri->first_time_t = 0;
1016
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
1017
+ }
1018
+
1019
+ if(ri->last_time_t) {
1020
+ ri->last_time_t = 0;
1021
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
1022
+ }
1023
+
1024
+ rrd_flag_set_deleted(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
1025
+ }
1026
+ else {
1027
+ // we have active metrics...
1028
+
1029
+ if (unlikely(min_first_time_t == LONG_MAX))
1030
+ min_first_time_t = 0;
1031
+
1032
+ if (unlikely(min_first_time_t == 0 || max_last_time_t == 0)) {
1033
+ if(ri->first_time_t) {
1034
+ ri->first_time_t = 0;
1035
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
1036
+ }
1037
+
1038
+ if(ri->last_time_t) {
1039
+ ri->last_time_t = 0;
1040
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
1041
+ }
1042
+
1043
+ if(unlikely(live_retention))
1044
+ rrd_flag_set_deleted(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
1045
+ }
1046
+ else {
1047
+ ri->flags &= ~RRD_FLAG_UPDATE_REASON_ZERO_RETENTION;
1048
+
1049
+ if (unlikely(ri->first_time_t != min_first_time_t)) {
1050
+ ri->first_time_t = min_first_time_t;
1051
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
1052
+ }
1053
+
1054
+ if (unlikely(ri->last_time_t != max_last_time_t)) {
1055
+ ri->last_time_t = max_last_time_t;
1056
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
1057
+ }
1058
+
1059
+ if(likely(currently_collected))
1060
+ rrd_flag_set_collected(ri);
1061
+ else
1062
+ rrd_flag_set_archived(ri);
1063
+ }
1064
+ }
1065
+
1066
+ if(unlikely(escalate && ri->flags & RRD_FLAG_UPDATED && !(ri->rc->flags & RRD_FLAG_DONT_PROCESS))) {
1067
+ log_transition(NULL, ri->id, ri->rc->id, ri->flags, "RRDINSTANCE");
1068
+ rrdcontext_trigger_updates(ri->rc, true);
1069
+ }
1070
+}
1071
+
1072
+static inline void rrdinstance_from_rrdset(RRDSET *st) {
1073
+ RRDCONTEXT trc = {
1074
+ .id = string_strdupz(st->context),
1075
+ .title = string_strdupz(st->title),
1076
+ .units = string_strdupz(st->units),
1077
+ .family = string_strdupz(st->family),
1078
+ .priority = st->priority,
1079
+ .chart_type = st->chart_type,
1080
+ .flags = RRD_FLAG_NONE,
1081
+ .rrdhost = st->rrdhost,
1082
+ };
1083
+
1084
+ RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_set_and_acquire_item((DICTIONARY *)st->rrdhost->rrdctx, string2str(trc.id), &trc, sizeof(trc));
1085
+ RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
1086
+
1087
+ RRDINSTANCE tri = {
1088
+ .id = string_strdupz(st->id),
1089
+ .name = string_strdupz(st->name),
1090
+ .units = string_strdupz(st->units),
1091
+ .family = string_strdupz(st->family),
1092
+ .title = string_strdupz(st->title),
1093
+ .chart_type = st->chart_type,
1094
+ .priority = st->priority,
1095
+ .update_every = st->update_every,
1096
+ .flags = RRD_FLAG_DONT_PROCESS,
1097
+ .rrdset = st,
1098
+ };
1099
+ uuid_copy(tri.uuid, *st->chart_uuid);
1100
+
1101
+ RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_set_and_acquire_item(rc->rrdinstances, string2str(tri.id), &tri, sizeof(tri));
1102
+
1103
+ RRDCONTEXT_ACQUIRED *rca_old = st->rrdcontext;
1104
+ RRDINSTANCE_ACQUIRED *ria_old = st->rrdinstance;
1105
+
1106
+ st->rrdcontext = rca;
1107
+ st->rrdinstance = ria;
1108
+
1109
+ if(rca == rca_old) {
1110
+ rrdcontext_release(rca_old);
1111
+ rca_old = NULL;
1112
+ }
1113
+
1114
+ if(ria == ria_old) {
1115
+ rrdinstance_release(ria_old);
1116
+ ria_old = NULL;
1117
+ }
1118
+
1119
+ if(rca_old && ria_old) {
1120
+ // the chart changed context
1121
+ RRDCONTEXT *rc_old = rrdcontext_acquired_value(rca_old);
1122
+ RRDINSTANCE *ri_old = rrdinstance_acquired_value(ria_old);
1123
+
1124
+ // migrate all dimensions to the new metrics
1125
+ rrdset_rdlock(st);
1126
+ RRDDIM *rd;
1127
+ rrddim_foreach_read(rd, st) {
1128
+ if (!rd->rrdmetric) continue;
1129
+
1130
+ RRDMETRIC *rm_old = rrdmetric_acquired_value(rd->rrdmetric);
1131
+ rm_old->flags = RRD_FLAG_DELETED|RRD_FLAG_UPDATED|RRD_FLAG_LIVE_RETENTION|RRD_FLAG_UPDATE_REASON_UNUSED|RRD_FLAG_UPDATE_REASON_ZERO_RETENTION;
1132
+ rm_old->rrddim = NULL;
1133
+ rm_old->first_time_t = 0;
1134
+ rm_old->last_time_t = 0;
1135
+
1136
+ rrdmetric_release(rd->rrdmetric);
1137
+ rd->rrdmetric = NULL;
1138
+
1139
+ rrdmetric_from_rrddim(rd);
1140
+ }
1141
+ rrdset_unlock(st);
1142
+
1143
+ // mark the old instance, ready to be deleted
1144
+ if(!(ri_old->flags & RRD_FLAG_OWN_LABELS))
1145
+ ri_old->rrdlabels = rrdlabels_create();
1146
+
1147
+ ri_old->flags = RRD_FLAG_OWN_LABELS|RRD_FLAG_DELETED|RRD_FLAG_UPDATED|RRD_FLAG_LIVE_RETENTION|RRD_FLAG_UPDATE_REASON_UNUSED|RRD_FLAG_UPDATE_REASON_ZERO_RETENTION;
1148
+ ri_old->rrdset = NULL;
1149
+ ri_old->first_time_t = 0;
1150
+ ri_old->last_time_t = 0;
1151
+
1152
+ ri_old->flags &= ~RRD_FLAG_DONT_PROCESS;
1153
+ rc_old->flags &= ~RRD_FLAG_DONT_PROCESS;
1154
+
1155
+ rrdinstance_trigger_updates(ri_old, true, true);
1156
+
1157
+ ri_old->flags |= RRD_FLAG_DONT_PROCESS;
1158
+ rrdinstance_release(ria_old);
1159
+
1160
+ /*
1161
+ // trigger updates on the old context
1162
+ if(!dictionary_stats_entries(rc_old->rrdinstances) && !dictionary_stats_referenced_items(rc_old->rrdinstances)) {
1163
+ rrdcontext_lock(rc_old);
1164
+ rc_old->flags = ((rc_old->flags & RRD_FLAG_QUEUED)?RRD_FLAG_QUEUED:RRD_FLAG_NONE)|RRD_FLAG_DELETED|RRD_FLAG_UPDATED|RRD_FLAG_LIVE_RETENTION|RRD_FLAG_UPDATE_REASON_UNUSED|RRD_FLAG_UPDATE_REASON_ZERO_RETENTION;
1165
+ rc_old->first_time_t = 0;
1166
+ rc_old->last_time_t = 0;
1167
+ rrdcontext_unlock(rc_old);
1168
+ rrdcontext_trigger_updates(rc_old, true);
1169
+ }
1170
+ else
1171
+ rrdcontext_trigger_updates(rc_old, true);
1172
+ */
1173
+
1174
+ rrdcontext_release(rca_old);
1175
+ rca_old = NULL;
1176
+ ria_old = NULL;
1177
+ }
1178
+
1179
+ if(rca_old || ria_old)
1180
+ fatal("RRDCONTEXT: cannot switch rrdcontext without switching rrdinstance too");
1181
+}
1182
+
1183
+#define rrdset_get_rrdinstance(st) rrdset_get_rrdinstance_with_trace(st, __FUNCTION__);
1184
+static inline RRDINSTANCE *rrdset_get_rrdinstance_with_trace(RRDSET *st, const char *function) {
1185
+ if(unlikely(!st->rrdinstance))
1186
+ fatal("RRDINSTANCE: RRDSET '%s' is not linked to an RRDINSTANCE at %s()", st->id, function);
1187
+
1188
+ RRDINSTANCE *ri = rrdinstance_acquired_value(st->rrdinstance);
1189
+
1190
+ if(unlikely(ri->rrdset != st))
1191
+ fatal("RRDINSTANCE: '%s' is not linked to RRDSET '%s' at %s()", string2str(ri->id), st->id, function);
1192
+
1193
+ return ri;
1194
+}
1195
+
1196
+static inline void rrdinstance_rrdset_is_freed(RRDSET *st) {
1197
+ RRDINSTANCE *ri = rrdset_get_rrdinstance(st);
1198
+
1199
+ rrd_flag_set_archived(ri);
1200
+
1201
+ if(!(ri->flags & RRD_FLAG_OWN_LABELS)) {
1202
+ ri->flags |= RRD_FLAG_OWN_LABELS;
1203
+ ri->rrdlabels = rrdlabels_create();
1204
+ rrdlabels_copy(ri->rrdlabels, st->state->chart_labels);
1205
+ }
1206
+
1207
+ ri->rrdset = NULL;
1208
+
1209
+ ri->flags &= ~RRD_FLAG_DONT_PROCESS;
1210
+ rrdinstance_trigger_updates(ri, false, true);
1211
+ ri->flags |= RRD_FLAG_DONT_PROCESS;
1212
+
1213
+ rrdinstance_release(st->rrdinstance);
1214
+ st->rrdinstance = NULL;
1215
+
1216
+ rrdcontext_release(st->rrdcontext);
1217
+ st->rrdcontext = NULL;
1218
+}
1219
+
1220
+static inline void rrdinstance_updated_rrdset_name(RRDSET *st) {
1221
+ // the chart may not be initialized when this is called
1222
+ if(unlikely(!st->rrdinstance)) return;
1223
+
1224
+ RRDINSTANCE *ri = rrdset_get_rrdinstance(st);
1225
+
1226
+ STRING *old = ri->name;
1227
+ ri->name = string_strdupz(st->name);
1228
+
1229
+ if(ri->name != old)
1230
+ rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_NAME);
1231
+
1232
+ string_freez(old);
1233
+
1234
+ rrdinstance_trigger_updates(ri, false, true);
1235
+}
1236
+
1237
+static inline void rrdinstance_updated_rrdset_flags(RRDSET *st) {
1238
+ RRDINSTANCE *ri = rrdset_get_rrdinstance(st);
1239
+
1240
+ if(unlikely(st->flags & (RRDSET_FLAG_ARCHIVED | RRDSET_FLAG_OBSOLETE)))
1241
+ rrd_flag_set_archived(ri);
1242
+
1243
+ ri->flags &= ~RRD_FLAG_DONT_PROCESS;
1244
+ rrdinstance_trigger_updates(ri, false, true);
1245
+ ri->flags |= RRD_FLAG_DONT_PROCESS;
1246
+}
1247
+
1248
+static inline void rrdinstance_collected_rrdset(RRDSET *st) {
1249
+ RRDINSTANCE *ri = rrdset_get_rrdinstance(st);
1250
+
1251
+ if(unlikely(!rrd_flag_is_collected(ri)))
1252
+ rrd_flag_set_collected(ri);
1253
+
1254
+ // the chart is collected, let's process it now
1255
+ if(unlikely(ri->flags & RRD_FLAG_DONT_PROCESS))
1256
+ ri->flags &= ~RRD_FLAG_DONT_PROCESS;
1257
+
1258
+ rrdinstance_trigger_updates(ri, false, true);
1259
+}
1260
+
1261
+// ----------------------------------------------------------------------------
1262
+// RRDCONTEXT
1263
+
1264
+static void rrdcontext_freez(RRDCONTEXT *rc) {
1265
+ string_freez(rc->id);
1266
+ string_freez(rc->title);
1267
+ string_freez(rc->units);
1268
+ string_freez(rc->family);
1269
+}
1270
+
1271
+static uint64_t rrdcontext_get_next_version(RRDCONTEXT *rc) {
1272
+ time_t now = now_realtime_sec();
1273
+ uint64_t version = MAX(rc->version, rc->hub.version);
1274
+ version = MAX((uint64_t)now, version);
1275
+ version++;
1276
+ return version;
1277
+}
1278
+
1279
+static void rrdcontext_message_send_unsafe(RRDCONTEXT *rc, bool snapshot __maybe_unused, void *bundle __maybe_unused) {
1280
+
1281
+ // save it, so that we know the last version we sent to hub
1282
+ rc->version = rc->hub.version = rrdcontext_get_next_version(rc);
1283
+ rc->hub.id = string2str(rc->id);
1284
+ rc->hub.title = string2str(rc->title);
1285
+ rc->hub.units = string2str(rc->units);
1286
+ rc->hub.family = string2str(rc->family);
1287
+ rc->hub.chart_type = rrdset_type_name(rc->chart_type);
1288
+ rc->hub.priority = rc->priority;
1289
+ rc->hub.first_time_t = rc->first_time_t;
1290
+ rc->hub.last_time_t = rrd_flag_is_collected(rc) ? 0 : rc->last_time_t;
1291
+ rc->hub.deleted = (rc->flags & RRD_FLAG_DELETED) ? true : false;
1292
+
1293
+#ifdef ENABLE_ACLK
1294
+ struct context_updated message = {
1295
+ .id = rc->hub.id,
1296
+ .version = rc->hub.version,
1297
+ .title = rc->hub.title,
1298
+ .units = rc->hub.units,
1299
+ .family = rc->hub.family,
1300
+ .chart_type = rc->hub.chart_type,
1301
+ .priority = rc->hub.priority,
1302
+ .first_entry = rc->hub.first_time_t,
1303
+ .last_entry = rc->hub.last_time_t,
1304
+ .deleted = rc->hub.deleted,
1305
+ };
1306
+
1307
+ if(snapshot) {
1308
+ if(!rc->hub.deleted)
1309
+ contexts_snapshot_add_ctx_update(bundle, &message);
1310
+ }
1311
+ else
1312
+ contexts_updated_add_ctx_update(bundle, &message);
1313
+#endif
1314
+
1315
+ // store it to SQL
1316
+
1317
+ if(rc->flags & RRD_FLAG_DELETED) {
1318
+ rrdcontext_delete_from_sql_unsafe(rc);
1319
+ }
1320
+ else {
1321
+ if (ctx_store_context(&rc->rrdhost->host_uuid, &rc->hub) != 0)
1322
+ error("RRDCONTEXT: failed to save context '%s' version %lu to SQL.", rc->hub.id, rc->hub.version);
1323
+ }
1324
+}
1325
+
1326
+static bool check_if_cloud_version_changed_unsafe(RRDCONTEXT *rc, bool sending __maybe_unused) {
1327
+ bool id_changed = false,
1328
+ title_changed = false,
1329
+ units_changed = false,
1330
+ family_changed = false,
1331
+ chart_type_changed = false,
1332
+ priority_changed = false,
1333
+ first_time_changed = false,
1334
+ last_time_changed = false,
1335
+ deleted_changed = false;
1336
+
1337
+ if(unlikely(string2str(rc->id) != rc->hub.id))
1338
+ id_changed = true;
1339
+
1340
+ if(unlikely(string2str(rc->title) != rc->hub.title))
1341
+ title_changed = true;
1342
+
1343
+ if(unlikely(string2str(rc->units) != rc->hub.units))
1344
+ units_changed = true;
1345
+
1346
+ if(unlikely(string2str(rc->family) != rc->hub.family))
1347
+ family_changed = true;
1348
+
1349
+ if(unlikely(rrdset_type_name(rc->chart_type) != rc->hub.chart_type))
1350
+ chart_type_changed = true;
1351
+
1352
+ if(unlikely(rc->priority != rc->hub.priority))
1353
+ priority_changed = true;
1354
+
1355
+ if(unlikely((uint64_t)rc->first_time_t != rc->hub.first_time_t))
1356
+ first_time_changed = true;
1357
+
1358
+ if(unlikely((uint64_t)(rrd_flag_is_collected(rc) ? 0 : rc->last_time_t) != rc->hub.last_time_t))
1359
+ last_time_changed = true;
1360
+
1361
+ if(unlikely(((rc->flags & RRD_FLAG_DELETED) ? true : false) != rc->hub.deleted))
1362
+ deleted_changed = true;
1363
+
1364
+ if(unlikely(id_changed || title_changed || units_changed || family_changed || chart_type_changed || priority_changed || first_time_changed || last_time_changed || deleted_changed)) {
1365
+
1366
+ internal_error(true, "RRDCONTEXT: %s NEW VERSION '%s'%s, version %zu, title '%s'%s, units '%s'%s, family '%s'%s, chart type '%s'%s, priority %u%s, first_time_t %ld%s, last_time_t %ld%s, deleted '%s'%s, (queued for %llu ms, expected %llu ms)",
1367
+ sending?"SENDING":"QUEUE",
1368
+ string2str(rc->id), id_changed ? " (CHANGED)" : "",
1369
+ rc->version,
1370
+ string2str(rc->title), title_changed ? " (CHANGED)" : "",
1371
+ string2str(rc->units), units_changed ? " (CHANGED)" : "",
1372
+ string2str(rc->family), family_changed ? " (CHANGED)" : "",
1373
+ rrdset_type_name(rc->chart_type), chart_type_changed ? " (CHANGED)" : "",
1374
+ rc->priority, priority_changed ? " (CHANGED)" : "",
1375
+ rc->first_time_t, first_time_changed ? " (CHANGED)" : "",
1376
+ rrd_flag_is_collected(rc) ? 0 : rc->last_time_t, last_time_changed ? " (CHANGED)" : "",
1377
+ (rc->flags & RRD_FLAG_DELETED) ? "true" : "false", deleted_changed ? " (CHANGED)" : "",
1378
+ sending ? (now_realtime_usec() - rc->queue.queued_ut) / USEC_PER_MS : 0,
1379
+ sending ? (rc->queue.scheduled_dispatch_ut - rc->queue.queued_ut) / USEC_PER_SEC : 0
1380
+ );
1381
+ return true;
1382
+ }
1383
+
1384
+ return false;
1385
+}
1386
+
1387
+static void rrdcontext_insert_callback(const char *id, void *value, void *data) {
1388
+ (void)id;
1389
+ RRDHOST *host = (RRDHOST *)data;
1390
+ RRDCONTEXT *rc = (RRDCONTEXT *)value;
1391
+
1392
+ rc->rrdhost = host;
1393
+ rc->flags = rc->flags & RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS;
1394
+
1395
+ if(rc->hub.version) {
1396
+ // we are loading data from the SQL database
1397
+
1398
+ if(rc->version)
1399
+ error("RRDCONTEXT: context '%s' is already initialized with version %lu, but it is loaded again from SQL with version %lu", string2str(rc->id), rc->version, rc->hub.version);
1400
+
1401
+ // IMPORTANT
1402
+ // replace all string pointers in rc->hub with our own versions
1403
+ // the originals are coming from a tmp allocation of sqlite
1404
+
1405
+ string_freez(rc->id);
1406
+ rc->id = string_strdupz(rc->hub.id);
1407
+ rc->hub.id = string2str(rc->id);
1408
+
1409
+ string_freez(rc->title);
1410
+ rc->title = string_strdupz(rc->hub.title);
1411
+ rc->hub.title = string2str(rc->title);
1412
+
1413
+ string_freez(rc->units);
1414
+ rc->units = string_strdupz(rc->hub.units);
1415
+ rc->hub.units = string2str(rc->units);
1416
+
1417
+ string_freez(rc->family);
1418
+ rc->family = string_strdupz(rc->hub.family);
1419
+ rc->hub.family = string2str(rc->family);
1420
+
1421
+ rc->chart_type = rrdset_type_id(rc->hub.chart_type);
1422
+ rc->hub.chart_type = rrdset_type_name(rc->chart_type);
1423
+
1424
+ rc->version = rc->hub.version;
1425
+ rc->priority = rc->hub.priority;
1426
+ rc->first_time_t = rc->hub.first_time_t;
1427
+ rc->last_time_t = rc->hub.last_time_t;
1428
+
1429
+ if(rc->hub.deleted || !rc->hub.first_time_t)
1430
+ rrd_flag_set_deleted(rc, 0);
1431
+ else {
1432
+ if (rc->last_time_t == 0)
1433
+ rrd_flag_set_collected(rc);
1434
+ else
1435
+ rrd_flag_set_archived(rc);
1436
+ }
1437
+
1438
+ rc->flags |= RRD_FLAG_UPDATE_REASON_LOAD_SQL;
1439
+ }
1440
+ else {
1441
+ // we are adding this context now for the first time
1442
+ rc->version = now_realtime_sec();
1443
+ }
1444
+
1445
+ rrdinstances_create(rc);
1446
+ netdata_mutex_init(&rc->mutex);
1447
+
1448
+ // signal the react callback to do the job
1449
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_NEW_OBJECT);
1450
+}
1451
+
1452
+static void rrdcontext_delete_callback(const char *id, void *value, void *data) {
1453
+ (void)id;
1454
+ RRDHOST *host = (RRDHOST *)data;
1455
+ (void)host;
1456
+
1457
+ RRDCONTEXT *rc = (RRDCONTEXT *)value;
1458
+
1459
+ rrdinstances_destroy(rc);
1460
+ netdata_mutex_destroy(&rc->mutex);
1461
+ rrdcontext_freez(rc);
1462
+}
1463
+
1464
+static void rrdcontext_conflict_callback(const char *id, void *oldv, void *newv, void *data) {
1465
+ (void)id;
1466
+ RRDHOST *host = (RRDHOST *)data;
1467
+ (void)host;
1468
+
1469
+ RRDCONTEXT *rc = (RRDCONTEXT *)oldv;
1470
+ RRDCONTEXT *rc_new = (RRDCONTEXT *)newv;
1471
+
1472
+ rrdcontext_lock(rc);
1473
+
1474
+ if(rc->title != rc_new->title) {
1475
+ STRING *old_title = rc->title;
1476
+ rc->title = string_2way_merge(rc->title, rc_new->title);
1477
+ string_freez(old_title);
1478
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_TITLE);
1479
+ }
1480
+
1481
+ if(rc->units != rc_new->units) {
1482
+ STRING *old_units = rc->units;
1483
+ rc->units = string_dup(rc_new->units);
1484
+ string_freez(old_units);
1485
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_UNITS);
1486
+ }
1487
+
1488
+ if(rc->family != rc_new->family) {
1489
+ STRING *old_family = rc->family;
1490
+ rc->family = string_2way_merge(rc->family, rc_new->family);
1491
+ string_freez(old_family);
1492
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FAMILY);
1493
+ }
1494
+
1495
+ if(rc->chart_type != rc_new->chart_type) {
1496
+ rc->chart_type = rc_new->chart_type;
1497
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_CHART_TYPE);
1498
+ }
1499
+
1500
+ if(rc->priority != rc_new->priority) {
1501
+ rc->priority = rc_new->priority;
1502
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY);
1503
+ }
1504
+
1505
+ rc->flags |= (rc_new->flags & RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS);
1506
+
1507
+ if(rrd_flag_is_collected(rc) && rrd_flag_is_archived(rc))
1508
+ rrd_flag_set_collected(rc);
1509
+
1510
+ if(rc->flags & RRD_FLAG_UPDATED)
1511
+ rc->flags |= RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT;
1512
+
1513
+ rrdcontext_unlock(rc);
1514
+
1515
+ // free the resources of the new one
1516
+ rrdcontext_freez(rc_new);
1517
+
1518
+ // the react callback will continue from here
1519
+}
1520
+
1521
+static void rrdcontext_react_callback(const char *id __maybe_unused, void *value, void *data __maybe_unused) {
1522
+ RRDCONTEXT *rc = (RRDCONTEXT *)value;
1523
+
1524
+ rrdcontext_trigger_updates(rc, false);
1525
+}
1526
+
1527
+void rrdhost_create_rrdcontexts(RRDHOST *host) {
1528
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1529
+ return;
1530
+
1531
+ if(unlikely(!host)) return;
1532
+ if(likely(host->rrdctx)) return;
1533
+
1534
+ host->rrdctx = (RRDCONTEXTS *)dictionary_create(DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
1535
+ dictionary_register_insert_callback((DICTIONARY *)host->rrdctx, rrdcontext_insert_callback, (void *)host);
1536
+ dictionary_register_delete_callback((DICTIONARY *)host->rrdctx, rrdcontext_delete_callback, (void *)host);
1537
+ dictionary_register_conflict_callback((DICTIONARY *)host->rrdctx, rrdcontext_conflict_callback, (void *)host);
1538
+ dictionary_register_react_callback((DICTIONARY *)host->rrdctx, rrdcontext_react_callback, (void *)host);
1539
+
1540
+ host->rrdctx_queue = (RRDCONTEXTS *)dictionary_create(DICTIONARY_FLAG_DONT_OVERWRITE_VALUE | DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE);
1541
+}
1542
+
1543
+void rrdhost_destroy_rrdcontexts(RRDHOST *host) {
1544
+ if(unlikely(!host)) return;
1545
+ if(unlikely(!host->rrdctx)) return;
1546
+
1547
+ if(host->rrdctx_queue) {
1548
+ dictionary_destroy((DICTIONARY *)host->rrdctx_queue);
1549
+ host->rrdctx_queue = NULL;
1550
+ }
1551
+
1552
+ dictionary_destroy((DICTIONARY *)host->rrdctx);
1553
+ host->rrdctx = NULL;
1554
+}
1555
+
1556
+static inline bool rrdcontext_should_be_deleted(RRDCONTEXT *rc) {
1557
+ if(likely(!(rc->flags & RRD_FLAG_DELETED)))
1558
+ return false;
1559
+
1560
+ if(likely(!(rc->flags & RRD_FLAG_LIVE_RETENTION)))
1561
+ return false;
1562
+
1563
+ if(unlikely(rc->flags & RRD_FLAGS_PREVENTING_DELETIONS))
1564
+ return false;
1565
+
1566
+ if(unlikely(dictionary_stats_referenced_items(rc->rrdinstances) != 0))
1567
+ return false;
1568
+
1569
+ if(unlikely(dictionary_stats_entries(rc->rrdinstances) != 0))
1570
+ return false;
1571
+
1572
+ if(unlikely(rc->first_time_t || rc->last_time_t))
1573
+ return false;
1574
+
1575
+ return true;
1576
+}
1577
+
1578
+static void rrdcontext_trigger_updates(RRDCONTEXT *rc, bool force) {
1579
+ if(unlikely(rc->flags & RRD_FLAG_DONT_PROCESS)) return;
1580
+ if(unlikely(!force && !(rc->flags & RRD_FLAG_UPDATED))) return;
1581
+
1582
+ rrdcontext_lock(rc);
1583
+
1584
+ size_t min_priority = LONG_MAX;
1585
+ time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
1586
+ size_t instances_active = 0, instances_deleted = 0;
1587
+ bool live_retention = true, currently_collected = false;
1588
+ {
1589
+ RRDINSTANCE *ri;
1590
+ dfe_start_read(rc->rrdinstances, ri) {
1591
+ if(!(ri->flags & RRD_FLAG_LIVE_RETENTION))
1592
+ live_retention = false;
1593
+
1594
+ if (unlikely(rrdinstance_should_be_deleted(ri))) {
1595
+ instances_deleted++;
1596
+ rrd_flag_unset_updated(ri);
1597
+ continue;
1598
+ }
1599
+
1600
+ if(ri->flags & RRD_FLAG_COLLECTED)
1601
+ currently_collected = true;
1602
+
1603
+ internal_error(rc->units != ri->units,
1604
+ "RRDCONTEXT: '%s' rrdinstance '%s' has different units, context '%s', instance '%s'",
1605
+ string2str(rc->id), string2str(ri->id),
1606
+ string2str(rc->units), string2str(ri->units));
1607
+
1608
+ instances_active++;
1609
+
1610
+ if (ri->priority >= RRDCONTEXT_MINIMUM_ALLOWED_PRIORITY && ri->priority < min_priority)
1611
+ min_priority = ri->priority;
1612
+
1613
+ if (ri->first_time_t && ri->first_time_t < min_first_time_t)
1614
+ min_first_time_t = ri->first_time_t;
1615
+
1616
+ if (ri->last_time_t && ri->last_time_t > max_last_time_t)
1617
+ max_last_time_t = ri->last_time_t;
1618
+
1619
+ rrd_flag_unset_updated(ri);
1620
+ }
1621
+ dfe_done(ri);
1622
+ }
1623
+
1624
+ if(live_retention && !(rc->flags & RRD_FLAG_LIVE_RETENTION))
1625
+ rc->flags |= RRD_FLAG_LIVE_RETENTION;
1626
+ else if(!live_retention && (rc->flags & RRD_FLAG_LIVE_RETENTION))
1627
+ rc->flags &= ~RRD_FLAG_LIVE_RETENTION;
1628
+
1629
+ if(unlikely(!instances_active)) {
1630
+ // we had some instances, but they are gone now...
1631
+
1632
+ if(rc->first_time_t) {
1633
+ rc->first_time_t = 0;
1634
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
1635
+ }
1636
+
1637
+ if(rc->last_time_t) {
1638
+ rc->last_time_t = 0;
1639
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
1640
+ }
1641
+
1642
+ rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
1643
+ }
1644
+ else {
1645
+ // we have some active instances...
1646
+
1647
+ if (unlikely(min_first_time_t == LONG_MAX))
1648
+ min_first_time_t = 0;
1649
+
1650
+ if (unlikely(min_first_time_t == 0 && max_last_time_t == 0)) {
1651
+ if(rc->first_time_t) {
1652
+ rc->first_time_t = 0;
1653
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
1654
+ }
1655
+
1656
+ if(rc->last_time_t) {
1657
+ rc->last_time_t = 0;
1658
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
1659
+ }
1660
+
1661
+ rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
1662
+ }
1663
+ else {
1664
+ rc->flags &= ~RRD_FLAG_UPDATE_REASON_ZERO_RETENTION;
1665
+
1666
+ if (unlikely(rc->first_time_t != min_first_time_t)) {
1667
+ rc->first_time_t = min_first_time_t;
1668
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
1669
+ }
1670
+
1671
+ if (rc->last_time_t != max_last_time_t) {
1672
+ rc->last_time_t = max_last_time_t;
1673
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
1674
+ }
1675
+
1676
+ if(likely(currently_collected))
1677
+ rrd_flag_set_collected(rc);
1678
+ else
1679
+ rrd_flag_set_archived(rc);
1680
+ }
1681
+
1682
+ if (min_priority != LONG_MAX && rc->priority != min_priority) {
1683
+ rc->priority = min_priority;
1684
+ rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_PRIORITY);
1685
+ }
1686
+ }
1687
+
1688
+ if(unlikely(rc->flags & RRD_FLAG_UPDATED)) {
1689
+ log_transition(NULL, NULL, rc->id, rc->flags, "RRDCONTEXT");
1690
+
1691
+ if(check_if_cloud_version_changed_unsafe(rc, false)) {
1692
+ rc->version = rrdcontext_get_next_version(rc);
1693
+
1694
+ if(rc->flags & RRD_FLAG_QUEUED) {
1695
+ rc->queue.queued_ut = now_realtime_usec();
1696
+ rc->queue.queued_flags |= rc->flags;
1697
+ }
1698
+ else {
1699
+ rc->queue.queued_ut = now_realtime_usec();
1700
+ rc->queue.queued_flags = rc->flags;
1701
+
1702
+ rc->flags |= RRD_FLAG_QUEUED;
1703
+ dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx_queue, string2str(rc->id), rc, sizeof(*rc));
1704
+ }
1705
+ }
1706
+
1707
+ rrd_flag_unset_updated(rc);
1708
+ }
1709
+
1710
+ rrdcontext_unlock(rc);
1711
+}
1712
+
1713
+// ----------------------------------------------------------------------------
1714
+// public API
1715
+
1716
+void rrdcontext_updated_rrddim(RRDDIM *rd) {
1717
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1718
+ return;
1719
+
1720
+ rrdmetric_from_rrddim(rd);
1721
+}
1722
+
1723
+void rrdcontext_removed_rrddim(RRDDIM *rd) {
1724
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1725
+ return;
1726
+
1727
+ rrdmetric_rrddim_is_freed(rd);
1728
+}
1729
+
1730
+void rrdcontext_updated_rrddim_algorithm(RRDDIM *rd) {
1731
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1732
+ return;
1733
+
1734
+ rrdmetric_updated_rrddim_flags(rd);
1735
+}
1736
+
1737
+void rrdcontext_updated_rrddim_multiplier(RRDDIM *rd) {
1738
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1739
+ return;
1740
+
1741
+ rrdmetric_updated_rrddim_flags(rd);
1742
+}
1743
+
1744
+void rrdcontext_updated_rrddim_divisor(RRDDIM *rd) {
1745
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1746
+ return;
1747
+
1748
+ rrdmetric_updated_rrddim_flags(rd);
1749
+}
1750
+
1751
+void rrdcontext_updated_rrddim_flags(RRDDIM *rd) {
1752
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1753
+ return;
1754
+
1755
+ rrdmetric_updated_rrddim_flags(rd);
1756
+}
1757
+
1758
+void rrdcontext_collected_rrddim(RRDDIM *rd) {
1759
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1760
+ return;
1761
+
1762
+ rrdmetric_collected_rrddim(rd);
1763
+}
1764
+
1765
+void rrdcontext_updated_rrdset(RRDSET *st) {
1766
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1767
+ return;
1768
+
1769
+ rrdinstance_from_rrdset(st);
1770
+}
1771
+
1772
+void rrdcontext_removed_rrdset(RRDSET *st) {
1773
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1774
+ return;
1775
+
1776
+ rrdinstance_rrdset_is_freed(st);
1777
+}
1778
+
1779
+void rrdcontext_updated_rrdset_name(RRDSET *st) {
1780
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1781
+ return;
1782
+
1783
+ rrdinstance_updated_rrdset_name(st);
1784
+}
1785
+
1786
+void rrdcontext_updated_rrdset_flags(RRDSET *st) {
1787
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1788
+ return;
1789
+
1790
+ rrdinstance_updated_rrdset_flags(st);
1791
+}
1792
+
1793
+void rrdcontext_collected_rrdset(RRDSET *st) {
1794
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1795
+ return;
1796
+
1797
+ rrdinstance_collected_rrdset(st);
1798
+}
1799
+
1800
+void rrdcontext_host_child_connected(RRDHOST *host) {
1801
+ (void)host;
1802
+
1803
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1804
+ return;
1805
+
1806
+ // no need to do anything here
1807
+ ;
1808
+}
1809
+
1810
+void rrdcontext_host_child_disconnected(RRDHOST *host) {
1811
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
1812
+ return;
1813
+
1814
+ rrdcontext_recalculate_host_retention(host, RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD, -1);
1815
+}
1816
+
1817
+// ----------------------------------------------------------------------------
1818
+// ACLK interface
1819
+
1820
+static bool rrdhost_check_our_claim_id(const char *claim_id) {
1821
+ if(!localhost->aclk_state.claimed_id) return false;
1822
+ return (strcasecmp(claim_id, localhost->aclk_state.claimed_id) == 0) ? true : false;
1823
+}
1824
+
1825
+static RRDHOST *rrdhost_find_by_node_id(const char *node_id) {
1826
+ uuid_t uuid;
1827
+ if (uuid_parse(node_id, uuid))
1828
+ return NULL;
1829
+
1830
+ RRDHOST *host = NULL;
1831
+
1832
+ rrd_rdlock();
1833
+ rrdhost_foreach_read(host) {
1834
+ if(!host->node_id) continue;
1835
+
1836
+ if(uuid_compare(uuid, *host->node_id) == 0)
1837
+ break;
1838
+ }
1839
+ rrd_unlock();
1840
+
1841
+ return host;
1842
+}
1843
+
1844
+void rrdcontext_hub_checkpoint_command(void *ptr) {
1845
+ struct ctxs_checkpoint *cmd = ptr;
1846
+
1847
+ if(!rrdhost_check_our_claim_id(cmd->claim_id)) {
1848
+ error("RRDCONTEXT: received checkpoint command for claim_id '%s', node id '%s', but this is not our claim id. Ours '%s', received '%s'. Ignoring command.",
1849
+ cmd->claim_id, cmd->node_id,
1850
+ localhost->aclk_state.claimed_id?localhost->aclk_state.claimed_id:"NOT SET",
1851
+ cmd->claim_id);
1852
+
1853
+ return;
1854
+ }
1855
+
1856
+ RRDHOST *host = rrdhost_find_by_node_id(cmd->node_id);
1857
+ if(!host) {
1858
+ error("RRDCONTEXT: received checkpoint command for claim id '%s', node id '%s', but there is no node with such node id here. Ignoring command.",
1859
+ cmd->claim_id, cmd->node_id);
1860
+
1861
+ return;
1862
+ }
1863
+
1864
+ if(rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS)) {
1865
+ info("RRDCONTEXT: received checkpoint command for claim id '%s', node id '%s', while node '%s' has an active context streaming.",
1866
+ cmd->claim_id, cmd->node_id, host->hostname);
1867
+
1868
+ // disable it temporarily, so that our worker will not attempt to send messages in parallel
1869
+ rrdhost_flag_clear(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS);
1870
+ }
1871
+
1872
+ uint64_t our_version_hash = rrdcontext_version_hash(host);
1873
+
1874
+ if(cmd->version_hash != our_version_hash) {
1875
+ error("RRDCONTEXT: received version hash %lu for host '%s', does not match our version hash %lu. Sending snapshot of all contexts.",
1876
+ cmd->version_hash, host->hostname, our_version_hash);
1877
+
1878
+#ifdef ENABLE_ACLK
1879
+ // prepare the snapshot
1880
+ char uuid[UUID_STR_LEN];
1881
+ uuid_unparse_lower(*host->node_id, uuid);
1882
+ contexts_snapshot_t bundle = contexts_snapshot_new(cmd->claim_id, uuid, our_version_hash);
1883
+
1884
+ // do a deep scan on every metric of the host to make sure all our data are updated
1885
+ rrdcontext_recalculate_host_retention(host, RRD_FLAG_NONE, -1);
1886
+
1887
+ // calculate version hash and pack all the messages together in one go
1888
+ our_version_hash = rrdcontext_version_hash_with_callback(host, rrdcontext_message_send_unsafe, true, bundle);
1889
+
1890
+ // update the version
1891
+ contexts_snapshot_set_version(bundle, our_version_hash);
1892
+
1893
+ // send it
1894
+ aclk_send_contexts_snapshot(bundle);
1895
+#endif
1896
+ }
1897
+
1898
+ internal_error(true, "RRDCONTEXT: host '%s' enabling streaming of contexts", host->hostname);
1899
+ rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS);
1900
+ char node_str[UUID_STR_LEN];
1901
+ uuid_unparse_lower(*host->node_id, node_str);
1902
+ log_access("ACLK REQ [%s (%s)]: STREAM CONTEXTS ENABLED", node_str, host->hostname);
1903
+}
1904
+
1905
+void rrdcontext_hub_stop_streaming_command(void *ptr) {
1906
+ struct stop_streaming_ctxs *cmd = ptr;
1907
+
1908
+ if(!rrdhost_check_our_claim_id(cmd->claim_id)) {
1909
+ error("RRDCONTEXT: received stop streaming command for claim_id '%s', node id '%s', but this is not our claim id. Ours '%s', received '%s'. Ignoring command.",
1910
+ cmd->claim_id, cmd->node_id,
1911
+ localhost->aclk_state.claimed_id?localhost->aclk_state.claimed_id:"NOT SET",
1912
+ cmd->claim_id);
1913
+
1914
+ return;
1915
+ }
1916
+
1917
+ RRDHOST *host = rrdhost_find_by_node_id(cmd->node_id);
1918
+ if(!host) {
1919
+ error("RRDCONTEXT: received stop streaming command for claim id '%s', node id '%s', but there is no node with such node id here. Ignoring command.",
1920
+ cmd->claim_id, cmd->node_id);
1921
+
1922
+ return;
1923
+ }
1924
+
1925
+ if(!rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS)) {
1926
+ error("RRDCONTEXT: received stop streaming command for claim id '%s', node id '%s', but node '%s' does not have active context streaming. Ignoring command.",
1927
+ cmd->claim_id, cmd->node_id, host->hostname);
1928
+
1929
+ return;
1930
+ }
1931
+
1932
+ internal_error(true, "RRDCONTEXT: host '%s' disabling streaming of contexts", host->hostname);
1933
+ rrdhost_flag_clear(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS);
1934
+}
1935
+
1936
+// ----------------------------------------------------------------------------
1937
+// web API
1938
+
1939
+struct rrdcontext_to_json {
1940
+ BUFFER *wb;
1941
+ RRDCONTEXT_TO_JSON_OPTIONS options;
1942
+ time_t after;
1943
+ time_t before;
1944
+ SIMPLE_PATTERN *chart_label_key;
1945
+ SIMPLE_PATTERN *chart_labels_filter;
1946
+ SIMPLE_PATTERN *chart_dimensions;
1947
+ size_t written;
1948
+ time_t now;
1949
+ time_t combined_first_time_t;
1950
+ time_t combined_last_time_t;
1951
+ RRD_FLAGS combined_flags;
1952
+};
1953
+
1954
+static inline int rrdmetric_to_json_callback(const char *id, void *value, void *data) {
1955
+ struct rrdcontext_to_json * t = data;
1956
+ RRDMETRIC *rm = value;
1957
+ BUFFER *wb = t->wb;
1958
+ RRDCONTEXT_TO_JSON_OPTIONS options = t->options;
1959
+ time_t after = t->after;
1960
+ time_t before = t->before;
1961
+
1962
+ if((rm->flags & RRD_FLAG_DELETED) && !(options & RRDCONTEXT_OPTION_SHOW_DELETED))
1963
+ return 0;
1964
+
1965
+ if(after && (!rm->last_time_t || after > rm->last_time_t))
1966
+ return 0;
1967
+
1968
+ if(before && (!rm->first_time_t || before < rm->first_time_t))
1969
+ return 0;
1970
+
1971
+ if(t->chart_dimensions
1972
+ && !simple_pattern_matches(t->chart_dimensions, string2str(rm->id))
1973
+ && !simple_pattern_matches(t->chart_dimensions, string2str(rm->name)))
1974
+ return 0;
1975
+
1976
+ if(t->written) {
1977
+ buffer_strcat(wb, ",\n");
1978
+ t->combined_first_time_t = MIN(t->combined_first_time_t, rm->first_time_t);
1979
+ t->combined_last_time_t = MAX(t->combined_last_time_t, rm->last_time_t);
1980
+ t->combined_flags |= rm->flags;
1981
+ }
1982
+ else {
1983
+ buffer_strcat(wb, "\n");
1984
+ t->combined_first_time_t = rm->first_time_t;
1985
+ t->combined_last_time_t = rm->last_time_t;
1986
+ t->combined_flags = rm->flags;
1987
+ }
1988
+
1989
+ buffer_sprintf(wb, "\t\t\t\t\t\t\"%s\": {", id);
1990
+
1991
+ if(options & RRDCONTEXT_OPTION_SHOW_UUIDS) {
1992
+ char uuid[UUID_STR_LEN];
1993
+ uuid_unparse(rm->uuid, uuid);
1994
+ buffer_sprintf(wb, "\n\t\t\t\t\t\t\t\"uuid\":\"%s\",", uuid);
1995
+ }
1996
+
1997
+ buffer_sprintf(wb,
1998
+ "\n\t\t\t\t\t\t\t\"name\":\"%s\""
1999
+ ",\n\t\t\t\t\t\t\t\"first_time_t\":%ld"
2000
+ ",\n\t\t\t\t\t\t\t\"last_time_t\":%ld"
2001
+ ",\n\t\t\t\t\t\t\t\"collected\":%s"
2002
+ , string2str(rm->name)
2003
+ , rm->first_time_t
2004
+ , rrd_flag_is_collected(rm) ? t->now : rm->last_time_t
2005
+ , rm->flags & RRD_FLAG_COLLECTED ? "true" : "false"
2006
+ );
2007
+
2008
+ if(options & RRDCONTEXT_OPTION_SHOW_DELETED) {
2009
+ buffer_sprintf(wb,
2010
+ ",\n\t\t\t\t\t\t\t\"deleted\":%s"
2011
+ , rm->flags & RRD_FLAG_DELETED ? "true" : "false"
2012
+ );
2013
+ }
2014
+
2015
+ if(options & RRDCONTEXT_OPTION_SHOW_FLAGS) {
2016
+ buffer_strcat(wb, ",\n\t\t\t\t\t\t\t\"flags\":\"");
2017
+ rrd_flags_to_buffer(rm->flags, wb);
2018
+ buffer_strcat(wb, "\"");
2019
+ }
2020
+
2021
+ buffer_strcat(wb, "\n\t\t\t\t\t\t}");
2022
+ t->written++;
2023
+ return 1;
2024
+}
2025
+
2026
+static inline int rrdinstance_to_json_callback(const char *id, void *value, void *data) {
2027
+ struct rrdcontext_to_json *t_parent = data;
2028
+ RRDINSTANCE *ri = value;
2029
+ BUFFER *wb = t_parent->wb;
2030
+ RRDCONTEXT_TO_JSON_OPTIONS options = t_parent->options;
2031
+ time_t after = t_parent->after;
2032
+ time_t before = t_parent->before;
2033
+ bool has_filter = t_parent->chart_label_key || t_parent->chart_labels_filter || t_parent->chart_dimensions;
2034
+
2035
+ if((ri->flags & RRD_FLAG_DELETED) && !(options & RRDCONTEXT_OPTION_SHOW_DELETED))
2036
+ return 0;
2037
+
2038
+ if(after && (!ri->last_time_t || after > ri->last_time_t))
2039
+ return 0;
2040
+
2041
+ if(before && (!ri->first_time_t || before < ri->first_time_t))
2042
+ return 0;
2043
+
2044
+ if(t_parent->chart_label_key && !rrdlabels_match_simple_pattern_parsed(ri->rrdlabels, t_parent->chart_label_key, '\0'))
2045
+ return 0;
2046
+
2047
+ if(t_parent->chart_labels_filter && !rrdlabels_match_simple_pattern_parsed(ri->rrdlabels, t_parent->chart_labels_filter, ':'))
2048
+ return 0;
2049
+
2050
+ time_t first_time_t = ri->first_time_t;
2051
+ time_t last_time_t = ri->last_time_t;
2052
+ RRD_FLAGS flags = ri->flags;
2053
+
2054
+ BUFFER *wb_metrics = NULL;
2055
+ if(options & RRDCONTEXT_OPTION_SHOW_METRICS || t_parent->chart_dimensions) {
2056
+
2057
+ wb_metrics = buffer_create(4096);
2058
+
2059
+ struct rrdcontext_to_json t_metrics = {
2060
+ .wb = wb_metrics,
2061
+ .options = options,
2062
+ .chart_label_key = t_parent->chart_label_key,
2063
+ .chart_labels_filter = t_parent->chart_labels_filter,
2064
+ .chart_dimensions = t_parent->chart_dimensions,
2065
+ .after = after,
2066
+ .before = before,
2067
+ .written = 0,
2068
+ .now = t_parent->now,
2069
+ };
2070
+ dictionary_walkthrough_read(ri->rrdmetrics, rrdmetric_to_json_callback, &t_metrics);
2071
+
2072
+ if(has_filter && !t_metrics.written) {
2073
+ buffer_free(wb_metrics);
2074
+ return 0;
2075
+ }
2076
+
2077
+ first_time_t = t_metrics.combined_first_time_t;
2078
+ last_time_t = t_metrics.combined_last_time_t;
2079
+ flags = t_metrics.combined_flags;
2080
+ }
2081
+
2082
+ if(t_parent->written) {
2083
+ buffer_strcat(wb, ",\n");
2084
+ t_parent->combined_first_time_t = MIN(t_parent->combined_first_time_t, first_time_t);
2085
+ t_parent->combined_last_time_t = MAX(t_parent->combined_last_time_t, last_time_t);
2086
+ t_parent->combined_flags |= flags;
2087
+ }
2088
+ else {
2089
+ buffer_strcat(wb, "\n");
2090
+ t_parent->combined_first_time_t = first_time_t;
2091
+ t_parent->combined_last_time_t = last_time_t;
2092
+ t_parent->combined_flags = flags;
2093
+ }
2094
+
2095
+ buffer_sprintf(wb, "\t\t\t\t\"%s\": {", id);
2096
+
2097
+ if(options & RRDCONTEXT_OPTION_SHOW_UUIDS) {
2098
+ char uuid[UUID_STR_LEN];
2099
+ uuid_unparse(ri->uuid, uuid);
2100
+ buffer_sprintf(wb,"\n\t\t\t\t\t\"uuid\":\"%s\",", uuid);
2101
+ }
2102
+
2103
+ buffer_sprintf(wb,
2104
+ "\n\t\t\t\t\t\"name\":\"%s\""
2105
+ ",\n\t\t\t\t\t\"context\":\"%s\""
2106
+ ",\n\t\t\t\t\t\"title\":\"%s\""
2107
+ ",\n\t\t\t\t\t\"units\":\"%s\""
2108
+ ",\n\t\t\t\t\t\"family\":\"%s\""
2109
+ ",\n\t\t\t\t\t\"chart_type\":\"%s\""
2110
+ ",\n\t\t\t\t\t\"priority\":%u"
2111
+ ",\n\t\t\t\t\t\"update_every\":%d"
2112
+ ",\n\t\t\t\t\t\"first_time_t\":%ld"
2113
+ ",\n\t\t\t\t\t\"last_time_t\":%ld"
2114
+ ",\n\t\t\t\t\t\"collected\":%s"
2115
+ , string2str(ri->name)
2116
+ , string2str(ri->rc->id)
2117
+ , string2str(ri->title)
2118
+ , string2str(ri->units)
2119
+ , string2str(ri->family)
2120
+ , rrdset_type_name(ri->chart_type)
2121
+ , ri->priority
2122
+ , ri->update_every
2123
+ , first_time_t
2124
+ , (flags & RRD_FLAG_COLLECTED) ? t_parent->now : last_time_t
2125
+ , (flags & RRD_FLAG_COLLECTED) ? "true" : "false"
2126
+ );
2127
+
2128
+ if(options & RRDCONTEXT_OPTION_SHOW_DELETED) {
2129
+ buffer_sprintf(wb,
2130
+ ",\n\t\t\t\t\t\"deleted\":%s"
2131
+ , (ri->flags & RRD_FLAG_DELETED) ? "true" : "false"
2132
+ );
2133
+ }
2134
+
2135
+ if(options & RRDCONTEXT_OPTION_SHOW_FLAGS) {
2136
+ buffer_strcat(wb, ",\n\t\t\t\t\t\"flags\":\"");
2137
+ rrd_flags_to_buffer(ri->flags, wb);
2138
+ buffer_strcat(wb, "\"");
2139
+ }
2140
+
2141
+ if(options & RRDCONTEXT_OPTION_SHOW_LABELS && ri->rrdlabels && dictionary_stats_entries(ri->rrdlabels)) {
2142
+ buffer_sprintf(wb, ",\n\t\t\t\t\t\"labels\": {\n");
2143
+ rrdlabels_to_buffer(ri->rrdlabels, wb, "\t\t\t\t\t\t", ":", "\"", ",\n", NULL, NULL, NULL, NULL);
2144
+ buffer_strcat(wb, "\n\t\t\t\t\t}");
2145
+ }
2146
+
2147
+ if(wb_metrics) {
2148
+ buffer_sprintf(wb, ",\n\t\t\t\t\t\"dimensions\": {");
2149
+ buffer_fast_strcat(wb, buffer_tostring(wb_metrics), buffer_strlen(wb_metrics));
2150
+ buffer_strcat(wb, "\n\t\t\t\t\t}");
2151
+
2152
+ buffer_free(wb_metrics);
2153
+ }
2154
+
2155
+ buffer_strcat(wb, "\n\t\t\t\t}");
2156
+ t_parent->written++;
2157
+ return 1;
2158
+}
2159
+
2160
+static inline int rrdcontext_to_json_callback(const char *id, void *value, void *data) {
2161
+ struct rrdcontext_to_json *t_parent = data;
2162
+ RRDCONTEXT *rc = value;
2163
+ BUFFER *wb = t_parent->wb;
2164
+ RRDCONTEXT_TO_JSON_OPTIONS options = t_parent->options;
2165
+ time_t after = t_parent->after;
2166
+ time_t before = t_parent->before;
2167
+ bool has_filter = t_parent->chart_label_key || t_parent->chart_labels_filter || t_parent->chart_dimensions;
2168
+
2169
+ if((rc->flags & RRD_FLAG_DELETED) && !(options & RRDCONTEXT_OPTION_SHOW_DELETED))
2170
+ return 0;
2171
+
2172
+ if(options & RRDCONTEXT_OPTION_DEEPSCAN)
2173
+ rrdcontext_recalculate_context_retention(rc, RRD_FLAG_NONE, -1);
2174
+
2175
+ if(after && (!rc->last_time_t || after > rc->last_time_t))
2176
+ return 0;
2177
+
2178
+ if(before && (!rc->first_time_t || before < rc->first_time_t))
2179
+ return 0;
2180
+
2181
+ time_t first_time_t = rc->first_time_t;
2182
+ time_t last_time_t = rc->last_time_t;
2183
+ RRD_FLAGS flags = rc->flags;
2184
+
2185
+ BUFFER *wb_instances = NULL;
2186
+ if((options & (RRDCONTEXT_OPTION_SHOW_LABELS|RRDCONTEXT_OPTION_SHOW_INSTANCES|RRDCONTEXT_OPTION_SHOW_METRICS))
2187
+ || t_parent->chart_label_key
2188
+ || t_parent->chart_labels_filter
2189
+ || t_parent->chart_dimensions) {
2190
+
2191
+ wb_instances = buffer_create(4096);
2192
+
2193
+ struct rrdcontext_to_json t_instances = {
2194
+ .wb = wb_instances,
2195
+ .options = options,
2196
+ .chart_label_key = t_parent->chart_label_key,
2197
+ .chart_labels_filter = t_parent->chart_labels_filter,
2198
+ .chart_dimensions = t_parent->chart_dimensions,
2199
+ .after = after,
2200
+ .before = before,
2201
+ .written = 0,
2202
+ .now = t_parent->now,
2203
+ };
2204
+ dictionary_walkthrough_read(rc->rrdinstances, rrdinstance_to_json_callback, &t_instances);
2205
+
2206
+ if(has_filter && !t_instances.written) {
2207
+ buffer_free(wb_instances);
2208
+ return 0;
2209
+ }
2210
+
2211
+ first_time_t = t_instances.combined_first_time_t;
2212
+ last_time_t = t_instances.combined_last_time_t;
2213
+ flags = t_instances.combined_flags;
2214
+ }
2215
+
2216
+ if(t_parent->written)
2217
+ buffer_strcat(wb, ",\n");
2218
+ else
2219
+ buffer_strcat(wb, "\n");
2220
+
2221
+ if(options & RRDCONTEXT_OPTION_SKIP_ID)
2222
+ buffer_sprintf(wb, "\t\t\{");
2223
+ else
2224
+ buffer_sprintf(wb, "\t\t\"%s\": {", id);
2225
+
2226
+ rrdcontext_lock(rc);
2227
+
2228
+ buffer_sprintf(wb,
2229
+ "\n\t\t\t\"title\":\"%s\""
2230
+ ",\n\t\t\t\"units\":\"%s\""
2231
+ ",\n\t\t\t\"family\":\"%s\""
2232
+ ",\n\t\t\t\"chart_type\":\"%s\""
2233
+ ",\n\t\t\t\"priority\":%u"
2234
+ ",\n\t\t\t\"first_time_t\":%ld"
2235
+ ",\n\t\t\t\"last_time_t\":%ld"
2236
+ ",\n\t\t\t\"collected\":%s"
2237
+ , string2str(rc->title)
2238
+ , string2str(rc->units)
2239
+ , string2str(rc->family)
2240
+ , rrdset_type_name(rc->chart_type)
2241
+ , rc->priority
2242
+ , first_time_t
2243
+ , (flags & RRD_FLAG_COLLECTED) ? t_parent->now : last_time_t
2244
+ , (flags & RRD_FLAG_COLLECTED) ? "true" : "false"
2245
+ );
2246
+
2247
+ if(options & RRDCONTEXT_OPTION_SHOW_DELETED) {
2248
+ buffer_sprintf(wb,
2249
+ ",\n\t\t\t\"deleted\":%s"
2250
+ , (rc->flags & RRD_FLAG_DELETED) ? "true" : "false"
2251
+ );
2252
+ }
2253
+
2254
+ if(options & RRDCONTEXT_OPTION_SHOW_FLAGS) {
2255
+ buffer_strcat(wb, ",\n\t\t\t\"flags\":\"");
2256
+ rrd_flags_to_buffer(rc->flags, wb);
2257
+ buffer_strcat(wb, "\"");
2258
+ }
2259
+
2260
+ if(options & RRDCONTEXT_OPTION_SHOW_QUEUED) {
2261
+ buffer_strcat(wb, ",\n\t\t\t\"queued_reasons\":\"");
2262
+ rrd_reasons_to_buffer(rc->queue.queued_flags, wb);
2263
+ buffer_strcat(wb, "\"");
2264
+
2265
+ buffer_sprintf(wb,
2266
+ ",\n\t\t\t\"last_queued\":%llu"
2267
+ ",\n\t\t\t\"scheduled_dispatch\":%llu"
2268
+ ",\n\t\t\t\"last_dequeued\":%llu"
2269
+ ",\n\t\t\t\"hub_version\":%lu"
2270
+ ",\n\t\t\t\"version\":%lu"
2271
+ , rc->queue.queued_ut / USEC_PER_SEC
2272
+ , rc->queue.scheduled_dispatch_ut / USEC_PER_SEC
2273
+ , rc->queue.dequeued_ut / USEC_PER_SEC
2274
+ , rc->hub.version
2275
+ , rc->version
2276
+ );
2277
+ }
2278
+
2279
+ rrdcontext_unlock(rc);
2280
+
2281
+ if(wb_instances) {
2282
+ buffer_sprintf(wb, ",\n\t\t\t\"charts\": {");
2283
+ buffer_fast_strcat(wb, buffer_tostring(wb_instances), buffer_strlen(wb_instances));
2284
+ buffer_strcat(wb, "\n\t\t\t}");
2285
+
2286
+ buffer_free(wb_instances);
2287
+ }
2288
+
2289
+ buffer_strcat(wb, "\n\t\t}");
2290
+ t_parent->written++;
2291
+ return 1;
2292
+}
2293
+
2294
+int rrdcontext_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, const char *context, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions) {
2295
+ RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item((DICTIONARY *)host->rrdctx, context);
2296
+ if(!rca) return HTTP_RESP_NOT_FOUND;
2297
+
2298
+ RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
2299
+
2300
+ if(after != 0 && before != 0) {
2301
+ long long after_wanted = after;
2302
+ long long before_wanted = before;
2303
+ rrdr_relative_window_to_absolute(&after_wanted, &before_wanted);
2304
+ after = after_wanted;
2305
+ before = before_wanted;
2306
+ }
2307
+
2308
+ struct rrdcontext_to_json t_contexts = {
2309
+ .wb = wb,
2310
+ .options = options|RRDCONTEXT_OPTION_SKIP_ID,
2311
+ .chart_label_key = chart_label_key,
2312
+ .chart_labels_filter = chart_labels_filter,
2313
+ .chart_dimensions = chart_dimensions,
2314
+ .after = after,
2315
+ .before = before,
2316
+ .written = 0,
2317
+ .now = now_realtime_sec(),
2318
+ };
2319
+ rrdcontext_to_json_callback(context, rc, &t_contexts);
2320
+
2321
+ rrdcontext_release(rca);
2322
+
2323
+ if(!t_contexts.written)
2324
+ return HTTP_RESP_NOT_FOUND;
2325
+
2326
+ return HTTP_RESP_OK;
2327
+}
2328
+
2329
+int rrdcontexts_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions) {
2330
+ char node_uuid[UUID_STR_LEN] = "";
2331
+
2332
+ if(host->node_id)
2333
+ uuid_unparse(*host->node_id, node_uuid);
2334
+
2335
+ if(after != 0 && before != 0) {
2336
+ long long after_wanted = after;
2337
+ long long before_wanted = before;
2338
+ rrdr_relative_window_to_absolute(&after_wanted, &before_wanted);
2339
+ after = after_wanted;
2340
+ before = before_wanted;
2341
+ }
2342
+
2343
+ buffer_sprintf(wb, "{\n"
2344
+ "\t\"hostname\": \"%s\""
2345
+ ",\n\t\"machine_guid\": \"%s\""
2346
+ ",\n\t\"node_id\": \"%s\""
2347
+ ",\n\t\"claim_id\": \"%s\""
2348
+ , host->hostname
2349
+ , host->machine_guid
2350
+ , node_uuid
2351
+ , host->aclk_state.claimed_id ? host->aclk_state.claimed_id : ""
2352
+ );
2353
+
2354
+ if(options & RRDCONTEXT_OPTION_SHOW_LABELS) {
2355
+ buffer_sprintf(wb, ",\n\t\"host_labels\": {\n");
2356
+ rrdlabels_to_buffer(host->host_labels, wb, "\t\t", ":", "\"", ",\n", NULL, NULL, NULL, NULL);
2357
+ buffer_strcat(wb, "\n\t}");
2358
+ }
2359
+
2360
+ buffer_sprintf(wb, ",\n\t\"contexts\": {");
2361
+ struct rrdcontext_to_json t_contexts = {
2362
+ .wb = wb,
2363
+ .options = options,
2364
+ .chart_label_key = chart_label_key,
2365
+ .chart_labels_filter = chart_labels_filter,
2366
+ .chart_dimensions = chart_dimensions,
2367
+ .after = after,
2368
+ .before = before,
2369
+ .written = 0,
2370
+ .now = now_realtime_sec(),
2371
+ };
2372
+ dictionary_walkthrough_read((DICTIONARY *)host->rrdctx, rrdcontext_to_json_callback, &t_contexts);
2373
+
2374
+ // close contexts, close main
2375
+ buffer_strcat(wb, "\n\t}\n}");
2376
+
2377
+ return HTTP_RESP_OK;
2378
+}
2379
+
2380
+// ----------------------------------------------------------------------------
2381
+// load from SQL
2382
+
2383
+static void rrdinstance_load_clabel(SQL_CLABEL_DATA *sld, void *data) {
2384
+ RRDINSTANCE *ri = data;
2385
+ rrdlabels_add(ri->rrdlabels, sld->label_key, sld->label_value, sld->label_source);
2386
+}
2387
+
2388
+static void rrdinstance_load_dimension(SQL_DIMENSION_DATA *sd, void *data) {
2389
+ RRDINSTANCE *ri = data;
2390
+
2391
+ RRDMETRIC trm = {
2392
+ .id = string_strdupz(sd->id),
2393
+ .name = string_strdupz(sd->name),
2394
+ .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL,
2395
+ };
2396
+ uuid_copy(trm.uuid, sd->dim_id);
2397
+
2398
+ dictionary_set(ri->rrdmetrics, string2str(trm.id), &trm, sizeof(trm));
2399
+}
2400
+
2401
+static void rrdinstance_load_chart_callback(SQL_CHART_DATA *sc, void *data) {
2402
+ RRDHOST *host = data;
2403
+
2404
+ RRDCONTEXT tc = {
2405
+ .id = string_strdupz(sc->context),
2406
+ .title = string_strdupz(sc->title),
2407
+ .units = string_strdupz(sc->units),
2408
+ .family = string_strdupz(sc->family),
2409
+ .priority = sc->priority,
2410
+ .chart_type = sc->chart_type,
2411
+ .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_DONT_PROCESS | RRD_FLAG_UPDATE_REASON_LOAD_SQL,
2412
+ .rrdhost = host,
2413
+ };
2414
+
2415
+ RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_set_and_acquire_item((DICTIONARY *)host->rrdctx, string2str(tc.id), &tc, sizeof(tc));
2416
+ RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
2417
+
2418
+ RRDINSTANCE tri = {
2419
+ .id = string_strdupz(sc->id),
2420
+ .name = string_strdupz(sc->name),
2421
+ .title = string_strdupz(sc->title),
2422
+ .units = string_strdupz(sc->units),
2423
+ .family = string_strdupz(sc->family),
2424
+ .chart_type = sc->chart_type,
2425
+ .priority = sc->priority,
2426
+ .update_every = sc->update_every,
2427
+ .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_DONT_PROCESS | RRD_FLAG_UPDATE_REASON_LOAD_SQL,
2428
+ };
2429
+ uuid_copy(tri.uuid, sc->chart_id);
2430
+
2431
+ RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_set_and_acquire_item(rc->rrdinstances, sc->id, &tri, sizeof(tri));
2432
+ RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
2433
+
2434
+ ctx_get_dimension_list(&ri->uuid, rrdinstance_load_dimension, ri);
2435
+ ctx_get_label_list(&ri->uuid, rrdinstance_load_clabel, ri);
2436
+ ri->flags &= ~RRD_FLAG_DONT_PROCESS;
2437
+ rrdinstance_trigger_updates(ri, true, true);
2438
+
2439
+ // let the instance be in "don't process" mode
2440
+ // so that we process it once, when it is collected
2441
+ ri->flags |= RRD_FLAG_DONT_PROCESS;
2442
+
2443
+ rrdinstance_release(ria);
2444
+ rrdcontext_release(rca);
2445
+}
2446
+
2447
+static void rrdcontext_load_context_callback(VERSIONED_CONTEXT_DATA *ctx_data, void *data) {
2448
+ RRDHOST *host = data;
2449
+ (void)host;
2450
+
2451
+ RRDCONTEXT trc = {
2452
+ .id = string_strdupz(ctx_data->id),
2453
+ .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_DONT_PROCESS | RRD_FLAG_UPDATE_REASON_LOAD_SQL,
2454
+
2455
+ // no need to set more data here
2456
+ // we only need the hub data
2457
+
2458
+ .hub = *ctx_data,
2459
+ };
2460
+ dictionary_set((DICTIONARY *)host->rrdctx, string2str(trc.id), &trc, sizeof(trc));
2461
+}
2462
+
2463
+void rrdhost_load_rrdcontext_data(RRDHOST *host) {
2464
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
2465
+ return;
2466
+
2467
+ if(host->rrdctx) return;
2468
+
2469
+ rrdhost_create_rrdcontexts(host);
2470
+ ctx_get_context_list(&host->host_uuid, rrdcontext_load_context_callback, host);
2471
+ ctx_get_chart_list(&host->host_uuid, rrdinstance_load_chart_callback, host);
2472
+
2473
+ RRDCONTEXT *rc;
2474
+ dfe_start_read((DICTIONARY *)host->rrdctx, rc) {
2475
+ rc->flags &= ~RRD_FLAG_DONT_PROCESS;
2476
+ rrdcontext_trigger_updates(rc, true);
2477
+ }
2478
+ dfe_done(rc);
2479
+}
2480
+
2481
+// ----------------------------------------------------------------------------
2482
+// the worker thread
2483
+
2484
+static inline usec_t rrdcontext_calculate_queued_dispatch_time_ut(RRDCONTEXT *rc, usec_t now_ut) {
2485
+
2486
+ if(likely(rc->queue.delay_calc_ut >= rc->queue.queued_ut))
2487
+ return rc->queue.scheduled_dispatch_ut;
2488
+
2489
+ RRD_FLAGS flags = rc->queue.queued_flags;
2490
+
2491
+ usec_t delay = LONG_MAX;
2492
+ int i;
2493
+ struct rrdcontext_reason *reason;
2494
+ for(i = 0, reason = &rrdcontext_reasons[i]; reason->name ; reason = &rrdcontext_reasons[++i]) {
2495
+ if(unlikely(flags & reason->flag)) {
2496
+ if(reason->delay_ut < delay)
2497
+ delay = reason->delay_ut;
2498
+ }
2499
+ }
2500
+
2501
+ if(unlikely(delay == LONG_MAX)) {
2502
+ internal_error(true, "RRDCONTEXT: '%s', cannot find minimum delay of flags %x", string2str(rc->id), (unsigned int)flags);
2503
+ delay = 60 * USEC_PER_SEC;
2504
+ }
2505
+
2506
+ rc->queue.delay_calc_ut = now_ut;
2507
+ usec_t dispatch_ut = rc->queue.scheduled_dispatch_ut = rc->queue.queued_ut + delay;
2508
+ return dispatch_ut;
2509
+}
2510
+
2511
+#define WORKER_JOB_HOSTS 1
2512
+#define WORKER_JOB_CHECK 2
2513
+#define WORKER_JOB_SEND 3
2514
+#define WORKER_JOB_DEQUEUE 4
2515
+#define WORKER_JOB_RETENTION 5
2516
+#define WORKER_JOB_QUEUED 6
2517
+#define WORKER_JOB_CLEANUP 7
2518
+#define WORKER_JOB_CLEANUP_DELETE 8
2519
+
2520
+static usec_t rrdcontext_next_db_rotation_ut = 0;
2521
+void rrdcontext_db_rotation(void) {
2522
+ // called when the db rotates its database
2523
+ rrdcontext_next_db_rotation_ut = now_realtime_usec() + FULL_RETENTION_SCAN_DELAY_AFTER_DB_ROTATION_SECS * USEC_PER_SEC;
2524
+}
2525
+
2526
+static uint64_t rrdcontext_version_hash_with_callback(
2527
+ RRDHOST *host,
2528
+ void (*callback)(RRDCONTEXT *, bool, void *),
2529
+ bool snapshot,
2530
+ void *bundle) {
2531
+
2532
+ if(unlikely(!host || !host->rrdctx)) return 0;
2533
+
2534
+ RRDCONTEXT *rc;
2535
+ uint64_t hash = 0;
2536
+
2537
+ // loop through all contexts of the host
2538
+ dfe_start_read((DICTIONARY *)host->rrdctx, rc) {
2539
+
2540
+ rrdcontext_lock(rc);
2541
+
2542
+ if(unlikely(callback))
2543
+ callback(rc, snapshot, bundle);
2544
+
2545
+ // skip any deleted contexts
2546
+ if(unlikely(rc->flags & RRD_FLAG_DELETED)) {
2547
+ rrdcontext_unlock(rc);
2548
+ continue;
2549
+ }
2550
+
2551
+ // we use rc->hub.* which has the latest
2552
+ // metadata we have sent to the hub
2553
+
2554
+ // if a context is currently queued, rc->hub.* does NOT
2555
+ // reflect the queued changes. rc->hub.* is updated with
2556
+ // their metadata, after messages are dispatched to hub.
2557
+
2558
+ // when the context is being collected,
2559
+ // rc->hub.last_time_t is already zero
2560
+
2561
+ hash += rc->hub.version + rc->hub.last_time_t - rc->hub.first_time_t;
2562
+
2563
+ rrdcontext_unlock(rc);
2564
+
2565
+ }
2566
+ dfe_done(rc);
2567
+
2568
+ return hash;
2569
+}
2570
+
2571
+static void rrdcontext_recalculate_context_retention(RRDCONTEXT *rc, RRD_FLAGS reason, int job_id) {
2572
+ RRDINSTANCE *ri;
2573
+ dfe_start_read(rc->rrdinstances, ri) {
2574
+ RRDMETRIC *rm;
2575
+ dfe_start_read(ri->rrdmetrics, rm) {
2576
+
2577
+ if(job_id >= 0)
2578
+ worker_is_busy(job_id);
2579
+
2580
+ rrd_flag_set_updated(rm, reason);
2581
+
2582
+ rm->flags &= ~RRD_FLAG_DONT_PROCESS;
2583
+ rrdmetric_trigger_updates(rm, true, false);
2584
+ }
2585
+ dfe_done(rm);
2586
+
2587
+ ri->flags &= ~RRD_FLAG_DONT_PROCESS;
2588
+ rrdinstance_trigger_updates(ri, true, false);
2589
+ ri->flags |= RRD_FLAG_DONT_PROCESS;
2590
+ }
2591
+ dfe_done(ri);
2592
+
2593
+ rc->flags &= ~RRD_FLAG_DONT_PROCESS;
2594
+ rrdcontext_trigger_updates(rc, true);
2595
+}
2596
+
2597
+static void rrdcontext_recalculate_host_retention(RRDHOST *host, RRD_FLAGS reason, int job_id) {
2598
+ if(unlikely(!host || !host->rrdctx)) return;
2599
+
2600
+ RRDCONTEXT *rc;
2601
+ dfe_start_read((DICTIONARY *)host->rrdctx, rc) {
2602
+ rrdcontext_recalculate_context_retention(rc, reason, job_id);
2603
+ }
2604
+ dfe_done(rc);
2605
+}
2606
+
2607
+static void rrdcontext_recalculate_retention(int job_id) {
2608
+ rrdcontext_next_db_rotation_ut = 0;
2609
+ rrd_rdlock();
2610
+ RRDHOST *host;
2611
+ rrdhost_foreach_read(host) {
2612
+ rrdcontext_recalculate_host_retention(host, RRD_FLAG_UPDATE_REASON_DB_ROTATION, job_id);
2613
+ }
2614
+ rrd_unlock();
2615
+}
2616
+
2617
+void rrdcontext_delete_from_sql_unsafe(RRDCONTEXT *rc) {
2618
+ // we need to refresh the string pointers in rc->hub
2619
+ // in case the context changed values
2620
+ rc->hub.id = string2str(rc->id);
2621
+ rc->hub.title = string2str(rc->title);
2622
+ rc->hub.units = string2str(rc->units);
2623
+ rc->hub.family = string2str(rc->family);
2624
+
2625
+ // delete it from SQL
2626
+ if(ctx_delete_context(&rc->rrdhost->host_uuid, &rc->hub) != 0)
2627
+ error("RRDCONTEXT: failed to delete context '%s' version %lu from SQL.", rc->hub.id, rc->hub.version);
2628
+}
2629
+
2630
+static void rrdcontext_garbage_collect(void) {
2631
+ rrd_rdlock();
2632
+ RRDHOST *host;
2633
+ rrdhost_foreach_read(host) {
2634
+ RRDCONTEXT *rc;
2635
+ dfe_start_write((DICTIONARY *)host->rrdctx, rc) {
2636
+ worker_is_busy(WORKER_JOB_CLEANUP);
2637
+
2638
+ rrdcontext_lock(rc);
2639
+
2640
+ if(unlikely(rrdcontext_should_be_deleted(rc))) {
2641
+ worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
2642
+ rrdcontext_delete_from_sql_unsafe(rc);
2643
+
2644
+ if(dictionary_del_having_write_lock((DICTIONARY *)host->rrdctx, string2str(rc->id)) != 0)
2645
+ error("RRDCONTEXT: '%s' of host '%s' failed to be deleted from rrdcontext dictionary.",
2646
+ string2str(rc->id), host->hostname);
2647
+ }
2648
+ else {
2649
+ RRDINSTANCE *ri;
2650
+ dfe_start_write(rc->rrdinstances, ri) {
2651
+ if(rrdinstance_should_be_deleted(ri)) {
2652
+ worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
2653
+ dictionary_del_having_write_lock(rc->rrdinstances, string2str(ri->id));
2654
+ }
2655
+ else {
2656
+ RRDMETRIC *rm;
2657
+ dfe_start_write(ri->rrdmetrics, rm) {
2658
+ if(rrdmetric_should_be_deleted(rm)) {
2659
+ worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
2660
+ dictionary_del_having_write_lock(ri->rrdmetrics, string2str(rm->id));
2661
+ }
2662
+ }
2663
+ dfe_done(rm);
2664
+ }
2665
+ }
2666
+ dfe_done(ri);
2667
+ }
2668
+
2669
+ // the item is referenced in the dictionary
2670
+ // so, it is still here to unlock, even if we have deleted it
2671
+ rrdcontext_unlock(rc);
2672
+ }
2673
+ dfe_done(rc);
2674
+ }
2675
+ rrd_unlock();
2676
+}
2677
+
2678
+static void rrdcontext_main_cleanup(void *ptr) {
2679
+ struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2680
+ static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
2681
+ // custom code
2682
+ static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
2683
+}
2684
+
2685
+void *rrdcontext_main(void *ptr) {
2686
+ netdata_thread_cleanup_push(rrdcontext_main_cleanup, ptr);
2687
+
2688
+ if(unlikely(rrdcontext_enabled == CONFIG_BOOLEAN_NO))
2689
+ goto exit;
2690
+
2691
+ worker_register("RRDCONTEXT");
2692
+ worker_register_job_name(WORKER_JOB_HOSTS, "hosts");
2693
+ worker_register_job_name(WORKER_JOB_CHECK, "dedup checks");
2694
+ worker_register_job_name(WORKER_JOB_SEND, "sent contexts");
2695
+ worker_register_job_name(WORKER_JOB_DEQUEUE, "deduped contexts");
2696
+ worker_register_job_name(WORKER_JOB_RETENTION, "metrics retention");
2697
+ worker_register_job_name(WORKER_JOB_QUEUED, "queued contexts");
2698
+ worker_register_job_name(WORKER_JOB_CLEANUP, "cleanups");
2699
+ worker_register_job_name(WORKER_JOB_CLEANUP_DELETE, "deletes");
2700
+
2701
+ heartbeat_t hb;
2702
+ heartbeat_init(&hb);
2703
+ usec_t step = USEC_PER_SEC * RRDCONTEXT_WORKER_THREAD_HEARTBEAT_SECS;
2704
+
2705
+ while (!netdata_exit) {
2706
+ worker_is_idle();
2707
+ heartbeat_next(&hb, step);
2708
+
2709
+ if(unlikely(netdata_exit)) break;
2710
+
2711
+ if(!aclk_connected) continue;
2712
+
2713
+ usec_t now_ut = now_realtime_usec();
2714
+
2715
+ if(rrdcontext_next_db_rotation_ut && now_ut > rrdcontext_next_db_rotation_ut) {
2716
+ rrdcontext_recalculate_retention(WORKER_JOB_RETENTION);
2717
+ rrdcontext_garbage_collect();
2718
+ rrdcontext_next_db_rotation_ut = 0;
2719
+ }
2720
+
2721
+ rrd_rdlock();
2722
+ RRDHOST *host;
2723
+ rrdhost_foreach_read(host) {
2724
+ if(unlikely(netdata_exit)) break;
2725
+
2726
+ worker_is_busy(WORKER_JOB_HOSTS);
2727
+
2728
+ // check if we have received a streaming command for this host
2729
+ if(!rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS))
2730
+ continue;
2731
+
2732
+ // check if there are queued items to send
2733
+ if(!dictionary_stats_entries((DICTIONARY *)host->rrdctx_queue))
2734
+ continue;
2735
+
2736
+ if(!host->node_id)
2737
+ continue;
2738
+
2739
+ size_t messages_added = 0;
2740
+ contexts_updated_t bundle = NULL;
2741
+
2742
+ RRDCONTEXT *rc;
2743
+ dfe_start_write((DICTIONARY *)host->rrdctx_queue, rc) {
2744
+ if(unlikely(netdata_exit)) break;
2745
+
2746
+ if(unlikely(messages_added >= MESSAGES_PER_BUNDLE_TO_SEND_TO_HUB_PER_HOST))
2747
+ break;
2748
+
2749
+ worker_is_busy(WORKER_JOB_QUEUED);
2750
+ usec_t dispatch_ut = rrdcontext_calculate_queued_dispatch_time_ut(rc, now_ut);
2751
+ char *claim_id = get_agent_claimid();
2752
+ if(unlikely(now_ut >= dispatch_ut) && claim_id) {
2753
+ worker_is_busy(WORKER_JOB_CHECK);
2754
+
2755
+ rrdcontext_lock(rc);
2756
+
2757
+ if(check_if_cloud_version_changed_unsafe(rc, true)) {
2758
+ worker_is_busy(WORKER_JOB_SEND);
2759
+
2760
+#ifdef ENABLE_ACLK
2761
+ if(!bundle) {
2762
+ // prepare the bundle to send the messages
2763
+ char uuid[UUID_STR_LEN];
2764
+ uuid_unparse_lower(*host->node_id, uuid);
2765
+
2766
+ bundle = contexts_updated_new(claim_id, uuid, 0, now_ut);
2767
+ }
2768
+#endif
2769
+ // update the hub data of the context, give a new version, pack the message
2770
+ // and save an update to SQL
2771
+ rrdcontext_message_send_unsafe(rc, false, bundle);
2772
+ messages_added++;
2773
+
2774
+ rc->queue.dequeued_ut = now_ut;
2775
+ }
2776
+ else
2777
+ rc->version = rc->hub.version;
2778
+
2779
+ // remove the queued flag, so that it can be queued again
2780
+ rc->flags &= ~RRD_FLAG_QUEUED;
2781
+
2782
+ // remove it from the queue
2783
+ worker_is_busy(WORKER_JOB_DEQUEUE);
2784
+ dictionary_del_having_write_lock((DICTIONARY *)host->rrdctx_queue, string2str(rc->id));
2785
+
2786
+ if(unlikely(rrdcontext_should_be_deleted(rc))) {
2787
+ // this is a deleted context - delete it forever...
2788
+
2789
+ worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
2790
+ rrdcontext_delete_from_sql_unsafe(rc);
2791
+
2792
+ STRING *id = string_dup(rc->id);
2793
+ rrdcontext_unlock(rc);
2794
+
2795
+ // delete it from the master dictionary
2796
+ if(dictionary_del((DICTIONARY *)host->rrdctx, string2str(rc->id)) != 0)
2797
+ error("RRDCONTEXT: '%s' of host '%s' failed to be deleted from rrdcontext dictionary.",
2798
+ string2str(id), host->hostname);
2799
+
2800
+ string_freez(id);
2801
+ }
2802
+ else
2803
+ rrdcontext_unlock(rc);
2804
+ }
2805
+ freez(claim_id);
2806
+ }
2807
+ dfe_done(rc);
2808
+
2809
+#ifdef ENABLE_ACLK
2810
+ if(!netdata_exit && bundle) {
2811
+ // we have a bundle to send messages
2812
+
2813
+ // update the version hash
2814
+ contexts_updated_update_version_hash(bundle, rrdcontext_version_hash(host));
2815
+
2816
+ // send it
2817
+ aclk_send_contexts_updated(bundle);
2818
+ }
2819
+ else if(bundle)
2820
+ contexts_updated_delete(bundle);
2821
+#endif
2822
+ }
2823
+ rrd_unlock();
2824
+
2825
+ }
2826
+
2827
+exit:
2828
+ netdata_thread_cleanup_pop(1);
2829
+ return NULL;
2830
+}