@cryptotaxi247 / netdata-1 / commits / 922ebd48b

Re-index when machine guid changes (#14535)

* sort per reachable in v0/info. re-index per hostname * remove whitespace * keep the new host if the old is archived or orphan

Emmanuel Vasilakis committed Feb 20, 2023 at 15:07 UTC 922ebd48beb78beedf0fca771cb38d12879ee5b4
2 files changed +43 -26
database/rrdhost.c
+19 -13
@@ -104,6 +104,17 @@ inline RRDHOST *rrdhost_find_by_hostname(const char *hostname) {
104 return dictionary_get(rrdhost_root_index_hostname, hostname);
105 }
106
107 +static inline void rrdhost_index_del_hostname(RRDHOST *host) {
108 + if(unlikely(!host->hostname)) return;
109 +
110 + if(rrdhost_option_check(host, RRDHOST_OPTION_INDEXED_HOSTNAME)) {
111 + if(!dictionary_del(rrdhost_root_index_hostname, rrdhost_hostname(host)))
112 + error("RRDHOST: %s() failed to delete hostname '%s' from index", __FUNCTION__, rrdhost_hostname(host));
113 +
114 + rrdhost_option_clear(host, RRDHOST_OPTION_INDEXED_HOSTNAME);
115 + }
116 +}
117 +
118 static inline RRDHOST *rrdhost_index_add_hostname(RRDHOST *host) {
119 if(!host->hostname) return host;
120
@@ -111,24 +122,17 @@ static inline RRDHOST *rrdhost_index_add_hostname(RRDHOST *host) {
122 if(ret_hostname == host)
123 rrdhost_option_set(host, RRDHOST_OPTION_INDEXED_HOSTNAME);
124 else {
114 - rrdhost_option_clear(host, RRDHOST_OPTION_INDEXED_HOSTNAME);
115 - error("RRDHOST: %s() host with hostname '%s' is already indexed", __FUNCTION__, rrdhost_hostname(host));
125 + //have the same hostname but it's not the same host
126 + //keep the new one only if the old one is orphan or archived
127 + if (rrdhost_flag_check(ret_hostname, RRDHOST_FLAG_ORPHAN) || rrdhost_flag_check(ret_hostname, RRDHOST_FLAG_ARCHIVED)) {
128 + rrdhost_index_del_hostname(ret_hostname);
129 + rrdhost_index_add_hostname(host);
130 + }
131 }
132
133 return host;
134 }
135
121 -static inline void rrdhost_index_del_hostname(RRDHOST *host) {
122 - if(unlikely(!host->hostname)) return;
123 -
124 - if(rrdhost_option_check(host, RRDHOST_OPTION_INDEXED_HOSTNAME)) {
125 - if(!dictionary_del(rrdhost_root_index_hostname, rrdhost_hostname(host)))
126 - error("RRDHOST: %s() failed to delete hostname '%s' from index", __FUNCTION__, rrdhost_hostname(host));
127 -
128 - rrdhost_option_clear(host, RRDHOST_OPTION_INDEXED_HOSTNAME);
129 - }
130 -}
131 -
136 // ----------------------------------------------------------------------------
137 // RRDHOST - internal helpers
138
@@ -582,6 +586,8 @@ static void rrdhost_update(RRDHOST *host
586 if(strcmp(rrdhost_hostname(host), hostname) != 0) {
587 info("Host '%s' has been renamed to '%s'. If this is not intentional it may mean multiple hosts are using the same machine_guid.", rrdhost_hostname(host), hostname);
588 rrdhost_init_hostname(host, hostname, true);
589 + } else {
590 + rrdhost_index_add_hostname(host);
591 }
592
593 if(strcmp(rrdhost_program_name(host), program_name) != 0) {
web/api/web_api_v1.c
+24 -13
@@ -1042,6 +1042,22 @@ static inline void web_client_api_request_v1_info_summary_alarm_statuses(RRDHOST
1042 buffer_json_object_close(wb);
1043 }
1044
1045 +static inline void web_client_api_request_v1_info_mirrored_hosts_status(BUFFER *wb, RRDHOST *host) {
1046 + buffer_json_add_array_item_object(wb);
1047 +
1048 + buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(host));
1049 + buffer_json_member_add_uint64(wb, "hops", host->system_info ? host->system_info->hops : (host == localhost) ? 0 : 1);
1050 + buffer_json_member_add_boolean(wb, "reachable", (host == localhost || !rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)));
1051 +
1052 + buffer_json_member_add_string(wb, "guid", host->machine_guid);
1053 + buffer_json_member_add_uuid(wb, "node_id", host->node_id);
1054 + rrdhost_aclk_state_lock(host);
1055 + buffer_json_member_add_string(wb, "claim_id", host->aclk_state.claimed_id);
1056 + rrdhost_aclk_state_unlock(host);
1057 +
1058 + buffer_json_object_close(wb);
1059 +}
1060 +
1061 static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
1062 RRDHOST *host;
1063
@@ -1054,19 +1070,14 @@ static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
1070
1071 buffer_json_member_add_array(wb, "mirrored_hosts_status");
1072 rrdhost_foreach_read(host) {
1057 - buffer_json_add_array_item_object(wb);
1058 -
1059 - buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(host));
1060 - buffer_json_member_add_uint64(wb, "hops", host->system_info ? host->system_info->hops : (host == localhost) ? 0 : 1);
1061 - buffer_json_member_add_boolean(wb, "reachable", (host == localhost || !rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)));
1062 -
1063 - buffer_json_member_add_string(wb, "guid", host->machine_guid);
1064 - buffer_json_member_add_uuid(wb, "node_id", host->node_id);
1065 - rrdhost_aclk_state_lock(host);
1066 - buffer_json_member_add_string(wb, "claim_id", host->aclk_state.claimed_id);
1067 - rrdhost_aclk_state_unlock(host);
1068 -
1069 - buffer_json_object_close(wb);
1073 + if ((host == localhost || !rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN))) {
1074 + web_client_api_request_v1_info_mirrored_hosts_status(wb, host);
1075 + }
1076 + }
1077 + rrdhost_foreach_read(host) {
1078 + if ((host != localhost && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN))) {
1079 + web_client_api_request_v1_info_mirrored_hosts_status(wb, host);
1080 + }
1081 }
1082 buffer_json_array_close(wb);
1083