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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "replication.h"
static time_t replicate_chart_timeframe(BUFFER *wb, RRDSET *st, time_t after, time_t before, bool enable_streaming) {
size_t dimensions = rrdset_number_of_dimensions(st);
struct storage_engine_query_ops *ops = &st->rrdhost->db[0].eng->api.query_ops;
struct {
DICTIONARY *dict;
const DICTIONARY_ITEM *rda;
RRDDIM *rd;
struct storage_engine_query_handle handle;
STORAGE_POINT sp;
} data[dimensions];
memset(data, 0, sizeof(data));
if(enable_streaming && st->last_updated.tv_sec > before) {
internal_error(true, "REPLAY: '%s' overwriting replication before from %llu to %llu",
rrdset_id(st),
(unsigned long long)before,
(unsigned long long)st->last_updated.tv_sec
);
before = st->last_updated.tv_sec;
}
// prepare our array of dimensions
{
RRDDIM *rd;
rrddim_foreach_read(rd, st) {
if (rd_dfe.counter >= dimensions)
break;
data[rd_dfe.counter].dict = rd_dfe.dict;
data[rd_dfe.counter].rda = dictionary_acquired_item_dup(rd_dfe.dict, rd_dfe.item);
data[rd_dfe.counter].rd = rd;
ops->init(rd->tiers[0]->db_metric_handle, &data[rd_dfe.counter].handle, after, before);
}
rrddim_foreach_done(rd);
}
time_t now = after, actual_after = 0, actual_before = 0;
while(now <= before) {
time_t min_start_time = 0, min_end_time = 0;
for (size_t i = 0; i < dimensions && data[i].rd; i++) {
// fetch the first valid point for the dimension
int max_skip = 100;
while(data[i].sp.end_time < now && !ops->is_finished(&data[i].handle) && max_skip-- > 0)
data[i].sp = ops->next_metric(&data[i].handle);
if(max_skip <= 0)
error("REPLAY: host '%s', chart '%s', dimension '%s': db does not advance the query beyond time %llu",
rrdhost_hostname(st->rrdhost), rrdset_id(st), rrddim_id(data[i].rd), (unsigned long long)now);
if(data[i].sp.end_time < now)
continue;
if(!min_start_time) {
min_start_time = data[i].sp.start_time;
min_end_time = data[i].sp.end_time;
}
else {
min_start_time = MIN(min_start_time, data[i].sp.start_time);
min_end_time = MIN(min_end_time, data[i].sp.end_time);
}
}
if(min_end_time < now) {
internal_error(true,
"REPLAY: host '%s', chart '%s': no data on any dimension beyond time %llu",
rrdhost_hostname(st->rrdhost), rrdset_id(st), (unsigned long long)now);
break;
}
if(min_end_time <= min_start_time)
min_start_time = min_end_time - st->update_every;
if(!actual_after) {
actual_after = min_end_time;
actual_before = min_end_time;
}
else
actual_before = min_end_time;
buffer_sprintf(wb, PLUGINSD_KEYWORD_REPLAY_BEGIN " '' %llu %llu\n"
, (unsigned long long)min_start_time
, (unsigned long long)min_end_time);
// output the replay values for this time
for (size_t i = 0; i < dimensions && data[i].rd; i++) {
if(data[i].sp.start_time <= min_end_time && data[i].sp.end_time >= min_end_time)
buffer_sprintf(wb, PLUGINSD_KEYWORD_REPLAY_SET " \"%s\" " NETDATA_DOUBLE_FORMAT_AUTO " \"%s\"\n",
rrddim_id(data[i].rd), data[i].sp.sum, data[i].sp.flags & SN_FLAG_RESET ? "R" : "");
else
buffer_sprintf(wb, PLUGINSD_KEYWORD_REPLAY_SET " \"%s\" NAN \"E\"\n",
rrddim_id(data[i].rd));
}
now = min_end_time + 1;
}
#ifdef NETDATA_INTERNAL_CHECKS
if(actual_after) {
char actual_after_buf[LOG_DATE_LENGTH + 1], actual_before_buf[LOG_DATE_LENGTH + 1];
log_date(actual_after_buf, LOG_DATE_LENGTH, actual_after);
log_date(actual_before_buf, LOG_DATE_LENGTH, actual_before);
internal_error(true,
"REPLAY: host '%s', chart '%s': sending data %llu [%s] to %llu [%s] (requested %llu [delta %lld] to %llu [delta %lld])",
rrdhost_hostname(st->rrdhost), rrdset_id(st),
(unsigned long long)actual_after, actual_after_buf, (unsigned long long)actual_before, actual_before_buf,
(unsigned long long)after, (long long)(actual_after - after), (unsigned long long)before, (long long)(actual_before - before));
}
else
internal_error(true,
"REPLAY: host '%s', chart '%s': nothing to send (requested %llu to %llu)",
rrdhost_hostname(st->rrdhost), rrdset_id(st),
(unsigned long long)after, (unsigned long long)before);
#endif
// release all the dictionary items acquired
// finalize the queries
for(size_t i = 0; i < dimensions && data[i].rda ;i++) {
ops->finalize(&data[i].handle);
dictionary_acquired_item_release(data[i].dict, data[i].rda);
}
return before;
}
static void replicate_chart_collection_state(BUFFER *wb, RRDSET *st) {
RRDDIM *rd;
rrddim_foreach_read(rd, st) {
buffer_sprintf(wb, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE " \"%s\" %llu %lld " NETDATA_DOUBLE_FORMAT_AUTO " " NETDATA_DOUBLE_FORMAT_AUTO "\n",
rrddim_id(rd),
(usec_t)rd->last_collected_time.tv_sec * USEC_PER_SEC + (usec_t)rd->last_collected_time.tv_usec,
rd->last_collected_value,
rd->last_calculated_value,
rd->last_stored_value
);
}
rrddim_foreach_done(rd);
buffer_sprintf(wb, PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE " %llu %llu " TOTAL_NUMBER_FORMAT " " TOTAL_NUMBER_FORMAT "\n",
(usec_t)st->last_collected_time.tv_sec * USEC_PER_SEC + (usec_t)st->last_collected_time.tv_usec,
(usec_t)st->last_updated.tv_sec * USEC_PER_SEC + (usec_t)st->last_updated.tv_usec,
st->last_collected_total,
st->collected_total
);
}
bool replicate_chart_response(RRDHOST *host, RRDSET *st, bool start_streaming, time_t after, time_t before) {
time_t query_after = after;
time_t query_before = before;
time_t now = now_realtime_sec();
// find the first entry we have
time_t first_entry_local = rrdset_first_entry_t(st);
if(first_entry_local > now) {
internal_error(true,
"RRDSET: '%s' first time %llu is in the future (now is %llu)",
rrdset_id(st), (unsigned long long)first_entry_local, (unsigned long long)now);
first_entry_local = now
|