Improve cleaning up of orphan hosts (#12201)
* Move the rrdhost_cleanup_orphan_hosts_nolock to the service that processes obsolete charts * Add OPCODE to mark a host as orphan * Queue cmd to mark a host as orphan
Stelios Fragkakis committed
Feb 23, 2022 at 12:20 UTC
f74eb995bf704e00a46c32b027f4a07107999a81
4 files changed
+33
-7
daemon/service.c
+5
@@ -31,6 +31,11 @@ void *service_main(void *ptr)
31
heartbeat_next(&hb, step);
32
33
rrd_cleanup_obsolete_charts();
34
+
35
+ rrd_wrlock();
36
+ rrdhost_cleanup_orphan_hosts_nolock(localhost);
37
+ rrd_unlock();
38
+
39
}
40
41
netdata_thread_cleanup_pop(1);
database/rrdhost.c
+19
-5
@@ -642,8 +642,6 @@ RRDHOST *rrdhost_find_or_create(
642
rrdhost_unlock(host);
643
}
644
645
- rrdhost_cleanup_orphan_hosts_nolock(host);
646
-
645
rrd_unlock();
646
647
return host;
@@ -652,7 +650,7 @@ inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, tim
650
if(host != protected_host
651
&& host != localhost
652
&& rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
655
- && host->receiver
653
+ && !host->receiver
654
&& host->senders_disconnected_time
655
&& host->senders_disconnected_time + rrdhost_free_orphan_time < now)
656
return 1;
@@ -880,7 +878,20 @@ void rrdhost_free(RRDHOST *host) {
878
879
880
rrdhost_wrlock(host); // lock this RRDHOST
883
-
881
+#if defined(ENABLE_ACLK) && defined(ENABLE_NEW_CLOUD_PROTOCOL)
882
+ struct aclk_database_worker_config *wc = host->dbsync_worker;
883
+ if (wc && !netdata_exit) {
884
+ struct aclk_database_cmd cmd;
885
+ memset(&cmd, 0, sizeof(cmd));
886
+ cmd.opcode = ACLK_DATABASE_ORPHAN_HOST;
887
+ struct aclk_completion compl ;
888
+ init_aclk_completion(&compl );
889
+ cmd.completion = &compl ;
890
+ aclk_database_enq_cmd(wc, &cmd);
891
+ wait_for_aclk_completion(&compl );
892
+ destroy_aclk_completion(&compl );
893
+ }
894
+#endif
895
// ------------------------------------------------------------------------
896
// release its children resources
897
@@ -983,7 +994,10 @@ void rrdhost_free(RRDHOST *host) {
994
freez(host->node_id);
995
996
freez(host);
986
-
997
+#if defined(ENABLE_ACLK) && defined(ENABLE_NEW_CLOUD_PROTOCOL)
998
+ if (wc)
999
+ wc->is_orphan = 0;
1000
+#endif
1001
rrd_hosts_available--;
1002
}
1003
database/sqlite/sqlite_aclk.c
+7
-2
@@ -508,11 +508,16 @@ void aclk_database_worker(void *arg)
508
aclk_update_retention(wc, cmd);
509
aclk_process_dimension_deletion(wc, cmd);
510
break;
511
-#endif
511
512
// NODE_INSTANCE DETECTION
513
+ case ACLK_DATABASE_ORPHAN_HOST:
514
+ wc->host = NULL;
515
+ wc->is_orphan = 1;
516
+ aclk_add_worker_thread(wc);
517
+ break;
518
+#endif
519
case ACLK_DATABASE_TIMER:
515
- if (unlikely(localhost && !wc->host)) {
520
+ if (unlikely(localhost && !wc->host && !wc->is_orphan)) {
521
if (claimed()) {
522
wc->host = rrdhost_find_by_guid(wc->host_guid, 0);
523
if (wc->host) {
database/sqlite/sqlite_aclk.h
+2
@@ -125,6 +125,7 @@ enum aclk_database_opcode {
125
ACLK_DATABASE_CHART_ACK,
126
ACLK_DATABASE_UPD_RETENTION,
127
ACLK_DATABASE_DIM_DELETION,
128
+ ACLK_DATABASE_ORPHAN_HOST,
129
#endif
130
ACLK_DATABASE_ALARM_HEALTH_LOG,
131
ACLK_DATABASE_CLEANUP,
@@ -194,6 +195,7 @@ struct aclk_database_worker_config {
195
int chart_pending;
196
int chart_reset_count;
197
volatile unsigned is_shutting_down;
198
+ volatile unsigned is_orphan;
199
struct aclk_database_worker_config *next;
200
};
201