worker.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. error("RRDCONTEXT: failed to delete context '%s' version %"PRIu64" from SQL.", rc->hub.id, rc->hub.version);
  263. }
  264. static void rrdcontext_garbage_collect_single_host(RRDHOST *host, bool worker_jobs) {
  265. internal_error(true, "RRDCONTEXT: garbage collecting context structures of host '%s'", rrdhost_hostname(host));
  266. RRDCONTEXT *rc;
  267. dfe_start_reentrant(host->rrdctx.contexts, rc) {
  268. if(unlikely(worker_jobs && !service_running(SERVICE_CONTEXT))) break;
  269. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP);
  270. rrdcontext_lock(rc);
  271. RRDINSTANCE *ri;
  272. dfe_start_reentrant(rc->rrdinstances, ri) {
  273. if(unlikely(worker_jobs && !service_running(SERVICE_CONTEXT))) break;
  274. RRDMETRIC *rm;
  275. dfe_start_write(ri->rrdmetrics, rm) {
  276. if(rrdmetric_should_be_deleted(rm)) {
  277. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  278. if(!dictionary_del(ri->rrdmetrics, string2str(rm->id)))
  279. error("RRDCONTEXT: metric '%s' of instance '%s' of context '%s' of host '%s', failed to be deleted from rrdmetrics dictionary.",
  280. string2str(rm->id),
  281. string2str(ri->id),
  282. string2str(rc->id),
  283. rrdhost_hostname(host));
  284. else
  285. internal_error(
  286. true,
  287. "RRDCONTEXT: metric '%s' of instance '%s' of context '%s' of host '%s', deleted from rrdmetrics dictionary.",
  288. string2str(rm->id),
  289. string2str(ri->id),
  290. string2str(rc->id),
  291. rrdhost_hostname(host));
  292. }
  293. }
  294. dfe_done(rm);
  295. if(rrdinstance_should_be_deleted(ri)) {
  296. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  297. if(!dictionary_del(rc->rrdinstances, string2str(ri->id)))
  298. error("RRDCONTEXT: instance '%s' of context '%s' of host '%s', failed to be deleted from rrdmetrics dictionary.",
  299. string2str(ri->id),
  300. string2str(rc->id),
  301. rrdhost_hostname(host));
  302. else
  303. internal_error(
  304. true,
  305. "RRDCONTEXT: instance '%s' of context '%s' of host '%s', deleted from rrdmetrics dictionary.",
  306. string2str(ri->id),
  307. string2str(rc->id),
  308. rrdhost_hostname(host));
  309. }
  310. }
  311. dfe_done(ri);
  312. if(unlikely(rrdcontext_should_be_deleted(rc))) {
  313. if(worker_jobs) worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  314. rrdcontext_dequeue_from_post_processing(rc);
  315. rrdcontext_delete_from_sql_unsafe(rc);
  316. if(!dictionary_del(host->rrdctx.contexts, string2str(rc->id)))
  317. error("RRDCONTEXT: context '%s' of host '%s', failed to be deleted from rrdmetrics dictionary.",
  318. string2str(rc->id),
  319. rrdhost_hostname(host));
  320. else
  321. internal_error(
  322. true,
  323. "RRDCONTEXT: context '%s' of host '%s', deleted from rrdmetrics dictionary.",
  324. string2str(rc->id),
  325. rrdhost_hostname(host));
  326. }
  327. // the item is referenced in the dictionary
  328. // so, it is still here to unlock, even if we have deleted it
  329. rrdcontext_unlock(rc);
  330. }
  331. dfe_done(rc);
  332. }
  333. static void rrdcontext_garbage_collect_for_all_hosts(void) {
  334. RRDHOST *host;
  335. dfe_start_reentrant(rrdhost_root_index, host) {
  336. rrdcontext_garbage_collect_single_host(host, true);
  337. }
  338. dfe_done(host);
  339. }
  340. // ----------------------------------------------------------------------------
  341. // post processing
  342. static void rrdmetric_process_updates(RRDMETRIC *rm, bool force, RRD_FLAGS reason, bool worker_jobs) {
  343. if(reason != RRD_FLAG_NONE)
  344. rrd_flag_set_updated(rm, reason);
  345. 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))
  346. return;
  347. if(worker_jobs)
  348. worker_is_busy(WORKER_JOB_PP_METRIC);
  349. if(reason & RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD) {
  350. rrd_flag_set_archived(rm);
  351. rrd_flag_set(rm, RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD);
  352. }
  353. if(rrd_flag_is_deleted(rm) && (reason & RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  354. rrd_flag_set_archived(rm);
  355. rrdmetric_update_retention(rm);
  356. rrd_flag_unset_updated(rm);
  357. }
  358. static void rrdinstance_post_process_updates(RRDINSTANCE *ri, bool force, RRD_FLAGS reason, bool worker_jobs) {
  359. if(reason != RRD_FLAG_NONE)
  360. rrd_flag_set_updated(ri, reason);
  361. if(!force && !rrd_flag_is_updated(ri) && rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION))
  362. return;
  363. if(worker_jobs)
  364. worker_is_busy(WORKER_JOB_PP_INSTANCE);
  365. time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
  366. size_t metrics_active = 0, metrics_deleted = 0;
  367. bool live_retention = true, currently_collected = false;
  368. if(dictionary_entries(ri->rrdmetrics) > 0) {
  369. RRDMETRIC *rm;
  370. dfe_start_read((DICTIONARY *)ri->rrdmetrics, rm) {
  371. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  372. RRD_FLAGS reason_to_pass = reason;
  373. if(rrd_flag_check(ri, RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  374. reason_to_pass |= RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION;
  375. rrdmetric_process_updates(rm, force, reason_to_pass, worker_jobs);
  376. if(unlikely(!rrd_flag_check(rm, RRD_FLAG_LIVE_RETENTION)))
  377. live_retention = false;
  378. if (unlikely((rrdmetric_should_be_deleted(rm)))) {
  379. metrics_deleted++;
  380. continue;
  381. }
  382. if(!currently_collected && rrd_flag_check(rm, RRD_FLAG_COLLECTED) && rm->first_time_s)
  383. currently_collected = true;
  384. metrics_active++;
  385. if (rm->first_time_s && rm->first_time_s < min_first_time_t)
  386. min_first_time_t = rm->first_time_s;
  387. if (rm->last_time_s && rm->last_time_s > max_last_time_t)
  388. max_last_time_t = rm->last_time_s;
  389. }
  390. dfe_done(rm);
  391. }
  392. if(unlikely(live_retention && !rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION)))
  393. rrd_flag_set(ri, RRD_FLAG_LIVE_RETENTION);
  394. else if(unlikely(!live_retention && rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION)))
  395. rrd_flag_clear(ri, RRD_FLAG_LIVE_RETENTION);
  396. if(unlikely(!metrics_active)) {
  397. // no metrics available
  398. if(ri->first_time_s) {
  399. ri->first_time_s = 0;
  400. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  401. }
  402. if(ri->last_time_s) {
  403. ri->last_time_s = 0;
  404. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  405. }
  406. rrd_flag_set_deleted(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  407. }
  408. else {
  409. // we have active metrics...
  410. if (unlikely(min_first_time_t == LONG_MAX))
  411. min_first_time_t = 0;
  412. if (unlikely(min_first_time_t == 0 || max_last_time_t == 0)) {
  413. if(ri->first_time_s) {
  414. ri->first_time_s = 0;
  415. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  416. }
  417. if(ri->last_time_s) {
  418. ri->last_time_s = 0;
  419. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  420. }
  421. if(likely(live_retention))
  422. rrd_flag_set_deleted(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  423. }
  424. else {
  425. rrd_flag_clear(ri, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  426. if (unlikely(ri->first_time_s != min_first_time_t)) {
  427. ri->first_time_s = min_first_time_t;
  428. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  429. }
  430. if (unlikely(ri->last_time_s != max_last_time_t)) {
  431. ri->last_time_s = max_last_time_t;
  432. rrd_flag_set_updated(ri, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  433. }
  434. if(likely(currently_collected))
  435. rrd_flag_set_collected(ri);
  436. else
  437. rrd_flag_set_archived(ri);
  438. }
  439. }
  440. rrd_flag_unset_updated(ri);
  441. }
  442. static void rrdcontext_post_process_updates(RRDCONTEXT *rc, bool force, RRD_FLAGS reason, bool worker_jobs) {
  443. if(reason != RRD_FLAG_NONE)
  444. rrd_flag_set_updated(rc, reason);
  445. if(worker_jobs)
  446. worker_is_busy(WORKER_JOB_PP_CONTEXT);
  447. size_t min_priority_collected = LONG_MAX;
  448. size_t min_priority_not_collected = LONG_MAX;
  449. size_t min_priority = LONG_MAX;
  450. time_t min_first_time_t = LONG_MAX, max_last_time_t = 0;
  451. size_t instances_active = 0, instances_deleted = 0, metrics = 0;
  452. bool live_retention = true, currently_collected = false, hidden = true;
  453. if(dictionary_entries(rc->rrdinstances) > 0) {
  454. RRDINSTANCE *ri;
  455. dfe_start_reentrant(rc->rrdinstances, ri) {
  456. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  457. RRD_FLAGS reason_to_pass = reason;
  458. if(rrd_flag_check(rc, RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION))
  459. reason_to_pass |= RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION;
  460. rrdinstance_post_process_updates(ri, force, reason_to_pass, worker_jobs);
  461. if(unlikely(hidden && !rrd_flag_check(ri, RRD_FLAG_HIDDEN)))
  462. hidden = false;
  463. if(unlikely(live_retention && !rrd_flag_check(ri, RRD_FLAG_LIVE_RETENTION)))
  464. live_retention = false;
  465. if (unlikely(rrdinstance_should_be_deleted(ri))) {
  466. instances_deleted++;
  467. continue;
  468. }
  469. if(unlikely(!currently_collected && rrd_flag_is_collected(ri) && ri->first_time_s))
  470. currently_collected = true;
  471. internal_error(rc->units != ri->units,
  472. "RRDCONTEXT: '%s' rrdinstance '%s' has different units, context '%s', instance '%s'",
  473. string2str(rc->id), string2str(ri->id),
  474. string2str(rc->units), string2str(ri->units));
  475. instances_active++;
  476. metrics += dictionary_entries(ri->rrdmetrics);
  477. if (ri->priority >= RRDCONTEXT_MINIMUM_ALLOWED_PRIORITY) {
  478. if(rrd_flag_check(ri, RRD_FLAG_COLLECTED)) {
  479. if(ri->priority < min_priority_collected)
  480. min_priority_collected = ri->priority;
  481. }
  482. else {
  483. if(ri->priority < min_priority_not_collected)
  484. min_priority_not_collected = ri->priority;
  485. }
  486. }
  487. if (ri->first_time_s && ri->first_time_s < min_first_time_t)
  488. min_first_time_t = ri->first_time_s;
  489. if (ri->last_time_s && ri->last_time_s > max_last_time_t)
  490. max_last_time_t = ri->last_time_s;
  491. }
  492. dfe_done(ri);
  493. rc->stats.metrics = metrics;
  494. if(min_priority_collected != LONG_MAX)
  495. // use the collected priority
  496. min_priority = min_priority_collected;
  497. else
  498. // use the non-collected priority
  499. min_priority = min_priority_not_collected;
  500. }
  501. {
  502. bool previous_hidden = rrd_flag_check(rc, RRD_FLAG_HIDDEN);
  503. if (hidden != previous_hidden) {
  504. if (hidden && !rrd_flag_check(rc, RRD_FLAG_HIDDEN))
  505. rrd_flag_set(rc, RRD_FLAG_HIDDEN);
  506. else if (!hidden && rrd_flag_check(rc, RRD_FLAG_HIDDEN))
  507. rrd_flag_clear(rc, RRD_FLAG_HIDDEN);
  508. }
  509. bool previous_live_retention = rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION);
  510. if (live_retention != previous_live_retention) {
  511. if (live_retention && !rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION))
  512. rrd_flag_set(rc, RRD_FLAG_LIVE_RETENTION);
  513. else if (!live_retention && rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION))
  514. rrd_flag_clear(rc, RRD_FLAG_LIVE_RETENTION);
  515. }
  516. }
  517. rrdcontext_lock(rc);
  518. rc->pp.executions++;
  519. if(unlikely(!instances_active)) {
  520. // we had some instances, but they are gone now...
  521. if(rc->first_time_s) {
  522. rc->first_time_s = 0;
  523. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  524. }
  525. if(rc->last_time_s) {
  526. rc->last_time_s = 0;
  527. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  528. }
  529. rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  530. }
  531. else {
  532. // we have some active instances...
  533. if (unlikely(min_first_time_t == LONG_MAX))
  534. min_first_time_t = 0;
  535. if (unlikely(min_first_time_t == 0 && max_last_time_t == 0)) {
  536. if(rc->first_time_s) {
  537. rc->first_time_s = 0;
  538. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  539. }
  540. if(rc->last_time_s) {
  541. rc->last_time_s = 0;
  542. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  543. }
  544. rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  545. }
  546. else {
  547. rrd_flag_clear(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  548. if (unlikely(rc->first_time_s != min_first_time_t)) {
  549. rc->first_time_s = min_first_time_t;
  550. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  551. }
  552. if (rc->last_time_s != max_last_time_t) {
  553. rc->last_time_s = max_last_time_t;
  554. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  555. }
  556. if(likely(currently_collected))
  557. rrd_flag_set_collected(rc);
  558. else
  559. rrd_flag_set_archived(rc);
  560. }
  561. if (min_priority != LONG_MAX && rc->priority != min_priority) {
  562. rc->priority = min_priority;
  563. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_METADATA);
  564. }
  565. }
  566. if(unlikely(rrd_flag_is_updated(rc) && rc->rrdhost->rrdctx.hub_queue)) {
  567. if(check_if_cloud_version_changed_unsafe(rc, false)) {
  568. rc->version = rrdcontext_get_next_version(rc);
  569. dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx.hub_queue,
  570. string2str(rc->id), rc, sizeof(*rc));
  571. }
  572. }
  573. rrd_flag_unset_updated(rc);
  574. rrdcontext_unlock(rc);
  575. }
  576. void rrdcontext_queue_for_post_processing(RRDCONTEXT *rc, const char *function __maybe_unused, RRD_FLAGS flags __maybe_unused) {
  577. if(unlikely(!rc->rrdhost->rrdctx.pp_queue)) return;
  578. if(!rrd_flag_check(rc, RRD_FLAG_QUEUED_FOR_PP)) {
  579. dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx.pp_queue,
  580. string2str(rc->id),
  581. rc,
  582. sizeof(*rc));
  583. #if(defined(NETDATA_INTERNAL_CHECKS) && defined(LOG_POST_PROCESSING_QUEUE_INSERTIONS))
  584. {
  585. BUFFER *wb_flags = buffer_create(1000);
  586. rrd_flags_to_buffer(flags, wb_flags);
  587. BUFFER *wb_reasons = buffer_create(1000);
  588. rrd_reasons_to_buffer(flags, wb_reasons);
  589. internal_error(true, "RRDCONTEXT: '%s' update triggered by function %s(), due to flags: %s, reasons: %s",
  590. string2str(rc->id), function,
  591. buffer_tostring(wb_flags),
  592. buffer_tostring(wb_reasons));
  593. buffer_free(wb_reasons);
  594. buffer_free(wb_flags);
  595. }
  596. #endif
  597. }
  598. }
  599. static void rrdcontext_dequeue_from_post_processing(RRDCONTEXT *rc) {
  600. if(unlikely(!rc->rrdhost->rrdctx.pp_queue)) return;
  601. dictionary_del(rc->rrdhost->rrdctx.pp_queue, string2str(rc->id));
  602. }
  603. static void rrdcontext_post_process_queued_contexts(RRDHOST *host) {
  604. if(unlikely(!host->rrdctx.pp_queue)) return;
  605. RRDCONTEXT *rc;
  606. dfe_start_reentrant(host->rrdctx.pp_queue, rc) {
  607. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  608. rrdcontext_dequeue_from_post_processing(rc);
  609. rrdcontext_post_process_updates(rc, false, RRD_FLAG_NONE, true);
  610. }
  611. dfe_done(rc);
  612. }
  613. // ----------------------------------------------------------------------------
  614. // dispatching contexts to cloud
  615. static uint64_t rrdcontext_get_next_version(RRDCONTEXT *rc) {
  616. time_t now = now_realtime_sec();
  617. uint64_t version = MAX(rc->version, rc->hub.version);
  618. version = MAX((uint64_t)now, version);
  619. version++;
  620. return version;
  621. }
  622. void rrdcontext_message_send_unsafe(RRDCONTEXT *rc, bool snapshot __maybe_unused, void *bundle __maybe_unused) {
  623. // save it, so that we know the last version we sent to hub
  624. rc->version = rc->hub.version = rrdcontext_get_next_version(rc);
  625. rc->hub.id = string2str(rc->id);
  626. rc->hub.title = string2str(rc->title);
  627. rc->hub.units = string2str(rc->units);
  628. rc->hub.family = string2str(rc->family);
  629. rc->hub.chart_type = rrdset_type_name(rc->chart_type);
  630. rc->hub.priority = rc->priority;
  631. rc->hub.first_time_s = rc->first_time_s;
  632. rc->hub.last_time_s = rrd_flag_is_collected(rc) ? 0 : rc->last_time_s;
  633. rc->hub.deleted = rrd_flag_is_deleted(rc) ? true : false;
  634. #ifdef ENABLE_ACLK
  635. struct context_updated message = {
  636. .id = rc->hub.id,
  637. .version = rc->hub.version,
  638. .title = rc->hub.title,
  639. .units = rc->hub.units,
  640. .family = rc->hub.family,
  641. .chart_type = rc->hub.chart_type,
  642. .priority = rc->hub.priority,
  643. .first_entry = rc->hub.first_time_s,
  644. .last_entry = rc->hub.last_time_s,
  645. .deleted = rc->hub.deleted,
  646. };
  647. if(likely(!rrd_flag_check(rc, RRD_FLAG_HIDDEN))) {
  648. if (snapshot) {
  649. if (!rc->hub.deleted)
  650. contexts_snapshot_add_ctx_update(bundle, &message);
  651. }
  652. else
  653. contexts_updated_add_ctx_update(bundle, &message);
  654. }
  655. #endif
  656. // store it to SQL
  657. if(rrd_flag_is_deleted(rc))
  658. rrdcontext_delete_from_sql_unsafe(rc);
  659. else if (ctx_store_context(&rc->rrdhost->host_uuid, &rc->hub) != 0)
  660. error("RRDCONTEXT: failed to save context '%s' version %"PRIu64" to SQL.", rc->hub.id, rc->hub.version);
  661. }
  662. static bool check_if_cloud_version_changed_unsafe(RRDCONTEXT *rc, bool sending __maybe_unused) {
  663. bool id_changed = false,
  664. title_changed = false,
  665. units_changed = false,
  666. family_changed = false,
  667. chart_type_changed = false,
  668. priority_changed = false,
  669. first_time_changed = false,
  670. last_time_changed = false,
  671. deleted_changed = false;
  672. RRD_FLAGS flags = rrd_flags_get(rc);
  673. if(unlikely(string2str(rc->id) != rc->hub.id))
  674. id_changed = true;
  675. if(unlikely(string2str(rc->title) != rc->hub.title))
  676. title_changed = true;
  677. if(unlikely(string2str(rc->units) != rc->hub.units))
  678. units_changed = true;
  679. if(unlikely(string2str(rc->family) != rc->hub.family))
  680. family_changed = true;
  681. if(unlikely(rrdset_type_name(rc->chart_type) != rc->hub.chart_type))
  682. chart_type_changed = true;
  683. if(unlikely(rc->priority != rc->hub.priority))
  684. priority_changed = true;
  685. if(unlikely((uint64_t)rc->first_time_s != rc->hub.first_time_s))
  686. first_time_changed = true;
  687. if(unlikely((uint64_t)((flags & RRD_FLAG_COLLECTED) ? 0 : rc->last_time_s) != rc->hub.last_time_s))
  688. last_time_changed = true;
  689. if(unlikely(((flags & RRD_FLAG_DELETED) ? true : false) != rc->hub.deleted))
  690. deleted_changed = true;
  691. if(unlikely(id_changed || title_changed || units_changed || family_changed || chart_type_changed || priority_changed || first_time_changed || last_time_changed || deleted_changed)) {
  692. internal_error(LOG_TRANSITIONS,
  693. "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)",
  694. sending?"SENDING":"QUEUE",
  695. string2str(rc->id), id_changed ? " (CHANGED)" : "",
  696. rrdhost_hostname(rc->rrdhost),
  697. rc->version,
  698. string2str(rc->title), title_changed ? " (CHANGED)" : "",
  699. string2str(rc->units), units_changed ? " (CHANGED)" : "",
  700. string2str(rc->family), family_changed ? " (CHANGED)" : "",
  701. rrdset_type_name(rc->chart_type), chart_type_changed ? " (CHANGED)" : "",
  702. rc->priority, priority_changed ? " (CHANGED)" : "",
  703. rc->first_time_s, first_time_changed ? " (CHANGED)" : "",
  704. (flags & RRD_FLAG_COLLECTED) ? 0 : rc->last_time_s, last_time_changed ? " (CHANGED)" : "",
  705. (flags & RRD_FLAG_DELETED) ? "true" : "false", deleted_changed ? " (CHANGED)" : "",
  706. sending ? (now_realtime_usec() - rc->queue.queued_ut) / USEC_PER_MS : 0,
  707. sending ? (rc->queue.scheduled_dispatch_ut - rc->queue.queued_ut) / USEC_PER_MS : 0
  708. );
  709. rrdhost_update_cached_retention(rc->rrdhost, rc->first_time_s, rc->last_time_s, false);
  710. return true;
  711. }
  712. if(!(flags & RRD_FLAG_COLLECTED))
  713. rrdhost_update_cached_retention(rc->rrdhost, rc->first_time_s, rc->last_time_s, false);
  714. return false;
  715. }
  716. static inline usec_t rrdcontext_calculate_queued_dispatch_time_ut(RRDCONTEXT *rc, usec_t now_ut) {
  717. if(likely(rc->queue.delay_calc_ut >= rc->queue.queued_ut))
  718. return rc->queue.scheduled_dispatch_ut;
  719. RRD_FLAGS flags = rc->queue.queued_flags;
  720. usec_t delay = LONG_MAX;
  721. int i;
  722. struct rrdcontext_reason *reason;
  723. for(i = 0, reason = &rrdcontext_reasons[i]; reason->name ; reason = &rrdcontext_reasons[++i]) {
  724. if(unlikely(flags & reason->flag)) {
  725. if(reason->delay_ut < delay)
  726. delay = reason->delay_ut;
  727. }
  728. }
  729. if(unlikely(delay == LONG_MAX)) {
  730. internal_error(true, "RRDCONTEXT: '%s', cannot find minimum delay of flags %x", string2str(rc->id), (unsigned int)flags);
  731. delay = 60 * USEC_PER_SEC;
  732. }
  733. rc->queue.delay_calc_ut = now_ut;
  734. usec_t dispatch_ut = rc->queue.scheduled_dispatch_ut = rc->queue.queued_ut + delay;
  735. return dispatch_ut;
  736. }
  737. static void rrdcontext_dequeue_from_hub_queue(RRDCONTEXT *rc) {
  738. dictionary_del(rc->rrdhost->rrdctx.hub_queue, string2str(rc->id));
  739. }
  740. static void rrdcontext_dispatch_queued_contexts_to_hub(RRDHOST *host, usec_t now_ut) {
  741. // check if we have received a streaming command for this host
  742. if(!rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS) || !aclk_connected || !host->rrdctx.hub_queue)
  743. return;
  744. // check if there are queued items to send
  745. if(!dictionary_entries(host->rrdctx.hub_queue))
  746. return;
  747. if(!host->node_id)
  748. return;
  749. size_t messages_added = 0;
  750. contexts_updated_t bundle = NULL;
  751. RRDCONTEXT *rc;
  752. dfe_start_reentrant(host->rrdctx.hub_queue, rc) {
  753. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  754. if(unlikely(messages_added >= MESSAGES_PER_BUNDLE_TO_SEND_TO_HUB_PER_HOST))
  755. break;
  756. worker_is_busy(WORKER_JOB_QUEUED);
  757. usec_t dispatch_ut = rrdcontext_calculate_queued_dispatch_time_ut(rc, now_ut);
  758. char *claim_id = get_agent_claimid();
  759. if(unlikely(now_ut >= dispatch_ut) && claim_id) {
  760. worker_is_busy(WORKER_JOB_CHECK);
  761. rrdcontext_lock(rc);
  762. if(check_if_cloud_version_changed_unsafe(rc, true)) {
  763. worker_is_busy(WORKER_JOB_SEND);
  764. #ifdef ENABLE_ACLK
  765. if(!bundle) {
  766. // prepare the bundle to send the messages
  767. char uuid[UUID_STR_LEN];
  768. uuid_unparse_lower(*host->node_id, uuid);
  769. bundle = contexts_updated_new(claim_id, uuid, 0, now_ut);
  770. }
  771. #endif
  772. // update the hub data of the context, give a new version, pack the message
  773. // and save an update to SQL
  774. rrdcontext_message_send_unsafe(rc, false, bundle);
  775. messages_added++;
  776. rc->queue.dispatches++;
  777. rc->queue.dequeued_ut = now_ut;
  778. }
  779. else
  780. rc->version = rc->hub.version;
  781. // remove it from the queue
  782. worker_is_busy(WORKER_JOB_DEQUEUE);
  783. rrdcontext_dequeue_from_hub_queue(rc);
  784. if(unlikely(rrdcontext_should_be_deleted(rc))) {
  785. // this is a deleted context - delete it forever...
  786. worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  787. rrdcontext_dequeue_from_post_processing(rc);
  788. rrdcontext_delete_from_sql_unsafe(rc);
  789. STRING *id = string_dup(rc->id);
  790. rrdcontext_unlock(rc);
  791. // delete it from the master dictionary
  792. if(!dictionary_del(host->rrdctx.contexts, string2str(rc->id)))
  793. error("RRDCONTEXT: '%s' of host '%s' failed to be deleted from rrdcontext dictionary.",
  794. string2str(id), rrdhost_hostname(host));
  795. string_freez(id);
  796. }
  797. else
  798. rrdcontext_unlock(rc);
  799. }
  800. freez(claim_id);
  801. }
  802. dfe_done(rc);
  803. #ifdef ENABLE_ACLK
  804. if(service_running(SERVICE_CONTEXT) && bundle) {
  805. // we have a bundle to send messages
  806. // update the version hash
  807. contexts_updated_update_version_hash(bundle, rrdcontext_version_hash(host));
  808. // send it
  809. aclk_send_contexts_updated(bundle);
  810. }
  811. else if(bundle)
  812. contexts_updated_delete(bundle);
  813. #endif
  814. }
  815. // ----------------------------------------------------------------------------
  816. // worker thread
  817. static void rrdcontext_main_cleanup(void *ptr) {
  818. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  819. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  820. // custom code
  821. worker_unregister();
  822. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  823. }
  824. void *rrdcontext_main(void *ptr) {
  825. netdata_thread_cleanup_push(rrdcontext_main_cleanup, ptr);
  826. worker_register("RRDCONTEXT");
  827. worker_register_job_name(WORKER_JOB_HOSTS, "hosts");
  828. worker_register_job_name(WORKER_JOB_CHECK, "dedup checks");
  829. worker_register_job_name(WORKER_JOB_SEND, "sent contexts");
  830. worker_register_job_name(WORKER_JOB_DEQUEUE, "deduplicated contexts");
  831. worker_register_job_name(WORKER_JOB_RETENTION, "metrics retention");
  832. worker_register_job_name(WORKER_JOB_QUEUED, "queued contexts");
  833. worker_register_job_name(WORKER_JOB_CLEANUP, "cleanups");
  834. worker_register_job_name(WORKER_JOB_CLEANUP_DELETE, "deletes");
  835. worker_register_job_name(WORKER_JOB_PP_METRIC, "check metrics");
  836. worker_register_job_name(WORKER_JOB_PP_INSTANCE, "check instances");
  837. worker_register_job_name(WORKER_JOB_PP_CONTEXT, "check contexts");
  838. worker_register_job_custom_metric(WORKER_JOB_HUB_QUEUE_SIZE, "hub queue size", "contexts", WORKER_METRIC_ABSOLUTE);
  839. worker_register_job_custom_metric(WORKER_JOB_PP_QUEUE_SIZE, "post processing queue size", "contexts", WORKER_METRIC_ABSOLUTE);
  840. heartbeat_t hb;
  841. heartbeat_init(&hb);
  842. usec_t step = RRDCONTEXT_WORKER_THREAD_HEARTBEAT_USEC;
  843. while (service_running(SERVICE_CONTEXT)) {
  844. worker_is_idle();
  845. heartbeat_next(&hb, step);
  846. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  847. usec_t now_ut = now_realtime_usec();
  848. if(rrdcontext_next_db_rotation_ut && now_ut > rrdcontext_next_db_rotation_ut) {
  849. rrdcontext_recalculate_retention_all_hosts();
  850. rrdcontext_garbage_collect_for_all_hosts();
  851. rrdcontext_next_db_rotation_ut = 0;
  852. }
  853. size_t hub_queued_contexts_for_all_hosts = 0;
  854. size_t pp_queued_contexts_for_all_hosts = 0;
  855. RRDHOST *host;
  856. dfe_start_reentrant(rrdhost_root_index, host) {
  857. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  858. worker_is_busy(WORKER_JOB_HOSTS);
  859. if(host->rrdctx.pp_queue) {
  860. pp_queued_contexts_for_all_hosts += dictionary_entries(host->rrdctx.pp_queue);
  861. rrdcontext_post_process_queued_contexts(host);
  862. dictionary_garbage_collect(host->rrdctx.pp_queue);
  863. }
  864. if(host->rrdctx.hub_queue) {
  865. hub_queued_contexts_for_all_hosts += dictionary_entries(host->rrdctx.hub_queue);
  866. rrdcontext_dispatch_queued_contexts_to_hub(host, now_ut);
  867. dictionary_garbage_collect(host->rrdctx.hub_queue);
  868. }
  869. if (host->rrdctx.contexts)
  870. dictionary_garbage_collect(host->rrdctx.contexts);
  871. // calculate the number of metrics and instances in the host
  872. RRDCONTEXT *rc;
  873. uint32_t metrics = 0, instances = 0;
  874. dfe_start_read(host->rrdctx.contexts, rc) {
  875. metrics += rc->stats.metrics;
  876. instances += dictionary_entries(rc->rrdinstances);
  877. }
  878. dfe_done(rc);
  879. host->rrdctx.metrics = metrics;
  880. host->rrdctx.instances = instances;
  881. }
  882. dfe_done(host);
  883. worker_set_metric(WORKER_JOB_HUB_QUEUE_SIZE, (NETDATA_DOUBLE)hub_queued_contexts_for_all_hosts);
  884. worker_set_metric(WORKER_JOB_PP_QUEUE_SIZE, (NETDATA_DOUBLE)pp_queued_contexts_for_all_hosts);
  885. }
  886. netdata_thread_cleanup_pop(1);
  887. return NULL;
  888. }