rrdcontext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "internal.h"
  3. // ----------------------------------------------------------------------------
  4. // visualizing flags
  5. struct rrdcontext_reason rrdcontext_reasons[] = {
  6. // context related
  7. {RRD_FLAG_UPDATE_REASON_TRIGGERED, "triggered transition", 65 * USEC_PER_SEC },
  8. {RRD_FLAG_UPDATE_REASON_NEW_OBJECT, "object created", 65 * USEC_PER_SEC },
  9. {RRD_FLAG_UPDATE_REASON_UPDATED_OBJECT, "object updated", 65 * USEC_PER_SEC },
  10. {RRD_FLAG_UPDATE_REASON_LOAD_SQL, "loaded from sql", 65 * USEC_PER_SEC },
  11. {RRD_FLAG_UPDATE_REASON_CHANGED_METADATA, "changed metadata", 65 * USEC_PER_SEC },
  12. {RRD_FLAG_UPDATE_REASON_ZERO_RETENTION, "has no retention", 65 * USEC_PER_SEC },
  13. {RRD_FLAG_UPDATE_REASON_CHANGED_FIRST_TIME_T, "updated first_time_t", 65 * USEC_PER_SEC },
  14. {RRD_FLAG_UPDATE_REASON_CHANGED_LAST_TIME_T, "updated last_time_t", 65 * USEC_PER_SEC },
  15. {RRD_FLAG_UPDATE_REASON_STOPPED_BEING_COLLECTED, "stopped collected", 65 * USEC_PER_SEC },
  16. {RRD_FLAG_UPDATE_REASON_STARTED_BEING_COLLECTED, "started collected", 5 * USEC_PER_SEC },
  17. {RRD_FLAG_UPDATE_REASON_UNUSED, "unused", 5 * USEC_PER_SEC },
  18. // not context related
  19. {RRD_FLAG_UPDATE_REASON_CHANGED_LINKING, "changed rrd link", 65 * USEC_PER_SEC },
  20. {RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD, "child disconnected", 65 * USEC_PER_SEC },
  21. {RRD_FLAG_UPDATE_REASON_DB_ROTATION, "db rotation", 65 * USEC_PER_SEC },
  22. {RRD_FLAG_UPDATE_REASON_UPDATE_RETENTION, "updated retention", 65 * USEC_PER_SEC },
  23. // terminator
  24. {0, NULL, 0 },
  25. };
  26. void rrd_reasons_to_buffer_json_array_items(RRD_FLAGS flags, BUFFER *wb) {
  27. for(int i = 0, added = 0; rrdcontext_reasons[i].name ; i++) {
  28. if (flags & rrdcontext_reasons[i].flag) {
  29. buffer_json_add_array_item_string(wb, rrdcontext_reasons[i].name);
  30. added++;
  31. }
  32. }
  33. }
  34. // ----------------------------------------------------------------------------
  35. // public API
  36. void rrdcontext_updated_rrddim(RRDDIM *rd) {
  37. rrdmetric_from_rrddim(rd);
  38. }
  39. void rrdcontext_removed_rrddim(RRDDIM *rd) {
  40. rrdmetric_rrddim_is_freed(rd);
  41. }
  42. void rrdcontext_updated_rrddim_algorithm(RRDDIM *rd) {
  43. rrdmetric_updated_rrddim_flags(rd);
  44. }
  45. void rrdcontext_updated_rrddim_multiplier(RRDDIM *rd) {
  46. rrdmetric_updated_rrddim_flags(rd);
  47. }
  48. void rrdcontext_updated_rrddim_divisor(RRDDIM *rd) {
  49. rrdmetric_updated_rrddim_flags(rd);
  50. }
  51. void rrdcontext_updated_rrddim_flags(RRDDIM *rd) {
  52. rrdmetric_updated_rrddim_flags(rd);
  53. }
  54. void rrdcontext_collected_rrddim(RRDDIM *rd) {
  55. rrdmetric_collected_rrddim(rd);
  56. }
  57. void rrdcontext_updated_rrdset(RRDSET *st) {
  58. rrdinstance_from_rrdset(st);
  59. }
  60. void rrdcontext_removed_rrdset(RRDSET *st) {
  61. rrdinstance_rrdset_is_freed(st);
  62. }
  63. void rrdcontext_updated_retention_rrdset(RRDSET *st) {
  64. rrdinstance_rrdset_has_updated_retention(st);
  65. }
  66. void rrdcontext_updated_rrdset_name(RRDSET *st) {
  67. rrdinstance_updated_rrdset_name(st);
  68. }
  69. void rrdcontext_updated_rrdset_flags(RRDSET *st) {
  70. rrdinstance_updated_rrdset_flags(st);
  71. }
  72. void rrdcontext_collected_rrdset(RRDSET *st) {
  73. rrdinstance_collected_rrdset(st);
  74. }
  75. void rrdcontext_host_child_connected(RRDHOST *host) {
  76. (void)host;
  77. // no need to do anything here
  78. ;
  79. }
  80. usec_t rrdcontext_next_db_rotation_ut = 0;
  81. void rrdcontext_db_rotation(void) {
  82. // called when the db rotates its database
  83. rrdcontext_next_db_rotation_ut = now_realtime_usec() + FULL_RETENTION_SCAN_DELAY_AFTER_DB_ROTATION_SECS * USEC_PER_SEC;
  84. }
  85. int rrdcontext_find_dimension_uuid(RRDSET *st, const char *id, uuid_t *store_uuid) {
  86. if(!st->rrdhost) return 1;
  87. if(!st->context) return 2;
  88. RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item(st->rrdhost->rrdctx.contexts, string2str(st->context));
  89. if(!rca) return 3;
  90. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  91. RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_get_and_acquire_item(rc->rrdinstances, string2str(st->id));
  92. if(!ria) {
  93. rrdcontext_release(rca);
  94. return 4;
  95. }
  96. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  97. RRDMETRIC_ACQUIRED *rma = (RRDMETRIC_ACQUIRED *)dictionary_get_and_acquire_item(ri->rrdmetrics, id);
  98. if(!rma) {
  99. rrdinstance_release(ria);
  100. rrdcontext_release(rca);
  101. return 5;
  102. }
  103. RRDMETRIC *rm = rrdmetric_acquired_value(rma);
  104. uuid_copy(*store_uuid, rm->uuid);
  105. rrdmetric_release(rma);
  106. rrdinstance_release(ria);
  107. rrdcontext_release(rca);
  108. return 0;
  109. }
  110. int rrdcontext_find_chart_uuid(RRDSET *st, uuid_t *store_uuid) {
  111. if(!st->rrdhost) return 1;
  112. if(!st->context) return 2;
  113. RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item(st->rrdhost->rrdctx.contexts, string2str(st->context));
  114. if(!rca) return 3;
  115. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  116. RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_get_and_acquire_item(rc->rrdinstances, string2str(st->id));
  117. if(!ria) {
  118. rrdcontext_release(rca);
  119. return 4;
  120. }
  121. RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
  122. uuid_copy(*store_uuid, ri->uuid);
  123. rrdinstance_release(ria);
  124. rrdcontext_release(rca);
  125. return 0;
  126. }
  127. void rrdcontext_host_child_disconnected(RRDHOST *host) {
  128. rrdcontext_recalculate_host_retention(host, RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD, false);
  129. }
  130. int rrdcontext_foreach_instance_with_rrdset_in_context(RRDHOST *host, const char *context, int (*callback)(RRDSET *st, void *data), void *data) {
  131. if(unlikely(!host || !context || !*context || !callback))
  132. return -1;
  133. RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item(host->rrdctx.contexts, context);
  134. if(unlikely(!rca)) return -1;
  135. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  136. if(unlikely(!rc)) return -1;
  137. int ret = 0;
  138. RRDINSTANCE *ri;
  139. dfe_start_read(rc->rrdinstances, ri) {
  140. if(ri->rrdset) {
  141. int r = callback(ri->rrdset, data);
  142. if(r >= 0) ret += r;
  143. else {
  144. ret = r;
  145. break;
  146. }
  147. }
  148. }
  149. dfe_done(ri);
  150. rrdcontext_release(rca);
  151. return ret;
  152. }
  153. // ----------------------------------------------------------------------------
  154. // ACLK interface
  155. static bool rrdhost_check_our_claim_id(const char *claim_id) {
  156. if(!localhost->aclk_state.claimed_id) return false;
  157. return (strcasecmp(claim_id, localhost->aclk_state.claimed_id) == 0) ? true : false;
  158. }
  159. static RRDHOST *rrdhost_find_by_node_id(const char *node_id) {
  160. uuid_t uuid;
  161. if (uuid_parse(node_id, uuid))
  162. return NULL;
  163. RRDHOST *host = NULL;
  164. dfe_start_read(rrdhost_root_index, host) {
  165. if(!host->node_id) continue;
  166. if(uuid_memcmp(&uuid, host->node_id) == 0)
  167. break;
  168. }
  169. dfe_done(host);
  170. return host;
  171. }
  172. void rrdcontext_hub_checkpoint_command(void *ptr) {
  173. struct ctxs_checkpoint *cmd = ptr;
  174. if(!rrdhost_check_our_claim_id(cmd->claim_id)) {
  175. error("RRDCONTEXT: received checkpoint command for claim_id '%s', node id '%s', but this is not our claim id. Ours '%s', received '%s'. Ignoring command.",
  176. cmd->claim_id, cmd->node_id,
  177. localhost->aclk_state.claimed_id?localhost->aclk_state.claimed_id:"NOT SET",
  178. cmd->claim_id);
  179. return;
  180. }
  181. RRDHOST *host = rrdhost_find_by_node_id(cmd->node_id);
  182. if(!host) {
  183. error("RRDCONTEXT: received checkpoint command for claim id '%s', node id '%s', but there is no node with such node id here. Ignoring command.",
  184. cmd->claim_id, cmd->node_id);
  185. return;
  186. }
  187. if(rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS)) {
  188. info("RRDCONTEXT: received checkpoint command for claim id '%s', node id '%s', while node '%s' has an active context streaming.",
  189. cmd->claim_id, cmd->node_id, rrdhost_hostname(host));
  190. // disable it temporarily, so that our worker will not attempt to send messages in parallel
  191. rrdhost_flag_clear(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS);
  192. }
  193. uint64_t our_version_hash = rrdcontext_version_hash(host);
  194. if(cmd->version_hash != our_version_hash) {
  195. error("RRDCONTEXT: received version hash %"PRIu64" for host '%s', does not match our version hash %"PRIu64". Sending snapshot of all contexts.",
  196. cmd->version_hash, rrdhost_hostname(host), our_version_hash);
  197. #ifdef ENABLE_ACLK
  198. // prepare the snapshot
  199. char uuid[UUID_STR_LEN];
  200. uuid_unparse_lower(*host->node_id, uuid);
  201. contexts_snapshot_t bundle = contexts_snapshot_new(cmd->claim_id, uuid, our_version_hash);
  202. // do a deep scan on every metric of the host to make sure all our data are updated
  203. rrdcontext_recalculate_host_retention(host, RRD_FLAG_NONE, false);
  204. // calculate version hash and pack all the messages together in one go
  205. our_version_hash = rrdcontext_version_hash_with_callback(host, rrdcontext_message_send_unsafe, true, bundle);
  206. // update the version
  207. contexts_snapshot_set_version(bundle, our_version_hash);
  208. // send it
  209. aclk_send_contexts_snapshot(bundle);
  210. #endif
  211. }
  212. internal_error(true, "RRDCONTEXT: host '%s' enabling streaming of contexts", rrdhost_hostname(host));
  213. rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS);
  214. char node_str[UUID_STR_LEN];
  215. uuid_unparse_lower(*host->node_id, node_str);
  216. log_access("ACLK REQ [%s (%s)]: STREAM CONTEXTS ENABLED", node_str, rrdhost_hostname(host));
  217. }
  218. void rrdcontext_hub_stop_streaming_command(void *ptr) {
  219. struct stop_streaming_ctxs *cmd = ptr;
  220. if(!rrdhost_check_our_claim_id(cmd->claim_id)) {
  221. error("RRDCONTEXT: received stop streaming command for claim_id '%s', node id '%s', but this is not our claim id. Ours '%s', received '%s'. Ignoring command.",
  222. cmd->claim_id, cmd->node_id,
  223. localhost->aclk_state.claimed_id?localhost->aclk_state.claimed_id:"NOT SET",
  224. cmd->claim_id);
  225. return;
  226. }
  227. RRDHOST *host = rrdhost_find_by_node_id(cmd->node_id);
  228. if(!host) {
  229. error("RRDCONTEXT: received stop streaming command for claim id '%s', node id '%s', but there is no node with such node id here. Ignoring command.",
  230. cmd->claim_id, cmd->node_id);
  231. return;
  232. }
  233. if(!rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS)) {
  234. error("RRDCONTEXT: received stop streaming command for claim id '%s', node id '%s', but node '%s' does not have active context streaming. Ignoring command.",
  235. cmd->claim_id, cmd->node_id, rrdhost_hostname(host));
  236. return;
  237. }
  238. internal_error(true, "RRDCONTEXT: host '%s' disabling streaming of contexts", rrdhost_hostname(host));
  239. rrdhost_flag_clear(host, RRDHOST_FLAG_ACLK_STREAM_CONTEXTS);
  240. }
  241. bool rrdcontext_retention_match(RRDCONTEXT_ACQUIRED *rca, time_t after, time_t before) {
  242. if(unlikely(!rca)) return false;
  243. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  244. if(rrd_flag_is_collected(rc))
  245. return query_matches_retention(after, before, rc->first_time_s, before > rc->last_time_s ? before : rc->last_time_s, 1);
  246. else
  247. return query_matches_retention(after, before, rc->first_time_s, rc->last_time_s, 1);
  248. }