@cryptotaxi247 / netdata-1 / commits / 128ba9501

Detect machine GUID change (#16979)

Detect machine guid change

Stelios Fragkakis committed Feb 11, 2024 at 21:30 UTC 128ba9501d5ef3664a4b93930c315792472bdd73
3 files changed +6 -10
src/database/rrdhost.c
+1 -1
@@ -1107,7 +1107,7 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info, bool unitt
1107 rrdhost_function_progress);
1108
1109 if (likely(system_info)) {
1110 - migrate_localhost(&localhost->host_uuid);
1110 + detect_machine_guid_change(&localhost->host_uuid);
1111 sql_aclk_sync_init();
1112 web_client_api_v1_management_init();
1113 }
src/database/sqlite/sqlite_metadata.c
+4 -8
@@ -51,9 +51,7 @@
51 "INSERT OR REPLACE INTO host_info (host_id, system_key, system_value, date_created) VALUES " \
52 "(@uuid, @name, @value, UNIXEPOCH())"
53
54 -#define MIGRATE_LOCALHOST_TO_NEW_MACHINE_GUID \
55 - "UPDATE chart SET host_id = @host_id WHERE host_id in (SELECT host_id FROM host where host_id <> @host_id and hops = 0)"
56 -#define DELETE_NON_EXISTING_LOCALHOST "DELETE FROM host WHERE hops = 0 AND host_id <> @host_id"
54 +#define CONVERT_EXISTING_LOCALHOST "UPDATE host SET hops = 1 WHERE hops = 0 AND host_id <> @host_id"
55 #define DELETE_MISSING_NODE_INSTANCES "DELETE FROM node_instance WHERE host_id NOT IN (SELECT host_id FROM host)"
56
57 #define METADATA_MAINTENANCE_FIRST_CHECK (1800) // Maintenance first run after agent startup in seconds
@@ -231,14 +229,12 @@ static int check_and_update_chart_labels(RRDSET *st, BUFFER *work_buffer, size_t
229 return rc;
230 }
231
234 -// Migrate all hosts with hops zero to this host_uuid
235 -void migrate_localhost(uuid_t *host_uuid)
232 +// If the machine guid has changed, then existing one with hops 0 will be marked as hops 1 (child)
233 +void detect_machine_guid_change(uuid_t *host_uuid)
234 {
235 int rc;
236
239 - rc = exec_statement_with_uuid(MIGRATE_LOCALHOST_TO_NEW_MACHINE_GUID, host_uuid);
240 - if (!rc)
241 - rc = exec_statement_with_uuid(DELETE_NON_EXISTING_LOCALHOST, host_uuid);
237 + rc = exec_statement_with_uuid(CONVERT_EXISTING_LOCALHOST, host_uuid);
238 if (!rc) {
239 if (unlikely(db_execute(db_meta, DELETE_MISSING_NODE_INSTANCES)))
240 error_report("Failed to remove deleted hosts from node instances");
src/database/sqlite/sqlite_metadata.h
+1 -1
@@ -15,7 +15,7 @@ void metaqueue_delete_dimension_uuid(uuid_t *uuid);
15 void metaqueue_store_claim_id(uuid_t *host_uuid, uuid_t *claim_uuid);
16 void metaqueue_host_update_info(RRDHOST *host);
17 void metaqueue_ml_load_models(RRDDIM *rd);
18 -void migrate_localhost(uuid_t *host_uuid);
18 +void detect_machine_guid_change(uuid_t *host_uuid);
19 void metadata_queue_load_host_context(RRDHOST *host);
20 void metadata_delete_host_chart_labels(char *machine_guid);
21 void vacuum_database(sqlite3 *database, const char *db_alias, int threshold, int vacuum_pc);