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