query_target.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. 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. 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. 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. spinlock_unlock(&query_target_base.available.spinlock);
  118. }
  119. }
  120. static QUERY_TARGET *query_target_get(void) {
  121. 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. spinlock_unlock(&query_target_base.available.spinlock);
  128. if(unlikely(!qt))
  129. qt = callocz(1, sizeof(*qt));
  130. 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. 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].smh) {
  175. STORAGE_ENGINE *eng = query_metric_storage_engine(qt, qm, tier);
  176. eng->api.metric_release(qm->tiers[tier].smh);
  177. qm->tiers[tier].smh = 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 *smh;
  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].smh)
  201. tier_retention[tier].smh = eng->api.metric_dup(rm->rrddim->tiers[tier].smh);
  202. else
  203. tier_retention[tier].smh = eng->api.metric_get(qn->rrdhost->db[tier].si, &rm->uuid);
  204. if(tier_retention[tier].smh) {
  205. tier_retention[tier].db_first_time_s = storage_engine_oldest_time_s(tier_retention[tier].eng->seb, tier_retention[tier].smh);
  206. tier_retention[tier].db_last_time_s = storage_engine_latest_time_s(tier_retention[tier].eng->seb, tier_retention[tier].smh);
  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].smh = tier_retention[tier].smh;
  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].smh)
  272. tier_retention[tier].eng->api.metric_release(tier_retention[tier].smh);
  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. rw_spinlock_read_lock(&st->alerts.spinlock);
  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. rw_spinlock_read_unlock(&st->alerts.spinlock);
  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. rw_spinlock_read_lock(&st->alerts.spinlock);
  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. rw_spinlock_read_unlock(&st->alerts.spinlock);
  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->rrdcontexts.rrdcontext == rca && qtl->st->rrdcontexts.rrdinstance)) {
  678. if(query_instance_add(qtl, qn, qc, qtl->st->rrdcontexts.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->rrdcontexts.rrdinstance || !qtl->st->rrdcontexts.rrdcontext))) {
  727. netdata_log_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->rrdcontexts.rrdinstance || !qtl->st->rrdcontexts.rrdcontext))) {
  730. netdata_log_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->rrdcontexts.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_group(qt))
  858. qt->window.options &= ~RRDR_OPTION_PERCENTAGE;
  859. qt->internal.relative = rrdr_relative_window_to_absolute_query(&qt->window.after, &qt->window.before
  860. , &qt->window.now, unittest_running
  861. );
  862. // prepare our local variables - we need these across all these functions
  863. QUERY_TARGET_LOCALS qtl = {
  864. .qt = qt,
  865. .start_s = now_realtime_sec(),
  866. .st = qt->request.st,
  867. .scope_nodes = qt->request.scope_nodes,
  868. .scope_contexts = qt->request.scope_contexts,
  869. .nodes = qt->request.nodes,
  870. .contexts = qt->request.contexts,
  871. .instances = qt->request.instances,
  872. .dimensions = qt->request.dimensions,
  873. .chart_label_key = qt->request.chart_label_key,
  874. .labels = qt->request.labels,
  875. .alerts = qt->request.alerts,
  876. };
  877. RRDHOST *host = qt->request.host;
  878. // prepare all the patterns
  879. qt->nodes.scope_pattern = string_to_simple_pattern(qtl.scope_nodes);
  880. qt->nodes.pattern = string_to_simple_pattern(qtl.nodes);
  881. qt->contexts.pattern = string_to_simple_pattern(qtl.contexts);
  882. qt->contexts.scope_pattern = string_to_simple_pattern(qtl.scope_contexts);
  883. qt->instances.pattern = string_to_simple_pattern(qtl.instances);
  884. qt->query.pattern = string_to_simple_pattern(qtl.dimensions);
  885. qt->instances.chart_label_key_pattern = string_to_simple_pattern(qtl.chart_label_key);
  886. qt->instances.labels_pattern = string_to_simple_pattern(qtl.labels);
  887. qt->instances.alerts_pattern = string_to_simple_pattern(qtl.alerts);
  888. qtl.match_ids = qt->request.options & RRDR_OPTION_MATCH_IDS;
  889. qtl.match_names = qt->request.options & RRDR_OPTION_MATCH_NAMES;
  890. if(likely(!qtl.match_ids && !qtl.match_names))
  891. qtl.match_ids = qtl.match_names = true;
  892. // verify that the chart belongs to the host we are interested
  893. if(qtl.st) {
  894. if (!host) {
  895. // It is NULL, set it ourselves.
  896. host = qtl.st->rrdhost;
  897. }
  898. else if (unlikely(host != qtl.st->rrdhost)) {
  899. // Oops! A different host!
  900. netdata_log_error("QUERY TARGET: RRDSET '%s' given does not belong to host '%s'. Switching query host to '%s'",
  901. rrdset_name(qtl.st), rrdhost_hostname(host), rrdhost_hostname(qtl.st->rrdhost));
  902. host = qtl.st->rrdhost;
  903. }
  904. }
  905. if(host) {
  906. if(host->node_id)
  907. uuid_unparse_lower(*host->node_id, qtl.host_node_id_str);
  908. else
  909. qtl.host_node_id_str[0] = '\0';
  910. // single host query
  911. qt->versions.contexts_hard_hash = dictionary_version(host->rrdctx.contexts);
  912. qt->versions.contexts_soft_hash = dictionary_version(host->rrdctx.hub_queue);
  913. qt->versions.alerts_hard_hash = dictionary_version(host->rrdcalc_root_index);
  914. qt->versions.alerts_soft_hash = __atomic_load_n(&host->health_transitions, __ATOMIC_RELAXED);
  915. query_node_add(&qtl, host, true);
  916. qtl.nodes = rrdhost_hostname(host);
  917. }
  918. else
  919. query_scope_foreach_host(qt->nodes.scope_pattern, qt->nodes.pattern,
  920. query_node_add, &qtl,
  921. &qt->versions,
  922. qtl.host_node_id_str);
  923. // we need the available db retention for this call
  924. // so it has to be done last
  925. query_target_calculate_window(qt);
  926. qt->timings.preprocessed_ut = now_monotonic_usec();
  927. return qt;
  928. }
  929. ssize_t weights_foreach_rrdmetric_in_context(RRDCONTEXT_ACQUIRED *rca,
  930. SIMPLE_PATTERN *instances_sp,
  931. SIMPLE_PATTERN *chart_label_key_sp,
  932. SIMPLE_PATTERN *labels_sp,
  933. SIMPLE_PATTERN *alerts_sp,
  934. SIMPLE_PATTERN *dimensions_sp,
  935. bool match_ids, bool match_names,
  936. size_t version,
  937. weights_add_metric_t cb,
  938. void *data
  939. ) {
  940. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  941. if(!rc || rrd_flag_is_deleted(rc))
  942. return 0;
  943. char host_node_id_str[UUID_STR_LEN] = "";
  944. bool proceed = true;
  945. ssize_t count = 0;
  946. RRDINSTANCE *ri;
  947. dfe_start_read(rc->rrdinstances, ri) {
  948. if(rrd_flag_is_deleted(ri))
  949. continue;
  950. RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *) ri_dfe.item;
  951. if(instances_sp) {
  952. QUERY_INSTANCE qi = { .ria = ria, };
  953. SIMPLE_PATTERN_RESULT ret = query_instance_matches(&qi, ri, instances_sp, match_ids, match_names, version, host_node_id_str);
  954. qi.ria = NULL;
  955. query_instance_release(&qi);
  956. if (ret != SP_MATCHED_POSITIVE)
  957. continue;
  958. }
  959. if(!query_instance_matches_labels(ri, chart_label_key_sp, labels_sp))
  960. continue;
  961. if(alerts_sp && !query_target_match_alert_pattern(ria, alerts_sp))
  962. continue;
  963. dfe_unlock(ri);
  964. RRDMETRIC *rm;
  965. dfe_start_read(ri->rrdmetrics, rm) {
  966. if(rrd_flag_is_deleted(rm))
  967. continue;
  968. if(dimensions_sp) {
  969. SIMPLE_PATTERN_RESULT ret = SP_NOT_MATCHED;
  970. if (match_ids)
  971. ret = simple_pattern_matches_string_extract(dimensions_sp, rm->id, NULL, 0);
  972. if (ret == SP_NOT_MATCHED && match_names && (rm->name != rm->id || !match_ids))
  973. ret = simple_pattern_matches_string_extract(dimensions_sp, rm->name, NULL, 0);
  974. if(ret != SP_MATCHED_POSITIVE)
  975. continue;
  976. }
  977. dfe_unlock(rm);
  978. RRDMETRIC_ACQUIRED *rma = (RRDMETRIC_ACQUIRED *)rm_dfe.item;
  979. ssize_t ret = cb(data, rc->rrdhost, rca, ria, rma);
  980. if(ret < 0) {
  981. proceed = false;
  982. break;
  983. }
  984. count += ret;
  985. }
  986. dfe_done(rm);
  987. if(unlikely(!proceed))
  988. break;
  989. }
  990. dfe_done(ri);
  991. return count;
  992. }