internal.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. RRD_FLAG_MERGED_COLLECTED_RI_TO_RC = (1 << 29),
  54. // action to perform on an object
  55. RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION = (1 << 30), // this object has to update its retention from the db
  56. } RRD_FLAGS;
  57. struct rrdcontext_reason {
  58. RRD_FLAGS flag;
  59. const char *name;
  60. usec_t delay_ut;
  61. };
  62. extern struct rrdcontext_reason rrdcontext_reasons[];
  63. #define RRD_FLAG_ALL_UPDATE_REASONS ( \
  64. RRD_FLAG_UPDATE_REASON_TRIGGERED \
  65. |RRD_FLAG_UPDATE_REASON_LOAD_SQL \
  66. |RRD_FLAG_UPDATE_REASON_NEW_OBJECT \
  67. |RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT \
  68. |RRD_FLAG_UPDATE_REASON_CHANGED_LINKING \
  69. |RRD_FLAG_UPDATE_REASON_CHANGED_METADATA \
  70. |RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
  71. |RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T \
  72. |RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T \
  73. |RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED \
  74. |RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED \
  75. |RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD \
  76. |RRD_FLAG_UPDATE_REASON_DB_ROTATION \
  77. |RRD_FLAG_UPDATE_REASON_UNUSED \
  78. )
  79. #define RRD_FLAGS_ALLOWED_EXTERNALLY_ON_NEW_OBJECTS ( \
  80. RRD_FLAG_ARCHIVED \
  81. |RRD_FLAG_HIDDEN \
  82. |RRD_FLAG_ALL_UPDATE_REASONS \
  83. )
  84. #define RRD_FLAGS_REQUIRED_FOR_DELETIONS ( \
  85. RRD_FLAG_DELETED \
  86. |RRD_FLAG_LIVE_RETENTION \
  87. )
  88. #define RRD_FLAGS_PREVENTING_DELETIONS ( \
  89. RRD_FLAG_QUEUED_FOR_HUB \
  90. |RRD_FLAG_COLLECTED \
  91. |RRD_FLAG_QUEUED_FOR_PP \
  92. )
  93. // get all the flags of an object
  94. #define rrd_flags_get(obj) __atomic_load_n(&((obj)->flags), __ATOMIC_SEQ_CST)
  95. // check if ANY of the given flags (bits) is set
  96. #define rrd_flag_check(obj, flag) (rrd_flags_get(obj) & (flag))
  97. // check if ALL the given flags (bits) are set
  98. #define rrd_flag_check_all(obj, flag) (rrd_flag_check(obj, flag) == (flag))
  99. // set one or more flags (bits)
  100. #define rrd_flag_set(obj, flag) __atomic_or_fetch(&((obj)->flags), flag, __ATOMIC_SEQ_CST)
  101. // clear one or more flags (bits)
  102. #define rrd_flag_clear(obj, flag) __atomic_and_fetch(&((obj)->flags), ~(flag), __ATOMIC_SEQ_CST)
  103. // replace the flags of an object, with the supplied ones
  104. #define rrd_flags_replace(obj, all_flags) __atomic_store_n(&((obj)->flags), all_flags, __ATOMIC_SEQ_CST)
  105. static inline void
  106. rrd_flag_add_remove_atomic(RRD_FLAGS *flags, RRD_FLAGS check, RRD_FLAGS conditionally_add, RRD_FLAGS always_remove) {
  107. RRD_FLAGS expected, desired;
  108. do {
  109. expected = *flags;
  110. desired = expected;
  111. desired &= ~(always_remove);
  112. if(!(expected & check))
  113. desired |= (check | conditionally_add);
  114. } while(!__atomic_compare_exchange_n(flags, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
  115. }
  116. #define rrd_flag_set_collected(obj) \
  117. rrd_flag_add_remove_atomic(&((obj)->flags) \
  118. /* check this flag */ \
  119. , RRD_FLAG_COLLECTED \
  120. \
  121. /* add these flags together with the above, if the above is not already set */ \
  122. , RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED | RRD_FLAG_UPDATED \
  123. \
  124. /* always remove these flags */ \
  125. , RRD_FLAG_ARCHIVED \
  126. | RRD_FLAG_DELETED \
  127. | RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED \
  128. | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
  129. | RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD \
  130. )
  131. #define rrd_flag_set_archived(obj) \
  132. rrd_flag_add_remove_atomic(&((obj)->flags) \
  133. /* check this flag */ \
  134. , RRD_FLAG_ARCHIVED \
  135. \
  136. /* add these flags together with the above, if the above is not already set */ \
  137. , RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED | RRD_FLAG_UPDATED \
  138. \
  139. /* always remove these flags */ \
  140. , RRD_FLAG_COLLECTED \
  141. | RRD_FLAG_DELETED \
  142. | RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED \
  143. | RRD_FLAG_UPDATE_REASON_ZERO_RETENTION \
  144. )
  145. #define rrd_flag_set_deleted(obj, reason) \
  146. rrd_flag_add_remove_atomic(&((obj)->flags) \
  147. /* check this flag */ \
  148. , RRD_FLAG_DELETED \
  149. \
  150. /* add these flags together with the above, if the above is not already set */ \
  151. , RRD_FLAG_UPDATE_REASON_ZERO_RETENTION | RRD_FLAG_UPDATED | (reason) \
  152. \
  153. /* always remove these flags */ \
  154. , RRD_FLAG_ARCHIVED \
  155. | RRD_FLAG_COLLECTED \
  156. )
  157. #define rrd_flag_is_collected(obj) rrd_flag_check(obj, RRD_FLAG_COLLECTED)
  158. #define rrd_flag_is_archived(obj) rrd_flag_check(obj, RRD_FLAG_ARCHIVED)
  159. #define rrd_flag_is_deleted(obj) rrd_flag_check(obj, RRD_FLAG_DELETED)
  160. #define rrd_flag_is_updated(obj) rrd_flag_check(obj, RRD_FLAG_UPDATED)
  161. // mark an object as updated, providing reasons (additional bits)
  162. #define rrd_flag_set_updated(obj, reason) rrd_flag_set(obj, RRD_FLAG_UPDATED | (reason))
  163. // clear an object as being updated, clearing also all the reasons
  164. #define rrd_flag_unset_updated(obj) rrd_flag_clear(obj, RRD_FLAG_UPDATED | RRD_FLAG_ALL_UPDATE_REASONS)
  165. typedef struct rrdmetric {
  166. uuid_t uuid;
  167. STRING *id;
  168. STRING *name;
  169. RRDDIM *rrddim;
  170. time_t first_time_s;
  171. time_t last_time_s;
  172. RRD_FLAGS flags;
  173. struct rrdinstance *ri;
  174. } RRDMETRIC;
  175. typedef struct rrdinstance {
  176. uuid_t uuid;
  177. STRING *id;
  178. STRING *name;
  179. STRING *title;
  180. STRING *units;
  181. STRING *family;
  182. uint32_t priority:24;
  183. RRDSET_TYPE chart_type;
  184. RRD_FLAGS flags; // flags related to this instance
  185. time_t first_time_s;
  186. time_t last_time_s;
  187. time_t update_every_s; // data collection frequency
  188. RRDSET *rrdset; // pointer to RRDSET when collected, or NULL
  189. RRDLABELS *rrdlabels; // linked to RRDSET->chart_labels or own version
  190. struct rrdcontext *rc;
  191. DICTIONARY *rrdmetrics;
  192. struct {
  193. uint32_t collected_metrics_count; // a temporary variable to detect BEGIN/END without SET
  194. // don't use it for other purposes
  195. // it goes up and then resets to zero, on every iteration
  196. } internal;
  197. } RRDINSTANCE;
  198. typedef struct rrdcontext {
  199. uint64_t version;
  200. STRING *id;
  201. STRING *title;
  202. STRING *units;
  203. STRING *family;
  204. uint32_t priority;
  205. RRDSET_TYPE chart_type;
  206. SPINLOCK spinlock;
  207. RRD_FLAGS flags;
  208. time_t first_time_s;
  209. time_t last_time_s;
  210. VERSIONED_CONTEXT_DATA hub;
  211. DICTIONARY *rrdinstances;
  212. RRDHOST *rrdhost;
  213. struct {
  214. RRD_FLAGS queued_flags; // the last flags that triggered the post-processing
  215. usec_t queued_ut; // the last time this was queued
  216. usec_t dequeued_ut; // the last time we sent (or deduplicated) this context
  217. size_t executions; // how many times this context has been processed
  218. } pp;
  219. struct {
  220. RRD_FLAGS queued_flags; // the last flags that triggered the queueing
  221. usec_t queued_ut; // the last time this was queued
  222. usec_t delay_calc_ut; // the last time we calculated the scheduled_dispatched_ut
  223. usec_t scheduled_dispatch_ut; // the time it was/is scheduled to be sent
  224. usec_t dequeued_ut; // the last time we sent (or deduplicated) this context
  225. size_t dispatches; // the number of times this has been dispatched to hub
  226. } queue;
  227. struct {
  228. uint32_t metrics; // the number of metrics in this context
  229. } stats;
  230. } RRDCONTEXT;
  231. // ----------------------------------------------------------------------------
  232. // helper one-liners for RRDMETRIC
  233. bool rrdmetric_update_retention(RRDMETRIC *rm);
  234. static inline RRDMETRIC *rrdmetric_acquired_value(RRDMETRIC_ACQUIRED *rma) {
  235. return dictionary_acquired_item_value((DICTIONARY_ITEM *)rma);
  236. }
  237. static inline RRDMETRIC_ACQUIRED *rrdmetric_acquired_dup(RRDMETRIC_ACQUIRED *rma) {
  238. RRDMETRIC *rm = rrdmetric_acquired_value(rma);
  239. return (RRDMETRIC_ACQUIRED *)dictionary_acquired_item_dup(rm->ri->rrdmetrics, (DICTIONARY_ITEM *)rma);
  240. }
  241. static inline void rrdmetric_release(RRDMETRIC_ACQUIRED *rma) {
  242. RRDMETRIC *rm = rrdmetric_acquired_value(rma);
  243. dictionary_acquired_item_release(rm->ri->rrdmetrics, (DICTIONARY_ITEM *)rma);
  244. }
  245. void rrdmetric_rrddim_is_freed(RRDDIM *rd);
  246. void rrdmetric_updated_rrddim_flags(RRDDIM *rd);
  247. void rrdmetric_collected_rrddim(RRDDIM *rd);
  248. // ----------------------------------------------------------------------------
  249. // helper one-liners for RRDINSTANCE
  250. static inline RRDINSTANCE *rrdinstance_acquired_value(RRDINSTANCE_ACQUIRED *ria) {
  251. return dictionary_acquired_item_value((DICTIONARY_ITEM *)ria);
  252. }
  253. static inline RRDINSTANCE_ACQUIRED *rrdinstance_acquired_dup(RRDINSTANCE_ACQUIRED *ria) {
  254. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  255. return (RRDINSTANCE_ACQUIRED *)dictionary_acquired_item_dup(ri->rc->rrdinstances, (DICTIONARY_ITEM *)ria);
  256. }
  257. static inline void rrdinstance_release(RRDINSTANCE_ACQUIRED *ria) {
  258. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  259. dictionary_acquired_item_release(ri->rc->rrdinstances, (DICTIONARY_ITEM *)ria);
  260. }
  261. void rrdinstance_from_rrdset(RRDSET *st);
  262. void rrdinstance_rrdset_is_freed(RRDSET *st);
  263. void rrdinstance_rrdset_has_updated_retention(RRDSET *st);
  264. void rrdinstance_updated_rrdset_name(RRDSET *st);
  265. void rrdinstance_updated_rrdset_flags_no_action(RRDINSTANCE *ri, RRDSET *st);
  266. void rrdinstance_updated_rrdset_flags(RRDSET *st);
  267. void rrdinstance_collected_rrdset(RRDSET *st);
  268. void rrdcontext_queue_for_post_processing(RRDCONTEXT *rc, const char *function, RRD_FLAGS flags);
  269. // ----------------------------------------------------------------------------
  270. // helper one-liners for RRDCONTEXT
  271. static inline RRDCONTEXT *rrdcontext_acquired_value(RRDCONTEXT_ACQUIRED *rca) {
  272. return dictionary_acquired_item_value((DICTIONARY_ITEM *)rca);
  273. }
  274. static inline RRDCONTEXT_ACQUIRED *rrdcontext_acquired_dup(RRDCONTEXT_ACQUIRED *rca) {
  275. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  276. return (RRDCONTEXT_ACQUIRED *)dictionary_acquired_item_dup(rc->rrdhost->rrdctx.contexts, (DICTIONARY_ITEM *)rca);
  277. }
  278. static inline void rrdcontext_release(RRDCONTEXT_ACQUIRED *rca) {
  279. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  280. dictionary_acquired_item_release(rc->rrdhost->rrdctx.contexts, (DICTIONARY_ITEM *)rca);
  281. }
  282. // ----------------------------------------------------------------------------
  283. // Forward definitions
  284. void rrdcontext_recalculate_context_retention(RRDCONTEXT *rc, RRD_FLAGS reason, bool worker_jobs);
  285. void rrdcontext_recalculate_host_retention(RRDHOST *host, RRD_FLAGS reason, bool worker_jobs);
  286. #define rrdcontext_lock(rc) spinlock_lock(&((rc)->spinlock))
  287. #define rrdcontext_unlock(rc) spinlock_unlock(&((rc)->spinlock))
  288. void rrdinstance_trigger_updates(RRDINSTANCE *ri, const char *function);
  289. void rrdcontext_trigger_updates(RRDCONTEXT *rc, const char *function);
  290. void rrdinstances_create_in_rrdcontext(RRDCONTEXT *rc);
  291. void rrdinstances_destroy_from_rrdcontext(RRDCONTEXT *rc);
  292. void rrdmetrics_destroy_from_rrdinstance(RRDINSTANCE *ri);
  293. void rrdmetrics_create_in_rrdinstance(RRDINSTANCE *ri);
  294. void rrdmetric_from_rrddim(RRDDIM *rd);
  295. void rrd_reasons_to_buffer_json_array_items(RRD_FLAGS flags, BUFFER *wb);
  296. #define rrdcontext_version_hash(host) rrdcontext_version_hash_with_callback(host, NULL, false, NULL)
  297. uint64_t rrdcontext_version_hash_with_callback(
  298. RRDHOST *host,
  299. void (*callback)(RRDCONTEXT *, bool, void *),
  300. bool snapshot,
  301. void *bundle);
  302. void rrdcontext_message_send_unsafe(RRDCONTEXT *rc, bool snapshot __maybe_unused, void *bundle __maybe_unused);
  303. void rrdcontext_update_from_collected_rrdinstance(RRDINSTANCE *ri);
  304. #endif //NETDATA_RRDCONTEXT_INTERNAL_H