summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStelios Fragkakis <52996999+stelfrag@users.noreply.github.com>2024-02-11 21:30:35 +0200
committerGitHub <noreply@github.com>2024-02-11 21:30:35 +0200
commit128ba9501d5ef3664a4b93930c315792472bdd73 (patch)
tree4ccea980a7b65ff21522c3fbd8957d9478de0d81
parentef48d380a1674ccbeb2785eda7d6d93aa72b9f4f (diff)
Detect machine GUID change (#16979)
Detect machine guid change
-rw-r--r--src/database/rrdhost.c2
-rw-r--r--src/database/sqlite/sqlite_metadata.c12
-rw-r--r--src/database/sqlite/sqlite_metadata.h2
3 files changed, 6 insertions, 10 deletions
diff --git a/src/database/rrdhost.c b/src/database/rrdhost.c
index 7a5558e360..2699ca8f34 100644
--- a/src/database/rrdhost.c
+++ b/src/database/rrdhost.c
@@ -1107,7 +1107,7 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info, bool unitt
rrdhost_function_progress);
if (likely(system_info)) {
- migrate_localhost(&localhost->host_uuid);
+ detect_machine_guid_change(&localhost->host_uuid);
sql_aclk_sync_init();
web_client_api_v1_management_init();
}
diff --git a/src/database/sqlite/sqlite_metadata.c b/src/database/sqlite/sqlite_metadata.c
index 82fd2b22b4..d074e06d79 100644
--- a/src/database/sqlite/sqlite_metadata.c
+++ b/src/database/sqlite/sqlite_metadata.c
@@ -51,9 +51,7 @@
"INSERT OR REPLACE INTO host_info (host_id, system_key, system_value, date_created) VALUES " \
"(@uuid, @name, @value, UNIXEPOCH())"
-#define MIGRATE_LOCALHOST_TO_NEW_MACHINE_GUID \
- "UPDATE chart SET host_id = @host_id WHERE host_id in (SELECT host_id FROM host where host_id <> @host_id and hops = 0)"
-#define DELETE_NON_EXISTING_LOCALHOST "DELETE FROM host WHERE hops = 0 AND host_id <> @host_id"
+#define CONVERT_EXISTING_LOCALHOST "UPDATE host SET hops = 1 WHERE hops = 0 AND host_id <> @host_id"
#define DELETE_MISSING_NODE_INSTANCES "DELETE FROM node_instance WHERE host_id NOT IN (SELECT host_id FROM host)"
#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
return rc;
}
-// Migrate all hosts with hops zero to this host_uuid
-void migrate_localhost(uuid_t *host_uuid)
+// If the machine guid has changed, then existing one with hops 0 will be marked as hops 1 (child)
+void detect_machine_guid_change(uuid_t *host_uuid)
{
int rc;
- rc = exec_statement_with_uuid(MIGRATE_LOCALHOST_TO_NEW_MACHINE_GUID, host_uuid);
- if (!rc)
- rc = exec_statement_with_uuid(DELETE_NON_EXISTING_LOCALHOST, host_uuid);
+ rc = exec_statement_with_uuid(CONVERT_EXISTING_LOCALHOST, host_uuid);
if (!rc) {
if (unlikely(db_execute(db_meta, DELETE_MISSING_NODE_INSTANCES)))
error_report("Failed to remove deleted hosts from node instances");
diff --git a/src/database/sqlite/sqlite_metadata.h b/src/database/sqlite/sqlite_metadata.h
index 6860cfedf8..8441a51b39 100644
--- a/src/database/sqlite/sqlite_metadata.h
+++ b/src/database/sqlite/sqlite_metadata.h
@@ -15,7 +15,7 @@ void metaqueue_delete_dimension_uuid(uuid_t *uuid);
void metaqueue_store_claim_id(uuid_t *host_uuid, uuid_t *claim_uuid);
void metaqueue_host_update_info(RRDHOST *host);
void metaqueue_ml_load_models(RRDDIM *rd);
-void migrate_localhost(uuid_t *host_uuid);
+void detect_machine_guid_change(uuid_t *host_uuid);
void metadata_queue_load_host_context(RRDHOST *host);
void metadata_delete_host_chart_labels(char *machine_guid);
void vacuum_database(sqlite3 *database, const char *db_alias, int threshold, int vacuum_pc);