@cryptotaxi247 / netdata-1 / commits / 076822eb8

Persist claim ids in local database for parent and children (#10993)

Stelios Fragkakis committed Apr 22, 2021 at 09:38 UTC 076822eb88e9e8adb97aa90f7132c3ac36587b9c
4 files changed +55 -1
claim/claim.c
+4
@@ -164,6 +164,10 @@ void load_claiming_state(void)
164 claimed_id = NULL;
165 }
166 localhost->aclk_state.claimed_id = claimed_id;
167 +
168 + if (likely(claimed_id))
169 + store_claim_id(&localhost->host_uuid, &uuid);
170 +
171 rrdhost_aclk_state_unlock(localhost);
172 if (!claimed_id) {
173 info("Unable to load '%s', setting state to AGENT_UNCLAIMED", filename);
database/sqlite/sqlite_functions.c
+46 -1
@@ -19,7 +19,7 @@ const char *database_config[] = {
19 "CREATE INDEX IF NOT EXISTS ind_c1 on chart (host_id, id, type, name);",
20 "CREATE TABLE IF NOT EXISTS chart_label(chart_id blob, source_type int, label_key text, "
21 "label_value text, date_created int, PRIMARY KEY (chart_id, label_key));",
22 -
22 + "CREATE TABLE IF NOT EXISTS node_instance (host_id blob PRIMARY KEY, claim_id, node_id, date_created);",
23 "delete from chart_active;",
24 "delete from dimension_active;",
25 "delete from chart where chart_id not in (select chart_id from dimension);",
@@ -1338,3 +1338,48 @@ failed:
1338 #endif
1339 return;
1340 }
1341 +
1342 +#define SQL_STORE_CLAIM_ID "insert into node_instance " \
1343 + "(host_id, claim_id, date_created) values (@host_id, @claim_id, strftime('%s')) " \
1344 + "on conflict(host_id) do update set node_id = null, claim_id = excluded.claim_id " \
1345 + "where claim_id <> excluded.claim_id;"
1346 +
1347 +void store_claim_id(uuid_t *host_id, uuid_t *claim_id)
1348 +{
1349 + sqlite3_stmt *res = NULL;
1350 + int rc;
1351 +
1352 + if (unlikely(!db_meta)) {
1353 + if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
1354 + error_report("Database has not been initialized");
1355 + return;
1356 + }
1357 +
1358 + rc = sqlite3_prepare_v2(db_meta, SQL_STORE_CLAIM_ID, -1, &res, 0);
1359 + if (unlikely(rc != SQLITE_OK)) {
1360 + error_report("Failed to prepare statement store chart labels");
1361 + return;
1362 + }
1363 +
1364 + rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
1365 + if (unlikely(rc != SQLITE_OK)) {
1366 + error_report("Failed to bind host_id parameter to store node instance information");
1367 + goto failed;
1368 + }
1369 +
1370 + rc = sqlite3_bind_blob(res, 2, claim_id, sizeof(*claim_id), SQLITE_STATIC);
1371 + if (unlikely(rc != SQLITE_OK)) {
1372 + error_report("Failed to bind claim_id parameter to store node instance information");
1373 + goto failed;
1374 + }
1375 +
1376 + rc = execute_insert(res);
1377 + if (unlikely(rc != SQLITE_DONE))
1378 + error_report("Failed to store node instance information, rc = %d", rc);
1379 +
1380 +failed:
1381 + if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
1382 + error_report("Failed to finalize the prepared statement when storing node instance information");
1383 +
1384 + return;
1385 +}
database/sqlite/sqlite_functions.h
+1
@@ -60,4 +60,5 @@ extern void db_lock(void);
60 extern void delete_dimension_uuid(uuid_t *dimension_uuid);
61 extern void sql_store_chart_label(uuid_t *chart_uuid, int source_type, char *label, char *value);
62 extern void sql_build_context_param_list(struct context_param **param_list, RRDHOST *host, char *context, char *chart);
63 +extern void store_claim_id(uuid_t *host_id, uuid_t *claim_id);
64 #endif //NETDATA_SQLITE_FUNCTIONS_H
streaming/receiver.c
+4
@@ -130,6 +130,10 @@ PARSER_RC streaming_claimed_id(char **words, void *user, PLUGINSD_ACTION *plugin
130 if (host->aclk_state.claimed_id)
131 freez(host->aclk_state.claimed_id);
132 host->aclk_state.claimed_id = strcmp(words[2], "NULL") ? strdupz(words[2]) : NULL;
133 +
134 + if (likely(host->aclk_state.claimed_id))
135 + store_claim_id(&host->host_uuid, &uuid);
136 +
137 rrdhost_aclk_state_unlock(host);
138
139 rrdpush_claimed_id(host);