worker.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "internal.h"
  3. static uint64_t rrdcontext_get_next_version(RRDCONTEXT *rc);
  4. static bool check_if_cloud_version_changed_unsafe(RRDCONTEXT *rc, bool sending __maybe_unused);
  5. static void rrdcontext_delete_from_sql_unsafe(RRDCONTEXT *rc);
  6. static void rrdcontext_dequeue_from_post_processing(RRDCONTEXT *rc);
  7. static void rrdcontext_post_process_updates(RRDCONTEXT *rc, bool force, RRD_FLAGS reason, bool worker_jobs);
  8. static void rrdcontext_garbage_collect_single_host(RRDHOST *host, bool worker_jobs);
  9. static void rrdcontext_garbage_collect_for_all_hosts(void);
  10. extern usec_t rrdcontext_next_db_rotation_ut;
  11. // ----------------------------------------------------------------------------
  12. // load from SQL
  13. static void rrdinstance_load_clabel(SQL_CLABEL_DATA *sld, void *data) {
  14. RRDINSTANCE *ri = data;
  15. rrdlabels_add(ri->rrdlabels, sld->label_key, sld->label_value, sld->label_source);
  16. }
  17. static void rrdinstance_load_dimension(SQL_DIMENSION_DATA *sd, void *data) {
  18. RRDINSTANCE *ri = data;
  19. RRDMETRIC trm = {
  20. .id = string_strdupz(sd->id),
  21. .name = string_strdupz(sd->name),
  22. .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomic
  23. };
  24. if(sd->hidden) trm.flags |= RRD_FLAG_HIDDEN;
  25. uuid_copy(trm.uuid, sd->dim_id);
  26. dictionary_set(ri->rrdmetrics, string2str(trm.id), &trm, sizeof(trm));
  27. }
  28. static void rrdinstance_load_chart_callback(SQL_CHART_DATA *sc, void *data) {
  29. RRDHOST *host = data;
  30. RRDCONTEXT tc = {
  31. .id = string_strdupz(sc->context),
  32. .title = string_strdupz(sc->title),
  33. .units = string_strdupz(sc->units),
  34. .family = string_strdupz(sc->family),
  35. .priority = sc->priority,
  36. .chart_type = sc->chart_type,
  37. .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomics
  38. .rrdhost = host,
  39. };
  40. RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_set_and_acquire_item(host->rrdctx.contexts, string2str(tc.id), &tc, sizeof(tc));
  41. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  42. RRDINSTANCE tri = {
  43. .id = string_strdupz(sc->id),
  44. .name = string_strdupz(sc->name),
  45. .title = string_strdupz(sc->title),
  46. .units = string_strdupz(sc->units),
  47. .family = string_strdupz(sc->family),
  48. .chart_type = sc->chart_type,
  49. .priority = sc->priority,
  50. .update_every_s = sc->update_every,
  51. .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomics
  52. };
  53. uuid_copy(tri.uuid, sc->chart_id);
  54. RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_set_and_acquire_item(rc->rrdinstances, sc->id, &tri, sizeof(tri));
  55. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  56. ctx_get_dimension_list(&ri->uuid, rrdinstance_load_dimension, ri);
  57. ctx_get_label_list(&ri->uuid, rrdinstance_load_clabel, ri);
  58. rrdinstance_trigger_updates(ri, __FUNCTION__ );
  59. rrdinstance_release(ria);
  60. rrdcontext_release(rca);
  61. }
  62. static void rrdcontext_load_context_callback(VERSIONED_CONTEXT_DATA *ctx_data, void *data) {
  63. RRDHOST *host = data;
  64. (void)host;
  65. RRDCONTEXT trc = {
  66. .id = string_strdupz(ctx_data->id),
  67. .flags = RRD_FLAG_ARCHIVED | RRD_FLAG_UPDATE_REASON_LOAD_SQL, // no need for atomics
  68. // no need to set more data here
  69. // we only need the hub data
  70. .hub = *ctx_data,
  71. };
  72. dictionary_set(host->rrdctx.contexts, string2str(trc.id), &trc, sizeof(trc));
  73. }
  74. void rrdhost_load_rrdcontext_data(RRDHOST *host) {
  75. if(host->rrdctx.contexts) return;
  76. rrdhost_create_rrdcontexts(host);
  77. ctx_get_context_list(&host->host_uuid, rrdcontext_load_context_callback, host);
  78. ctx_get_chart_list(&host->host_uuid, rrdinstance_load_chart_callback, host);
  79. RRDCONTEXT *rc;
  80. dfe_start_read(host->rrdctx.contexts, rc) {
  81. rrdcontext_trigger_updates(rc, __FUNCTION__ );
  82. }
  83. dfe_done(rc);
  84. rrdcontext_garbage_collect_single_host(host, false);
  85. }
  86. // ----------------------------------------------------------------------------
  87. // version hash calculation
  88. uint64_t rrdcontext_version_hash_with_callback(
  89. RRDHOST *host,
  90. void (*callback)(RRDCONTEXT *, bool, void *),
  91. bool snapshot,
  92. void *bundle) {
  93. if(unlikely(!host || !host->rrdctx.contexts)) return 0;
  94. RRDCONTEXT *rc;
  95. uint64_t hash = 0;
  96. // loop through all contexts of the host
  97. dfe_start_read(host->rrdctx.contexts, rc) {
  98. rrdcontext_lock(rc);
  99. if(unlikely(rrd_flag_check(rc, RRD_FLAG_HIDDEN))) {
  100. rrdcontext_unlock(rc);
  101. continue;
  102. }
  103. if(unlikely(callback))
  104. callback(rc, snapshot, bundle);
  105. // skip any deleted contexts
  106. if(unlikely(rrd_flag_is_deleted(rc))) {
  107. rrdcontext_unlock(rc);
  108. continue;
  109. }
  110. // we use rc->hub.* which has the latest
  111. // metadata we have sent to the hub
  112. // if a context is currently queued, rc->hub.* does NOT
  113. // reflect the queued changes. rc->hub.* is updated with
  114. // their metadata, after messages are dispatched to hub.
  115. // when the context is being collected,
  116. // rc->hub.last_time_t is already zero
  117. hash += rc->hub.version + rc->hub.last_time_s - rc->hub.first_time_s;
  118. rrdcontext_unlock(rc);
  119. }
  120. dfe_done(rc);
  121. return hash;
  122. }
  123. // ----------------------------------------------------------------------------
  124. // retention recalculation
  125. static void rrdhost_update_cached_retention(RRDHOST *host, time_t first_time_s, time_t last_time_s, bool global) {
  126. if(unlikely(!host))
  127. return;
  128. spinlock_lock(&host->retention.spinlock);
  129. if(global) {
  130. host->retention.first_time_s = first_time_s;
  131. host->retention.last_time_s = last_time_s;
  132. }
  133. else {
  134. if(!host->retention.first_time_s || first_time_s < host->retention.first_time_s)
  135. host->retention.first_time_s = first_time_s;
  136. if(!host->retention.last_time_s || last_time_s > host->retention.last_time_s)
  137. host->retention.last_time_s = last_time_s;
  138. }
  139. spinlock_unlock(&host->retention.spinlock);
  140. }
  141. void rrdcontext_recalculate_context_retention(RRDCONTEXT *rc, RRD_FLAGS reason, bool worker_jobs) {
  142. rrdcontext_post_process_updates(rc, true, reason, worker_jobs);
  143. }
  144. void rrdcontext_recalculate_host_retention(RRDHOST *host, RRD_FLAGS reason, bool worker_jobs) {
  145. if(unlikely(!host || !host->rrdctx.contexts)) return;
  146. time_t first_time_s = 0;
  147. time_t last_time_s = 0;
  148. RRDCONTEXT *rc;
  149. dfe_start_read(host->rrdctx.contexts, rc) {
  150. rrdcontext_recalculate_context_retention(rc, reason, worker_jobs);
  151. if(!first_time_s || rc->first_time_s < first_time_s)
  152. first_time_s = rc->first_time_s;
  153. if(!last_time_s || rc->last_time_s > last_time_s)
  154. last_time_s = rc->last_time_s;
  155. }
  156. dfe_done(rc);
  157. rrdhost_update_cached_retention(host, first_time_s, last_time_s, true);
  158. }
  159. static void rrdcontext_recalculate_retention_all_hosts(void) {
  160. rrdcontext_next_db_rotation_ut = 0;
  161. RRDHOST *host;
  162. dfe_start_reentrant(rrdhost_root_index, host) {
  163. worker_is_busy(WORKER_JOB_RETENTION);
  164. rrdcontext_recalculate_host_retention(host, RRD_FLAG_UPDATE_REASON_DB_ROTATION, true);
  165. }
  166. dfe_done(host);
  167. }
  168. // ----------------------------------------------------------------------------
  169. // garbage collector
  170. bool rrdmetric_update_retention(RRDMETRIC *rm) {
  171. time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
  172. if(rm->rrddim) {
  173. min_first_time_t = rrddim_first_entry_s(rm->rrddim);
  174. max_last_time_t = rrddim_last_entry_s(rm->rrddim);
  175. }
  176. else {
  177. RRDHOST *rrdhost = rm->ri->rc->rrdhost;
  178. for (size_t tier = 0; tier < storage_tiers; tier++) {
  179. STORAGE_ENGINE *eng = rrdhost->db[tier].eng;
  180. time_t first_time_t, last_time_t;
  181. if (eng->api.metric_retention_by_uuid(rrdhost->db[tier].instance, &rm->uuid, &first_time_t, &last_time_t)) {
  182. if (first_time_t < min_first_time_t)
  183. min_first_time_t = first_time_t;
  184. if (last_time_t > max_last_time_t)
  185. max_last_time_t = last_time_t;
  186. }
  187. }
  188. }
  189. if((min_first_time_t == LONG_MAX || min_first_time_t == 0) && max_last_time_t == 0)
  190. return false;
  191. if(min_first_time_t == LONG_MAX)
  192. min_first_time_t = 0;
  193. if(min_first_time_t > max_last_time_t) {
  194. internal_error(true, "RRDMETRIC: retention of '%s' is flipped, first_time_t = %ld, last_time_t = %ld", string2str(rm->id), min_first_time_t, max_last_time_t);
  195. time_t tmp = min_first_time_t;
  196. min_first_time_t = max_last_time_t;
  197. max_last_time_t = tmp;
  198. }
  199. // check if retention changed
  200. if (min_first_time_t != rm->first_time_s) {
  201. rm->first_time_s = min_first_time_t;
  202. rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  203. }
  204. if (max_last_time_t != rm->last_time_s) {
  205. rm->last_time_s = max_last_time_t;
  206. rrd_flag_set_updated(rm, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  207. }
  208. if(unlikely(!rm->first_time_s && !rm->last_time_s))
  209. rrd_flag_set_deleted(rm, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  210. rrd_flag_set(rm, RRD_FLAG_LIVE_RETENTION);
  211. return true;
  212. }
  213. static inline bool rrdmetric_should_be_deleted(RRDMETRIC *rm) {
  214. if(likely(!rrd_flag_check(rm, RRD_FLAGS_REQUIRED_FOR_DELETIONS)))
  215. return false;
  216. if(likely(rrd_flag_check(rm, RRD_FLAGS_PREVENTING_DELETIONS)))
  217. return false;
  218. if(likely(rm->rrddim))
  219. return false;
  220. rrdmetric_update_retention(rm);
  221. if(rm->first_time_s || rm->last_time_s)
  222. return false;
  223. return true;
  224. }
  225. static inline bool rrdinstance_should_be_deleted(RRDINSTANCE *ri) {
  226. if(likely(!rrd_flag_check(ri, RRD_FLAGS_REQUIRED_FOR_DELETIONS)))
  227. return false;
  228. if(likely(rrd_flag_check(ri, RRD_FLAGS_PREVENTING_DELETIONS)))
  229. return false;
  230. if(likely(ri->rrdset))
  231. return false;
  232. if(unlikely(dictionary_referenced_items(ri->rrdmetrics) != 0))
  233. return false;
  234. if(unlikely(dictionary_entries(ri->rrdmetrics) != 0))
  235. return false;
  236. if(ri->first_time_s || ri->last_time_s)
  237. return false;
  238. return true;
  239. }
  240. static inline bool rrdcontext_should_be_deleted(RRDCONTEXT *rc) {
  241. if(likely(!rrd_flag_check(rc, RRD_FLAGS_REQUIRED_FOR_DELETIONS)))
  242. return false;
  243. if(likely(rrd_flag_check(rc, RRD_FLAGS_PREVENTING_DELETIONS)))
  244. return false;
  245. if(unlikely(dictionary_referenced_items(rc->rrdinstances) != 0))
  246. return false;
  247. if(unlikely(dictionary_entries(rc->rrdinstances) != 0))
  248. return false;
  249. if(unlikely(rc->first_time_s || rc->last_time_s))
  250. return false;
  251. return true;
  252. }
  253. void rrdcontext_delete_from_sql_unsafe(RRDCONTEXT *rc) {
  254. // we need to refresh the string pointers in rc->hub
  255. // in case the context changed values
  256. rc->hub.id = string2str(rc->id);
  257. rc->hub.title = string2str(rc->title);
  258. rc->hub.units = string2str(rc->units);
  259. rc->hub.family = string2str(rc->family);
  260. // delete it from SQL
  261. if(ctx_delete_context(&rc->rrdhost->host_uuid, &rc->hub) != 0)
  262. netdata_log_error("RRDCONTEXT: failed to delete context '%s' version %"PRIu64" from SQL.",
  263. rc->hub.id, rc->hub.version);
  264. }
  265. static void rrdcontext_garbage_collect_single_host(RRDHOST *host, bool worker_jobs) {
  266. internal_error(true, "RRDCONTEXT: garbage collecting context structures of host '%s'", rrdhost_hostname(host));
  267. RRDCONTEXT *rc;
  268. dfe_start_reentrant(host->rrdctx.contexts, rc) {
  269. if(unlikely(worker_jobs && !service_running(SERVICE_CONTEXT))) break;
  270. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP);
  271. rrdcontext_lock(rc);
  272. RRDINSTANCE *ri;
  273. dfe_start_reentrant(rc->rrdinstances, ri) {
  274. if(unlikely(worker_jobs && !service_running(SERVICE_CONTEXT))) break;
  275. RRDMETRIC *rm;
  276. dfe_start_write(ri->rrdmetrics, rm) {
  277. if(rrdmetric_should_be_deleted(rm)) {
  278. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  279. if(!dictionary_del(ri->rrdmetrics, string2str(rm->id)))
  280. netdata_log_error("RRDCONTEXT: metric '%s' of instance '%s' of context '%s' of host '%s', failed to be deleted from rrdmetrics dictionary.",
  281. string2str(rm->id),
  282. string2str(ri->id),
  283. string2str(rc->id),
  284. rrdhost_hostname(host));
  285. else
  286. internal_error(
  287. true,
  288. "RRDCONTEXT: metric '%s' of instance '%s' of context '%s' of host '%s', deleted from rrdmetrics dictionary.",
  289. string2str(rm->id),
  290. string2str(ri->id),
  291. string2str(rc->id),
  292. rrdhost_hostname(host));
  293. }
  294. }
  295. dfe_done(rm);
  296. if(rrdinstance_should_be_deleted(ri)) {
  297. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  298. if(!dictionary_del(rc->rrdinstances, string2str(ri->id)))
  299. netdata_log_error("RRDCONTEXT: instance '%s' of context '%s' of host '%s', failed to be deleted from rrdmetrics dictionary.",
  300. string2str(ri->id),
  301. string2str(rc->id),
  302. rrdhost_hostname(host));
  303. else
  304. internal_error(
  305. true,
  306. "RRDCONTEXT: instance '%s' of context '%s' of host '%s', deleted from rrdmetrics dictionary.",
  307. string2str(ri->id),
  308. string2str(rc->id),
  309. rrdhost_hostname(host));
  310. }
  311. }
  312. dfe_done(ri);
  313. if(unlikely(rrdcontext_should_be_deleted(rc))) {
  314. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  315. rrdcontext_dequeue_from_post_processing(rc);
  316. rrdcontext_delete_from_sql_unsafe(rc);
  317. if(!dictionary_del(host->rrdctx.contexts, string2str(rc->id)))
  318. netdata_log_error("RRDCONTEXT: context '%s' of host '%s', failed to be deleted from rrdmetrics dictionary.",
  319. string2str(rc->id),
  320. rrdhost_hostname(host));
  321. else
  322. internal_error(
  323. true,
  324. "RRDCONTEXT: context '%s' of host '%s', deleted from rrdmetrics dictionary.",
  325. string2str(rc->id),
  326. rrdhost_hostname(host));
  327. }
  328. // the item is referenced in the dictionary
  329. // so, it is still here to unlock, even if we have deleted it
  330. rrdcontext_unlock(rc);
  331. }
  332. dfe_done(rc);
  333. }
  334. static void rrdcontext_garbage_collect_for_all_hosts(void) {
  335. RRDHOST *host;
  336. dfe_start_reentrant(rrdhost_root_index, host) {
  337. rrdcontext_garbage_collect_single_host(host, true);
  338. }
  339. dfe_done(host);
  340. }
  341. // ----------------------------------------------------------------------------
  342. // post processing
  343. static void rrdmetric_process_updates(RRDMETRIC *rm, bool force, RRD_FLAGS reason, bool worker_jobs) {
  344. if(reason != RRD_FLAG_NONE)
  345. rrd_flag_set_updated(rm, reason);
  346. if(!force && !rrd_flag_is_updated(rm) && rrd_flag_check(rm, RRD_FLAG_LIVE_RETENTION) && !rrd_flag_check(rm, RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  347. return;
  348. if(worker_jobs)
  349. worker_is_busy(WORKER_JOB_PP_METRIC);
  350. if(reason & RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD) {
  351. rrd_flag_set_archived(rm);
  352. rrd_flag_set(rm, RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD);
  353. }
  354. if(rrd_flag_is_deleted(rm) && (reason & RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  355. rrd_flag_set_archived(rm);
  356. rrdmetric_update_retention(rm);
  357. rrd_flag_unset_updated(rm);
  358. }
  359. static void rrdinstance_post_process_updates(RRDINSTANCE *ri, bool force, RRD_FLAGS reason, bool worker_jobs) {
  360. if(reason != RRD_FLAG_NONE)
  361. rrd_flag_set_updated(ri, reason);
  362. if(!force && !rrd_flag_is_updated(ri) && rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION))
  363. return;
  364. if(worker_jobs)
  365. worker_is_busy(WORKER_JOB_PP_INSTANCE);
  366. time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
  367. size_t metrics_active = 0, metrics_deleted = 0;
  368. bool live_retention = true, currently_collected = false;
  369. if(dictionary_entries(ri->rrdmetrics) > 0) {
  370. RRDMETRIC *rm;
  371. dfe_start_read((DICTIONARY *)ri->rrdmetrics, rm) {
  372. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  373. RRD_FLAGS reason_to_pass = reason;
  374. if(rrd_flag_check(ri, RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  375. reason_to_pass |= RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION;
  376. rrdmetric_process_updates(rm, force, reason_to_pass, worker_jobs);
  377. if(unlikely(!rrd_flag_check(rm, RRD_FLAG_LIVE_RETENTION)))
  378. live_retention = false;
  379. if (unlikely((rrdmetric_should_be_deleted(rm)))) {
  380. metrics_deleted++;
  381. continue;
  382. }
  383. if(!currently_collected && rrd_flag_check(rm, RRD_FLAG_COLLECTED) && rm->first_time_s)
  384. currently_collected = true;
  385. metrics_active++;
  386. if (rm->first_time_s && rm->first_time_s < min_first_time_t)
  387. min_first_time_t = rm->first_time_s;
  388. if (rm->last_time_s && rm->last_time_s > max_last_time_t)
  389. max_last_time_t = rm->last_time_s;
  390. }
  391. dfe_done(rm);
  392. }
  393. if(unlikely(live_retention && !rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION)))
  394. rrd_flag_set(ri, RRD_FLAG_LIVE_RETENTION);
  395. else if(unlikely(!live_retention && rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION)))
  396. rrd_flag_clear(ri, RRD_FLAG_LIVE_RETENTION);
  397. if(unlikely(!metrics_active)) {
  398. // no metrics available
  399. if(ri->first_time_s) {
  400. ri->first_time_s = 0;
  401. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  402. }
  403. if(ri->last_time_s) {
  404. ri->last_time_s = 0;
  405. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  406. }
  407. rrd_flag_set_deleted(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  408. }
  409. else {
  410. // we have active metrics...
  411. if (unlikely(min_first_time_t == LONG_MAX))
  412. min_first_time_t = 0;
  413. if (unlikely(min_first_time_t == 0 || max_last_time_t == 0)) {
  414. if(ri->first_time_s) {
  415. ri->first_time_s = 0;
  416. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  417. }
  418. if(ri->last_time_s) {
  419. ri->last_time_s = 0;
  420. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  421. }
  422. if(likely(live_retention))
  423. rrd_flag_set_deleted(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  424. }
  425. else {
  426. rrd_flag_clear(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  427. if (unlikely(ri->first_time_s != min_first_time_t)) {
  428. ri->first_time_s = min_first_time_t;
  429. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  430. }
  431. if (unlikely(ri->last_time_s != max_last_time_t)) {
  432. ri->last_time_s = max_last_time_t;
  433. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  434. }
  435. if(likely(currently_collected))
  436. rrd_flag_set_collected(ri);
  437. else
  438. rrd_flag_set_archived(ri);
  439. }
  440. }
  441. rrd_flag_unset_updated(ri);
  442. }
  443. static void rrdcontext_post_process_updates(RRDCONTEXT *rc, bool force, RRD_FLAGS reason, bool worker_jobs) {
  444. if(reason != RRD_FLAG_NONE)
  445. rrd_flag_set_updated(rc, reason);
  446. if(worker_jobs)
  447. worker_is_busy(WORKER_JOB_PP_CONTEXT);
  448. size_t min_priority_collected = LONG_MAX;
  449. size_t min_priority_not_collected = LONG_MAX;
  450. size_t min_priority = LONG_MAX;
  451. time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
  452. size_t instances_active = 0, instances_deleted = 0, metrics = 0;
  453. bool live_retention = true, currently_collected = false, hidden = true;
  454. if(dictionary_entries(rc->rrdinstances) > 0) {
  455. RRDINSTANCE *ri;
  456. dfe_start_reentrant(rc->rrdinstances, ri) {
  457. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  458. RRD_FLAGS reason_to_pass = reason;
  459. if(rrd_flag_check(rc, RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  460. reason_to_pass |= RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION;
  461. rrdinstance_post_process_updates(ri, force, reason_to_pass, worker_jobs);
  462. if(unlikely(hidden && !rrd_flag_check(ri, RRD_FLAG_HIDDEN)))
  463. hidden = false;
  464. if(unlikely(live_retention && !rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION)))
  465. live_retention = false;
  466. if (unlikely(rrdinstance_should_be_deleted(ri))) {
  467. instances_deleted++;
  468. continue;
  469. }
  470. bool ri_collected = rrd_flag_is_collected(ri);
  471. if(ri_collected && !rrd_flag_check(ri, RRD_FLAG_MERGED_COLLECTED_RI_TO_RC)) {
  472. rrdcontext_update_from_collected_rrdinstance(ri);
  473. rrd_flag_set(ri, RRD_FLAG_MERGED_COLLECTED_RI_TO_RC);
  474. }
  475. if(unlikely(!currently_collected && rrd_flag_is_collected(ri) && ri->first_time_s))
  476. currently_collected = true;
  477. internal_error(rc->units != ri->units,
  478. "RRDCONTEXT: '%s' rrdinstance '%s' has different units, context '%s', instance '%s'",
  479. string2str(rc->id), string2str(ri->id),
  480. string2str(rc->units), string2str(ri->units));
  481. instances_active++;
  482. metrics += dictionary_entries(ri->rrdmetrics);
  483. if (ri->priority >= RRDCONTEXT_MINIMUM_ALLOWED_PRIORITY) {
  484. if(rrd_flag_check(ri, RRD_FLAG_COLLECTED)) {
  485. if(ri->priority < min_priority_collected)
  486. min_priority_collected = ri->priority;
  487. }
  488. else {
  489. if(ri->priority < min_priority_not_collected)
  490. min_priority_not_collected = ri->priority;
  491. }
  492. }
  493. if (ri->first_time_s && ri->first_time_s < min_first_time_t)
  494. min_first_time_t = ri->first_time_s;
  495. if (ri->last_time_s && ri->last_time_s > max_last_time_t)
  496. max_last_time_t = ri->last_time_s;
  497. }
  498. dfe_done(ri);
  499. rc->stats.metrics = metrics;
  500. if(min_priority_collected != LONG_MAX)
  501. // use the collected priority
  502. min_priority = min_priority_collected;
  503. else
  504. // use the non-collected priority
  505. min_priority = min_priority_not_collected;
  506. }
  507. {
  508. bool previous_hidden = rrd_flag_check(rc, RRD_FLAG_HIDDEN);
  509. if (hidden != previous_hidden) {
  510. if (hidden && !rrd_flag_check(rc, RRD_FLAG_HIDDEN))
  511. rrd_flag_set(rc, RRD_FLAG_HIDDEN);
  512. else if (!hidden && rrd_flag_check(rc, RRD_FLAG_HIDDEN))
  513. rrd_flag_clear(rc, RRD_FLAG_HIDDEN);
  514. }
  515. bool previous_live_retention = rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION);
  516. if (live_retention != previous_live_retention) {
  517. if (live_retention && !rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION))
  518. rrd_flag_set(rc, RRD_FLAG_LIVE_RETENTION);
  519. else if (!live_retention && rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION))
  520. rrd_flag_clear(rc, RRD_FLAG_LIVE_RETENTION);
  521. }
  522. }
  523. rrdcontext_lock(rc);
  524. rc->pp.executions++;
  525. if(unlikely(!instances_active)) {
  526. // we had some instances, but they are gone now...
  527. if(rc->first_time_s) {
  528. rc->first_time_s = 0;
  529. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  530. }
  531. if(rc->last_time_s) {
  532. rc->last_time_s = 0;
  533. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  534. }
  535. rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  536. }
  537. else {
  538. // we have some active instances...
  539. if (unlikely(min_first_time_t == LONG_MAX))
  540. min_first_time_t = 0;
  541. if (unlikely(min_first_time_t == 0 && max_last_time_t == 0)) {
  542. if(rc->first_time_s) {
  543. rc->first_time_s = 0;
  544. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  545. }
  546. if(rc->last_time_s) {
  547. rc->last_time_s = 0;
  548. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  549. }
  550. rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  551. }
  552. else {
  553. rrd_flag_clear(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  554. if (unlikely(rc->first_time_s != min_first_time_t)) {
  555. rc->first_time_s = min_first_time_t;
  556. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  557. }
  558. if (rc->last_time_s != max_last_time_t) {
  559. rc->last_time_s = max_last_time_t;
  560. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  561. }
  562. if(likely(currently_collected))
  563. rrd_flag_set_collected(rc);
  564. else
  565. rrd_flag_set_archived(rc);
  566. }
  567. if (min_priority != LONG_MAX && rc->priority != min_priority) {
  568. rc->priority = min_priority;
  569. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_METADATA);
  570. }
  571. }
  572. if(unlikely(rrd_flag_is_updated(rc) && rc->rrdhost->rrdctx.hub_queue)) {
  573. if(check_if_cloud_version_changed_unsafe(rc, false)) {
  574. rc->version = rrdcontext_get_next_version(rc);
  575. dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx.hub_queue,
  576. string2str(rc->id), rc, sizeof(*rc));
  577. }
  578. }
  579. rrd_flag_unset_updated(rc);
  580. rrdcontext_unlock(rc);
  581. }
  582. void rrdcontext_queue_for_post_processing(RRDCONTEXT *rc, const char *function __maybe_unused, RRD_FLAGS flags __maybe_unused) {
  583. if(unlikely(!rc->rrdhost->rrdctx.pp_queue)) return;
  584. if(!rrd_flag_check(rc, RRD_FLAG_QUEUED_FOR_PP)) {
  585. dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx.pp_queue,
  586. string2str(rc->id),
  587. rc,
  588. sizeof(*rc));
  589. #if(defined(NETDATA_INTERNAL_CHECKS) && defined(LOG_POST_PROCESSING_QUEUE_INSERTIONS))
  590. {
  591. BUFFER *wb_flags = buffer_create(1000);
  592. rrd_flags_to_buffer(flags, wb_flags);
  593. BUFFER *wb_reasons = buffer_create(1000);
  594. rrd_reasons_to_buffer(flags, wb_reasons);
  595. internal_error(true, "RRDCONTEXT: '%s' update triggered by function %s(), due to flags: %s, reasons: %s",
  596. string2str(rc->id), function,
  597. buffer_tostring(wb_flags),
  598. buffer_tostring(wb_reasons));
  599. buffer_free(wb_reasons);
  600. buffer_free(wb_flags);
  601. }
  602. #endif
  603. }
  604. }
  605. static void rrdcontext_dequeue_from_post_processing(RRDCONTEXT *rc) {
  606. if(unlikely(!rc->rrdhost->rrdctx.pp_queue)) return;
  607. dictionary_del(rc->rrdhost->rrdctx.pp_queue, string2str(rc->id));
  608. }
  609. static void rrdcontext_post_process_queued_contexts(RRDHOST *host) {
  610. if(unlikely(!host->rrdctx.pp_queue)) return;
  611. RRDCONTEXT *rc;
  612. dfe_start_reentrant(host->rrdctx.pp_queue, rc) {
  613. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  614. rrdcontext_dequeue_from_post_processing(rc);
  615. rrdcontext_post_process_updates(rc, false, RRD_FLAG_NONE, true);
  616. }
  617. dfe_done(rc);
  618. }
  619. // ----------------------------------------------------------------------------
  620. // dispatching contexts to cloud
  621. static uint64_t rrdcontext_get_next_version(RRDCONTEXT *rc) {
  622. time_t now = now_realtime_sec();
  623. uint64_t version = MAX(rc->version, rc->hub.version);
  624. version = MAX((uint64_t)now, version);
  625. version++;
  626. return version;
  627. }
  628. void rrdcontext_message_send_unsafe(RRDCONTEXT *rc, bool snapshot __maybe_unused, void *bundle __maybe_unused) {
  629. // save it, so that we know the last version we sent to hub
  630. rc->version = rc->hub.version = rrdcontext_get_next_version(rc);
  631. rc->hub.id = string2str(rc->id);
  632. rc->hub.title = string2str(rc->title);
  633. rc->hub.units = string2str(rc->units);
  634. rc->hub.family = string2str(rc->family);
  635. rc->hub.chart_type = rrdset_type_name(rc->chart_type);
  636. rc->hub.priority = rc->priority;
  637. rc->hub.first_time_s = rc->first_time_s;
  638. rc->hub.last_time_s = rrd_flag_is_collected(rc) ? 0 : rc->last_time_s;
  639. rc->hub.deleted = rrd_flag_is_deleted(rc) ? true : false;
  640. #ifdef ENABLE_ACLK
  641. struct context_updated message = {
  642. .id = rc->hub.id,
  643. .version = rc->hub.version,
  644. .title = rc->hub.title,
  645. .units = rc->hub.units,
  646. .family = rc->hub.family,
  647. .chart_type = rc->hub.chart_type,
  648. .priority = rc->hub.priority,
  649. .first_entry = rc->hub.first_time_s,
  650. .last_entry = rc->hub.last_time_s,
  651. .deleted = rc->hub.deleted,
  652. };
  653. if(likely(!rrd_flag_check(rc, RRD_FLAG_HIDDEN))) {
  654. if (snapshot) {
  655. if (!rc->hub.deleted)
  656. contexts_snapshot_add_ctx_update(bundle, &message);
  657. }
  658. else
  659. contexts_updated_add_ctx_update(bundle, &message);
  660. }
  661. #endif
  662. // store it to SQL
  663. if(rrd_flag_is_deleted(rc))
  664. rrdcontext_delete_from_sql_unsafe(rc);
  665. else if (ctx_store_context(&rc->rrdhost->host_uuid, &rc->hub) != 0)
  666. netdata_log_error("RRDCONTEXT: failed to save context '%s' version %"PRIu64" to SQL.", rc->hub.id, rc->hub.version);
  667. }
  668. static bool check_if_cloud_version_changed_unsafe(RRDCONTEXT *rc, bool sending __maybe_unused) {
  669. bool id_changed = false,
  670. title_changed = false,
  671. units_changed = false,
  672. family_changed = false,
  673. chart_type_changed = false,
  674. priority_changed = false,
  675. first_time_changed = false,
  676. last_time_changed = false,
  677. deleted_changed = false;
  678. RRD_FLAGS flags = rrd_flags_get(rc);
  679. if(unlikely(string2str(rc->id) != rc->hub.id))
  680. id_changed = true;
  681. if(unlikely(string2str(rc->title) != rc->hub.title))
  682. title_changed = true;
  683. if(unlikely(string2str(rc->units) != rc->hub.units))
  684. units_changed = true;
  685. if(unlikely(string2str(rc->family) != rc->hub.family))
  686. family_changed = true;
  687. if(unlikely(rrdset_type_name(rc->chart_type) != rc->hub.chart_type))
  688. chart_type_changed = true;
  689. if(unlikely(rc->priority != rc->hub.priority))
  690. priority_changed = true;
  691. if(unlikely((uint64_t)rc->first_time_s != rc->hub.first_time_s))
  692. first_time_changed = true;
  693. if(unlikely((uint64_t)((flags & RRD_FLAG_COLLECTED) ? 0 : rc->last_time_s) != rc->hub.last_time_s))
  694. last_time_changed = true;
  695. if(unlikely(((flags & RRD_FLAG_DELETED) ? true : false) != rc->hub.deleted))
  696. deleted_changed = true;
  697. if(unlikely(id_changed || title_changed || units_changed || family_changed || chart_type_changed || priority_changed || first_time_changed || last_time_changed || deleted_changed)) {
  698. internal_error(LOG_TRANSITIONS,
  699. "RRDCONTEXT: %s NEW VERSION '%s'%s of host '%s', version %"PRIu64", 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)",
  700. sending?"SENDING":"QUEUE",
  701. string2str(rc->id), id_changed ? " (CHANGED)" : "",
  702. rrdhost_hostname(rc->rrdhost),
  703. rc->version,
  704. string2str(rc->title), title_changed ? " (CHANGED)" : "",
  705. string2str(rc->units), units_changed ? " (CHANGED)" : "",
  706. string2str(rc->family), family_changed ? " (CHANGED)" : "",
  707. rrdset_type_name(rc->chart_type), chart_type_changed ? " (CHANGED)" : "",
  708. rc->priority, priority_changed ? " (CHANGED)" : "",
  709. rc->first_time_s, first_time_changed ? " (CHANGED)" : "",
  710. (flags & RRD_FLAG_COLLECTED) ? 0 : rc->last_time_s, last_time_changed ? " (CHANGED)" : "",
  711. (flags & RRD_FLAG_DELETED) ? "true" : "false", deleted_changed ? " (CHANGED)" : "",
  712. sending ? (now_realtime_usec() - rc->queue.queued_ut) / USEC_PER_MS : 0,
  713. sending ? (rc->queue.scheduled_dispatch_ut - rc->queue.queued_ut) / USEC_PER_MS : 0
  714. );
  715. rrdhost_update_cached_retention(rc->rrdhost, rc->first_time_s, rc->last_time_s, false);
  716. return true;
  717. }
  718. if(!(flags & RRD_FLAG_COLLECTED))
  719. rrdhost_update_cached_retention(rc->rrdhost, rc->first_time_s, rc->last_time_s, false);
  720. return false;
  721. }
  722. static inline usec_t rrdcontext_calculate_queued_dispatch_time_ut(RRDCONTEXT *rc, usec_t now_ut) {
  723. if(likely(rc->queue.delay_calc_ut >= rc->queue.queued_ut))
  724. return rc->queue.scheduled_dispatch_ut;
  725. RRD_FLAGS flags = rc->queue.queued_flags;
  726. usec_t delay = LONG_MAX;
  727. int i;
  728. struct rrdcontext_reason *reason;
  729. for(i = 0, reason = &rrdcontext_reasons[i]; reason->name ; reason = &rrdcontext_reasons[++i]) {
  730. if(unlikely(flags & reason->flag)) {
  731. if(reason->delay_ut < delay)
  732. delay = reason->delay_ut;
  733. }
  734. }
  735. if(unlikely(delay == LONG_MAX)) {
  736. internal_error(true, "RRDCONTEXT: '%s', cannot find minimum delay of flags %x", string2str(rc->id), (unsigned int)flags);
  737. delay = 60 * USEC_PER_SEC;
  738. }
  739. rc->queue.delay_calc_ut = now_ut;
  740. usec_t dispatch_ut = rc->queue.scheduled_dispatch_ut = rc->queue.queued_ut + delay;
  741. return dispatch_ut;
  742. }
  743. static void rrdcontext_dequeue_from_hub_queue(RRDCONTEXT *rc) {
  744. dictionary_del(rc->rrdhost->rrdctx.hub_queue, string2str(rc->id));
  745. }
  746. static void rrdcontext_dispatch_queued_contexts_to_hub(RRDHOST *host, usec_t now_ut) {
  747. // check if we have received a streaming command for this host
  748. if(!rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS) || !aclk_connected || !host->rrdctx.hub_queue)
  749. return;
  750. // check if there are queued items to send
  751. if(!dictionary_entries(host->rrdctx.hub_queue))
  752. return;
  753. if(!host->node_id)
  754. return;
  755. size_t messages_added = 0;
  756. contexts_updated_t bundle = NULL;
  757. RRDCONTEXT *rc;
  758. dfe_start_reentrant(host->rrdctx.hub_queue, rc) {
  759. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  760. if(unlikely(messages_added >= MESSAGES_PER_BUNDLE_TO_SEND_TO_HUB_PER_HOST))
  761. break;
  762. worker_is_busy(WORKER_JOB_QUEUED);
  763. usec_t dispatch_ut = rrdcontext_calculate_queued_dispatch_time_ut(rc, now_ut);
  764. char *claim_id = get_agent_claimid();
  765. if(unlikely(now_ut >= dispatch_ut) && claim_id) {
  766. worker_is_busy(WORKER_JOB_CHECK);
  767. rrdcontext_lock(rc);
  768. if(check_if_cloud_version_changed_unsafe(rc, true)) {
  769. worker_is_busy(WORKER_JOB_SEND);
  770. #ifdef ENABLE_ACLK
  771. if(!bundle) {
  772. // prepare the bundle to send the messages
  773. char uuid[UUID_STR_LEN];
  774. uuid_unparse_lower(*host->node_id, uuid);
  775. bundle = contexts_updated_new(claim_id, uuid, 0, now_ut);
  776. }
  777. #endif
  778. // update the hub data of the context, give a new version, pack the message
  779. // and save an update to SQL
  780. rrdcontext_message_send_unsafe(rc, false, bundle);
  781. messages_added++;
  782. rc->queue.dispatches++;
  783. rc->queue.dequeued_ut = now_ut;
  784. }
  785. else
  786. rc->version = rc->hub.version;
  787. // remove it from the queue
  788. worker_is_busy(WORKER_JOB_DEQUEUE);
  789. rrdcontext_dequeue_from_hub_queue(rc);
  790. if(unlikely(rrdcontext_should_be_deleted(rc))) {
  791. // this is a deleted context - delete it forever...
  792. worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  793. rrdcontext_dequeue_from_post_processing(rc);
  794. rrdcontext_delete_from_sql_unsafe(rc);
  795. STRING *id = string_dup(rc->id);
  796. rrdcontext_unlock(rc);
  797. // delete it from the master dictionary
  798. if(!dictionary_del(host->rrdctx.contexts, string2str(rc->id)))
  799. netdata_log_error("RRDCONTEXT: '%s' of host '%s' failed to be deleted from rrdcontext dictionary.",
  800. string2str(id), rrdhost_hostname(host));
  801. string_freez(id);
  802. }
  803. else
  804. rrdcontext_unlock(rc);
  805. }
  806. freez(claim_id);
  807. }
  808. dfe_done(rc);
  809. #ifdef ENABLE_ACLK
  810. if(service_running(SERVICE_CONTEXT) && bundle) {
  811. // we have a bundle to send messages
  812. // update the version hash
  813. contexts_updated_update_version_hash(bundle, rrdcontext_version_hash(host));
  814. // send it
  815. aclk_send_contexts_updated(bundle);
  816. }
  817. else if(bundle)
  818. contexts_updated_delete(bundle);
  819. #endif
  820. }
  821. // ----------------------------------------------------------------------------
  822. // worker thread
  823. static void rrdcontext_main_cleanup(void *ptr) {
  824. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  825. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  826. // custom code
  827. worker_unregister();
  828. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  829. }
  830. void *rrdcontext_main(void *ptr) {
  831. netdata_thread_cleanup_push(rrdcontext_main_cleanup, ptr);
  832. worker_register("RRDCONTEXT");
  833. worker_register_job_name(WORKER_JOB_HOSTS, "hosts");
  834. worker_register_job_name(WORKER_JOB_CHECK, "dedup checks");
  835. worker_register_job_name(WORKER_JOB_SEND, "sent contexts");
  836. worker_register_job_name(WORKER_JOB_DEQUEUE, "deduplicated contexts");
  837. worker_register_job_name(WORKER_JOB_RETENTION, "metrics retention");
  838. worker_register_job_name(WORKER_JOB_QUEUED, "queued contexts");
  839. worker_register_job_name(WORKER_JOB_CLEANUP, "cleanups");
  840. worker_register_job_name(WORKER_JOB_CLEANUP_DELETE, "deletes");
  841. worker_register_job_name(WORKER_JOB_PP_METRIC, "check metrics");
  842. worker_register_job_name(WORKER_JOB_PP_INSTANCE, "check instances");
  843. worker_register_job_name(WORKER_JOB_PP_CONTEXT, "check contexts");
  844. worker_register_job_custom_metric(WORKER_JOB_HUB_QUEUE_SIZE, "hub queue size", "contexts", WORKER_METRIC_ABSOLUTE);
  845. worker_register_job_custom_metric(WORKER_JOB_PP_QUEUE_SIZE, "post processing queue size", "contexts", WORKER_METRIC_ABSOLUTE);
  846. heartbeat_t hb;
  847. heartbeat_init(&hb);
  848. usec_t step = RRDCONTEXT_WORKER_THREAD_HEARTBEAT_USEC;
  849. while (service_running(SERVICE_CONTEXT)) {
  850. worker_is_idle();
  851. heartbeat_next(&hb, step);
  852. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  853. usec_t now_ut = now_realtime_usec();
  854. if(rrdcontext_next_db_rotation_ut && now_ut > rrdcontext_next_db_rotation_ut) {
  855. rrdcontext_recalculate_retention_all_hosts();
  856. rrdcontext_garbage_collect_for_all_hosts();
  857. rrdcontext_next_db_rotation_ut = 0;
  858. }
  859. size_t hub_queued_contexts_for_all_hosts = 0;
  860. size_t pp_queued_contexts_for_all_hosts = 0;
  861. RRDHOST *host;
  862. dfe_start_reentrant(rrdhost_root_index, host) {
  863. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  864. worker_is_busy(WORKER_JOB_HOSTS);
  865. if(host->rrdctx.pp_queue) {
  866. pp_queued_contexts_for_all_hosts += dictionary_entries(host->rrdctx.pp_queue);
  867. rrdcontext_post_process_queued_contexts(host);
  868. dictionary_garbage_collect(host->rrdctx.pp_queue);
  869. }
  870. if(host->rrdctx.hub_queue) {
  871. hub_queued_contexts_for_all_hosts += dictionary_entries(host->rrdctx.hub_queue);
  872. rrdcontext_dispatch_queued_contexts_to_hub(host, now_ut);
  873. dictionary_garbage_collect(host->rrdctx.hub_queue);
  874. }
  875. if (host->rrdctx.contexts)
  876. dictionary_garbage_collect(host->rrdctx.contexts);
  877. // calculate the number of metrics and instances in the host
  878. RRDCONTEXT *rc;
  879. uint32_t metrics = 0, instances = 0;
  880. dfe_start_read(host->rrdctx.contexts, rc) {
  881. metrics += rc->stats.metrics;
  882. instances += dictionary_entries(rc->rrdinstances);
  883. }
  884. dfe_done(rc);
  885. host->rrdctx.metrics = metrics;
  886. host->rrdctx.instances = instances;
  887. }
  888. dfe_done(host);
  889. worker_set_metric(WORKER_JOB_HUB_QUEUE_SIZE, (NETDATA_DOUBLE)hub_queued_contexts_for_all_hosts);
  890. worker_set_metric(WORKER_JOB_PP_QUEUE_SIZE, (NETDATA_DOUBLE)pp_queued_contexts_for_all_hosts);
  891. }
  892. netdata_thread_cleanup_pop(1);
  893. return NULL;
  894. }