pluginsd_internals.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_PLUGINSD_INTERNALS_H
  3. #define NETDATA_PLUGINSD_INTERNALS_H
  4. #include "pluginsd_parser.h"
  5. #include "pluginsd_functions.h"
  6. #include "pluginsd_dyncfg.h"
  7. #include "pluginsd_replication.h"
  8. #define SERVING_STREAMING(parser) ((parser)->repertoire == PARSER_INIT_STREAMING)
  9. #define SERVING_PLUGINSD(parser) ((parser)->repertoire == PARSER_INIT_PLUGINSD)
  10. PARSER_RC PLUGINSD_DISABLE_PLUGIN(PARSER *parser, const char *keyword, const char *msg);
  11. ssize_t send_to_plugin(const char *txt, void *data);
  12. static inline RRDHOST *pluginsd_require_scope_host(PARSER *parser, const char *cmd) {
  13. RRDHOST *host = parser->user.host;
  14. if(unlikely(!host))
  15. netdata_log_error("PLUGINSD: command %s requires a host, but is not set.", cmd);
  16. return host;
  17. }
  18. static inline RRDSET *pluginsd_require_scope_chart(PARSER *parser, const char *cmd, const char *parent_cmd) {
  19. RRDSET *st = parser->user.st;
  20. if(unlikely(!st))
  21. netdata_log_error("PLUGINSD: command %s requires a chart defined via command %s, but is not set.", cmd, parent_cmd);
  22. return st;
  23. }
  24. static inline RRDSET *pluginsd_get_scope_chart(PARSER *parser) {
  25. return parser->user.st;
  26. }
  27. static inline void pluginsd_lock_rrdset_data_collection(PARSER *parser) {
  28. if(parser->user.st && !parser->user.v2.locked_data_collection) {
  29. spinlock_lock(&parser->user.st->data_collection_lock);
  30. parser->user.v2.locked_data_collection = true;
  31. }
  32. }
  33. static inline bool pluginsd_unlock_rrdset_data_collection(PARSER *parser) {
  34. if(parser->user.st && parser->user.v2.locked_data_collection) {
  35. spinlock_unlock(&parser->user.st->data_collection_lock);
  36. parser->user.v2.locked_data_collection = false;
  37. return true;
  38. }
  39. return false;
  40. }
  41. static inline void pluginsd_unlock_previous_scope_chart(PARSER *parser, const char *keyword, bool stale) {
  42. if(unlikely(pluginsd_unlock_rrdset_data_collection(parser))) {
  43. if(stale)
  44. netdata_log_error("PLUGINSD: 'host:%s/chart:%s/' stale data collection lock found during %s; it has been unlocked",
  45. rrdhost_hostname(parser->user.st->rrdhost),
  46. rrdset_id(parser->user.st),
  47. keyword);
  48. }
  49. if(unlikely(parser->user.v2.ml_locked)) {
  50. ml_chart_update_end(parser->user.st);
  51. parser->user.v2.ml_locked = false;
  52. if(stale)
  53. netdata_log_error("PLUGINSD: 'host:%s/chart:%s/' stale ML lock found during %s, it has been unlocked",
  54. rrdhost_hostname(parser->user.st->rrdhost),
  55. rrdset_id(parser->user.st),
  56. keyword);
  57. }
  58. }
  59. static inline void pluginsd_clear_scope_chart(PARSER *parser, const char *keyword) {
  60. pluginsd_unlock_previous_scope_chart(parser, keyword, true);
  61. if(parser->user.cleanup_slots && parser->user.st)
  62. rrdset_pluginsd_receive_unslot(parser->user.st);
  63. parser->user.st = NULL;
  64. parser->user.cleanup_slots = false;
  65. }
  66. static inline bool pluginsd_set_scope_chart(PARSER *parser, RRDSET *st, const char *keyword) {
  67. RRDSET *old_st = parser->user.st;
  68. pid_t old_collector_tid = (old_st) ? old_st->pluginsd.collector_tid : 0;
  69. pid_t my_collector_tid = gettid();
  70. if(unlikely(old_collector_tid)) {
  71. if(old_collector_tid != my_collector_tid) {
  72. nd_log_limit_static_global_var(erl, 1, 0);
  73. nd_log_limit(&erl, NDLS_COLLECTORS, NDLP_WARNING,
  74. "PLUGINSD: keyword %s: 'host:%s/chart:%s' is collected twice (my tid %d, other collector tid %d)",
  75. keyword ? keyword : "UNKNOWN",
  76. rrdhost_hostname(st->rrdhost), rrdset_id(st),
  77. my_collector_tid, old_collector_tid);
  78. return false;
  79. }
  80. old_st->pluginsd.collector_tid = 0;
  81. }
  82. st->pluginsd.collector_tid = my_collector_tid;
  83. pluginsd_clear_scope_chart(parser, keyword);
  84. st->pluginsd.pos = 0;
  85. parser->user.st = st;
  86. parser->user.cleanup_slots = false;
  87. return true;
  88. }
  89. static inline void pluginsd_rrddim_put_to_slot(PARSER *parser, RRDSET *st, RRDDIM *rd, ssize_t slot, bool obsolete) {
  90. size_t wanted_size = st->pluginsd.size;
  91. if(slot >= 1) {
  92. st->pluginsd.dims_with_slots = true;
  93. wanted_size = slot;
  94. }
  95. else {
  96. st->pluginsd.dims_with_slots = false;
  97. wanted_size = dictionary_entries(st->rrddim_root_index);
  98. }
  99. if(wanted_size > st->pluginsd.size) {
  100. st->pluginsd.prd_array = reallocz(st->pluginsd.prd_array, wanted_size * sizeof(struct pluginsd_rrddim));
  101. // initialize the empty slots
  102. for(ssize_t i = (ssize_t) wanted_size - 1; i >= (ssize_t) st->pluginsd.size; i--) {
  103. st->pluginsd.prd_array[i].rda = NULL;
  104. st->pluginsd.prd_array[i].rd = NULL;
  105. st->pluginsd.prd_array[i].id = NULL;
  106. }
  107. st->pluginsd.size = wanted_size;
  108. }
  109. if(st->pluginsd.dims_with_slots) {
  110. struct pluginsd_rrddim *prd = &st->pluginsd.prd_array[slot - 1];
  111. if(prd->rd != rd) {
  112. prd->rda = rrddim_find_and_acquire(st, string2str(rd->id));
  113. prd->rd = rrddim_acquired_to_rrddim(prd->rda);
  114. prd->id = string2str(prd->rd->id);
  115. }
  116. if(obsolete)
  117. parser->user.cleanup_slots = true;
  118. }
  119. }
  120. static inline RRDDIM *pluginsd_acquire_dimension(RRDHOST *host, RRDSET *st, const char *dimension, ssize_t slot, const char *cmd) {
  121. if (unlikely(!dimension || !*dimension)) {
  122. netdata_log_error("PLUGINSD: 'host:%s/chart:%s' got a %s, without a dimension.",
  123. rrdhost_hostname(host), rrdset_id(st), cmd);
  124. return NULL;
  125. }
  126. if (unlikely(!st->pluginsd.size)) {
  127. netdata_log_error("PLUGINSD: 'host:%s/chart:%s' got a %s, but the chart has no dimensions.",
  128. rrdhost_hostname(host), rrdset_id(st), cmd);
  129. return NULL;
  130. }
  131. struct pluginsd_rrddim *prd;
  132. RRDDIM *rd;
  133. if(likely(st->pluginsd.dims_with_slots)) {
  134. // caching with slots
  135. if(unlikely(slot < 1 || slot > st->pluginsd.size)) {
  136. netdata_log_error("PLUGINSD: 'host:%s/chart:%s' got a %s with slot %zd, but slots in the range [1 - %u] are expected.",
  137. rrdhost_hostname(host), rrdset_id(st), cmd, slot, st->pluginsd.size);
  138. return NULL;
  139. }
  140. prd = &st->pluginsd.prd_array[slot - 1];
  141. rd = prd->rd;
  142. if(likely(rd)) {
  143. #ifdef NETDATA_INTERNAL_CHECKS
  144. if(strcmp(prd->id, dimension) != 0) {
  145. ssize_t t;
  146. for(t = 0; t < st->pluginsd.size ;t++) {
  147. if (strcmp(st->pluginsd.prd_array[t].id, dimension) == 0)
  148. break;
  149. }
  150. if(t >= st->pluginsd.size)
  151. t = -1;
  152. internal_fatal(true,
  153. "PLUGINSD: expected to find dimension '%s' on slot %zd, but found '%s', "
  154. "the right slot is %zd",
  155. dimension, slot, prd->id, t);
  156. }
  157. #endif
  158. return rd;
  159. }
  160. }
  161. else {
  162. // caching without slots
  163. if(unlikely(st->pluginsd.pos >= st->pluginsd.size))
  164. st->pluginsd.pos = 0;
  165. prd = &st->pluginsd.prd_array[st->pluginsd.pos++];
  166. rd = prd->rd;
  167. if(likely(rd)) {
  168. const char *id = prd->id;
  169. if(strcmp(id, dimension) == 0) {
  170. // we found it cached
  171. return rd;
  172. }
  173. else {
  174. // the cached one is not good for us
  175. rrddim_acquired_release(prd->rda);
  176. prd->rda = NULL;
  177. prd->rd = NULL;
  178. prd->id = NULL;
  179. }
  180. }
  181. }
  182. // we need to find the dimension and set it to prd
  183. RRDDIM_ACQUIRED *rda = rrddim_find_and_acquire(st, dimension);
  184. if (unlikely(!rda)) {
  185. netdata_log_error("PLUGINSD: 'host:%s/chart:%s/dim:%s' got a %s but dimension does not exist.",
  186. rrdhost_hostname(host), rrdset_id(st), dimension, cmd);
  187. return NULL;
  188. }
  189. prd->rda = rda;
  190. prd->rd = rd = rrddim_acquired_to_rrddim(rda);
  191. prd->id = string2str(rd->id);
  192. return rd;
  193. }
  194. static inline RRDSET *pluginsd_find_chart(RRDHOST *host, const char *chart, const char *cmd) {
  195. if (unlikely(!chart || !*chart)) {
  196. netdata_log_error("PLUGINSD: 'host:%s' got a %s without a chart id.",
  197. rrdhost_hostname(host), cmd);
  198. return NULL;
  199. }
  200. RRDSET *st = rrdset_find(host, chart);
  201. if (unlikely(!st))
  202. netdata_log_error("PLUGINSD: 'host:%s/chart:%s' got a %s but chart does not exist.",
  203. rrdhost_hostname(host), chart, cmd);
  204. return st;
  205. }
  206. static inline ssize_t pluginsd_parse_rrd_slot(char **words, size_t num_words) {
  207. ssize_t slot = -1;
  208. char *id = get_word(words, num_words, 1);
  209. if(id && id[0] == PLUGINSD_KEYWORD_SLOT[0] && id[1] == PLUGINSD_KEYWORD_SLOT[1] &&
  210. id[2] == PLUGINSD_KEYWORD_SLOT[2] && id[3] == PLUGINSD_KEYWORD_SLOT[3] && id[4] == ':') {
  211. slot = (ssize_t) str2ull_encoded(&id[5]);
  212. if(slot < 0) slot = 0; // to make the caller increment its idx of the words
  213. }
  214. return slot;
  215. }
  216. static inline void pluginsd_rrdset_cache_put_to_slot(PARSER *parser, RRDSET *st, ssize_t slot, bool obsolete) {
  217. // clean possible old cached data
  218. rrdset_pluginsd_receive_unslot(st);
  219. if(unlikely(slot < 1 || slot >= INT32_MAX))
  220. return;
  221. RRDHOST *host = st->rrdhost;
  222. if(unlikely((size_t)slot > host->rrdpush.receive.pluginsd_chart_slots.size)) {
  223. spinlock_lock(&host->rrdpush.receive.pluginsd_chart_slots.spinlock);
  224. size_t old_slots = host->rrdpush.receive.pluginsd_chart_slots.size;
  225. size_t new_slots = (old_slots < PLUGINSD_MIN_RRDSET_POINTERS_CACHE) ? PLUGINSD_MIN_RRDSET_POINTERS_CACHE : old_slots * 2;
  226. if(new_slots < (size_t)slot)
  227. new_slots = slot;
  228. host->rrdpush.receive.pluginsd_chart_slots.array =
  229. reallocz(host->rrdpush.receive.pluginsd_chart_slots.array, new_slots * sizeof(RRDSET *));
  230. for(size_t i = old_slots; i < new_slots ;i++)
  231. host->rrdpush.receive.pluginsd_chart_slots.array[i] = NULL;
  232. host->rrdpush.receive.pluginsd_chart_slots.size = new_slots;
  233. spinlock_unlock(&host->rrdpush.receive.pluginsd_chart_slots.spinlock);
  234. }
  235. host->rrdpush.receive.pluginsd_chart_slots.array[slot - 1] = st;
  236. st->pluginsd.last_slot = (int32_t)slot - 1;
  237. parser->user.cleanup_slots = obsolete;
  238. }
  239. static inline RRDSET *pluginsd_rrdset_cache_get_from_slot(PARSER *parser, RRDHOST *host, const char *id, ssize_t slot, const char *keyword) {
  240. if(unlikely(slot < 1 || (size_t)slot > host->rrdpush.receive.pluginsd_chart_slots.size))
  241. return pluginsd_find_chart(host, id, keyword);
  242. RRDSET *st = host->rrdpush.receive.pluginsd_chart_slots.array[slot - 1];
  243. if(!st) {
  244. st = pluginsd_find_chart(host, id, keyword);
  245. if(st)
  246. pluginsd_rrdset_cache_put_to_slot(parser, st, slot, rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE));
  247. }
  248. else {
  249. internal_fatal(string_strcmp(st->id, id) != 0,
  250. "PLUGINSD: wrong chart in slot %zd, expected '%s', found '%s'",
  251. slot - 1, id, string2str(st->id));
  252. }
  253. return st;
  254. }
  255. static inline SN_FLAGS pluginsd_parse_storage_number_flags(const char *flags_str) {
  256. SN_FLAGS flags = SN_FLAG_NONE;
  257. char c;
  258. while ((c = *flags_str++)) {
  259. switch (c) {
  260. case 'A':
  261. flags |= SN_FLAG_NOT_ANOMALOUS;
  262. break;
  263. case 'R':
  264. flags |= SN_FLAG_RESET;
  265. break;
  266. case 'E':
  267. flags = SN_EMPTY_SLOT;
  268. return flags;
  269. default:
  270. internal_error(true, "Unknown SN_FLAGS flag '%c'", c);
  271. break;
  272. }
  273. }
  274. return flags;
  275. }
  276. #endif //NETDATA_PLUGINSD_INTERNALS_H