worker.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. if(unlikely(!currently_collected && rrd_flag_is_collected(ri) && ri->first_time_s))
  471. currently_collected = true;
  472. internal_error(rc->units != ri->units,
  473. "RRDCONTEXT: '%s' rrdinstance '%s' has different units, context '%s', instance '%s'",
  474. string2str(rc->id), string2str(ri->id),
  475. string2str(rc->units), string2str(ri->units));
  476. instances_active++;
  477. metrics += dictionary_entries(ri->rrdmetrics);
  478. if (ri->priority >= RRDCONTEXT_MINIMUM_ALLOWED_PRIORITY) {
  479. if(rrd_flag_check(ri, RRD_FLAG_COLLECTED)) {
  480. if(ri->priority < min_priority_collected)
  481. min_priority_collected = ri->priority;
  482. }
  483. else {
  484. if(ri->priority < min_priority_not_collected)
  485. min_priority_not_collected = ri->priority;
  486. }
  487. }
  488. if (ri->first_time_s && ri->first_time_s < min_first_time_t)
  489. min_first_time_t = ri->first_time_s;
  490. if (ri->last_time_s && ri->last_time_s > max_last_time_t)
  491. max_last_time_t = ri->last_time_s;
  492. }
  493. dfe_done(ri);
  494. rc->stats.metrics = metrics;
  495. if(min_priority_collected != LONG_MAX)
  496. // use the collected priority
  497. min_priority = min_priority_collected;
  498. else
  499. // use the non-collected priority
  500. min_priority = min_priority_not_collected;
  501. }
  502. {
  503. bool previous_hidden = rrd_flag_check(rc, RRD_FLAG_HIDDEN);
  504. if (hidden != previous_hidden) {
  505. if (hidden && !rrd_flag_check(rc, RRD_FLAG_HIDDEN))
  506. rrd_flag_set(rc, RRD_FLAG_HIDDEN);
  507. else if (!hidden && rrd_flag_check(rc, RRD_FLAG_HIDDEN))
  508. rrd_flag_clear(rc, RRD_FLAG_HIDDEN);
  509. }
  510. bool previous_live_retention = rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION);
  511. if (live_retention != previous_live_retention) {
  512. if (live_retention && !rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION))
  513. rrd_flag_set(rc, RRD_FLAG_LIVE_RETENTION);
  514. else if (!live_retention && rrd_flag_check(rc, RRD_FLAG_LIVE_RETENTION))
  515. rrd_flag_clear(rc, RRD_FLAG_LIVE_RETENTION);
  516. }
  517. }
  518. rrdcontext_lock(rc);
  519. rc->pp.executions++;
  520. if(unlikely(!instances_active)) {
  521. // we had some instances, but they are gone now...
  522. if(rc->first_time_s) {
  523. rc->first_time_s = 0;
  524. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  525. }
  526. if(rc->last_time_s) {
  527. rc->last_time_s = 0;
  528. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  529. }
  530. rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  531. }
  532. else {
  533. // we have some active instances...
  534. if (unlikely(min_first_time_t == LONG_MAX))
  535. min_first_time_t = 0;
  536. if (unlikely(min_first_time_t == 0 && max_last_time_t == 0)) {
  537. if(rc->first_time_s) {
  538. rc->first_time_s = 0;
  539. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  540. }
  541. if(rc->last_time_s) {
  542. rc->last_time_s = 0;
  543. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  544. }
  545. rrd_flag_set_deleted(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  546. }
  547. else {
  548. rrd_flag_clear(rc, RRD_FLAG_UPDATE_REASON_ZERO_RETENTION);
  549. if (unlikely(rc->first_time_s != min_first_time_t)) {
  550. rc->first_time_s = min_first_time_t;
  551. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T);
  552. }
  553. if (rc->last_time_s != max_last_time_t) {
  554. rc->last_time_s = max_last_time_t;
  555. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T);
  556. }
  557. if(likely(currently_collected))
  558. rrd_flag_set_collected(rc);
  559. else
  560. rrd_flag_set_archived(rc);
  561. }
  562. if (min_priority != LONG_MAX && rc->priority != min_priority) {
  563. rc->priority = min_priority;
  564. rrd_flag_set_updated(rc, RRD_FLAG_UPDATE_REASON_CHANGED_METADATA);
  565. }
  566. }
  567. if(unlikely(rrd_flag_is_updated(rc) && rc->rrdhost->rrdctx.hub_queue)) {
  568. if(check_if_cloud_version_changed_unsafe(rc, false)) {
  569. rc->version = rrdcontext_get_next_version(rc);
  570. dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx.hub_queue,
  571. string2str(rc->id), rc, sizeof(*rc));
  572. }
  573. }
  574. rrd_flag_unset_updated(rc);
  575. rrdcontext_unlock(rc);
  576. }
  577. void rrdcontext_queue_for_post_processing(RRDCONTEXT *rc, const char *function __maybe_unused, RRD_FLAGS flags __maybe_unused) {
  578. if(unlikely(!rc->rrdhost->rrdctx.pp_queue)) return;
  579. if(!rrd_flag_check(rc, RRD_FLAG_QUEUED_FOR_PP)) {
  580. dictionary_set((DICTIONARY *)rc->rrdhost->rrdctx.pp_queue,
  581. string2str(rc->id),
  582. rc,
  583. sizeof(*rc));
  584. #if(defined(NETDATA_INTERNAL_CHECKS) && defined(LOG_POST_PROCESSING_QUEUE_INSERTIONS))
  585. {
  586. BUFFER *wb_flags = buffer_create(1000);
  587. rrd_flags_to_buffer(flags, wb_flags);
  588. BUFFER *wb_reasons = buffer_create(1000);
  589. rrd_reasons_to_buffer(flags, wb_reasons);
  590. internal_error(true, "RRDCONTEXT: '%s' update triggered by function %s(), due to flags: %s, reasons: %s",
  591. string2str(rc->id), function,
  592. buffer_tostring(wb_flags),
  593. buffer_tostring(wb_reasons));
  594. buffer_free(wb_reasons);
  595. buffer_free(wb_flags);
  596. }
  597. #endif
  598. }
  599. }
  600. static void rrdcontext_dequeue_from_post_processing(RRDCONTEXT *rc) {
  601. if(unlikely(!rc->rrdhost->rrdctx.pp_queue)) return;
  602. dictionary_del(rc->rrdhost->rrdctx.pp_queue, string2str(rc->id));
  603. }
  604. static void rrdcontext_post_process_queued_contexts(RRDHOST *host) {
  605. if(unlikely(!host->rrdctx.pp_queue)) return;
  606. RRDCONTEXT *rc;
  607. dfe_start_reentrant(host->rrdctx.pp_queue, rc) {
  608. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  609. rrdcontext_dequeue_from_post_processing(rc);
  610. rrdcontext_post_process_updates(rc, false, RRD_FLAG_NONE, true);
  611. }
  612. dfe_done(rc);
  613. }
  614. // ----------------------------------------------------------------------------
  615. // dispatching contexts to cloud
  616. static uint64_t rrdcontext_get_next_version(RRDCONTEXT *rc) {
  617. time_t now = now_realtime_sec();
  618. uint64_t version = MAX(rc->version, rc->hub.version);
  619. version = MAX((uint64_t)now, version);
  620. version++;
  621. return version;
  622. }
  623. void rrdcontext_message_send_unsafe(RRDCONTEXT *rc, bool snapshot __maybe_unused, void *bundle __maybe_unused) {
  624. // save it, so that we know the last version we sent to hub
  625. rc->version = rc->hub.version = rrdcontext_get_next_version(rc);
  626. rc->hub.id = string2str(rc->id);
  627. rc->hub.title = string2str(rc->title);
  628. rc->hub.units = string2str(rc->units);
  629. rc->hub.family = string2str(rc->family);
  630. rc->hub.chart_type = rrdset_type_name(rc->chart_type);
  631. rc->hub.priority = rc->priority;
  632. rc->hub.first_time_s = rc->first_time_s;
  633. rc->hub.last_time_s = rrd_flag_is_collected(rc) ? 0 : rc->last_time_s;
  634. rc->hub.deleted = rrd_flag_is_deleted(rc) ? true : false;
  635. #ifdef ENABLE_ACLK
  636. struct context_updated message = {
  637. .id = rc->hub.id,
  638. .version = rc->hub.version,
  639. .title = rc->hub.title,
  640. .units = rc->hub.units,
  641. .family = rc->hub.family,
  642. .chart_type = rc->hub.chart_type,
  643. .priority = rc->hub.priority,
  644. .first_entry = rc->hub.first_time_s,
  645. .last_entry = rc->hub.last_time_s,
  646. .deleted = rc->hub.deleted,
  647. };
  648. if(likely(!rrd_flag_check(rc, RRD_FLAG_HIDDEN))) {
  649. if (snapshot) {
  650. if (!rc->hub.deleted)
  651. contexts_snapshot_add_ctx_update(bundle, &message);
  652. }
  653. else
  654. contexts_updated_add_ctx_update(bundle, &message);
  655. }
  656. #endif
  657. // store it to SQL
  658. if(rrd_flag_is_deleted(rc))
  659. rrdcontext_delete_from_sql_unsafe(rc);
  660. else if (ctx_store_context(&rc->rrdhost->host_uuid, &rc->hub) != 0)
  661. netdata_log_error("RRDCONTEXT: failed to save context '%s' version %"PRIu64" to SQL.", rc->hub.id, rc->hub.version);
  662. }
  663. static bool check_if_cloud_version_changed_unsafe(RRDCONTEXT *rc, bool sending __maybe_unused) {
  664. bool id_changed = false,
  665. title_changed = false,
  666. units_changed = false,
  667. family_changed = false,
  668. chart_type_changed = false,
  669. priority_changed = false,
  670. first_time_changed = false,
  671. last_time_changed = false,
  672. deleted_changed = false;
  673. RRD_FLAGS flags = rrd_flags_get(rc);
  674. if(unlikely(string2str(rc->id) != rc->hub.id))
  675. id_changed = true;
  676. if(unlikely(string2str(rc->title) != rc->hub.title))
  677. title_changed = true;
  678. if(unlikely(string2str(rc->units) != rc->hub.units))
  679. units_changed = true;
  680. if(unlikely(string2str(rc->family) != rc->hub.family))
  681. family_changed = true;
  682. if(unlikely(rrdset_type_name(rc->chart_type) != rc->hub.chart_type))
  683. chart_type_changed = true;
  684. if(unlikely(rc->priority != rc->hub.priority))
  685. priority_changed = true;
  686. if(unlikely((uint64_t)rc->first_time_s != rc->hub.first_time_s))
  687. first_time_changed = true;
  688. if(unlikely((uint64_t)((flags & RRD_FLAG_COLLECTED) ? 0 : rc->last_time_s) != rc->hub.last_time_s))
  689. last_time_changed = true;
  690. if(unlikely(((flags & RRD_FLAG_DELETED) ? true : false) != rc->hub.deleted))
  691. deleted_changed = true;
  692. if(unlikely(id_changed || title_changed || units_changed || family_changed || chart_type_changed || priority_changed || first_time_changed || last_time_changed || deleted_changed)) {
  693. internal_error(LOG_TRANSITIONS,
  694. "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)",
  695. sending?"SENDING":"QUEUE",
  696. string2str(rc->id), id_changed ? " (CHANGED)" : "",
  697. rrdhost_hostname(rc->rrdhost),
  698. rc->version,
  699. string2str(rc->title), title_changed ? " (CHANGED)" : "",
  700. string2str(rc->units), units_changed ? " (CHANGED)" : "",
  701. string2str(rc->family), family_changed ? " (CHANGED)" : "",
  702. rrdset_type_name(rc->chart_type), chart_type_changed ? " (CHANGED)" : "",
  703. rc->priority, priority_changed ? " (CHANGED)" : "",
  704. rc->first_time_s, first_time_changed ? " (CHANGED)" : "",
  705. (flags & RRD_FLAG_COLLECTED) ? 0 : rc->last_time_s, last_time_changed ? " (CHANGED)" : "",
  706. (flags & RRD_FLAG_DELETED) ? "true" : "false", deleted_changed ? " (CHANGED)" : "",
  707. sending ? (now_realtime_usec() - rc->queue.queued_ut) / USEC_PER_MS : 0,
  708. sending ? (rc->queue.scheduled_dispatch_ut - rc->queue.queued_ut) / USEC_PER_MS : 0
  709. );
  710. rrdhost_update_cached_retention(rc->rrdhost, rc->first_time_s, rc->last_time_s, false);
  711. return true;
  712. }
  713. if(!(flags & RRD_FLAG_COLLECTED))
  714. rrdhost_update_cached_retention(rc->rrdhost, rc->first_time_s, rc->last_time_s, false);
  715. return false;
  716. }
  717. static inline usec_t rrdcontext_calculate_queued_dispatch_time_ut(RRDCONTEXT *rc, usec_t now_ut) {
  718. if(likely(rc->queue.delay_calc_ut >= rc->queue.queued_ut))
  719. return rc->queue.scheduled_dispatch_ut;
  720. RRD_FLAGS flags = rc->queue.queued_flags;
  721. usec_t delay = LONG_MAX;
  722. int i;
  723. struct rrdcontext_reason *reason;
  724. for(i = 0, reason = &rrdcontext_reasons[i]; reason->name ; reason = &rrdcontext_reasons[++i]) {
  725. if(unlikely(flags & reason->flag)) {
  726. if(reason->delay_ut < delay)
  727. delay = reason->delay_ut;
  728. }
  729. }
  730. if(unlikely(delay == LONG_MAX)) {
  731. internal_error(true, "RRDCONTEXT: '%s', cannot find minimum delay of flags %x", string2str(rc->id), (unsigned int)flags);
  732. delay = 60 * USEC_PER_SEC;
  733. }
  734. rc->queue.delay_calc_ut = now_ut;
  735. usec_t dispatch_ut = rc->queue.scheduled_dispatch_ut = rc->queue.queued_ut + delay;
  736. return dispatch_ut;
  737. }
  738. static void rrdcontext_dequeue_from_hub_queue(RRDCONTEXT *rc) {
  739. dictionary_del(rc->rrdhost->rrdctx.hub_queue, string2str(rc->id));
  740. }
  741. static void rrdcontext_dispatch_queued_contexts_to_hub(RRDHOST *host, usec_t now_ut) {
  742. // check if we have received a streaming command for this host
  743. if(!rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS) || !aclk_connected || !host->rrdctx.hub_queue)
  744. return;
  745. // check if there are queued items to send
  746. if(!dictionary_entries(host->rrdctx.hub_queue))
  747. return;
  748. if(!host->node_id)
  749. return;
  750. size_t messages_added = 0;
  751. contexts_updated_t bundle = NULL;
  752. RRDCONTEXT *rc;
  753. dfe_start_reentrant(host->rrdctx.hub_queue, rc) {
  754. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  755. if(unlikely(messages_added >= MESSAGES_PER_BUNDLE_TO_SEND_TO_HUB_PER_HOST))
  756. break;
  757. worker_is_busy(WORKER_JOB_QUEUED);
  758. usec_t dispatch_ut = rrdcontext_calculate_queued_dispatch_time_ut(rc, now_ut);
  759. char *claim_id = get_agent_claimid();
  760. if(unlikely(now_ut >= dispatch_ut) && claim_id) {
  761. worker_is_busy(WORKER_JOB_CHECK);
  762. rrdcontext_lock(rc);
  763. if(check_if_cloud_version_changed_unsafe(rc, true)) {
  764. worker_is_busy(WORKER_JOB_SEND);
  765. #ifdef ENABLE_ACLK
  766. if(!bundle) {
  767. // prepare the bundle to send the messages
  768. char uuid[UUID_STR_LEN];
  769. uuid_unparse_lower(*host->node_id, uuid);
  770. bundle = contexts_updated_new(claim_id, uuid, 0, now_ut);
  771. }
  772. #endif
  773. // update the hub data of the context, give a new version, pack the message
  774. // and save an update to SQL
  775. rrdcontext_message_send_unsafe(rc, false, bundle);
  776. messages_added++;
  777. rc->queue.dispatches++;
  778. rc->queue.dequeued_ut = now_ut;
  779. }
  780. else
  781. rc->version = rc->hub.version;
  782. // remove it from the queue
  783. worker_is_busy(WORKER_JOB_DEQUEUE);
  784. rrdcontext_dequeue_from_hub_queue(rc);
  785. if(unlikely(rrdcontext_should_be_deleted(rc))) {
  786. // this is a deleted context - delete it forever...
  787. worker_is_busy(WORKER_JOB_CLEANUP_DELETE);
  788. rrdcontext_dequeue_from_post_processing(rc);
  789. rrdcontext_delete_from_sql_unsafe(rc);
  790. STRING *id = string_dup(rc->id);
  791. rrdcontext_unlock(rc);
  792. // delete it from the master dictionary
  793. if(!dictionary_del(host->rrdctx.contexts, string2str(rc->id)))
  794. netdata_log_error("RRDCONTEXT: '%s' of host '%s' failed to be deleted from rrdcontext dictionary.",
  795. string2str(id), rrdhost_hostname(host));
  796. string_freez(id);
  797. }
  798. else
  799. rrdcontext_unlock(rc);
  800. }
  801. freez(claim_id);
  802. }
  803. dfe_done(rc);
  804. #ifdef ENABLE_ACLK
  805. if(service_running(SERVICE_CONTEXT) && bundle) {
  806. // we have a bundle to send messages
  807. // update the version hash
  808. contexts_updated_update_version_hash(bundle, rrdcontext_version_hash(host));
  809. // send it
  810. aclk_send_contexts_updated(bundle);
  811. }
  812. else if(bundle)
  813. contexts_updated_delete(bundle);
  814. #endif
  815. }
  816. // ----------------------------------------------------------------------------
  817. // worker thread
  818. static void rrdcontext_main_cleanup(void *ptr) {
  819. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  820. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  821. // custom code
  822. worker_unregister();
  823. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  824. }
  825. void *rrdcontext_main(void *ptr) {
  826. netdata_thread_cleanup_push(rrdcontext_main_cleanup, ptr);
  827. worker_register("RRDCONTEXT");
  828. worker_register_job_name(WORKER_JOB_HOSTS, "hosts");
  829. worker_register_job_name(WORKER_JOB_CHECK, "dedup checks");
  830. worker_register_job_name(WORKER_JOB_SEND, "sent contexts");
  831. worker_register_job_name(WORKER_JOB_DEQUEUE, "deduplicated contexts");
  832. worker_register_job_name(WORKER_JOB_RETENTION, "metrics retention");
  833. worker_register_job_name(WORKER_JOB_QUEUED, "queued contexts");
  834. worker_register_job_name(WORKER_JOB_CLEANUP, "cleanups");
  835. worker_register_job_name(WORKER_JOB_CLEANUP_DELETE, "deletes");
  836. worker_register_job_name(WORKER_JOB_PP_METRIC, "check metrics");
  837. worker_register_job_name(WORKER_JOB_PP_INSTANCE, "check instances");
  838. worker_register_job_name(WORKER_JOB_PP_CONTEXT, "check contexts");
  839. worker_register_job_custom_metric(WORKER_JOB_HUB_QUEUE_SIZE, "hub queue size", "contexts", WORKER_METRIC_ABSOLUTE);
  840. worker_register_job_custom_metric(WORKER_JOB_PP_QUEUE_SIZE, "post processing queue size", "contexts", WORKER_METRIC_ABSOLUTE);
  841. heartbeat_t hb;
  842. heartbeat_init(&hb);
  843. usec_t step = RRDCONTEXT_WORKER_THREAD_HEARTBEAT_USEC;
  844. while (service_running(SERVICE_CONTEXT)) {
  845. worker_is_idle();
  846. heartbeat_next(&hb, step);
  847. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  848. usec_t now_ut = now_realtime_usec();
  849. if(rrdcontext_next_db_rotation_ut && now_ut > rrdcontext_next_db_rotation_ut) {
  850. rrdcontext_recalculate_retention_all_hosts();
  851. rrdcontext_garbage_collect_for_all_hosts();
  852. rrdcontext_next_db_rotation_ut = 0;
  853. }
  854. size_t hub_queued_contexts_for_all_hosts = 0;
  855. size_t pp_queued_contexts_for_all_hosts = 0;
  856. RRDHOST *host;
  857. dfe_start_reentrant(rrdhost_root_index, host) {
  858. if(unlikely(!service_running(SERVICE_CONTEXT))) break;
  859. worker_is_busy(WORKER_JOB_HOSTS);
  860. if(host->rrdctx.pp_queue) {
  861. pp_queued_contexts_for_all_hosts += dictionary_entries(host->rrdctx.pp_queue);
  862. rrdcontext_post_process_queued_contexts(host);
  863. dictionary_garbage_collect(host->rrdctx.pp_queue);
  864. }
  865. if(host->rrdctx.hub_queue) {
  866. hub_queued_contexts_for_all_hosts += dictionary_entries(host->rrdctx.hub_queue);
  867. rrdcontext_dispatch_queued_contexts_to_hub(host, now_ut);
  868. dictionary_garbage_collect(host->rrdctx.hub_queue);
  869. }
  870. if (host->rrdctx.contexts)
  871. dictionary_garbage_collect(host->rrdctx.contexts);
  872. // calculate the number of metrics and instances in the host
  873. RRDCONTEXT *rc;
  874. uint32_t metrics = 0, instances = 0;
  875. dfe_start_read(host->rrdctx.contexts, rc) {
  876. metrics += rc->stats.metrics;
  877. instances += dictionary_entries(rc->rrdinstances);
  878. }
  879. dfe_done(rc);
  880. host->rrdctx.metrics = metrics;
  881. host->rrdctx.instances = instances;
  882. }
  883. dfe_done(host);
  884. worker_set_metric(WORKER_JOB_HUB_QUEUE_SIZE, (NETDATA_DOUBLE)hub_queued_contexts_for_all_hosts);
  885. worker_set_metric(WORKER_JOB_PP_QUEUE_SIZE, (NETDATA_DOUBLE)pp_queued_contexts_for_all_hosts);
  886. }
  887. netdata_thread_cleanup_pop(1);
  888. return NULL;
  889. }