148
149
if (unlikely(!compressed_message_size)) {
150
nd_log(NDLS_DAEMON, NDLP_ERR,
151
- "STREAM RECEIVE[x] '%s' [from [%s]:%s]: multiplexed uncompressed data in compressed stream!",
151
+ "STREAM RCV[x] '%s' [from [%s]:%s]: multiplexed uncompressed data in compressed stream!",
152
rrdhost_hostname(r->host), r->client_ip, r->client_port);
153
return DECOMPRESS_FAILED;
154
}
155
156
if(unlikely(compressed_message_size > COMPRESSION_MAX_MSG_SIZE)) {
157
nd_log(NDLS_DAEMON, NDLP_ERR,
158
- "STREAM RECEIVE[x] '%s' [from [%s]:%s]: received a compressed message of %zu bytes, "
158
+ "STREAM RCV[x] '%s' [from [%s]:%s]: received a compressed message of %zu bytes, "
159
"which is bigger than the max compressed message "
160
"size supported of %zu. Ignoring message.",
161
rrdhost_hostname(r->host), r->client_ip, r->client_port,
174
175
if (unlikely(!bytes_to_parse)) {
176
nd_log(NDLS_DAEMON, NDLP_ERR,
177
- "STREAM RECEIVE[x] '%s' [from [%s]:%s]: no bytes to decompress.",
177
+ "STREAM RCV[x] '%s' [from [%s]:%s]: no bytes to decompress.",
178
rrdhost_hostname(r->host), r->client_ip, r->client_port);
179
return DECOMPRESS_FAILED;
180
}
265
STREAM_CIRCULAR_BUFFER_STATS stats = *stream_circular_buffer_stats_unsafe(rpt->thread.send_to_child.scb);
266
spinlock_unlock(&rpt->thread.send_to_child.spinlock);
267
nd_log(NDLS_DAEMON, NDLP_ERR,
268
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: send buffer is full (buffer size %u, max %u, used %u, available %u). "
268
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: send buffer is full (buffer size %u, max %u, used %u, available %u). "
269
"Restarting connection.",
270
sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port,
271
stats.bytes_size, stats.bytes_max_size, stats.bytes_outstanding, stats.bytes_available);
275
}
276
277
nd_log(NDLS_DAEMON, NDLP_ERR,
278
- "STREAM RECEIVE[%zu]: invalid msg id %u", sth->id, (unsigned)msg->opcode);
278
+ "STREAM RCV[%zu]: invalid msg id %u", sth->id, (unsigned)msg->opcode);
279
}
280
281
static ssize_t send_to_child(const char *txt, void *data, STREAM_TRAFFIC_TYPE type) {
400
char buf[128];
401
duration_snprintf(buf, sizeof(buf), now - last_db_entry, "s", true);
402
nd_log(NDLS_DAEMON, NDLP_NOTICE,
403
- "STREAM RECEIVE '%s' [from [%s]:%s]: node connected; last sample in the database %s ago",
403
+ "STREAM RCV '%s' [from [%s]:%s]: node connected; last sample in the database %s ago",
404
rrdhost_hostname(host), rpt->client_ip, rpt->client_port, buf);
405
}
406
407
-void stream_receiver_move_queue_to_running_unsafe(struct stream_thread *sth) {
407
+void stream_receiver_move_to_running_unsafe(struct stream_thread *sth, struct receiver_state *rpt) {
408
internal_fatal(sth->tid != gettid_cached(), "Function %s() should only be used by the dispatcher thread", __FUNCTION__ );
409
410
- // process the queue
411
- Word_t idx = 0;
412
- for(struct receiver_state *rpt = RECEIVERS_FIRST(&sth->queue.receivers, &idx);
413
- rpt;
414
- rpt = RECEIVERS_NEXT(&sth->queue.receivers, &idx)) {
415
- worker_is_busy(WORKER_STREAM_JOB_DEQUEUE);
410
+ worker_is_busy(WORKER_STREAM_JOB_DEQUEUE);
411
417
- RECEIVERS_DEL(&sth->queue.receivers, (Word_t)rpt);
412
+ ND_LOG_STACK lgs[] = {
413
+ ND_LOG_FIELD_STR(NDF_NIDL_NODE, rpt->host->hostname),
414
+ ND_LOG_FIELD_UUID(NDF_MESSAGE_ID, &streaming_to_parent_msgid),
415
+ ND_LOG_FIELD_END(),
416
+ };
417
+ ND_LOG_STACK_PUSH(lgs);
418
419
- ND_LOG_STACK lgs[] = {
420
- ND_LOG_FIELD_STR(NDF_NIDL_NODE, rpt->host->hostname),
421
- ND_LOG_FIELD_UUID(NDF_MESSAGE_ID, &streaming_to_parent_msgid),
422
- ND_LOG_FIELD_END(),
423
- };
424
- ND_LOG_STACK_PUSH(lgs);
419
+ nd_log(NDLS_DAEMON, NDLP_DEBUG,
420
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: moving host from receiver queue to receiver running...",
421
+ sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port);
422
426
- nd_log(NDLS_DAEMON, NDLP_DEBUG,
427
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: moving host from receiver queue to receiver running...",
428
- sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port);
423
+ rpt->host->stream.rcv.status.tid = gettid_cached();
424
+ rpt->thread.meta.type = POLLFD_TYPE_RECEIVER;
425
+ rpt->thread.meta.rpt = rpt;
426
430
- rpt->host->stream.rcv.status.tid = gettid_cached();
431
- rpt->thread.meta.type = POLLFD_TYPE_RECEIVER;
432
- rpt->thread.meta.rpt = rpt;
427
+ spinlock_lock(&rpt->thread.send_to_child.spinlock);
428
+ rpt->thread.send_to_child.scb = stream_circular_buffer_create();
429
+ rpt->thread.send_to_child.msg.thread_slot = (int32_t)sth->id;
430
+ rpt->thread.send_to_child.msg.session = os_random32();
431
+ rpt->thread.send_to_child.msg.meta = &rpt->thread.meta;
432
+ spinlock_unlock(&rpt->thread.send_to_child.spinlock);
433
434
- spinlock_lock(&rpt->thread.send_to_child.spinlock);
435
- rpt->thread.send_to_child.scb = stream_circular_buffer_create();
436
- rpt->thread.send_to_child.msg.thread_slot = (int32_t)sth->id;
437
- rpt->thread.send_to_child.msg.session = os_random32();
438
- rpt->thread.send_to_child.msg.meta = &rpt->thread.meta;
439
- spinlock_unlock(&rpt->thread.send_to_child.spinlock);
434
+ internal_fatal(META_GET(&sth->run.meta, (Word_t)&rpt->thread.meta) != NULL, "Receiver to be added is already in the list of receivers");
435
+ META_SET(&sth->run.meta, (Word_t)&rpt->thread.meta, &rpt->thread.meta);
436
441
- internal_fatal(META_GET(&sth->run.meta, (Word_t)&rpt->thread.meta) != NULL, "Receiver to be added is already in the list of receivers");
442
- META_SET(&sth->run.meta, (Word_t)&rpt->thread.meta, &rpt->thread.meta);
437
+ if(sock_setnonblock(rpt->sock.fd) < 0)
438
+ nd_log(NDLS_DAEMON, NDLP_ERR,
439
+ "STREAM RCV '%s' [from [%s]:%s]: cannot set the non-blocking flag from socket %d",
440
+ rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, rpt->sock.fd);
441
444
- if(sock_setnonblock(rpt->sock.fd) < 0)
445
- nd_log(NDLS_DAEMON, NDLP_ERR,
446
- "STREAM RECEIVE '%s' [from [%s]:%s]: cannot set the non-blocking flag from socket %d",
447
- rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, rpt->sock.fd);
442
+ if(!nd_poll_add(sth->run.ndpl, rpt->sock.fd, ND_POLL_READ, &rpt->thread.meta))
443
+ nd_log(NDLS_DAEMON, NDLP_ERR,
444
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]:"
445
+ "Failed to add receiver socket to nd_poll()",
446
+ sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port);
447
449
- if(!nd_poll_add(sth->run.ndpl, rpt->sock.fd, ND_POLL_READ, &rpt->thread.meta))
450
- nd_log(NDLS_DAEMON, NDLP_ERR,
451
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]:"
452
- "Failed to add receiver socket to nd_poll()",
453
- sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port);
448
+ stream_receive_log_database_gap(rpt);
449
455
- stream_receive_log_database_gap(rpt);
450
+ // keep this last, since it sends commands back to the child
451
+ streaming_parser_init(rpt);
452
+}
453
457
- // keep this last, since it sends commands back to the child
458
- streaming_parser_init(rpt);
454
+void stream_receiver_move_entire_queue_to_running_unsafe(struct stream_thread *sth) {
455
+ internal_fatal(sth->tid != gettid_cached(), "Function %s() should only be used by the dispatcher thread", __FUNCTION__ );
456
+
457
+ // process the queue
458
+ Word_t idx = 0;
459
+ for(struct receiver_state *rpt = RECEIVERS_FIRST(&sth->queue.receivers, &idx);
460
+ rpt;
461
+ rpt = RECEIVERS_NEXT(&sth->queue.receivers, &idx)) {
462
+ RECEIVERS_DEL(&sth->queue.receivers, idx);
463
+ stream_receiver_move_to_running_unsafe(sth, rpt);
464
}
465
}
466
469
470
errno_clear();
471
nd_log(NDLS_DAEMON, NDLP_ERR,
467
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: "
472
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: "
473
"receiver disconnected: %s"
474
, sth->id
475
, rpt->hostname ? rpt->hostname : "-"
661
worker_is_busy(WORKER_SENDER_JOB_DISCONNECT_SOCKET_ERROR);
662
663
nd_log(NDLS_DAEMON, NDLP_ERR,
659
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: %s - closing connection",
664
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: %s - closing connection",
665
sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, error);
666
667
receiver_set_exit_reason(rpt, STREAM_HANDSHAKE_DISCONNECT_SOCKET_ERROR, false);
688
if (!stats->bytes_outstanding) {
689
if (!nd_poll_upd(sth->run.ndpl, rpt->sock.fd, ND_POLL_READ, &rpt->thread.meta))
690
nd_log(NDLS_DAEMON, NDLP_ERR,
686
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: cannot update nd_poll()",
691
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: cannot update nd_poll()",
692
sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port);
693
694
// recreate the circular buffer if we have to
716
if (disconnect_reason) {
717
worker_is_busy(WORKER_SENDER_JOB_DISCONNECT_SEND_ERROR);
718
nd_log(NDLS_DAEMON, NDLP_ERR,
714
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: %s (%zd, on fd %d) - closing connection - "
719
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: %s (%zd, on fd %d) - closing connection - "
720
"we have sent %zu bytes in %zu operations.",
721
sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port,
722
disconnect_reason, rc, rpt->sock.fd, stats->bytes_sent, stats->sends);
750
else if (rc == 0 || errno == ECONNRESET) {
751
worker_is_busy(WORKER_SENDER_JOB_DISCONNECT_REMOTE_CLOSED);
752
nd_log(NDLS_DAEMON, NDLP_ERR,
748
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: socket %d reports EOF (closed by child).",
753
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: socket %d reports EOF (closed by child).",
754
sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, rpt->sock.fd);
755
receiver_set_exit_reason(rpt, STREAM_HANDSHAKE_DISCONNECT_SOCKET_CLOSED_BY_REMOTE_END, false);
756
stream_receiver_remove(sth, rpt, "socket reports EOF (closed by child)");
766
else {
767
worker_is_busy(WORKER_SENDER_JOB_DISCONNECT_RECEIVE_ERROR);
768
nd_log(NDLS_DAEMON, NDLP_ERR,
764
- "STREAM RECEIVE[%zu] '%s' [from [%s]:%s]: error during receive (%zd, on fd %d) - closing connection.",
769
+ "STREAM RCV[%zu] '%s' [from [%s]:%s]: error during receive (%zd, on fd %d) - closing connection.",
770
sth->id, rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, rc, rpt->sock.fd);
771
receiver_set_exit_reason(rpt, STREAM_HANDSHAKE_DISCONNECT_SOCKET_READ_FAILED, false);
772
stream_receiver_remove(sth, rpt, "error during receive");
806
rrdhost_receiver_lock(host);
807
808
if (!host->receiver) {
809
+ __atomic_add_fetch(&host->stream.rcv.status.state_id, 1, __ATOMIC_RELAXED);
810
+
811
rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
812
813
host->stream.rcv.status.connections++;
826
if (rpt->config.health.delay > 0) {
827
host->health.delay_up_to = now_realtime_sec() + rpt->config.health.delay;
828
nd_log(NDLS_DAEMON, NDLP_DEBUG,
822
- "STREAM RECEIVE '%s' [from [%s]:%s]: "
829
+ "STREAM RCV '%s' [from [%s]:%s]: "
830
"Postponing health checks for %" PRId64 " seconds, because it was just connected.",
831
rrdhost_hostname(host), rpt->client_ip, rpt->client_port,
832
(int64_t) rpt->config.health.delay);
870
// Make sure that we detach this thread and don't kill a freshly arriving receiver
871
872
if (host->receiver == rpt) {
873
+ __atomic_add_fetch(&host->stream.rcv.status.state_id, 1, __ATOMIC_RELAXED);
874
rrdhost_flag_clear(host, RRDHOST_FLAG_COLLECTOR_ONLINE);
875
rrdhost_receiver_unlock(host);
876
{
936
}
937
938
if(host->receiver)
931
- netdata_log_error("STREAM RECEIVE[x] '%s' [from [%s]:%s]: "
939
+ netdata_log_error("STREAM RCV[x] '%s' [from [%s]:%s]: "
940
"streaming thread takes too long to stop, giving up..."
941
, rrdhost_hostname(host)
942
, host->receiver->client_ip, host->receiver->client_port);