Add hostname in the worker structure to avoid constant lookups (#13199)
Stelios Fragkakis committed
Jul 7, 2022 at 00:19 UTC
072fadc74deaa7435482e090c308cf53bbee202b
5 files changed
+40
-69
database/sqlite/sqlite_aclk.c
+11
-4
@@ -622,6 +622,8 @@ void aclk_database_worker(void *arg)
622
snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "AS_%s", wc->host->hostname);
623
uv_thread_set_name_np(wc->thread, threadname);
624
wc->host->dbsync_worker = wc;
625
+ if (unlikely(!wc->hostname))
626
+ wc->hostname = strdupz(wc->host->hostname);
627
aclk_del_worker_thread(wc);
628
wc->node_info_send = 1;
629
}
@@ -674,6 +676,7 @@ void aclk_database_worker(void *arg)
676
rrd_rdlock();
677
if (likely(wc->host))
678
wc->host->dbsync_worker = NULL;
679
+ freez(wc->hostname);
680
freez(wc);
681
rrd_unlock();
682
@@ -743,13 +746,17 @@ void sql_create_aclk_table(RRDHOST *host, uuid_t *host_uuid, uuid_t *node_id)
746
return;
747
748
struct aclk_database_worker_config *wc = callocz(1, sizeof(struct aclk_database_worker_config));
746
- if (likely(host))
747
- host->dbsync_worker = (void *) wc;
749
+ if (node_id && !uuid_is_null(*node_id))
750
+ uuid_unparse_lower(*node_id, wc->node_id);
751
+ if (likely(host)) {
752
+ host->dbsync_worker = (void *)wc;
753
+ wc->hostname = strdupz(host->hostname);
754
+ }
755
+ else
756
+ wc->hostname = get_hostname_by_node_id(wc->node_id);
757
wc->host = host;
758
strcpy(wc->uuid_str, uuid_str);
759
strcpy(wc->host_guid, host_guid);
751
- if (node_id && !uuid_is_null(*node_id))
752
- uuid_unparse_lower(*node_id, wc->node_id);
760
wc->chart_updates = 0;
761
wc->alert_updates = 0;
762
wc->retry_count = 0;
database/sqlite/sqlite_aclk.h
+1
@@ -168,6 +168,7 @@ struct aclk_database_worker_config {
168
char uuid_str[GUID_LEN + 1];
169
char node_id[GUID_LEN + 1];
170
char host_guid[GUID_LEN + 1];
171
+ char *hostname; // hostname to avoid constant lookups
172
uint64_t chart_sequence_id; // last chart_sequence_id
173
time_t chart_timestamp; // last chart timestamp
174
time_t cleanup_after; // Start a cleanup after this timestamp
database/sqlite/sqlite_aclk_alert.c
+18
-23
@@ -416,33 +416,28 @@ void aclk_send_alarm_health_log(char *node_id)
416
if (unlikely(!node_id))
417
return;
418
419
- char *hostname= NULL;
419
+ struct aclk_database_worker_config *wc = find_inactive_wc_by_node_id(node_id);
420
421
- struct aclk_database_worker_config *wc = NULL;
422
- struct aclk_database_cmd cmd;
423
- memset(&cmd, 0, sizeof(cmd));
424
- cmd.opcode = ACLK_DATABASE_ALARM_HEALTH_LOG;
421
+ if (likely(!wc)) {
422
+ rrd_rdlock();
423
+ RRDHOST *host = find_host_by_node_id(node_id);
424
+ rrd_unlock();
425
+ if (likely(host))
426
+ wc = (struct aclk_database_worker_config *)host->dbsync_worker;
427
+ }
428
426
- rrd_rdlock();
427
- RRDHOST *host = find_host_by_node_id(node_id);
428
- if (likely(host)) {
429
- wc = (struct aclk_database_worker_config *)host->dbsync_worker;
430
- hostname = host->hostname;
429
+ if (!wc) {
430
+ log_access("ACLK REQ [%s (N/A)]: HEALTH LOG REQUEST RECEIVED FOR INVALID NODE", node_id);
431
+ return;
432
}
432
- else
433
- hostname = get_hostname_by_node_id(node_id);
434
- rrd_unlock();
433
436
- log_access("ACLK REQ [%s (%s)]: HEALTH LOG request received", node_id, hostname ? hostname : "N/A");
437
- if (unlikely(!host))
438
- freez(hostname);
434
+ log_access("ACLK REQ [%s (%s)]: HEALTH LOG REQUEST RECEIVED", node_id, wc->hostname ? wc->hostname : "N/A");
435
440
- if (wc)
441
- aclk_database_enq_cmd(wc, &cmd);
442
- else {
443
- if (aclk_worker_enq_cmd(node_id, &cmd))
444
- log_access("ACLK STA [%s (N/A)]: ACLK synchronization thread is not active.", node_id);
445
- }
436
+ struct aclk_database_cmd cmd;
437
+ memset(&cmd, 0, sizeof(cmd));
438
+ cmd.opcode = ACLK_DATABASE_ALARM_HEALTH_LOG;
439
+
440
+ aclk_database_enq_cmd(wc, &cmd);
441
return;
442
}
443
@@ -528,7 +523,7 @@ void aclk_push_alarm_health_log(struct aclk_database_worker_config *wc, struct a
523
wc->alert_sequence_id = last_sequence;
524
525
aclk_send_alarm_log_health(&alarm_log);
531
- log_access("ACLK RES [%s (%s)]: HEALTH LOG SENT from %"PRIu64" to %"PRIu64, wc->node_id, wc->host ? wc->host->hostname : "N/A", first_sequence, last_sequence);
526
+ log_access("ACLK RES [%s (%s)]: HEALTH LOG SENT from %"PRIu64" to %"PRIu64, wc->node_id, wc->hostname ? wc->hostname : "N/A", first_sequence, last_sequence);
527
528
rc = sqlite3_finalize(res);
529
if (unlikely(rc != SQLITE_OK))
database/sqlite/sqlite_aclk_chart.c
+10
-36
@@ -325,12 +325,6 @@ void aclk_send_chart_event(struct aclk_database_worker_config *wc, struct aclk_d
325
char sql[ACLK_SYNC_QUERY_SIZE];
326
static __thread sqlite3_stmt *res = NULL;
327
328
- char *hostname = NULL;
329
- if (wc->host)
330
- hostname = strdupz(wc->host->hostname);
331
- else
332
- hostname = get_hostname_by_node_id(wc->node_id);
333
-
328
if (unlikely(!res)) {
329
snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1,"SELECT ac.sequence_id, acp.payload, ac.date_created, ac.type, ac.uuid " \
330
"FROM aclk_chart_%s ac, aclk_chart_payload_%s acp " \
@@ -340,7 +334,6 @@ void aclk_send_chart_event(struct aclk_database_worker_config *wc, struct aclk_d
334
if (rc != SQLITE_OK) {
335
error_report("Failed to prepare statement when trying to send a chart update via ACLK");
336
freez(claim_id);
343
- freez(hostname);
337
return;
338
}
339
}
@@ -414,7 +407,7 @@ void aclk_send_chart_event(struct aclk_database_worker_config *wc, struct aclk_d
407
log_access(
408
"ACLK RES [%s (%s)]: CHARTS SENT from %" PRIu64 " to %" PRIu64 " batch=%" PRIu64,
409
wc->node_id,
417
- hostname ? hostname : "N/A",
410
+ wc->hostname ? wc->hostname : "N/A",
411
first_sequence,
412
last_sequence,
413
wc->batch_id);
@@ -435,7 +428,7 @@ void aclk_send_chart_event(struct aclk_database_worker_config *wc, struct aclk_d
428
log_access(
429
"ACLK STA [%s (%s)]: Sync of charts and dimensions done in %ld seconds.",
430
wc->node_id,
438
- hostname ? hostname : "N/A",
431
+ wc->hostname ? wc->hostname : "N/A",
432
now_realtime_sec() - wc->startup_time);
433
}
434
@@ -454,7 +447,6 @@ bind_fail:
447
error_report("Failed to reset statement when pushing chart events, rc = %d", rc);
448
449
freez(claim_id);
457
- freez(hostname);
450
return;
451
}
452
@@ -579,13 +571,8 @@ void aclk_receive_chart_reset(struct aclk_database_worker_config *wc, struct acl
571
cmd.param1);
572
db_execute(buffer_tostring(sql));
573
if (cmd.param1 == 1) {
582
- char *hostname = NULL;
583
- if (wc->host)
584
- hostname = strdupz(wc->host->hostname);
585
- else
586
- hostname = get_hostname_by_node_id(wc->node_id);
574
buffer_flush(sql);
588
- log_access("ACLK REQ [%s (%s)]: Received chart full resync.", wc->node_id, hostname? hostname : "N/A");
575
+ log_access("ACLK REQ [%s (%s)]: Received chart full resync.", wc->node_id, wc->hostname ? wc->hostname: "N/A");
576
buffer_sprintf(sql, "DELETE FROM aclk_chart_payload_%s; DELETE FROM aclk_chart_%s; " \
577
"DELETE FROM aclk_chart_latest_%s;", wc->uuid_str, wc->uuid_str, wc->uuid_str);
578
db_lock();
@@ -618,12 +605,11 @@ void aclk_receive_chart_reset(struct aclk_database_worker_config *wc, struct acl
605
rrdhost_unlock(host);
606
} else
607
error_report("ACLK synchronization thread for %s is not linked to HOST", wc->host_guid);
621
- freez(hostname);
608
} else {
609
log_access(
610
"ACLK STA [%s (%s)]: RESTARTING CHART SYNC FROM SEQUENCE %" PRIu64,
611
wc->node_id,
626
- wc->host ? wc->host->hostname : "N/A",
612
+ wc->hostname ? wc->hostname : "N/A",
613
cmd.param1);
614
wc->chart_payload_count = sql_get_pending_count(wc);
615
sql_get_last_chart_sequence(wc);
@@ -724,12 +710,7 @@ void aclk_start_streaming(char *node_id, uint64_t sequence_id, time_t created_at
710
wc = (struct aclk_database_worker_config *)host->dbsync_worker ?
711
(struct aclk_database_worker_config *)host->dbsync_worker :
712
(struct aclk_database_worker_config *)find_inactive_wc_by_node_id(node_id);
727
- char *hostname = NULL;
713
if (likely(wc)) {
729
- if (wc->host)
730
- hostname = strdupz(wc->host->hostname);
731
- else
732
- hostname = get_hostname_by_node_id(node_id);
714
wc->chart_reset_count++;
715
__sync_synchronize();
716
wc->chart_updates = 0;
@@ -739,7 +720,7 @@ void aclk_start_streaming(char *node_id, uint64_t sequence_id, time_t created_at
720
log_access(
721
"ACLK REQ [%s (%s)]: CHARTS STREAM from %"PRIu64" (LOCAL %"PRIu64") t=%ld resets=%d" ,
722
wc->node_id,
742
- hostname ? hostname : "N/A",
723
+ wc->hostname ? wc->hostname : "N/A",
724
sequence_id + 1,
725
wc->chart_sequence_id,
726
wc->chart_timestamp,
@@ -749,7 +730,7 @@ void aclk_start_streaming(char *node_id, uint64_t sequence_id, time_t created_at
730
"ACLK RES [%s (%s)]: CHARTS FULL RESYNC REQUEST "
731
"remote_seq=%" PRIu64 " local_seq=%" PRIu64 " resets=%d ",
732
wc->node_id,
752
- hostname ? hostname : "N/A",
733
+ wc->hostname ? wc->hostname : "N/A",
734
sequence_id,
735
wc->chart_sequence_id,
736
wc->chart_reset_count);
@@ -772,7 +753,7 @@ void aclk_start_streaming(char *node_id, uint64_t sequence_id, time_t created_at
753
log_access(
754
"ACLK REQ [%s (%s)]: CHART RESET from %" PRIu64 " t=%ld batch=%" PRIu64,
755
wc->node_id,
775
- hostname ? hostname : "N/A",
756
+ wc->hostname ? wc->hostname : "N/A",
757
sequence_id + 1,
758
wc->chart_timestamp,
759
wc->batch_id);
@@ -786,10 +767,8 @@ void aclk_start_streaming(char *node_id, uint64_t sequence_id, time_t created_at
767
}
768
}
769
} else {
789
- hostname = get_hostname_by_node_id(node_id);
790
- log_access("ACLK STA [%s (%s)]: ACLK synchronization thread is not active.", node_id, hostname ? hostname : "N/A");
770
+ log_access("ACLK STA [%s (%s)]: ACLK synchronization thread is not active.", node_id, wc->hostname ? wc->hostname : "N/A");
771
}
792
- freez(hostname);
772
return;
773
}
774
host = host->next;
@@ -992,17 +971,12 @@ void aclk_update_retention(struct aclk_database_worker_config *wc)
971
rotate_data.interval_duration_count++;
972
}
973
995
- char *hostname = NULL;
996
- if (!wc->host)
997
- hostname = get_hostname_by_node_id(wc->node_id);
998
-
974
if (dimension_update_count < ACLK_MAX_DIMENSION_CLEANUP && !netdata_exit)
975
log_access("ACLK STA [%s (%s)]: UPDATES %d RETENTION MESSAGE SENT. CHECKED %u DIMENSIONS. %u DELETED, %u STOPPED COLLECTING",
1001
- wc->node_id, wc->host ? wc->host->hostname : hostname ? hostname : "N/A", wc->chart_updates, total_checked, total_deleted, total_stopped);
976
+ wc->node_id, wc->hostname ? wc->hostname : "N/A", wc->chart_updates, total_checked, total_deleted, total_stopped);
977
else
978
log_access("ACLK STA [%s (%s)]: UPDATES %d RETENTION MESSAGE NOT SENT. CHECKED %u DIMENSIONS. %u DELETED, %u STOPPED COLLECTING",
1004
- wc->node_id, wc->host ? wc->host->hostname : hostname ? hostname : "N/A", wc->chart_updates, total_checked, total_deleted, total_stopped);
1005
- freez(hostname);
979
+ wc->node_id, wc->hostname ? wc->hostname : "N/A", wc->chart_updates, total_checked, total_deleted, total_stopped);
980
981
#ifdef NETDATA_INTERNAL_CHECKS
982
info("Retention update for %s (chart updates = %d)", wc->host_guid, wc->chart_updates);
database/sqlite/sqlite_functions.c
-6
@@ -2052,12 +2052,6 @@ char *get_hostname_by_node_id(char *node)
2052
char *hostname = NULL;
2053
int rc;
2054
2055
- rrd_rdlock();
2056
- RRDHOST *host = find_host_by_node_id(node);
2057
- rrd_unlock();
2058
- if (host)
2059
- return strdupz(host->hostname);
2060
-
2055
if (unlikely(!db_meta)) {
2056
if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
2057
error_report("Database has not been initialized");