| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "pluginsd_replication.h" |
| 4 | #include "streaming/stream-receiver-internals.h" |
| 5 | #include "streaming/stream-replication-receiver.h" |
| 6 | #include "streaming/stream-waiting-list.h" |
| 7 | #include "web/api/queries/backfill.h" |
| 8 | #include "database/rrddim-collection.h" |
| 9 | |
| 10 | static bool backfill_callback(size_t successful_dims __maybe_unused, size_t failed_dims __maybe_unused, struct backfill_request_data *brd) { |
| 11 | if(!object_state_acquire(&brd->host->state_id, brd->host_state_id)) { |
| 12 | // this may happen because the host got reconnected |
| 13 | |
| 14 | nd_log(NDLS_DAEMON, NDLP_DEBUG, |
| 15 | "PLUGINSD REPLAY ERROR: 'host:%s' failed to acquire host for sending replication" |
| 16 | " command for 'chart:%s'", |
| 17 | rrdhost_hostname(brd->host), |
| 18 | rrdset_id(brd->st)); |
| 19 | |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | __atomic_sub_fetch(&brd->host->stream.rcv.status.replication.backfill_pending, 1, __ATOMIC_RELAXED); |
| 24 | |
| 25 | bool rc = replicate_chart_request(send_to_plugin, brd->parser, brd->host, brd->st, |
| 26 | brd->first_entry_child, brd->last_entry_child, brd->child_wall_clock_time, |
| 27 | 0, 0); |
| 28 | |
| 29 | if (!rc) { |
| 30 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 31 | "PLUGINSD REPLAY ERROR: 'host:%s' failed to initiate replication for 'chart:%s' - replication may not proceed for this instance.", |
| 32 | rrdhost_hostname(brd->host), |
| 33 | rrdset_id(brd->st)); |
| 34 | } |
| 35 | |
| 36 | object_state_release(&brd->host->state_id); |
| 37 | return rc; |
| 38 | } |
| 39 | |
| 40 | PARSER_RC pluginsd_chart_definition_end(char **words, size_t num_words, PARSER *parser) { |
| 41 | const char *first_entry_txt = get_word(words, num_words, 1); |
| 42 | const char *last_entry_txt = get_word(words, num_words, 2); |
| 43 | const char *wall_clock_time_txt = get_word(words, num_words, 3); |
| 44 | |
| 45 | RRDHOST *host = pluginsd_require_scope_host(parser, PLUGINSD_KEYWORD_CHART_DEFINITION_END); |
| 46 | if(!host) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 47 | |
| 48 | RRDSET *st = pluginsd_require_scope_chart(parser, PLUGINSD_KEYWORD_CHART_DEFINITION_END, PLUGINSD_KEYWORD_CHART); |
| 49 | if(!st) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 50 | |
| 51 | time_t first_entry_child = (first_entry_txt && *first_entry_txt) ? (time_t)str2ul(first_entry_txt) : 0; |
| 52 | time_t last_entry_child = (last_entry_txt && *last_entry_txt) ? (time_t)str2ul(last_entry_txt) : 0; |
| 53 | time_t child_wall_clock_time = (wall_clock_time_txt && *wall_clock_time_txt) ? (time_t)str2ul(wall_clock_time_txt) : now_realtime_sec(); |
| 54 | |
| 55 | bool ok = true; |
| 56 | RRDSET_FLAGS old = rrdset_flag_set_and_clear( |
| 57 | st, RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS, RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED); |
| 58 | |
| 59 | if(!(old & RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS)) { |
| 60 | if(rrdhost_receiver_replicating_charts_plus_one(st->rrdhost) == 1) |
| 61 | pulse_host_status(host, PULSE_HOST_STATUS_RCV_REPLICATING, 0); |
| 62 | |
| 63 | __atomic_add_fetch(&host->stream.rcv.status.replication.counter_in, 1, __ATOMIC_RELAXED); |
| 64 | |
| 65 | #ifdef REPLICATION_TRACKING |
| 66 | st->stream.rcv.who = REPLAY_WHO_ME; |
| 67 | #endif |
| 68 | |
| 69 | #ifdef NETDATA_LOG_REPLICATION_REQUESTS |
| 70 | st->replay.start_streaming = false; |
| 71 | st->replay.after = 0; |
| 72 | st->replay.before = 0; |
| 73 | #endif |
| 74 | |
| 75 | struct backfill_request_data brd = { |
| 76 | .host_state_id = object_state_id(&host->state_id), |
| 77 | .parser = parser, |
| 78 | .host = host, |
| 79 | .st = st, |
| 80 | .first_entry_child = first_entry_child, |
| 81 | .last_entry_child = last_entry_child, |
| 82 | .child_wall_clock_time = child_wall_clock_time, |
| 83 | }; |
| 84 | |
| 85 | __atomic_add_fetch(&host->stream.rcv.status.replication.backfill_pending, 1, __ATOMIC_RELAXED); |
| 86 | |
| 87 | if(!rrdset_flag_check(st, RRDSET_FLAG_BACKFILLED_HIGH_TIERS)) { |
| 88 | ok = backfill_request_add(st, backfill_callback, &brd); |
| 89 | if (!ok) |
| 90 | ok = backfill_callback(0, 0, &brd); |
| 91 | else |
| 92 | rrdset_flag_set(st, RRDSET_FLAG_BACKFILLED_HIGH_TIERS); |
| 93 | } |
| 94 | else |
| 95 | ok = backfill_callback(0, 0, &brd); |
| 96 | } |
| 97 | else { |
| 98 | // this is normal, since dimensions may be added to a chart, |
| 99 | // and the child will send another CHART_DEFINITION_END command. |
| 100 | |
| 101 | #ifdef NETDATA_LOG_REPLICATION_REQUESTS |
| 102 | internal_error(true, "REPLAY: 'host:%s/chart:%s' not sending duplicate replication request", |
| 103 | rrdhost_hostname(st->rrdhost), rrdset_id(st)); |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | stream_thread_received_metadata(); |
| 108 | |
| 109 | return ok ? PARSER_RC_OK : PARSER_RC_ERROR; |
| 110 | } |
| 111 | |
| 112 | ALWAYS_INLINE PARSER_RC pluginsd_replay_begin(char **words, size_t num_words, PARSER *parser) { |
| 113 | int idx = 1; |
| 114 | ssize_t slot = pluginsd_parse_rrd_slot(words, num_words, PLUGINSD_CHART_SLOT_MAX); |
| 115 | if(slot >= 0) idx++; |
| 116 | |
| 117 | char *id = get_word(words, num_words, idx++); |
| 118 | char *start_time_str = get_word(words, num_words, idx++); |
| 119 | char *end_time_str = get_word(words, num_words, idx++); |
| 120 | char *child_now_str = get_word(words, num_words, idx++); |
| 121 | |
| 122 | RRDHOST *host = pluginsd_require_scope_host(parser, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 123 | if(!host) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 124 | |
| 125 | RRDSET *st; |
| 126 | if (likely(!id || !*id)) |
| 127 | st = pluginsd_require_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_BEGIN, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 128 | else |
| 129 | st = pluginsd_rrdset_cache_get_from_slot(parser, host, id, slot, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 130 | |
| 131 | if(!st) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 132 | |
| 133 | if(!pluginsd_set_scope_chart(parser, st, PLUGINSD_KEYWORD_REPLAY_BEGIN)) |
| 134 | return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 135 | |
| 136 | if(start_time_str && end_time_str) { |
| 137 | time_t start_time = (time_t) str2ull_encoded(start_time_str); |
| 138 | time_t end_time = (time_t) str2ull_encoded(end_time_str); |
| 139 | |
| 140 | time_t wall_clock_time = 0, tolerance; |
| 141 | bool wall_clock_comes_from_child; (void)wall_clock_comes_from_child; |
| 142 | if(child_now_str) { |
| 143 | wall_clock_time = (time_t) str2ull_encoded(child_now_str); |
| 144 | tolerance = st->update_every + 1; |
| 145 | wall_clock_comes_from_child = true; |
| 146 | } |
| 147 | |
| 148 | if(wall_clock_time <= 0) { |
| 149 | wall_clock_time = now_realtime_sec(); |
| 150 | tolerance = st->update_every + 5; |
| 151 | wall_clock_comes_from_child = false; |
| 152 | } |
| 153 | |
| 154 | #ifdef NETDATA_LOG_REPLICATION_REQUESTS |
| 155 | internal_error( |
| 156 | (!st->replay.start_streaming && (end_time < st->replay.after || start_time > st->replay.before)), |
| 157 | "REPLAY ERROR: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_BEGIN " from %ld to %ld, which does not match our request (%ld to %ld).", |
| 158 | rrdhost_hostname(st->rrdhost), rrdset_id(st), start_time, end_time, st->replay.after, st->replay.before); |
| 159 | |
| 160 | internal_error( |
| 161 | true, |
| 162 | "REPLAY: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_BEGIN " from %ld to %ld, child wall clock is %ld (%s), had requested %ld to %ld", |
| 163 | rrdhost_hostname(st->rrdhost), rrdset_id(st), |
| 164 | start_time, end_time, wall_clock_time, wall_clock_comes_from_child ? "from child" : "parent time", |
| 165 | st->replay.after, st->replay.before); |
| 166 | #endif |
| 167 | |
| 168 | if(start_time && end_time && start_time < wall_clock_time + tolerance && end_time < wall_clock_time + tolerance && start_time < end_time) { |
| 169 | if (unlikely(end_time - start_time != st->update_every)) |
| 170 | rrdset_set_update_every_s(st, end_time - start_time); |
| 171 | |
| 172 | st->last_collected_time.tv_sec = end_time; |
| 173 | st->last_collected_time.tv_usec = 0; |
| 174 | |
| 175 | st->last_updated.tv_sec = end_time; |
| 176 | st->last_updated.tv_usec = 0; |
| 177 | |
| 178 | st->counter++; |
| 179 | st->counter_done++; |
| 180 | |
| 181 | // these are only needed for db mode RAM, ALLOC |
| 182 | st->db.current_entry++; |
| 183 | if(st->db.current_entry >= st->db.entries) |
| 184 | st->db.current_entry -= st->db.entries; |
| 185 | |
| 186 | parser->user.replay.start_time = start_time; |
| 187 | parser->user.replay.end_time = end_time; |
| 188 | parser->user.replay.start_time_ut = (usec_t) start_time * USEC_PER_SEC; |
| 189 | parser->user.replay.end_time_ut = (usec_t) end_time * USEC_PER_SEC; |
| 190 | parser->user.replay.wall_clock_time = wall_clock_time; |
| 191 | parser->user.replay.rset_enabled = true; |
| 192 | |
| 193 | return PARSER_RC_OK; |
| 194 | } |
| 195 | |
| 196 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 197 | "PLUGINSD REPLAY ERROR: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_BEGIN |
| 198 | " from %ld to %ld, but timestamps are invalid " |
| 199 | "(now is %ld [%s], tolerance %ld). Ignoring " PLUGINSD_KEYWORD_REPLAY_SET, |
| 200 | rrdhost_hostname(st->rrdhost), rrdset_id(st), start_time, end_time, |
| 201 | wall_clock_time, wall_clock_comes_from_child ? "child wall clock" : "parent wall clock", |
| 202 | tolerance); |
| 203 | } |
| 204 | |
| 205 | // the child sends an RBEGIN without any parameters initially |
| 206 | // setting rset_enabled to false, means the RSET should not store any metrics |
| 207 | // to store metrics, the RBEGIN needs to have timestamps |
| 208 | parser->user.replay.start_time = 0; |
| 209 | parser->user.replay.end_time = 0; |
| 210 | parser->user.replay.start_time_ut = 0; |
| 211 | parser->user.replay.end_time_ut = 0; |
| 212 | parser->user.replay.wall_clock_time = 0; |
| 213 | parser->user.replay.rset_enabled = false; |
| 214 | return PARSER_RC_OK; |
| 215 | } |
| 216 | |
| 217 | ALWAYS_INLINE PARSER_RC pluginsd_replay_set(char **words, size_t num_words, PARSER *parser) { |
| 218 | int idx = 1; |
| 219 | ssize_t slot = pluginsd_parse_rrd_slot(words, num_words, PLUGINSD_DIMENSION_SLOT_MAX); |
| 220 | if(slot >= 0) idx++; |
| 221 | |
| 222 | char *dimension = get_word(words, num_words, idx++); |
| 223 | char *value_str = get_word(words, num_words, idx++); |
| 224 | char *flags_str = get_word(words, num_words, idx++); |
| 225 | |
| 226 | RRDHOST *host = pluginsd_require_scope_host(parser, PLUGINSD_KEYWORD_REPLAY_SET); |
| 227 | if(!host) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 228 | |
| 229 | RRDSET *st = pluginsd_require_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_SET, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 230 | if(!st) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 231 | |
| 232 | if(!parser->user.replay.rset_enabled) { |
| 233 | nd_log_limit_static_thread_var(erl, 1, 0); |
| 234 | nd_log_limit(&erl, NDLS_COLLECTORS, NDLP_ERR, |
| 235 | "PLUGINSD REPLAY ERROR: 'host:%s/chart:%s' got a %s but it is disabled by %s errors", |
| 236 | rrdhost_hostname(host), rrdset_id(st), PLUGINSD_KEYWORD_REPLAY_SET, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 237 | |
| 238 | // we have to return OK here |
| 239 | return PARSER_RC_OK; |
| 240 | } |
| 241 | |
| 242 | RRDDIM *rd = pluginsd_acquire_dimension(host, st, dimension, slot, PLUGINSD_KEYWORD_REPLAY_SET); |
| 243 | if(!rd) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 244 | |
| 245 | st->pluginsd.set = true; |
| 246 | |
| 247 | if (unlikely(!parser->user.replay.start_time || !parser->user.replay.end_time)) { |
| 248 | |
| 249 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 250 | "PLUGINSD REPLAY ERROR: 'host:%s/chart:%s/dim:%s' got a %s with " |
| 251 | "invalid timestamps %ld to %ld from a %s. Disabling it.", |
| 252 | rrdhost_hostname(host), rrdset_id(st), dimension, PLUGINSD_KEYWORD_REPLAY_SET, |
| 253 | parser->user.replay.start_time, parser->user.replay.end_time, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 254 | |
| 255 | return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 256 | } |
| 257 | |
| 258 | if (unlikely(!value_str || !*value_str)) |
| 259 | value_str = "NAN"; |
| 260 | |
| 261 | if(unlikely(!flags_str)) |
| 262 | flags_str = ""; |
| 263 | |
| 264 | if (likely(value_str)) { |
| 265 | NETDATA_DOUBLE value = str2ndd_encoded(value_str, NULL); |
| 266 | SN_FLAGS flags = pluginsd_parse_storage_number_flags(flags_str); |
| 267 | |
| 268 | if (!netdata_double_isnumber(value) || (flags == SN_EMPTY_SLOT)) { |
| 269 | value = NAN; |
| 270 | flags = SN_EMPTY_SLOT; |
| 271 | } |
| 272 | |
| 273 | rrddim_store_metric(rd, parser->user.replay.end_time_ut, value, flags); |
| 274 | rd->collector.last_collected_time.tv_sec = parser->user.replay.end_time; |
| 275 | rd->collector.last_collected_time.tv_usec = 0; |
| 276 | rd->collector.counter++; |
| 277 | } |
| 278 | |
| 279 | return PARSER_RC_OK; |
| 280 | } |
| 281 | |
| 282 | ALWAYS_INLINE PARSER_RC pluginsd_replay_rrddim_collection_state(char **words, size_t num_words, PARSER *parser) { |
| 283 | if(parser->user.replay.rset_enabled == false) |
| 284 | return PARSER_RC_OK; |
| 285 | |
| 286 | int idx = 1; |
| 287 | ssize_t slot = pluginsd_parse_rrd_slot(words, num_words, PLUGINSD_DIMENSION_SLOT_MAX); |
| 288 | if(slot >= 0) idx++; |
| 289 | |
| 290 | char *dimension = get_word(words, num_words, idx++); |
| 291 | char *last_collected_ut_str = get_word(words, num_words, idx++); |
| 292 | char *last_collected_value_str = get_word(words, num_words, idx++); |
| 293 | char *last_calculated_value_str = get_word(words, num_words, idx++); |
| 294 | char *last_stored_value_str = get_word(words, num_words, idx++); |
| 295 | |
| 296 | RRDHOST *host = pluginsd_require_scope_host(parser, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE); |
| 297 | if(!host) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 298 | |
| 299 | RRDSET *st = pluginsd_require_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 300 | if(!st) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 301 | |
| 302 | if(st->pluginsd.set) { |
| 303 | // reset pos to reuse the same RDAs |
| 304 | __atomic_store_n(&st->pluginsd.pos, 0, __ATOMIC_RELAXED); |
| 305 | st->pluginsd.set = false; |
| 306 | } |
| 307 | |
| 308 | RRDDIM *rd = pluginsd_acquire_dimension(host, st, dimension, slot, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE); |
| 309 | if(!rd) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 310 | |
| 311 | usec_t dim_last_collected_ut = (usec_t)rd->collector.last_collected_time.tv_sec * USEC_PER_SEC + (usec_t)rd->collector.last_collected_time.tv_usec; |
| 312 | usec_t last_collected_ut = last_collected_ut_str ? str2ull_encoded(last_collected_ut_str) : 0; |
| 313 | if(last_collected_ut > dim_last_collected_ut) { |
| 314 | rd->collector.last_collected_time.tv_sec = (time_t)(last_collected_ut / USEC_PER_SEC); |
| 315 | rd->collector.last_collected_time.tv_usec = (suseconds_t)(last_collected_ut % USEC_PER_SEC); |
| 316 | } |
| 317 | |
| 318 | // The sender only sends float baselines when it has STREAM_CAP_FLOAT_BASELINE; |
| 319 | // older senders always send int64, even for float dimensions. |
| 320 | bool sender_sent_float = rrddim_is_float(rd) && (parser->user.capabilities & STREAM_CAP_FLOAT_BASELINE); |
| 321 | if(sender_sent_float) |
| 322 | rrddim_set_last_collected_float(rd, last_collected_value_str ? str2ndd_encoded(last_collected_value_str, NULL) : 0.0); |
| 323 | else if(rrddim_is_float(rd)) |
| 324 | rrddim_set_last_collected_float(rd, last_collected_value_str ? (NETDATA_DOUBLE)str2ll_encoded(last_collected_value_str) : 0.0); |
| 325 | else |
| 326 | rrddim_set_last_collected_int(rd, last_collected_value_str ? str2ll_encoded(last_collected_value_str) : 0); |
| 327 | rd->collector.last_calculated_value = last_calculated_value_str ? str2ndd_encoded(last_calculated_value_str, NULL) : 0; |
| 328 | rd->collector.last_stored_value = last_stored_value_str ? str2ndd_encoded(last_stored_value_str, NULL) : 0.0; |
| 329 | |
| 330 | return PARSER_RC_OK; |
| 331 | } |
| 332 | |
| 333 | ALWAYS_INLINE PARSER_RC pluginsd_replay_rrdset_collection_state(char **words, size_t num_words, PARSER *parser) { |
| 334 | if(parser->user.replay.rset_enabled == false) |
| 335 | return PARSER_RC_OK; |
| 336 | |
| 337 | char *last_collected_ut_str = get_word(words, num_words, 1); |
| 338 | char *last_updated_ut_str = get_word(words, num_words, 2); |
| 339 | |
| 340 | RRDHOST *host = pluginsd_require_scope_host(parser, PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE); |
| 341 | if(!host) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 342 | |
| 343 | RRDSET *st = pluginsd_require_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE, |
| 344 | PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 345 | if(!st) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 346 | |
| 347 | usec_t chart_last_collected_ut = (usec_t)st->last_collected_time.tv_sec * USEC_PER_SEC + (usec_t)st->last_collected_time.tv_usec; |
| 348 | usec_t last_collected_ut = last_collected_ut_str ? str2ull_encoded(last_collected_ut_str) : 0; |
| 349 | if(last_collected_ut > chart_last_collected_ut) { |
| 350 | st->last_collected_time.tv_sec = (time_t)(last_collected_ut / USEC_PER_SEC); |
| 351 | st->last_collected_time.tv_usec = (suseconds_t)(last_collected_ut % USEC_PER_SEC); |
| 352 | } |
| 353 | |
| 354 | usec_t chart_last_updated_ut = (usec_t)st->last_updated.tv_sec * USEC_PER_SEC + (usec_t)st->last_updated.tv_usec; |
| 355 | usec_t last_updated_ut = last_updated_ut_str ? str2ull_encoded(last_updated_ut_str) : 0; |
| 356 | if(last_updated_ut > chart_last_updated_ut) { |
| 357 | st->last_updated.tv_sec = (time_t)(last_updated_ut / USEC_PER_SEC); |
| 358 | st->last_updated.tv_usec = (suseconds_t)(last_updated_ut % USEC_PER_SEC); |
| 359 | } |
| 360 | |
| 361 | st->counter++; |
| 362 | st->counter_done++; |
| 363 | |
| 364 | return PARSER_RC_OK; |
| 365 | } |
| 366 | |
| 367 | ALWAYS_INLINE PARSER_RC pluginsd_replay_end(char **words, size_t num_words, PARSER *parser) { |
| 368 | if (num_words < 7) { // accepts 7, but the 7th is optional |
| 369 | nd_log(NDLS_DAEMON, NDLP_ERR, "REPLAY: malformed " PLUGINSD_KEYWORD_REPLAY_END " command"); |
| 370 | RRDSET *st = pluginsd_get_scope_chart(parser); |
| 371 | if(st) |
| 372 | st->replication_empty_response_count = 0; |
| 373 | return PARSER_RC_ERROR; |
| 374 | } |
| 375 | |
| 376 | const char *update_every_child_txt = get_word(words, num_words, 1); |
| 377 | const char *first_entry_child_txt = get_word(words, num_words, 2); |
| 378 | const char *last_entry_child_txt = get_word(words, num_words, 3); |
| 379 | const char *start_streaming_txt = get_word(words, num_words, 4); |
| 380 | const char *first_entry_requested_txt = get_word(words, num_words, 5); |
| 381 | const char *last_entry_requested_txt = get_word(words, num_words, 6); |
| 382 | const char *child_world_time_txt = get_word(words, num_words, 7); // optional |
| 383 | |
| 384 | time_t update_every_child = (time_t) str2ull_encoded(update_every_child_txt); |
| 385 | time_t first_entry_child = (time_t) str2ull_encoded(first_entry_child_txt); |
| 386 | time_t last_entry_child = (time_t) str2ull_encoded(last_entry_child_txt); |
| 387 | |
| 388 | bool start_streaming = stream_parse_enable_streaming(start_streaming_txt); |
| 389 | time_t first_entry_requested = (time_t) str2ull_encoded(first_entry_requested_txt); |
| 390 | time_t last_entry_requested = (time_t) str2ull_encoded(last_entry_requested_txt); |
| 391 | |
| 392 | // the optional child world time |
| 393 | time_t child_world_time = (child_world_time_txt && *child_world_time_txt) ? (time_t) str2ull_encoded( |
| 394 | child_world_time_txt) : now_realtime_sec(); |
| 395 | |
| 396 | RRDHOST *host = pluginsd_require_scope_host(parser, PLUGINSD_KEYWORD_REPLAY_END); |
| 397 | if(!host) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 398 | __atomic_add_fetch(&host->stream.rcv.status.replication.counter_in, 1, __ATOMIC_RELAXED); |
| 399 | |
| 400 | RRDSET *st = pluginsd_require_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_END, PLUGINSD_KEYWORD_REPLAY_BEGIN); |
| 401 | if(!st) return PLUGINSD_DISABLE_PLUGIN(parser, NULL, NULL); |
| 402 | |
| 403 | #ifdef NETDATA_LOG_REPLICATION_REQUESTS |
| 404 | internal_error(true, |
| 405 | "PLUGINSD REPLAY: 'host:%s/chart:%s': got a " PLUGINSD_KEYWORD_REPLAY_END " child db from %llu to %llu, start_streaming %s, had requested from %llu to %llu, wall clock %llu", |
| 406 | rrdhost_hostname(host), rrdset_id(st), |
| 407 | (unsigned long long)first_entry_child, (unsigned long long)last_entry_child, |
| 408 | start_streaming?"true":"false", |
| 409 | (unsigned long long)first_entry_requested, (unsigned long long)last_entry_requested, |
| 410 | (unsigned long long)child_world_time |
| 411 | ); |
| 412 | #endif |
| 413 | |
| 414 | parser->user.data_collections_count++; |
| 415 | |
| 416 | // Reset empty response counter when we receive actual data |
| 417 | if(parser->user.replay.rset_enabled && st) |
| 418 | st->replication_empty_response_count = 0; |
| 419 | |
| 420 | if(parser->user.replay.rset_enabled && st->rrdhost->receiver) { |
| 421 | time_t now = now_realtime_sec(); |
| 422 | time_t started = st->rrdhost->receiver->replication.first_time_s; |
| 423 | time_t current = parser->user.replay.end_time; |
| 424 | |
| 425 | if(started && current > started) { |
| 426 | host->stream.rcv.status.replication.percent = (NETDATA_DOUBLE) (current - started) * 100.0 / (NETDATA_DOUBLE) (now - started); |
| 427 | worker_set_metric(WORKER_RECEIVER_JOB_REPLICATION_COMPLETION, |
| 428 | host->stream.rcv.status.replication.percent); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | parser->user.replay.start_time = 0; |
| 433 | parser->user.replay.end_time = 0; |
| 434 | parser->user.replay.start_time_ut = 0; |
| 435 | parser->user.replay.end_time_ut = 0; |
| 436 | parser->user.replay.wall_clock_time = 0; |
| 437 | parser->user.replay.rset_enabled = false; |
| 438 | |
| 439 | st->counter++; |
| 440 | st->counter_done++; |
| 441 | store_metric_collection_completed(); |
| 442 | |
| 443 | #ifdef NETDATA_LOG_REPLICATION_REQUESTS |
| 444 | st->replay.start_streaming = false; |
| 445 | st->replay.after = 0; |
| 446 | st->replay.before = 0; |
| 447 | if(start_streaming) |
| 448 | st->replay.log_next_data_collection = true; |
| 449 | #endif |
| 450 | |
| 451 | if(start_streaming) |
| 452 | st->replication_empty_response_count = 0; |
| 453 | |
| 454 | if (start_streaming) { |
| 455 | #ifdef REPLICATION_TRACKING |
| 456 | st->stream.rcv.who = REPLAY_WHO_FINISHED; |
| 457 | #endif |
| 458 | |
| 459 | if (st->update_every != update_every_child) |
| 460 | rrdset_set_update_every_s(st, update_every_child); |
| 461 | |
| 462 | RRDSET_FLAGS old = rrdset_flag_set_and_clear( |
| 463 | st, RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED, |
| 464 | RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS | RRDSET_FLAG_SYNC_CLOCK); |
| 465 | |
| 466 | if(!(old & RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED)) { |
| 467 | if(rrdhost_receiver_replicating_charts_minus_one(st->rrdhost) == 0) |
| 468 | pulse_host_status(host, PULSE_HOST_STATUS_RCV_RUNNING, 0); |
| 469 | } |
| 470 | else |
| 471 | nd_log(NDLS_DAEMON, NDLP_WARNING, |
| 472 | "PLUGINSD REPLAY ERROR: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_END " " |
| 473 | "with enable_streaming = true, but there was no replication in progress for this chart.", |
| 474 | rrdhost_hostname(host), rrdset_id(st)); |
| 475 | |
| 476 | pluginsd_clear_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_END, NULL); |
| 477 | |
| 478 | host->stream.rcv.status.replication.percent = 100.0; |
| 479 | worker_set_metric(WORKER_RECEIVER_JOB_REPLICATION_COMPLETION, host->stream.rcv.status.replication.percent); |
| 480 | |
| 481 | stream_thread_received_replication(); |
| 482 | |
| 483 | return PARSER_RC_OK; |
| 484 | } |
| 485 | |
| 486 | // ======================================================================== |
| 487 | // SAFETY NET: Detect stuck replication loops |
| 488 | // ======================================================================== |
| 489 | // |
| 490 | // We received start_streaming=false, which means we need to send another |
| 491 | // replication request. However, we need to detect if we're stuck in an |
| 492 | // infinite retry loop where no progress is being made. |
| 493 | // |
| 494 | // This can happen when: |
| 495 | // 1. Parent already has newer data than child |
| 496 | // 2. Child keeps splitting responses due to buffer constraints |
| 497 | // 3. Network issues causing repeated empty/failed responses |
| 498 | |
| 499 | // Check parent's current retention to detect if we're already caught up |
| 500 | time_t local_first_entry = 0, local_last_entry = 0; |
| 501 | rrdset_get_retention_of_tier_for_collected_chart( |
| 502 | st, &local_first_entry, &local_last_entry, now_realtime_sec(), 0); |
| 503 | |
| 504 | // Detect suspicious pattern: parent requested data but is already caught up |
| 505 | // This indicates we're in a loop where child keeps splitting responses |
| 506 | // even though parent doesn't need more data. |
| 507 | bool parent_already_caught_up = (local_last_entry >= last_entry_child); |
| 508 | bool requested_non_empty_range = (first_entry_requested != 0 || last_entry_requested != 0); |
| 509 | bool is_suspicious_response = (requested_non_empty_range && parent_already_caught_up); |
| 510 | |
| 511 | bool should_check_for_stuck_replication = false; |
| 512 | |
| 513 | // Track consecutive suspicious responses - applies to all builds |
| 514 | if(is_suspicious_response) { |
| 515 | st->replication_empty_response_count++; |
| 516 | // After 3 consecutive suspicious responses, we need to investigate |
| 517 | if(st->replication_empty_response_count >= 3) { |
| 518 | should_check_for_stuck_replication = true; |
| 519 | } |
| 520 | } else { |
| 521 | // Reset counter if this was a legitimate response (parent still catching up) |
| 522 | st->replication_empty_response_count = 0; |
| 523 | } |
| 524 | |
| 525 | if (should_check_for_stuck_replication) { |
| 526 | // We already have local_first_entry and local_last_entry from above |
| 527 | |
| 528 | // Check multiple conditions to ensure we're truly stuck: |
| 529 | // |
| 530 | // Condition 1: Parent has data that covers or exceeds child's retention |
| 531 | // (We already checked this in parent_already_caught_up, but verify again) |
| 532 | bool parent_has_equal_or_newer_data = (local_last_entry >= last_entry_child); |
| 533 | |
| 534 | // Calculate the gap for logging purposes |
| 535 | time_t gap_to_child = (last_entry_child > local_last_entry) ? |
| 536 | (last_entry_child - local_last_entry) : 0; |
| 537 | |
| 538 | // Condition 2: Parent's data is reasonably recent |
| 539 | time_t wall_clock = now_realtime_sec(); |
| 540 | bool parent_data_is_recent = (local_last_entry > 0 && |
| 541 | (wall_clock - local_last_entry) < 300); |
| 542 | |
| 543 | // Only finish replication if parent has equal or newer data than child |
| 544 | // Do NOT terminate if there's any gap, as that would cause data loss |
| 545 | if (parent_has_equal_or_newer_data) { |
| 546 | |
| 547 | // Log with appropriate level based on confidence |
| 548 | ND_LOG_FIELD_PRIORITY level = (parent_has_equal_or_newer_data && parent_data_is_recent) ? |
| 549 | NDLP_INFO : NDLP_WARNING; |
| 550 | |
| 551 | nd_log(NDLS_DAEMON, level, |
| 552 | "PLUGINSD REPLAY: 'host:%s/chart:%s' detected stuck replication loop. " |
| 553 | "Parent last entry: %llu, Child last entry: %llu, Gap: %llu seconds, " |
| 554 | "Empty responses: %u. Forcing replication to finish.", |
| 555 | rrdhost_hostname(host), rrdset_id(st), |
| 556 | (unsigned long long)local_last_entry, |
| 557 | (unsigned long long)last_entry_child, |
| 558 | (unsigned long long)gap_to_child, |
| 559 | (unsigned int)st->replication_empty_response_count |
| 560 | ); |
| 561 | |
| 562 | st->replication_empty_response_count = 0; |
| 563 | |
| 564 | // IMPORTANT: Mark as finished and decrement counter NOW, before sending final request. |
| 565 | // This prevents infinite loops even if child continues to respond with start_streaming=false. |
| 566 | // The next REPLAY_END will see FINISHED flag and handle accordingly. |
| 567 | RRDSET_FLAGS old = rrdset_flag_set_and_clear( |
| 568 | st, RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED, |
| 569 | RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS | RRDSET_FLAG_SYNC_CLOCK); |
| 570 | |
| 571 | if(!(old & RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED)) { |
| 572 | if(rrdhost_receiver_replicating_charts_minus_one(st->rrdhost) == 0) |
| 573 | pulse_host_status(host, PULSE_HOST_STATUS_RCV_RUNNING, 0); |
| 574 | } |
| 575 | |
| 576 | pluginsd_clear_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_END, NULL); |
| 577 | host->stream.rcv.status.replication.percent = 100.0; |
| 578 | worker_set_metric(WORKER_RECEIVER_JOB_REPLICATION_COMPLETION, host->stream.rcv.status.replication.percent); |
| 579 | |
| 580 | // Send one final request to notify child. If child responds with start_streaming=true, |
| 581 | // it will start streaming. If it responds with start_streaming=false, the next |
| 582 | // REPLAY_END will see the FINISHED flag and log a warning but not loop forever. |
| 583 | bool ok = replicate_chart_request(send_to_plugin, parser, host, st, |
| 584 | first_entry_child, last_entry_child, child_world_time, |
| 585 | 0, 0); // prev_wanted = 0,0 to trigger empty request path |
| 586 | |
| 587 | return ok ? PARSER_RC_OK : PARSER_RC_ERROR; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | #ifdef REPLICATION_TRACKING |
| 592 | st->stream.rcv.who = REPLAY_WHO_ME; |
| 593 | #endif |
| 594 | |
| 595 | pluginsd_clear_scope_chart(parser, PLUGINSD_KEYWORD_REPLAY_END, NULL); |
| 596 | |
| 597 | rrdcontext_updated_retention_rrdset(st); |
| 598 | |
| 599 | bool ok = replicate_chart_request(send_to_plugin, parser, host, st, |
| 600 | first_entry_child, last_entry_child, child_world_time, |
| 601 | first_entry_requested, last_entry_requested); |
| 602 | |
| 603 | return ok ? PARSER_RC_OK : PARSER_RC_ERROR; |
| 604 | } |