internal.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRDCONTEXT_INTERNAL_H
  3. #define NETDATA_RRDCONTEXT_INTERNAL_H 1
  4. #include "rrdcontext.h"
  5. #include "../sqlite/sqlite_context.h"
  6. #include "../../aclk/schema-wrappers/context.h"
  7. #include "../../aclk/aclk_contexts_api.h"
  8. #include "../../aclk/aclk.h"
  9. #include "../storage_engine.h"
  10. #define MESSAGES_PER_BUNDLE_TO_SEND_TO_HUB_PER_HOST 5000
  11. #define FULL_RETENTION_SCAN_DELAY_AFTER_DB_ROTATION_SECS 120
  12. #define RRDCONTEXT_WORKER_THREAD_HEARTBEAT_USEC (1000 * USEC_PER_MS)
  13. #define RRDCONTEXT_MINIMUM_ALLOWED_PRIORITY 10
  14. #define LOG_TRANSITIONS false
  15. #define WORKER_JOB_HOSTS 1
  16. #define WORKER_JOB_CHECK 2
  17. #define WORKER_JOB_SEND 3
  18. #define WORKER_JOB_DEQUEUE 4
  19. #define WORKER_JOB_RETENTION 5
  20. #define WORKER_JOB_QUEUED 6
  21. #define WORKER_JOB_CLEANUP 7
  22. #define WORKER_JOB_CLEANUP_DELETE 8
  23. #define WORKER_JOB_PP_METRIC 9 // post-processing metrics
  24. #define WORKER_JOB_PP_INSTANCE 10 // post-processing instances
  25. #define WORKER_JOB_PP_CONTEXT 11 // post-processing contexts
  26. #define WORKER_JOB_HUB_QUEUE_SIZE 12
  27. #define WORKER_JOB_PP_QUEUE_SIZE 13
  28. typedef enum __attribute__ ((__packed__)) {
  29. RRD_FLAG_NONE = 0,
  30. RRD_FLAG_DELETED = (1 << 0), // this is a deleted object (metrics, instances, contexts)
  31. RRD_FLAG_COLLECTED = (1 << 1), // this object is currently being collected
  32. RRD_FLAG_UPDATED = (1 << 2), // this object has updates to propagate
  33. RRD_FLAG_ARCHIVED = (1 << 3), // this object is not currently being collected
  34. RRD_FLAG_OWN_LABELS = (1 << 4), // this instance has its own labels - not linked to an RRDSET
  35. RRD_FLAG_LIVE_RETENTION = (1 << 5), // we have got live retention from the database
  36. RRD_FLAG_QUEUED_FOR_HUB = (1 << 6), // this context is currently queued to be dispatched to hub
  37. RRD_FLAG_QUEUED_FOR_PP = (1 << 7), // this context is currently queued to be post-processed
  38. RRD_FLAG_HIDDEN = (1 << 8), // don't expose this to the hub or the API
  39. RRD_FLAG_UPDATE_REASON_TRIGGERED = (1 << 9), // the update was triggered by the child object
  40. RRD_FLAG_UPDATE_REASON_LOAD_SQL = (1 << 10), // this object has just been loaded from SQL
  41. RRD_FLAG_UPDATE_REASON_NEW_OBJECT = (1 << 11), // this object has just been created
  42. RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT = (1 << 12), // we received an update on this object
  43. RRD_FLAG_UPDATE_REASON_CHANGED_LINKING = (1 << 13), // an instance or a metric switched RRDSET or RRDDIM
  44. RRD_FLAG_UPDATE_REASON_CHANGED_METADATA = (1 << 14), // this context or instance changed uuid, name, units, title, family, chart type, priority, update every, rrd changed flags
  45. RRD_FLAG_UPDATE_REASON_ZERO_RETENTION = (1 << 15), // this object has no retention
  46. RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T = (1 << 16), // this object changed its oldest time in the db
  47. RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T = (1 << 17), // this object change its latest time in the db
  48. RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED = (1 << 18), // this object has stopped being collected
  49. RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED = (1 << 19), // this object has started being collected
  50. RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD = (1 << 20), // this context belongs to a host that just disconnected
  51. RRD_FLAG_UPDATE_REASON_UNUSED = (1 << 21), // this context is not used anymore
  52. RRD_FLAG_UPDATE_REASON_DB_ROTATION = (1 << 22), // this context changed because of a db rotation
  53. // action to perform on an object
  54. RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION = (1 << 30), // this object has to update its retention from the db
  55. } RRD_FLAGS;
  56. struct rrdcontext_reason {
  57. RRD_FLAGS flag;
  58. const char *name;
  59. usec_t delay_ut;
  60. };
  61. extern struct rrdcontext_reason rrdcontext_reasons[];
  62. #define RRD_FLAG_ALL_UPDATE_REASONS ( \
  63. RRD_FLAG_UPDATE_REASON_TRIGGERED \
  64. |RRD_FLAG_UPDATE_REASON_LOAD_SQL \
  65. |RRD_FLAG_UPDATE_REASON_NEW_OBJECT \
  66. |RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT \
  67. |RRD_FLAG_UPDATE_REASON_CHANGED_LINKING \
  68. |RRD_FLAG_UPDATE_REASON_CHANGED_METADATA \
  69. |RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
  70. |RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T \
  71. |RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T \
  72. |RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED \
  73. |RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED \
  74. |RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD \
  75. |RRD_FLAG_UPDATE_REASON_DB_ROTATION \
  76. |RRD_FLAG_UPDATE_REASON_UNUSED \
  77. )
  78. #define RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS ( \
  79. RRD_FLAG_ARCHIVED \
  80. |RRD_FLAG_HIDDEN \
  81. |RRD_FLAG_ALL_UPDATE_REASONS \
  82. )
  83. #define RRD_FLAGS_REQUIRED_FOR_DELETIONS ( \
  84. RRD_FLAG_DELETED \
  85. |RRD_FLAG_LIVE_RETENTION \
  86. )
  87. #define RRD_FLAGS_PREVENTING_DELETIONS ( \
  88. RRD_FLAG_QUEUED_FOR_HUB \
  89. |RRD_FLAG_COLLECTED \
  90. |RRD_FLAG_QUEUED_FOR_PP \
  91. )
  92. // get all the flags of an object
  93. #define rrd_flags_get(obj) __atomic_load_n(&((obj)->flags), __ATOMIC_SEQ_CST)
  94. // check if ANY of the given flags (bits) is set
  95. #define rrd_flag_check(obj, flag) (rrd_flags_get(obj) & (flag))
  96. // check if ALL the given flags (bits) are set
  97. #define rrd_flag_check_all(obj, flag) (rrd_flag_check(obj, flag) == (flag))
  98. // set one or more flags (bits)
  99. #define rrd_flag_set(obj, flag) __atomic_or_fetch(&((obj)->flags), flag, __ATOMIC_SEQ_CST)
  100. // clear one or more flags (bits)
  101. #define rrd_flag_clear(obj, flag) __atomic_and_fetch(&((obj)->flags), ~(flag), __ATOMIC_SEQ_CST)
  102. // replace the flags of an object, with the supplied ones
  103. #define rrd_flags_replace(obj, all_flags) __atomic_store_n(&((obj)->flags), all_flags, __ATOMIC_SEQ_CST)
  104. static inline void
  105. rrd_flag_add_remove_atomic(RRD_FLAGS *flags, RRD_FLAGS check, RRD_FLAGS conditionally_add, RRD_FLAGS always_remove) {
  106. RRD_FLAGS expected, desired;
  107. do {
  108. expected = *flags;
  109. desired = expected;
  110. desired &= ~(always_remove);
  111. if(!(expected & check))
  112. desired |= (check | conditionally_add);
  113. } while(!__atomic_compare_exchange_n(flags, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
  114. }
  115. #define rrd_flag_set_collected(obj) \
  116. rrd_flag_add_remove_atomic(&((obj)->flags) \
  117. /* check this flag */ \
  118. , RRD_FLAG_COLLECTED \
  119. \
  120. /* add these flags together with the above, if the above is not already set */ \
  121. , RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED | RRD_FLAG_UPDATED \
  122. \
  123. /* always remove these flags */ \
  124. , RRD_FLAG_ARCHIVED \
  125. | RRD_FLAG_DELETED \
  126. | RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED \
  127. | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
  128. | RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD \
  129. )
  130. #define rrd_flag_set_archived(obj) \
  131. rrd_flag_add_remove_atomic(&((obj)->flags) \
  132. /* check this flag */ \
  133. , RRD_FLAG_ARCHIVED \
  134. \
  135. /* add these flags together with the above, if the above is not already set */ \
  136. , RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED | RRD_FLAG_UPDATED \
  137. \
  138. /* always remove these flags */ \
  139. , RRD_FLAG_COLLECTED \
  140. | RRD_FLAG_DELETED \
  141. | RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED \
  142. | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
  143. )
  144. #define rrd_flag_set_deleted(obj, reason) \
  145. rrd_flag_add_remove_atomic(&((obj)->flags) \
  146. /* check this flag */ \
  147. , RRD_FLAG_DELETED \
  148. \
  149. /* add these flags together with the above, if the above is not already set */ \
  150. , RRD_FLAG_UPDATE_REASON_ZERO_RETENTION | RRD_FLAG_UPDATED | (reason) \
  151. \
  152. /* always remove these flags */ \
  153. , RRD_FLAG_ARCHIVED \
  154. | RRD_FLAG_COLLECTED \
  155. )
  156. #define rrd_flag_is_collected(obj) rrd_flag_check(obj, RRD_FLAG_COLLECTED)
  157. #define rrd_flag_is_archived(obj) rrd_flag_check(obj, RRD_FLAG_ARCHIVED)
  158. #define rrd_flag_is_deleted(obj) rrd_flag_check(obj, RRD_FLAG_DELETED)
  159. #define rrd_flag_is_updated(obj) rrd_flag_check(obj, RRD_FLAG_UPDATED)
  160. // mark an object as updated, providing reasons (additional bits)
  161. #define rrd_flag_set_updated(obj, reason) rrd_flag_set(obj, RRD_FLAG_UPDATED | (reason))
  162. // clear an object as being updated, clearing also all the reasons
  163. #define rrd_flag_unset_updated(obj) rrd_flag_clear(obj, RRD_FLAG_UPDATED | RRD_FLAG_ALL_UPDATE_REASONS)
  164. typedef struct rrdmetric {
  165. uuid_t uuid;
  166. STRING *id;
  167. STRING *name;
  168. RRDDIM *rrddim;
  169. time_t first_time_s;
  170. time_t last_time_s;
  171. RRD_FLAGS flags;
  172. struct rrdinstance *ri;
  173. } RRDMETRIC;
  174. typedef struct rrdinstance {
  175. uuid_t uuid;
  176. STRING *id;
  177. STRING *name;
  178. STRING *title;
  179. STRING *units;
  180. STRING *family;
  181. uint32_t priority:24;
  182. RRDSET_TYPE chart_type;
  183. RRD_FLAGS flags; // flags related to this instance
  184. time_t first_time_s;
  185. time_t last_time_s;
  186. time_t update_every_s; // data collection frequency
  187. RRDSET *rrdset; // pointer to RRDSET when collected, or NULL
  188. DICTIONARY *rrdlabels; // linked to RRDSET->chart_labels or own version
  189. struct rrdcontext *rc;
  190. DICTIONARY *rrdmetrics;
  191. struct {
  192. uint32_t collected_metrics_count; // a temporary variable to detect BEGIN/END without SET
  193. // don't use it for other purposes
  194. // it goes up and then resets to zero, on every iteration
  195. } internal;
  196. } RRDINSTANCE;
  197. typedef struct rrdcontext {
  198. uint64_t version;
  199. STRING *id;
  200. STRING *title;
  201. STRING *units;
  202. STRING *family;
  203. uint32_t priority;
  204. RRDSET_TYPE chart_type;
  205. RRD_FLAGS flags;
  206. time_t first_time_s;
  207. time_t last_time_s;
  208. VERSIONED_CONTEXT_DATA hub;
  209. DICTIONARY *rrdinstances;
  210. RRDHOST *rrdhost;
  211. struct {
  212. RRD_FLAGS queued_flags; // the last flags that triggered the post-processing
  213. usec_t queued_ut; // the last time this was queued
  214. usec_t dequeued_ut; // the last time we sent (or deduplicated) this context
  215. size_t executions; // how many times this context has been processed
  216. } pp;
  217. struct {
  218. RRD_FLAGS queued_flags; // the last flags that triggered the queueing
  219. usec_t queued_ut; // the last time this was queued
  220. usec_t delay_calc_ut; // the last time we calculated the scheduled_dispatched_ut
  221. usec_t scheduled_dispatch_ut; // the time it was/is scheduled to be sent
  222. usec_t dequeued_ut; // the last time we sent (or deduplicated) this context
  223. size_t dispatches; // the number of times this has been dispatched to hub
  224. } queue;
  225. netdata_mutex_t mutex;
  226. } RRDCONTEXT;
  227. // ----------------------------------------------------------------------------
  228. // helper one-liners for RRDMETRIC
  229. bool rrdmetric_update_retention(RRDMETRIC *rm);
  230. static inline RRDMETRIC *rrdmetric_acquired_value(RRDMETRIC_ACQUIRED *rma) {
  231. return dictionary_acquired_item_value((DICTIONARY_ITEM *)rma);
  232. }
  233. static inline RRDMETRIC_ACQUIRED *rrdmetric_acquired_dup(RRDMETRIC_ACQUIRED *rma) {
  234. RRDMETRIC *rm = rrdmetric_acquired_value(rma);
  235. return (RRDMETRIC_ACQUIRED *)dictionary_acquired_item_dup(rm->ri->rrdmetrics, (DICTIONARY_ITEM *)rma);
  236. }
  237. static inline void rrdmetric_release(RRDMETRIC_ACQUIRED *rma) {
  238. RRDMETRIC *rm = rrdmetric_acquired_value(rma);
  239. dictionary_acquired_item_release(rm->ri->rrdmetrics, (DICTIONARY_ITEM *)rma);
  240. }
  241. void rrdmetric_rrddim_is_freed(RRDDIM *rd);
  242. void rrdmetric_updated_rrddim_flags(RRDDIM *rd);
  243. void rrdmetric_collected_rrddim(RRDDIM *rd);
  244. // ----------------------------------------------------------------------------
  245. // helper one-liners for RRDINSTANCE
  246. static inline RRDINSTANCE *rrdinstance_acquired_value(RRDINSTANCE_ACQUIRED *ria) {
  247. return dictionary_acquired_item_value((DICTIONARY_ITEM *)ria);
  248. }
  249. static inline RRDINSTANCE_ACQUIRED *rrdinstance_acquired_dup(RRDINSTANCE_ACQUIRED *ria) {
  250. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  251. return (RRDINSTANCE_ACQUIRED *)dictionary_acquired_item_dup(ri->rc->rrdinstances, (DICTIONARY_ITEM *)ria);
  252. }
  253. static inline void rrdinstance_release(RRDINSTANCE_ACQUIRED *ria) {
  254. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  255. dictionary_acquired_item_release(ri->rc->rrdinstances, (DICTIONARY_ITEM *)ria);
  256. }
  257. void rrdinstance_from_rrdset(RRDSET *st);
  258. void rrdinstance_rrdset_is_freed(RRDSET *st);
  259. void rrdinstance_rrdset_has_updated_retention(RRDSET *st);
  260. void rrdinstance_updated_rrdset_name(RRDSET *st);
  261. void rrdinstance_updated_rrdset_flags_no_action(RRDINSTANCE *ri, RRDSET *st);
  262. void rrdinstance_updated_rrdset_flags(RRDSET *st);
  263. void rrdinstance_collected_rrdset(RRDSET *st);
  264. void rrdcontext_queue_for_post_processing(RRDCONTEXT *rc, const char *function, RRD_FLAGS flags);
  265. // ----------------------------------------------------------------------------
  266. // helper one-liners for RRDCONTEXT
  267. static inline RRDCONTEXT *rrdcontext_acquired_value(RRDCONTEXT_ACQUIRED *rca) {
  268. return dictionary_acquired_item_value((DICTIONARY_ITEM *)rca);
  269. }
  270. static inline RRDCONTEXT_ACQUIRED *rrdcontext_acquired_dup(RRDCONTEXT_ACQUIRED *rca) {
  271. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  272. return (RRDCONTEXT_ACQUIRED *)dictionary_acquired_item_dup(rc->rrdhost->rrdctx.contexts, (DICTIONARY_ITEM *)rca);
  273. }
  274. static inline void rrdcontext_release(RRDCONTEXT_ACQUIRED *rca) {
  275. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  276. dictionary_acquired_item_release(rc->rrdhost->rrdctx.contexts, (DICTIONARY_ITEM *)rca);
  277. }
  278. // ----------------------------------------------------------------------------
  279. // Forward definitions
  280. void rrdcontext_recalculate_context_retention(RRDCONTEXT *rc, RRD_FLAGS reason, bool worker_jobs);
  281. void rrdcontext_recalculate_host_retention(RRDHOST *host, RRD_FLAGS reason, bool worker_jobs);
  282. #define rrdcontext_lock(rc) netdata_mutex_lock(&((rc)->mutex))
  283. #define rrdcontext_unlock(rc) netdata_mutex_unlock(&((rc)->mutex))
  284. void rrdinstance_trigger_updates(RRDINSTANCE *ri, const char *function);
  285. void rrdcontext_trigger_updates(RRDCONTEXT *rc, const char *function);
  286. void rrdinstances_create_in_rrdcontext(RRDCONTEXT *rc);
  287. void rrdinstances_destroy_from_rrdcontext(RRDCONTEXT *rc);
  288. void rrdmetrics_destroy_from_rrdinstance(RRDINSTANCE *ri);
  289. void rrdmetrics_create_in_rrdinstance(RRDINSTANCE *ri);
  290. void rrdmetric_from_rrddim(RRDDIM *rd);
  291. void rrd_reasons_to_buffer_json_array_items(RRD_FLAGS flags, BUFFER *wb);
  292. #define rrdcontext_version_hash(host) rrdcontext_version_hash_with_callback(host, NULL, false, NULL)
  293. uint64_t rrdcontext_version_hash_with_callback(
  294. RRDHOST *host,
  295. void (*callback)(RRDCONTEXT *, bool, void *),
  296. bool snapshot,
  297. void *bundle);
  298. void rrdcontext_message_send_unsafe(RRDCONTEXT *rc, bool snapshot __maybe_unused, void *bundle __maybe_unused);
  299. #endif //NETDATA_RRDCONTEXT_INTERNAL_H