worker.c 41 KB

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