query_target.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "internal.h"
  3. #define QUERY_TARGET_MAX_REALLOC_INCREASE 500
  4. #define query_target_realloc_size(size, start) \
  5. (size) ? ((size) < QUERY_TARGET_MAX_REALLOC_INCREASE ? (size) * 2 : (size) + QUERY_TARGET_MAX_REALLOC_INCREASE) : (start);
  6. static void query_metric_release(QUERY_TARGET *qt, QUERY_METRIC *qm);
  7. static void query_dimension_release(QUERY_DIMENSION *qd);
  8. static void query_instance_release(QUERY_INSTANCE *qi);
  9. static void query_context_release(QUERY_CONTEXT *qc);
  10. static void query_node_release(QUERY_NODE *qn);
  11. static __thread QUERY_TARGET *thread_qt = NULL;
  12. static struct {
  13. struct {
  14. SPINLOCK spinlock;
  15. size_t count;
  16. QUERY_TARGET *base;
  17. } available;
  18. struct {
  19. SPINLOCK spinlock;
  20. size_t count;
  21. QUERY_TARGET *base;
  22. } used;
  23. } query_target_base = {
  24. .available = {
  25. .spinlock = NETDATA_SPINLOCK_INITIALIZER,
  26. .base = NULL,
  27. .count = 0,
  28. },
  29. .used = {
  30. .spinlock = NETDATA_SPINLOCK_INITIALIZER,
  31. .base = NULL,
  32. .count = 0,
  33. },
  34. };
  35. static void query_target_destroy(QUERY_TARGET *qt) {
  36. __atomic_sub_fetch(&netdata_buffers_statistics.query_targets_size, qt->query.size * sizeof(*qt->query.array), __ATOMIC_RELAXED);
  37. freez(qt->query.array);
  38. __atomic_sub_fetch(&netdata_buffers_statistics.query_targets_size, qt->dimensions.size * sizeof(*qt->dimensions.array), __ATOMIC_RELAXED);
  39. freez(qt->dimensions.array);
  40. __atomic_sub_fetch(&netdata_buffers_statistics.query_targets_size, qt->instances.size * sizeof(*qt->instances.array), __ATOMIC_RELAXED);
  41. freez(qt->instances.array);
  42. __atomic_sub_fetch(&netdata_buffers_statistics.query_targets_size, qt->contexts.size * sizeof(*qt->contexts.array), __ATOMIC_RELAXED);
  43. freez(qt->contexts.array);
  44. __atomic_sub_fetch(&netdata_buffers_statistics.query_targets_size, qt->nodes.size * sizeof(*qt->nodes.array), __ATOMIC_RELAXED);
  45. freez(qt->nodes.array);
  46. freez(qt);
  47. }
  48. void query_target_release(QUERY_TARGET *qt) {
  49. if(unlikely(!qt)) return;
  50. internal_fatal(!qt->internal.used, "QUERY TARGET: qt to be released is not used");
  51. simple_pattern_free(qt->nodes.scope_pattern);
  52. qt->nodes.scope_pattern = NULL;
  53. simple_pattern_free(qt->nodes.pattern);
  54. qt->nodes.pattern = NULL;
  55. simple_pattern_free(qt->contexts.scope_pattern);
  56. qt->contexts.scope_pattern = NULL;
  57. simple_pattern_free(qt->contexts.pattern);
  58. qt->contexts.pattern = NULL;
  59. simple_pattern_free(qt->instances.pattern);
  60. qt->instances.pattern = NULL;
  61. simple_pattern_free(qt->instances.chart_label_key_pattern);
  62. qt->instances.chart_label_key_pattern = NULL;
  63. simple_pattern_free(qt->instances.labels_pattern);
  64. qt->instances.labels_pattern = NULL;
  65. simple_pattern_free(qt->query.pattern);
  66. qt->query.pattern = NULL;
  67. // release the query
  68. for(size_t i = 0, used = qt->query.used; i < used ;i++) {
  69. QUERY_METRIC *qm = query_metric(qt, i);
  70. query_metric_release(qt, qm);
  71. }
  72. qt->query.used = 0;
  73. // release the dimensions
  74. for(size_t i = 0, used = qt->dimensions.used; i < used ; i++) {
  75. QUERY_DIMENSION *qd = query_dimension(qt, i);
  76. query_dimension_release(qd);
  77. }
  78. qt->dimensions.used = 0;
  79. // release the instances
  80. for(size_t i = 0, used = qt->instances.used; i < used ;i++) {
  81. QUERY_INSTANCE *qi = query_instance(qt, i);
  82. query_instance_release(qi);
  83. }
  84. qt->instances.used = 0;
  85. // release the contexts
  86. for(size_t i = 0, used = qt->contexts.used; i < used ;i++) {
  87. QUERY_CONTEXT *qc = query_context(qt, i);
  88. rrdcontext_release(qc->rca);
  89. qc->rca = NULL;
  90. }
  91. qt->contexts.used = 0;
  92. // release the nodes
  93. for(size_t i = 0, used = qt->nodes.used; i < used ; i++) {
  94. QUERY_NODE *qn = query_node(qt, i);
  95. query_node_release(qn);
  96. }
  97. qt->nodes.used = 0;
  98. qt->db.minimum_latest_update_every_s = 0;
  99. qt->db.first_time_s = 0;
  100. qt->db.last_time_s = 0;
  101. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++)
  102. qt->group_by[g].used = 0;
  103. qt->id[0] = '\0';
  104. netdata_spinlock_lock(&query_target_base.used.spinlock);
  105. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(query_target_base.used.base, qt, internal.prev, internal.next);
  106. query_target_base.used.count--;
  107. netdata_spinlock_unlock(&query_target_base.used.spinlock);
  108. qt->internal.used = false;
  109. thread_qt = NULL;
  110. if (qt->internal.queries > 1000) {
  111. query_target_destroy(qt);
  112. }
  113. else {
  114. netdata_spinlock_lock(&query_target_base.available.spinlock);
  115. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(query_target_base.available.base, qt, internal.prev, internal.next);
  116. query_target_base.available.count++;
  117. netdata_spinlock_unlock(&query_target_base.available.spinlock);
  118. }
  119. }
  120. static QUERY_TARGET *query_target_get(void) {
  121. netdata_spinlock_lock(&query_target_base.available.spinlock);
  122. QUERY_TARGET *qt = query_target_base.available.base;
  123. if (qt) {
  124. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(query_target_base.available.base, qt, internal.prev, internal.next);
  125. query_target_base.available.count--;
  126. }
  127. netdata_spinlock_unlock(&query_target_base.available.spinlock);
  128. if(unlikely(!qt))
  129. qt = callocz(1, sizeof(*qt));
  130. netdata_spinlock_lock(&query_target_base.used.spinlock);
  131. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(query_target_base.used.base, qt, internal.prev, internal.next);
  132. query_target_base.used.count++;
  133. netdata_spinlock_unlock(&query_target_base.used.spinlock);
  134. qt->internal.used = true;
  135. qt->internal.queries++;
  136. thread_qt = qt;
  137. return qt;
  138. }
  139. // this is used to release a query target from a cancelled thread
  140. void query_target_free(void) {
  141. query_target_release(thread_qt);
  142. }
  143. // ----------------------------------------------------------------------------
  144. // query API
  145. typedef struct query_target_locals {
  146. time_t start_s;
  147. QUERY_TARGET *qt;
  148. RRDSET *st;
  149. const char *scope_nodes;
  150. const char *scope_contexts;
  151. const char *nodes;
  152. const char *contexts;
  153. const char *instances;
  154. const char *dimensions;
  155. const char *chart_label_key;
  156. const char *labels;
  157. const char *alerts;
  158. long long after;
  159. long long before;
  160. bool match_ids;
  161. bool match_names;
  162. size_t metrics_skipped_due_to_not_matching_timeframe;
  163. char host_node_id_str[UUID_STR_LEN];
  164. QUERY_NODE *qn; // temp to pass on callbacks, ignore otherwise - no need to free
  165. } QUERY_TARGET_LOCALS;
  166. struct storage_engine *query_metric_storage_engine(QUERY_TARGET *qt, QUERY_METRIC *qm, size_t tier) {
  167. QUERY_NODE *qn = query_node(qt, qm->link.query_node_id);
  168. return qn->rrdhost->db[tier].eng;
  169. }
  170. static inline void query_metric_release(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  171. qm->plan.used = 0;
  172. // reset the tiers
  173. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  174. if(qm->tiers[tier].db_metric_handle) {
  175. STORAGE_ENGINE *eng = query_metric_storage_engine(qt, qm, tier);
  176. eng->api.metric_release(qm->tiers[tier].db_metric_handle);
  177. qm->tiers[tier].db_metric_handle = NULL;
  178. }
  179. }
  180. }
  181. static bool query_metric_add(QUERY_TARGET_LOCALS *qtl, QUERY_NODE *qn, QUERY_CONTEXT *qc,
  182. QUERY_INSTANCE *qi, size_t qd_slot, RRDMETRIC *rm, RRDR_DIMENSION_FLAGS options) {
  183. QUERY_TARGET *qt = qtl->qt;
  184. RRDINSTANCE *ri = rm->ri;
  185. time_t common_first_time_s = 0;
  186. time_t common_last_time_s = 0;
  187. time_t common_update_every_s = 0;
  188. size_t tiers_added = 0;
  189. struct {
  190. STORAGE_ENGINE *eng;
  191. STORAGE_METRIC_HANDLE *db_metric_handle;
  192. time_t db_first_time_s;
  193. time_t db_last_time_s;
  194. time_t db_update_every_s;
  195. } tier_retention[storage_tiers];
  196. for (size_t tier = 0; tier < storage_tiers; tier++) {
  197. STORAGE_ENGINE *eng = qn->rrdhost->db[tier].eng;
  198. tier_retention[tier].eng = eng;
  199. tier_retention[tier].db_update_every_s = (time_t) (qn->rrdhost->db[tier].tier_grouping * ri->update_every_s);
  200. if(rm->rrddim && rm->rrddim->tiers[tier].db_metric_handle)
  201. tier_retention[tier].db_metric_handle = eng->api.metric_dup(rm->rrddim->tiers[tier].db_metric_handle);
  202. else
  203. tier_retention[tier].db_metric_handle = eng->api.metric_get(qn->rrdhost->db[tier].instance, &rm->uuid);
  204. if(tier_retention[tier].db_metric_handle) {
  205. tier_retention[tier].db_first_time_s = storage_engine_oldest_time_s(tier_retention[tier].eng->backend, tier_retention[tier].db_metric_handle);
  206. tier_retention[tier].db_last_time_s = storage_engine_latest_time_s(tier_retention[tier].eng->backend, tier_retention[tier].db_metric_handle);
  207. if(!common_first_time_s)
  208. common_first_time_s = tier_retention[tier].db_first_time_s;
  209. else if(tier_retention[tier].db_first_time_s)
  210. common_first_time_s = MIN(common_first_time_s, tier_retention[tier].db_first_time_s);
  211. if(!common_last_time_s)
  212. common_last_time_s = tier_retention[tier].db_last_time_s;
  213. else
  214. common_last_time_s = MAX(common_last_time_s, tier_retention[tier].db_last_time_s);
  215. if(!common_update_every_s)
  216. common_update_every_s = tier_retention[tier].db_update_every_s;
  217. else if(tier_retention[tier].db_update_every_s)
  218. common_update_every_s = MIN(common_update_every_s, tier_retention[tier].db_update_every_s);
  219. tiers_added++;
  220. }
  221. else {
  222. tier_retention[tier].db_first_time_s = 0;
  223. tier_retention[tier].db_last_time_s = 0;
  224. tier_retention[tier].db_update_every_s = 0;
  225. }
  226. }
  227. for (size_t tier = 0; tier < storage_tiers; tier++) {
  228. if(!qt->db.tiers[tier].update_every || (tier_retention[tier].db_update_every_s && tier_retention[tier].db_update_every_s < qt->db.tiers[tier].update_every))
  229. qt->db.tiers[tier].update_every = tier_retention[tier].db_update_every_s;
  230. if(!qt->db.tiers[tier].retention.first_time_s || (tier_retention[tier].db_first_time_s && tier_retention[tier].db_first_time_s < qt->db.tiers[tier].retention.first_time_s))
  231. qt->db.tiers[tier].retention.first_time_s = tier_retention[tier].db_first_time_s;
  232. if(!qt->db.tiers[tier].retention.last_time_s || (tier_retention[tier].db_last_time_s && tier_retention[tier].db_last_time_s > qt->db.tiers[tier].retention.last_time_s))
  233. qt->db.tiers[tier].retention.last_time_s = tier_retention[tier].db_last_time_s;
  234. }
  235. bool timeframe_matches =
  236. (tiers_added &&
  237. query_matches_retention(qt->window.after, qt->window.before, common_first_time_s, common_last_time_s, common_update_every_s))
  238. ? true : false;
  239. if(timeframe_matches) {
  240. if(ri->rrdset)
  241. ri->rrdset->last_accessed_time_s = qtl->start_s;
  242. if (qt->query.used == qt->query.size) {
  243. size_t old_mem = qt->query.size * sizeof(*qt->query.array);
  244. qt->query.size = query_target_realloc_size(qt->query.size, 4);
  245. size_t new_mem = qt->query.size * sizeof(*qt->query.array);
  246. qt->query.array = reallocz(qt->query.array, new_mem);
  247. __atomic_add_fetch(&netdata_buffers_statistics.query_targets_size, new_mem - old_mem, __ATOMIC_RELAXED);
  248. }
  249. QUERY_METRIC *qm = &qt->query.array[qt->query.used++];
  250. memset(qm, 0, sizeof(*qm));
  251. qm->status = options;
  252. qm->link.query_node_id = qn->slot;
  253. qm->link.query_context_id = qc->slot;
  254. qm->link.query_instance_id = qi->slot;
  255. qm->link.query_dimension_id = qd_slot;
  256. if (!qt->db.first_time_s || common_first_time_s < qt->db.first_time_s)
  257. qt->db.first_time_s = common_first_time_s;
  258. if (!qt->db.last_time_s || common_last_time_s > qt->db.last_time_s)
  259. qt->db.last_time_s = common_last_time_s;
  260. for (size_t tier = 0; tier < storage_tiers; tier++) {
  261. internal_fatal(tier_retention[tier].eng != query_metric_storage_engine(qt, qm, tier), "QUERY TARGET: storage engine mismatch");
  262. qm->tiers[tier].db_metric_handle = tier_retention[tier].db_metric_handle;
  263. qm->tiers[tier].db_first_time_s = tier_retention[tier].db_first_time_s;
  264. qm->tiers[tier].db_last_time_s = tier_retention[tier].db_last_time_s;
  265. qm->tiers[tier].db_update_every_s = tier_retention[tier].db_update_every_s;
  266. }
  267. return true;
  268. }
  269. // cleanup anything we allocated to the retention we will not use
  270. for(size_t tier = 0; tier < storage_tiers ;tier++) {
  271. if (tier_retention[tier].db_metric_handle)
  272. tier_retention[tier].eng->api.metric_release(tier_retention[tier].db_metric_handle);
  273. }
  274. return false;
  275. }
  276. static inline bool rrdmetric_retention_matches_query(QUERY_TARGET *qt, RRDMETRIC *rm, time_t now_s) {
  277. time_t first_time_s = rm->first_time_s;
  278. time_t last_time_s = rrd_flag_is_collected(rm) ? now_s : rm->last_time_s;
  279. time_t update_every_s = rm->ri->update_every_s;
  280. return query_matches_retention(qt->window.after, qt->window.before, first_time_s, last_time_s, update_every_s);
  281. }
  282. static inline void query_dimension_release(QUERY_DIMENSION *qd) {
  283. rrdmetric_release(qd->rma);
  284. qd->rma = NULL;
  285. }
  286. static QUERY_DIMENSION *query_dimension_allocate(QUERY_TARGET *qt, RRDMETRIC_ACQUIRED *rma, QUERY_STATUS status, size_t priority) {
  287. if(qt->dimensions.used == qt->dimensions.size) {
  288. size_t old_mem = qt->dimensions.size * sizeof(*qt->dimensions.array);
  289. qt->dimensions.size = query_target_realloc_size(qt->dimensions.size, 4);
  290. size_t new_mem = qt->dimensions.size * sizeof(*qt->dimensions.array);
  291. qt->dimensions.array = reallocz(qt->dimensions.array, new_mem);
  292. __atomic_add_fetch(&netdata_buffers_statistics.query_targets_size, new_mem - old_mem, __ATOMIC_RELAXED);
  293. }
  294. QUERY_DIMENSION *qd = &qt->dimensions.array[qt->dimensions.used];
  295. memset(qd, 0, sizeof(*qd));
  296. qd->slot = qt->dimensions.used++;
  297. qd->rma = rrdmetric_acquired_dup(rma);
  298. qd->status = status;
  299. qd->priority = priority;
  300. return qd;
  301. }
  302. static bool query_dimension_add(QUERY_TARGET_LOCALS *qtl, QUERY_NODE *qn, QUERY_CONTEXT *qc, QUERY_INSTANCE *qi,
  303. RRDMETRIC_ACQUIRED *rma, bool queryable_instance, size_t *metrics_added, size_t priority) {
  304. QUERY_TARGET *qt = qtl->qt;
  305. RRDMETRIC *rm = rrdmetric_acquired_value(rma);
  306. if(rrd_flag_is_deleted(rm))
  307. return false;
  308. QUERY_STATUS status = QUERY_STATUS_NONE;
  309. bool undo = false;
  310. if(!queryable_instance) {
  311. if(rrdmetric_retention_matches_query(qt, rm, qtl->start_s)) {
  312. qi->metrics.excluded++;
  313. qc->metrics.excluded++;
  314. qn->metrics.excluded++;
  315. status |= QUERY_STATUS_EXCLUDED;
  316. }
  317. else
  318. undo = true;
  319. }
  320. else {
  321. RRDR_DIMENSION_FLAGS options = RRDR_DIMENSION_DEFAULT;
  322. bool needed = false;
  323. if (qt->query.pattern) {
  324. // the user asked for specific dimensions
  325. SIMPLE_PATTERN_RESULT ret = SP_NOT_MATCHED;
  326. if(qtl->match_ids)
  327. ret = simple_pattern_matches_string_extract(qt->query.pattern, rm->id, NULL, 0);
  328. if(ret == SP_NOT_MATCHED && qtl->match_names && (rm->name != rm->id || !qtl->match_ids))
  329. ret = simple_pattern_matches_string_extract(qt->query.pattern, rm->name, NULL, 0);
  330. if(ret == SP_MATCHED_POSITIVE) {
  331. needed = true;
  332. options |= RRDR_DIMENSION_SELECTED | RRDR_DIMENSION_NONZERO;
  333. }
  334. else {
  335. // the user selection does not match this dimension
  336. // but, we may still need to query it
  337. if (query_target_needs_all_dimensions(qt)) {
  338. // this is percentage calculation
  339. // so, we need this dimension to calculate the percentage
  340. needed = true;
  341. options |= RRDR_DIMENSION_HIDDEN;
  342. }
  343. else {
  344. // the user did not select this dimension
  345. // and the calculation is not percentage
  346. // so, no need to query it
  347. ;
  348. }
  349. }
  350. }
  351. else {
  352. // we don't have a dimensions pattern
  353. // so this is a selected dimension
  354. // if it is not hidden
  355. if(rrd_flag_check(rm, RRD_FLAG_HIDDEN) || (rm->rrddim && rrddim_option_check(rm->rrddim, RRDDIM_OPTION_HIDDEN))) {
  356. // this is a hidden dimension
  357. // we don't need to query it
  358. status |= QUERY_STATUS_DIMENSION_HIDDEN;
  359. options |= RRDR_DIMENSION_HIDDEN;
  360. if (query_target_needs_all_dimensions(qt)) {
  361. // this is percentage calculation
  362. // so, we need this dimension to calculate the percentage
  363. needed = true;
  364. }
  365. }
  366. else {
  367. // this is a not hidden dimension
  368. // and the user did not provide any selection for dimensions
  369. // so, we need to query it
  370. needed = true;
  371. options |= RRDR_DIMENSION_SELECTED;
  372. }
  373. }
  374. if (needed) {
  375. if(query_metric_add(qtl, qn, qc, qi, qt->dimensions.used, rm, options)) {
  376. (*metrics_added)++;
  377. qi->metrics.selected++;
  378. qc->metrics.selected++;
  379. qn->metrics.selected++;
  380. }
  381. else {
  382. undo = true;
  383. qtl->metrics_skipped_due_to_not_matching_timeframe++;
  384. }
  385. }
  386. else if(rrdmetric_retention_matches_query(qt, rm, qtl->start_s)) {
  387. qi->metrics.excluded++;
  388. qc->metrics.excluded++;
  389. qn->metrics.excluded++;
  390. status |= QUERY_STATUS_EXCLUDED;
  391. }
  392. else
  393. undo = true;
  394. }
  395. if(undo)
  396. return false;
  397. query_dimension_allocate(qt, rma, status, priority);
  398. return true;
  399. }
  400. static inline STRING *rrdinstance_create_id_fqdn_v1(RRDINSTANCE_ACQUIRED *ria) {
  401. if(unlikely(!ria))
  402. return NULL;
  403. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  404. return string_dup(ri->id);
  405. }
  406. static inline STRING *rrdinstance_create_name_fqdn_v1(RRDINSTANCE_ACQUIRED *ria) {
  407. if(unlikely(!ria))
  408. return NULL;
  409. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  410. return string_dup(ri->name);
  411. }
  412. static inline STRING *rrdinstance_create_id_fqdn_v2(RRDINSTANCE_ACQUIRED *ria) {
  413. if(unlikely(!ria))
  414. return NULL;
  415. char buffer[RRD_ID_LENGTH_MAX + 1];
  416. RRDHOST *host = rrdinstance_acquired_rrdhost(ria);
  417. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s@%s", rrdinstance_acquired_id(ria), host->machine_guid);
  418. return string_strdupz(buffer);
  419. }
  420. static inline STRING *rrdinstance_create_name_fqdn_v2(RRDINSTANCE_ACQUIRED *ria) {
  421. if(unlikely(!ria))
  422. return NULL;
  423. char buffer[RRD_ID_LENGTH_MAX + 1];
  424. RRDHOST *host = rrdinstance_acquired_rrdhost(ria);
  425. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s@%s", rrdinstance_acquired_name(ria), rrdhost_hostname(host));
  426. return string_strdupz(buffer);
  427. }
  428. inline STRING *query_instance_id_fqdn(QUERY_INSTANCE *qi, size_t version) {
  429. if(!qi->id_fqdn) {
  430. if (version <= 1)
  431. qi->id_fqdn = rrdinstance_create_id_fqdn_v1(qi->ria);
  432. else
  433. qi->id_fqdn = rrdinstance_create_id_fqdn_v2(qi->ria);
  434. }
  435. return qi->id_fqdn;
  436. }
  437. inline STRING *query_instance_name_fqdn(QUERY_INSTANCE *qi, size_t version) {
  438. if(!qi->name_fqdn) {
  439. if (version <= 1)
  440. qi->name_fqdn = rrdinstance_create_name_fqdn_v1(qi->ria);
  441. else
  442. qi->name_fqdn = rrdinstance_create_name_fqdn_v2(qi->ria);
  443. }
  444. return qi->name_fqdn;
  445. }
  446. RRDSET *rrdinstance_acquired_rrdset(RRDINSTANCE_ACQUIRED *ria) {
  447. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  448. return ri->rrdset;
  449. }
  450. const char *rrdcontext_acquired_units(RRDCONTEXT_ACQUIRED *rca) {
  451. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  452. return string2str(rc->units);
  453. }
  454. RRDSET_TYPE rrdcontext_acquired_chart_type(RRDCONTEXT_ACQUIRED *rca) {
  455. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  456. return rc->chart_type;
  457. }
  458. const char *rrdcontext_acquired_title(RRDCONTEXT_ACQUIRED *rca) {
  459. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  460. return string2str(rc->title);
  461. }
  462. static void query_target_eval_instance_rrdcalc(QUERY_TARGET_LOCALS *qtl __maybe_unused,
  463. QUERY_NODE *qn, QUERY_CONTEXT *qc, QUERY_INSTANCE *qi) {
  464. RRDSET *st = rrdinstance_acquired_rrdset(qi->ria);
  465. if (st) {
  466. netdata_rwlock_rdlock(&st->alerts.rwlock);
  467. for (RRDCALC *rc = st->alerts.base; rc; rc = rc->next) {
  468. switch(rc->status) {
  469. case RRDCALC_STATUS_CLEAR:
  470. qi->alerts.clear++;
  471. qc->alerts.clear++;
  472. qn->alerts.clear++;
  473. break;
  474. case RRDCALC_STATUS_WARNING:
  475. qi->alerts.warning++;
  476. qc->alerts.warning++;
  477. qn->alerts.warning++;
  478. break;
  479. case RRDCALC_STATUS_CRITICAL:
  480. qi->alerts.critical++;
  481. qc->alerts.critical++;
  482. qn->alerts.critical++;
  483. break;
  484. default:
  485. case RRDCALC_STATUS_UNINITIALIZED:
  486. case RRDCALC_STATUS_UNDEFINED:
  487. case RRDCALC_STATUS_REMOVED:
  488. qi->alerts.other++;
  489. qc->alerts.other++;
  490. qn->alerts.other++;
  491. break;
  492. }
  493. }
  494. netdata_rwlock_unlock(&st->alerts.rwlock);
  495. }
  496. }
  497. static bool query_target_match_alert_pattern(RRDINSTANCE_ACQUIRED *ria, SIMPLE_PATTERN *pattern) {
  498. if(!pattern)
  499. return true;
  500. RRDSET *st = rrdinstance_acquired_rrdset(ria);
  501. if (!st)
  502. return false;
  503. BUFFER *wb = NULL;
  504. bool matched = false;
  505. netdata_rwlock_rdlock(&st->alerts.rwlock);
  506. if (st->alerts.base) {
  507. for (RRDCALC *rc = st->alerts.base; rc; rc = rc->next) {
  508. SIMPLE_PATTERN_RESULT ret = simple_pattern_matches_string_extract(pattern, rc->name, NULL, 0);
  509. if(ret == SP_MATCHED_POSITIVE) {
  510. matched = true;
  511. break;
  512. }
  513. else if(ret == SP_MATCHED_NEGATIVE)
  514. break;
  515. if (!wb)
  516. wb = buffer_create(0, NULL);
  517. else
  518. buffer_flush(wb);
  519. buffer_fast_strcat(wb, string2str(rc->name), string_strlen(rc->name));
  520. buffer_fast_strcat(wb, ":", 1);
  521. buffer_strcat(wb, rrdcalc_status2string(rc->status));
  522. ret = simple_pattern_matches_buffer_extract(pattern, wb, NULL, 0);
  523. if(ret == SP_MATCHED_POSITIVE) {
  524. matched = true;
  525. break;
  526. }
  527. else if(ret == SP_MATCHED_NEGATIVE)
  528. break;
  529. }
  530. }
  531. netdata_rwlock_unlock(&st->alerts.rwlock);
  532. buffer_free(wb);
  533. return matched;
  534. }
  535. static inline void query_instance_release(QUERY_INSTANCE *qi) {
  536. if(qi->ria) {
  537. rrdinstance_release(qi->ria);
  538. qi->ria = NULL;
  539. }
  540. string_freez(qi->id_fqdn);
  541. qi->id_fqdn = NULL;
  542. string_freez(qi->name_fqdn);
  543. qi->name_fqdn = NULL;
  544. }
  545. static inline QUERY_INSTANCE *query_instance_allocate(QUERY_TARGET *qt, RRDINSTANCE_ACQUIRED *ria, size_t qn_slot) {
  546. if(qt->instances.used == qt->instances.size) {
  547. size_t old_mem = qt->instances.size * sizeof(*qt->instances.array);
  548. qt->instances.size = query_target_realloc_size(qt->instances.size, 2);
  549. size_t new_mem = qt->instances.size * sizeof(*qt->instances.array);
  550. qt->instances.array = reallocz(qt->instances.array, new_mem);
  551. __atomic_add_fetch(&netdata_buffers_statistics.query_targets_size, new_mem - old_mem, __ATOMIC_RELAXED);
  552. }
  553. QUERY_INSTANCE *qi = &qt->instances.array[qt->instances.used];
  554. memset(qi, 0, sizeof(*qi));
  555. qi->slot = qt->instances.used;
  556. qt->instances.used++;
  557. qi->ria = rrdinstance_acquired_dup(ria);
  558. qi->query_host_id = qn_slot;
  559. return qi;
  560. }
  561. static inline SIMPLE_PATTERN_RESULT query_instance_matches(QUERY_INSTANCE *qi,
  562. RRDINSTANCE *ri,
  563. SIMPLE_PATTERN *instances_sp,
  564. bool match_ids,
  565. bool match_names,
  566. size_t version,
  567. char *host_node_id_str) {
  568. SIMPLE_PATTERN_RESULT ret = SP_MATCHED_POSITIVE;
  569. if(instances_sp) {
  570. ret = SP_NOT_MATCHED;
  571. if(match_ids)
  572. ret = simple_pattern_matches_string_extract(instances_sp, ri->id, NULL, 0);
  573. if (ret == SP_NOT_MATCHED && match_names && (ri->name != ri->id || !match_ids))
  574. ret = simple_pattern_matches_string_extract(instances_sp, ri->name, NULL, 0);
  575. if (ret == SP_NOT_MATCHED && match_ids)
  576. ret = simple_pattern_matches_string_extract(instances_sp, query_instance_id_fqdn(qi, version), NULL, 0);
  577. if (ret == SP_NOT_MATCHED && match_names)
  578. ret = simple_pattern_matches_string_extract(instances_sp, query_instance_name_fqdn(qi, version), NULL, 0);
  579. if (ret == SP_NOT_MATCHED && match_ids && host_node_id_str[0]) {
  580. char buffer[RRD_ID_LENGTH_MAX + 1];
  581. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s@%s", rrdinstance_acquired_id(qi->ria), host_node_id_str);
  582. ret = simple_pattern_matches_extract(instances_sp, buffer, NULL, 0);
  583. }
  584. }
  585. return ret;
  586. }
  587. static inline bool query_instance_matches_labels(RRDINSTANCE *ri, SIMPLE_PATTERN *chart_label_key_sp, SIMPLE_PATTERN *labels_sp) {
  588. if ((chart_label_key_sp && !rrdlabels_match_simple_pattern_parsed(
  589. ri->rrdlabels, chart_label_key_sp, '\0', NULL)) ||
  590. (labels_sp && !rrdlabels_match_simple_pattern_parsed(
  591. ri->rrdlabels, labels_sp, ':', NULL)))
  592. return false;
  593. return true;
  594. }
  595. static bool query_instance_add(QUERY_TARGET_LOCALS *qtl, QUERY_NODE *qn, QUERY_CONTEXT *qc,
  596. RRDINSTANCE_ACQUIRED *ria, bool queryable_instance, bool filter_instances) {
  597. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  598. if(rrd_flag_is_deleted(ri))
  599. return false;
  600. QUERY_TARGET *qt = qtl->qt;
  601. QUERY_INSTANCE *qi = query_instance_allocate(qt, ria, qn->slot);
  602. if(qt->db.minimum_latest_update_every_s == 0 || ri->update_every_s < qt->db.minimum_latest_update_every_s)
  603. qt->db.minimum_latest_update_every_s = ri->update_every_s;
  604. if(queryable_instance && filter_instances)
  605. queryable_instance = (SP_MATCHED_POSITIVE == query_instance_matches(
  606. qi, ri, qt->instances.pattern, qtl->match_ids, qtl->match_names, qt->request.version, qtl->host_node_id_str));
  607. if(queryable_instance)
  608. queryable_instance = query_instance_matches_labels(ri, qt->instances.chart_label_key_pattern, qt->instances.labels_pattern);
  609. if(queryable_instance) {
  610. if(qt->instances.alerts_pattern && !query_target_match_alert_pattern(ria, qt->instances.alerts_pattern))
  611. queryable_instance = false;
  612. }
  613. if(queryable_instance && qt->request.version >= 2)
  614. query_target_eval_instance_rrdcalc(qtl, qn, qc, qi);
  615. size_t dimensions_added = 0, metrics_added = 0, priority = 0;
  616. if(unlikely(qt->request.rma)) {
  617. if(query_dimension_add(qtl, qn, qc, qi, qt->request.rma, queryable_instance, &metrics_added, priority++))
  618. dimensions_added++;
  619. }
  620. else {
  621. RRDMETRIC *rm;
  622. dfe_start_read(ri->rrdmetrics, rm) {
  623. if(query_dimension_add(qtl, qn, qc, qi, (RRDMETRIC_ACQUIRED *) rm_dfe.item,
  624. queryable_instance, &metrics_added, priority++))
  625. dimensions_added++;
  626. }
  627. dfe_done(rm);
  628. }
  629. if(!dimensions_added) {
  630. qt->instances.used--;
  631. query_instance_release(qi);
  632. return false;
  633. }
  634. else {
  635. if(metrics_added) {
  636. qc->instances.selected++;
  637. qn->instances.selected++;
  638. }
  639. else {
  640. qc->instances.excluded++;
  641. qn->instances.excluded++;
  642. }
  643. }
  644. return true;
  645. }
  646. static inline void query_context_release(QUERY_CONTEXT *qc) {
  647. rrdcontext_release(qc->rca);
  648. qc->rca = NULL;
  649. }
  650. static inline QUERY_CONTEXT *query_context_allocate(QUERY_TARGET *qt, RRDCONTEXT_ACQUIRED *rca) {
  651. if(qt->contexts.used == qt->contexts.size) {
  652. size_t old_mem = qt->contexts.size * sizeof(*qt->contexts.array);
  653. qt->contexts.size = query_target_realloc_size(qt->contexts.size, 2);
  654. size_t new_mem = qt->contexts.size * sizeof(*qt->contexts.array);
  655. qt->contexts.array = reallocz(qt->contexts.array, new_mem);
  656. __atomic_add_fetch(&netdata_buffers_statistics.query_targets_size, new_mem - old_mem, __ATOMIC_RELAXED);
  657. }
  658. QUERY_CONTEXT *qc = &qt->contexts.array[qt->contexts.used];
  659. memset(qc, 0, sizeof(*qc));
  660. qc->slot = qt->contexts.used++;
  661. qc->rca = rrdcontext_acquired_dup(rca);
  662. return qc;
  663. }
  664. static ssize_t query_context_add(void *data, RRDCONTEXT_ACQUIRED *rca, bool queryable_context) {
  665. QUERY_TARGET_LOCALS *qtl = data;
  666. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  667. if(rrd_flag_is_deleted(rc))
  668. return 0;
  669. QUERY_NODE *qn = qtl->qn;
  670. QUERY_TARGET *qt = qtl->qt;
  671. QUERY_CONTEXT *qc = query_context_allocate(qt, rca);
  672. ssize_t added = 0;
  673. if(unlikely(qt->request.ria)) {
  674. if(query_instance_add(qtl, qn, qc, qt->request.ria, queryable_context, false))
  675. added++;
  676. }
  677. else if(unlikely(qtl->st && qtl->st->rrdcontext == rca && qtl->st->rrdinstance)) {
  678. if(query_instance_add(qtl, qn, qc, qtl->st->rrdinstance, queryable_context, false))
  679. added++;
  680. }
  681. else {
  682. RRDINSTANCE *ri;
  683. dfe_start_read(rc->rrdinstances, ri) {
  684. if(query_instance_add(qtl, qn, qc, (RRDINSTANCE_ACQUIRED *) ri_dfe.item, queryable_context, true))
  685. added++;
  686. }
  687. dfe_done(ri);
  688. }
  689. if(!added) {
  690. query_context_release(qc);
  691. qt->contexts.used--;
  692. return 0;
  693. }
  694. return added;
  695. }
  696. static inline void query_node_release(QUERY_NODE *qn) {
  697. qn->rrdhost = NULL;
  698. }
  699. static inline QUERY_NODE *query_node_allocate(QUERY_TARGET *qt, RRDHOST *host) {
  700. if(qt->nodes.used == qt->nodes.size) {
  701. size_t old_mem = qt->nodes.size * sizeof(*qt->nodes.array);
  702. qt->nodes.size = query_target_realloc_size(qt->nodes.size, 2);
  703. size_t new_mem = qt->nodes.size * sizeof(*qt->nodes.array);
  704. qt->nodes.array = reallocz(qt->nodes.array, new_mem);
  705. __atomic_add_fetch(&netdata_buffers_statistics.query_targets_size, new_mem - old_mem, __ATOMIC_RELAXED);
  706. }
  707. QUERY_NODE *qn = &qt->nodes.array[qt->nodes.used];
  708. memset(qn, 0, sizeof(*qn));
  709. qn->slot = qt->nodes.used++;
  710. qn->rrdhost = host;
  711. return qn;
  712. }
  713. static ssize_t query_node_add(void *data, RRDHOST *host, bool queryable_host) {
  714. QUERY_TARGET_LOCALS *qtl = data;
  715. QUERY_TARGET *qt = qtl->qt;
  716. QUERY_NODE *qn = query_node_allocate(qt, host);
  717. if(host->node_id) {
  718. if(!qtl->host_node_id_str[0])
  719. uuid_unparse_lower(*host->node_id, qn->node_id);
  720. else
  721. memcpy(qn->node_id, qtl->host_node_id_str, sizeof(qn->node_id));
  722. }
  723. else
  724. qn->node_id[0] = '\0';
  725. // is the chart given valid?
  726. if(unlikely(qtl->st && (!qtl->st->rrdinstance || !qtl->st->rrdcontext))) {
  727. error("QUERY TARGET: RRDSET '%s' given, but it is not linked to rrdcontext structures. Linking it now.", rrdset_name(qtl->st));
  728. rrdinstance_from_rrdset(qtl->st);
  729. if(unlikely(qtl->st && (!qtl->st->rrdinstance || !qtl->st->rrdcontext))) {
  730. error("QUERY TARGET: RRDSET '%s' given, but failed to be linked to rrdcontext structures. Switching to context query.",
  731. rrdset_name(qtl->st));
  732. if (!is_valid_sp(qtl->instances))
  733. qtl->instances = rrdset_name(qtl->st);
  734. qtl->st = NULL;
  735. }
  736. }
  737. qtl->qn = qn;
  738. ssize_t added = 0;
  739. if(unlikely(qt->request.rca)) {
  740. if(query_context_add(qtl, qt->request.rca, true))
  741. added++;
  742. }
  743. else if(unlikely(qtl->st)) {
  744. // single chart data queries
  745. if(query_context_add(qtl, qtl->st->rrdcontext, true))
  746. added++;
  747. }
  748. else {
  749. // context pattern queries
  750. added = query_scope_foreach_context(
  751. host, qtl->scope_contexts,
  752. qt->contexts.scope_pattern, qt->contexts.pattern,
  753. query_context_add, queryable_host, qtl);
  754. if(added < 0)
  755. added = 0;
  756. }
  757. qtl->qn = NULL;
  758. if(!added) {
  759. query_node_release(qn);
  760. qt->nodes.used--;
  761. return false;
  762. }
  763. return true;
  764. }
  765. void query_target_generate_name(QUERY_TARGET *qt) {
  766. char options_buffer[100 + 1];
  767. web_client_api_request_v1_data_options_to_string(options_buffer, 100, qt->request.options);
  768. char resampling_buffer[20 + 1] = "";
  769. if(qt->request.resampling_time > 1)
  770. snprintfz(resampling_buffer, 20, "/resampling:%lld", (long long)qt->request.resampling_time);
  771. char tier_buffer[20 + 1] = "";
  772. if(qt->request.options & RRDR_OPTION_SELECTED_TIER)
  773. snprintfz(tier_buffer, 20, "/tier:%zu", qt->request.tier);
  774. if(qt->request.st)
  775. snprintfz(qt->id, MAX_QUERY_TARGET_ID_LENGTH, "chart://hosts:%s/instance:%s/dimensions:%s/after:%lld/before:%lld/points:%zu/group:%s%s/options:%s%s%s"
  776. , rrdhost_hostname(qt->request.st->rrdhost)
  777. , rrdset_name(qt->request.st)
  778. , (qt->request.dimensions) ? qt->request.dimensions : "*"
  779. , (long long)qt->request.after
  780. , (long long)qt->request.before
  781. , qt->request.points
  782. , time_grouping_tostring(qt->request.time_group_method)
  783. , qt->request.time_group_options ? qt->request.time_group_options : ""
  784. , options_buffer
  785. , resampling_buffer
  786. , tier_buffer
  787. );
  788. else if(qt->request.host && qt->request.rca && qt->request.ria && qt->request.rma)
  789. snprintfz(qt->id, MAX_QUERY_TARGET_ID_LENGTH, "metric://hosts:%s/context:%s/instance:%s/dimension:%s/after:%lld/before:%lld/points:%zu/group:%s%s/options:%s%s%s"
  790. , rrdhost_hostname(qt->request.host)
  791. , rrdcontext_acquired_id(qt->request.rca)
  792. , rrdinstance_acquired_id(qt->request.ria)
  793. , rrdmetric_acquired_id(qt->request.rma)
  794. , (long long)qt->request.after
  795. , (long long)qt->request.before
  796. , qt->request.points
  797. , time_grouping_tostring(qt->request.time_group_method)
  798. , qt->request.time_group_options ? qt->request.time_group_options : ""
  799. , options_buffer
  800. , resampling_buffer
  801. , tier_buffer
  802. );
  803. else if(qt->request.version >= 2)
  804. snprintfz(qt->id, MAX_QUERY_TARGET_ID_LENGTH, "data_v2://scope_nodes:%s/scope_contexts:%s/nodes:%s/contexts:%s/instances:%s/labels:%s/dimensions:%s/after:%lld/before:%lld/points:%zu/time_group:%s%s/options:%s%s%s"
  805. , qt->request.scope_nodes ? qt->request.scope_nodes : "*"
  806. , qt->request.scope_contexts ? qt->request.scope_contexts : "*"
  807. , qt->request.nodes ? qt->request.nodes : "*"
  808. , (qt->request.contexts) ? qt->request.contexts : "*"
  809. , (qt->request.instances) ? qt->request.instances : "*"
  810. , (qt->request.labels) ? qt->request.labels : "*"
  811. , (qt->request.dimensions) ? qt->request.dimensions : "*"
  812. , (long long)qt->request.after
  813. , (long long)qt->request.before
  814. , qt->request.points
  815. , time_grouping_tostring(qt->request.time_group_method)
  816. , qt->request.time_group_options ? qt->request.time_group_options : ""
  817. , options_buffer
  818. , resampling_buffer
  819. , tier_buffer
  820. );
  821. else
  822. snprintfz(qt->id, MAX_QUERY_TARGET_ID_LENGTH, "context://hosts:%s/contexts:%s/instances:%s/dimensions:%s/after:%lld/before:%lld/points:%zu/group:%s%s/options:%s%s%s"
  823. , (qt->request.host) ? rrdhost_hostname(qt->request.host) : ((qt->request.nodes) ? qt->request.nodes : "*")
  824. , (qt->request.contexts) ? qt->request.contexts : "*"
  825. , (qt->request.instances) ? qt->request.instances : "*"
  826. , (qt->request.dimensions) ? qt->request.dimensions : "*"
  827. , (long long)qt->request.after
  828. , (long long)qt->request.before
  829. , qt->request.points
  830. , time_grouping_tostring(qt->request.time_group_method)
  831. , qt->request.time_group_options ? qt->request.time_group_options : ""
  832. , options_buffer
  833. , resampling_buffer
  834. , tier_buffer
  835. );
  836. json_fix_string(qt->id);
  837. }
  838. QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr) {
  839. if(!service_running(ABILITY_DATA_QUERIES))
  840. return NULL;
  841. QUERY_TARGET *qt = query_target_get();
  842. if(!qtr->received_ut)
  843. qtr->received_ut = now_monotonic_usec();
  844. qt->timings.received_ut = qtr->received_ut;
  845. if(qtr->nodes && !qtr->scope_nodes)
  846. qtr->scope_nodes = qtr->nodes;
  847. if(qtr->contexts && !qtr->scope_contexts)
  848. qtr->scope_contexts = qtr->contexts;
  849. memset(&qt->db, 0, sizeof(qt->db));
  850. qt->query_points = STORAGE_POINT_UNSET;
  851. // copy the request into query_thread_target
  852. qt->request = *qtr;
  853. query_target_generate_name(qt);
  854. qt->window.after = qt->request.after;
  855. qt->window.before = qt->request.before;
  856. qt->window.options = qt->request.options;
  857. if(query_target_has_percentage_of_instance(qt))
  858. qt->window.options &= ~RRDR_OPTION_PERCENTAGE;
  859. rrdr_relative_window_to_absolute(&qt->window.after, &qt->window.before, &qt->window.now);
  860. // prepare our local variables - we need these across all these functions
  861. QUERY_TARGET_LOCALS qtl = {
  862. .qt = qt,
  863. .start_s = now_realtime_sec(),
  864. .st = qt->request.st,
  865. .scope_nodes = qt->request.scope_nodes,
  866. .scope_contexts = qt->request.scope_contexts,
  867. .nodes = qt->request.nodes,
  868. .contexts = qt->request.contexts,
  869. .instances = qt->request.instances,
  870. .dimensions = qt->request.dimensions,
  871. .chart_label_key = qt->request.chart_label_key,
  872. .labels = qt->request.labels,
  873. .alerts = qt->request.alerts,
  874. };
  875. RRDHOST *host = qt->request.host;
  876. // prepare all the patterns
  877. qt->nodes.scope_pattern = string_to_simple_pattern(qtl.scope_nodes);
  878. qt->nodes.pattern = string_to_simple_pattern(qtl.nodes);
  879. qt->contexts.pattern = string_to_simple_pattern(qtl.contexts);
  880. qt->contexts.scope_pattern = string_to_simple_pattern(qtl.scope_contexts);
  881. qt->instances.pattern = string_to_simple_pattern(qtl.instances);
  882. qt->query.pattern = string_to_simple_pattern(qtl.dimensions);
  883. qt->instances.chart_label_key_pattern = string_to_simple_pattern(qtl.chart_label_key);
  884. qt->instances.labels_pattern = string_to_simple_pattern(qtl.labels);
  885. qt->instances.alerts_pattern = string_to_simple_pattern(qtl.alerts);
  886. qtl.match_ids = qt->request.options & RRDR_OPTION_MATCH_IDS;
  887. qtl.match_names = qt->request.options & RRDR_OPTION_MATCH_NAMES;
  888. if(likely(!qtl.match_ids && !qtl.match_names))
  889. qtl.match_ids = qtl.match_names = true;
  890. // verify that the chart belongs to the host we are interested
  891. if(qtl.st) {
  892. if (!host) {
  893. // It is NULL, set it ourselves.
  894. host = qtl.st->rrdhost;
  895. }
  896. else if (unlikely(host != qtl.st->rrdhost)) {
  897. // Oops! A different host!
  898. error("QUERY TARGET: RRDSET '%s' given does not belong to host '%s'. Switching query host to '%s'",
  899. rrdset_name(qtl.st), rrdhost_hostname(host), rrdhost_hostname(qtl.st->rrdhost));
  900. host = qtl.st->rrdhost;
  901. }
  902. }
  903. if(host) {
  904. if(host->node_id)
  905. uuid_unparse_lower(*host->node_id, qtl.host_node_id_str);
  906. else
  907. qtl.host_node_id_str[0] = '\0';
  908. // single host query
  909. qt->versions.contexts_hard_hash = dictionary_version(host->rrdctx.contexts);
  910. qt->versions.contexts_soft_hash = dictionary_version(host->rrdctx.hub_queue);
  911. qt->versions.alerts_hard_hash = dictionary_version(host->rrdcalc_root_index);
  912. qt->versions.alerts_soft_hash = __atomic_load_n(&host->health_transitions, __ATOMIC_RELAXED);
  913. query_node_add(&qtl, host, true);
  914. qtl.nodes = rrdhost_hostname(host);
  915. }
  916. else
  917. query_scope_foreach_host(qt->nodes.scope_pattern, qt->nodes.pattern,
  918. query_node_add, &qtl,
  919. &qt->versions,
  920. qtl.host_node_id_str);
  921. // we need the available db retention for this call
  922. // so it has to be done last
  923. query_target_calculate_window(qt);
  924. qt->timings.preprocessed_ut = now_monotonic_usec();
  925. return qt;
  926. }
  927. ssize_t weights_foreach_rrdmetric_in_context(RRDCONTEXT_ACQUIRED *rca,
  928. SIMPLE_PATTERN *instances_sp,
  929. SIMPLE_PATTERN *chart_label_key_sp,
  930. SIMPLE_PATTERN *labels_sp,
  931. SIMPLE_PATTERN *alerts_sp,
  932. SIMPLE_PATTERN *dimensions_sp,
  933. bool match_ids, bool match_names,
  934. size_t version,
  935. weights_add_metric_t cb,
  936. void *data
  937. ) {
  938. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  939. if(!rc || rrd_flag_is_deleted(rc))
  940. return 0;
  941. char host_node_id_str[UUID_STR_LEN] = "";
  942. bool proceed = true;
  943. ssize_t count = 0;
  944. RRDINSTANCE *ri;
  945. dfe_start_read(rc->rrdinstances, ri) {
  946. if(rrd_flag_is_deleted(ri))
  947. continue;
  948. RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *) ri_dfe.item;
  949. if(instances_sp) {
  950. QUERY_INSTANCE qi = { .ria = ria, };
  951. SIMPLE_PATTERN_RESULT ret = query_instance_matches(&qi, ri, instances_sp, match_ids, match_names, version, host_node_id_str);
  952. qi.ria = NULL;
  953. query_instance_release(&qi);
  954. if (ret != SP_MATCHED_POSITIVE)
  955. continue;
  956. }
  957. if(!query_instance_matches_labels(ri, chart_label_key_sp, labels_sp))
  958. continue;
  959. if(alerts_sp && !query_target_match_alert_pattern(ria, alerts_sp))
  960. continue;
  961. dfe_unlock(ri);
  962. RRDMETRIC *rm;
  963. dfe_start_read(ri->rrdmetrics, rm) {
  964. if(rrd_flag_is_deleted(rm))
  965. continue;
  966. if(dimensions_sp) {
  967. SIMPLE_PATTERN_RESULT ret = SP_NOT_MATCHED;
  968. if (match_ids)
  969. ret = simple_pattern_matches_string_extract(dimensions_sp, rm->id, NULL, 0);
  970. if (ret == SP_NOT_MATCHED && match_names && (rm->name != rm->id || !match_ids))
  971. ret = simple_pattern_matches_string_extract(dimensions_sp, rm->name, NULL, 0);
  972. if(ret != SP_MATCHED_POSITIVE)
  973. continue;
  974. }
  975. dfe_unlock(rm);
  976. RRDMETRIC_ACQUIRED *rma = (RRDMETRIC_ACQUIRED *)rm_dfe.item;
  977. ssize_t ret = cb(data, rc->rrdhost, rca, ria, rma);
  978. if(ret < 0) {
  979. proceed = false;
  980. break;
  981. }
  982. count += ret;
  983. }
  984. dfe_done(rm);
  985. if(unlikely(!proceed))
  986. break;
  987. }
  988. dfe_done(ri);
  989. return count;
  990. }