summaryrefslogtreecommitdiffstats
path: root/database/sqlite/sqlite_functions.c
diff options
context:
space:
mode:
Diffstat (limited to 'database/sqlite/sqlite_functions.c')
-rw-r--r--database/sqlite/sqlite_functions.c428
1 files changed, 165 insertions, 263 deletions
diff --git a/database/sqlite/sqlite_functions.c b/database/sqlite/sqlite_functions.c
index d976a3c6e..e081fe9ae 100644
--- a/database/sqlite/sqlite_functions.c
+++ b/database/sqlite/sqlite_functions.c
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "sqlite_functions.h"
+#include "sqlite3recover.h"
#include "sqlite_db_migration.h"
-#define DB_METADATA_VERSION 11
+#define DB_METADATA_VERSION 14
const char *database_config[] = {
"CREATE TABLE IF NOT EXISTS host(host_id BLOB PRIMARY KEY, hostname TEXT NOT NULL, "
@@ -13,7 +14,7 @@ const char *database_config[] = {
"memory_mode INT DEFAULT 0, abbrev_timezone TEXT DEFAULT '', utc_offset INT NOT NULL DEFAULT 0,"
"program_name TEXT NOT NULL DEFAULT 'unknown', program_version TEXT NOT NULL DEFAULT 'unknown', "
"entries INT NOT NULL DEFAULT 0,"
- "health_enabled INT NOT NULL DEFAULT 0);",
+ "health_enabled INT NOT NULL DEFAULT 0, last_connected INT NOT NULL DEFAULT 0);",
"CREATE TABLE IF NOT EXISTS chart(chart_id blob PRIMARY KEY, host_id blob, type text, id text, name text, "
"family text, context text, title text, unit text, plugin text, module text, priority int, update_every int, "
@@ -32,7 +33,7 @@ const char *database_config[] = {
"every text, units text, calc text, families text, plugin text, module text, charts text, green text, "
"red text, warn text, crit text, exec text, to_key text, info text, delay text, options text, "
"repeat text, host_labels text, p_db_lookup_dimensions text, p_db_lookup_method text, p_db_lookup_options int, "
- "p_db_lookup_after int, p_db_lookup_before int, p_update_every int, source text, chart_labels text);",
+ "p_db_lookup_after int, p_db_lookup_before int, p_update_every int, source text, chart_labels text, summary text);",
"CREATE TABLE IF NOT EXISTS host_info(host_id blob, system_key text NOT NULL, system_value text NOT NULL, "
"date_created INT, PRIMARY KEY(host_id, system_key));",
@@ -53,18 +54,17 @@ const char *database_config[] = {
"updated_by_id int, updates_id int, when_key int, duration int, non_clear_duration int, "
"flags int, exec_run_timestamp int, delay_up_to_timestamp int, "
"info text, exec_code int, new_status real, old_status real, delay int, "
- "new_value double, old_value double, last_repeat int, transition_id blob, global_id int);",
+ "new_value double, old_value double, last_repeat int, transition_id blob, global_id int, summary text);",
- "CREATE INDEX IF NOT EXISTS health_log_d_ind_1 ON health_log_detail (unique_id);",
"CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id);",
"CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id);",
- "CREATE INDEX IF NOT EXISTS health_log_d_ind_4 ON health_log_detail (health_log_id);",
+ "CREATE INDEX IF NOT EXISTS health_log_d_ind_5 ON health_log_detail (health_log_id, unique_id DESC);",
+ "CREATE INDEX IF NOT EXISTS health_log_d_ind_6 on health_log_detail (health_log_id, when_key)",
NULL
};
const char *database_cleanup[] = {
- "DELETE FROM chart WHERE chart_id NOT IN (SELECT chart_id FROM dimension);",
"DELETE FROM host WHERE host_id NOT IN (SELECT host_id FROM chart);",
"DELETE FROM node_instance WHERE host_id NOT IN (SELECT host_id FROM host);",
"DELETE FROM host_info WHERE host_id NOT IN (SELECT host_id FROM host);",
@@ -74,6 +74,8 @@ const char *database_cleanup[] = {
"DROP INDEX IF EXISTS ind_c1;",
"DROP INDEX IF EXISTS ind_c2;",
"DROP INDEX IF EXISTS alert_hash_index;",
+ "DROP INDEX IF EXISTS health_log_d_ind_4;",
+ "DROP INDEX IF EXISTS health_log_d_ind_1;",
NULL
};
@@ -120,23 +122,126 @@ SQLITE_API int sqlite3_step_monitored(sqlite3_stmt *stmt) {
return rc;
}
-int execute_insert(sqlite3_stmt *res)
+static bool mark_database_to_recover(sqlite3_stmt *res, sqlite3 *database)
{
- int rc;
- int cnt = 0;
- while ((rc = sqlite3_step_monitored(res)) != SQLITE_DONE && ++cnt < SQL_MAX_RETRY && likely(!netdata_exit)) {
- if (likely(rc == SQLITE_BUSY || rc == SQLITE_LOCKED)) {
- usleep(SQLITE_INSERT_DELAY * USEC_PER_MS);
- error_report("Failed to insert/update, rc = %d -- attempt %d", rc, cnt);
+
+ if (!res && !database)
+ return false;
+
+ if (!database)
+ database = sqlite3_db_handle(res);
+
+ if (db_meta == database) {
+ char recover_file[FILENAME_MAX + 1];
+ snprintfz(recover_file, FILENAME_MAX, "%s/.netdata-meta.db.recover", netdata_configured_cache_dir);
+ int fd = open(recover_file, O_WRONLY | O_CREAT | O_TRUNC, 444);
+ if (fd >= 0) {
+ close(fd);
+ return true;
}
- else {
- error_report("SQLite error %d", rc);
- break;
+ }
+ return false;
+}
+
+static void recover_database(const char *sqlite_database, const char *new_sqlite_database)
+{
+ sqlite3 *database;
+ int rc = sqlite3_open(sqlite_database, &database);
+ if (rc != SQLITE_OK)
+ return;
+
+ netdata_log_info("Recover %s", sqlite_database);
+ netdata_log_info(" to %s", new_sqlite_database);
+
+ // This will remove the -shm and -wal files when we close the database
+ (void) db_execute(database, "select count(*) from sqlite_master limit 0");
+
+ sqlite3_recover *recover = sqlite3_recover_init(database, "main", new_sqlite_database);
+ if (recover) {
+
+ rc = sqlite3_recover_run(recover);
+
+ if (rc == SQLITE_OK)
+ netdata_log_info("Recover complete");
+ else
+ netdata_log_info("Recover encountered an error but the database may be usable");
+
+ rc = sqlite3_recover_finish(recover);
+
+ (void) sqlite3_close(database);
+
+ if (rc == SQLITE_OK) {
+ rc = rename(new_sqlite_database, sqlite_database);
+ if (rc == 0) {
+ netdata_log_info("Renamed %s", new_sqlite_database);
+ netdata_log_info(" to %s", sqlite_database);
+ }
}
}
+ else
+ (void) sqlite3_close(database);
+}
+
+int execute_insert(sqlite3_stmt *res)
+{
+ int rc;
+ rc = sqlite3_step_monitored(res);
+ if (rc == SQLITE_CORRUPT) {
+ (void)mark_database_to_recover(res, NULL);
+ error_report("SQLite error %d", rc);
+ }
return rc;
}
+int configure_sqlite_database(sqlite3 *database, int target_version)
+{
+ char buf[1024 + 1] = "";
+ const char *list[2] = { buf, NULL };
+
+ // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
+ // PRAGMA schema.auto_vacuum = 0 | NONE | 1 | FULL | 2 | INCREMENTAL;
+ snprintfz(buf, 1024, "PRAGMA auto_vacuum=%s;", config_get(CONFIG_SECTION_SQLITE, "auto vacuum", "INCREMENTAL"));
+ if (init_database_batch(database, list))
+ return 1;
+
+ // https://www.sqlite.org/pragma.html#pragma_synchronous
+ // PRAGMA schema.synchronous = 0 | OFF | 1 | NORMAL | 2 | FULL | 3 | EXTRA;
+ snprintfz(buf, 1024, "PRAGMA synchronous=%s;", config_get(CONFIG_SECTION_SQLITE, "synchronous", "NORMAL"));
+ if (init_database_batch(database, list))
+ return 1;
+
+ // https://www.sqlite.org/pragma.html#pragma_journal_mode
+ // PRAGMA schema.journal_mode = DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
+ snprintfz(buf, 1024, "PRAGMA journal_mode=%s;", config_get(CONFIG_SECTION_SQLITE, "journal mode", "WAL"));
+ if (init_database_batch(database, list))
+ return 1;
+
+ // https://www.sqlite.org/pragma.html#pragma_temp_store
+ // PRAGMA temp_store = 0 | DEFAULT | 1 | FILE | 2 | MEMORY;
+ snprintfz(buf, 1024, "PRAGMA temp_store=%s;", config_get(CONFIG_SECTION_SQLITE, "temp store", "MEMORY"));
+ if (init_database_batch(database, list))
+ return 1;
+
+ // https://www.sqlite.org/pragma.html#pragma_journal_size_limit
+ // PRAGMA schema.journal_size_limit = N ;
+ snprintfz(buf, 1024, "PRAGMA journal_size_limit=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "journal size limit", 16777216));
+ if (init_database_batch(database, list))
+ return 1;
+
+ // https://www.sqlite.org/pragma.html#pragma_cache_size
+ // PRAGMA schema.cache_size = pages;
+ // PRAGMA schema.cache_size = -kibibytes;
+ snprintfz(buf, 1024, "PRAGMA cache_size=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "cache size", -2000));
+ if (init_database_batch(database, list))
+ return 1;
+
+ snprintfz(buf, 1024, "PRAGMA user_version=%d;", target_version);
+ if (init_database_batch(database, list))
+ return 1;
+
+ return 0;
+}
+
#define MAX_OPEN_STATEMENTS (512)
static void add_stmt_to_list(sqlite3_stmt *res)
@@ -202,134 +307,21 @@ int prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **state
return rc;
}
-static int check_table_integrity_cb(void *data, int argc, char **argv, char **column)
-{
- int *status = data;
- UNUSED(argc);
- UNUSED(column);
- netdata_log_info("---> %s", argv[0]);
- *status = (strcmp(argv[0], "ok") != 0);
- return 0;
-}
-
-
-static int check_table_integrity(char *table)
-{
- int status = 0;
- char *err_msg = NULL;
- char wstr[255];
-
- if (table) {
- netdata_log_info("Checking table %s", table);
- snprintfz(wstr, 254, "PRAGMA integrity_check(%s);", table);
- }
- else {
- netdata_log_info("Checking entire database");
- strcpy(wstr,"PRAGMA integrity_check;");
- }
-
- int rc = sqlite3_exec_monitored(db_meta, wstr, check_table_integrity_cb, (void *) &status, &err_msg);
- if (rc != SQLITE_OK) {
- error_report("SQLite error during database integrity check for %s, rc = %d (%s)",
- table ? table : "the entire database", rc, err_msg);
- sqlite3_free(err_msg);
- }
-
- return status;
-}
-
-const char *rebuild_chart_commands[] = {
- "BEGIN TRANSACTION; ",
- "DROP INDEX IF EXISTS ind_c1;" ,
- "DROP TABLE IF EXISTS chart_backup; " ,
- "CREATE TABLE chart_backup AS SELECT * FROM chart; " ,
- "DROP TABLE chart; ",
- "CREATE TABLE IF NOT EXISTS chart(chart_id blob PRIMARY KEY, host_id blob, type text, id text, "
- "name text, family text, context text, title text, unit text, plugin text, "
- "module text, priority int, update_every int, chart_type int, memory_mode int, history_entries); ",
- "INSERT INTO chart SELECT DISTINCT * FROM chart_backup; ",
- "DROP TABLE chart_backup; " ,
- "CREATE INDEX IF NOT EXISTS ind_c1 on chart (host_id, id, type, name);",
- "COMMIT TRANSACTION;",
- NULL
-};
-
-static void rebuild_chart()
-{
- int rc;
- char *err_msg = NULL;
- netdata_log_info("Rebuilding chart table");
- for (int i = 0; rebuild_chart_commands[i]; i++) {
- netdata_log_info("Executing %s", rebuild_chart_commands[i]);
- rc = sqlite3_exec_monitored(db_meta, rebuild_chart_commands[i], 0, 0, &err_msg);
- if (rc != SQLITE_OK) {
- error_report("SQLite error during database setup, rc = %d (%s)", rc, err_msg);
- error_report("SQLite failed statement %s", rebuild_chart_commands[i]);
- sqlite3_free(err_msg);
- }
- }
-}
-
-const char *rebuild_dimension_commands[] = {
- "BEGIN TRANSACTION; ",
- "DROP INDEX IF EXISTS ind_d1;" ,
- "DROP TABLE IF EXISTS dimension_backup; " ,
- "CREATE TABLE dimension_backup AS SELECT * FROM dimension; " ,
- "DROP TABLE dimension; " ,
- "CREATE TABLE IF NOT EXISTS dimension(dim_id blob PRIMARY KEY, chart_id blob, id text, name text, "
- "multiplier int, divisor int , algorithm int, options text);" ,
- "INSERT INTO dimension SELECT distinct * FROM dimension_backup; " ,
- "DROP TABLE dimension_backup; " ,
- "CREATE INDEX IF NOT EXISTS ind_d1 on dimension (chart_id, id, name);",
- "COMMIT TRANSACTION;",
- NULL
-};
-
-void rebuild_dimension()
-{
- int rc;
- char *err_msg = NULL;
-
- netdata_log_info("Rebuilding dimension table");
- for (int i = 0; rebuild_dimension_commands[i]; i++) {
- netdata_log_info("Executing %s", rebuild_dimension_commands[i]);
- rc = sqlite3_exec_monitored(db_meta, rebuild_dimension_commands[i], 0, 0, &err_msg);
- if (rc != SQLITE_OK) {
- error_report("SQLite error during database setup, rc = %d (%s)", rc, err_msg);
- error_report("SQLite failed statement %s", rebuild_dimension_commands[i]);
- sqlite3_free(err_msg);
- }
- }
-}
-
-static int attempt_database_fix()
-{
- netdata_log_info("Closing database and attempting to fix it");
- int rc = sqlite3_close(db_meta);
- if (rc != SQLITE_OK)
- error_report("Failed to close database, rc = %d", rc);
- netdata_log_info("Attempting to fix database");
- db_meta = NULL;
- return sql_init_database(DB_CHECK_FIX_DB | DB_CHECK_CONT, 0);
-}
-
-int init_database_batch(sqlite3 *database, int rebuild, int init_type, const char *batch[])
+int init_database_batch(sqlite3 *database, const char *batch[])
{
int rc;
char *err_msg = NULL;
for (int i = 0; batch[i]; i++) {
- netdata_log_debug(D_METADATALOG, "Executing %s", batch[i]);
rc = sqlite3_exec_monitored(database, batch[i], 0, 0, &err_msg);
if (rc != SQLITE_OK) {
- error_report("SQLite error during database %s, rc = %d (%s)", init_type ? "cleanup" : "setup", rc, err_msg);
+ error_report("SQLite error during database initialization, rc = %d (%s)", rc, err_msg);
error_report("SQLite failed statement %s", batch[i]);
+ analytics_set_data_str(&analytics_data.netdata_fail_reason, sqlite3_errstr(sqlite3_extended_errcode(database)));
sqlite3_free(err_msg);
if (SQLITE_CORRUPT == rc) {
- if (!rebuild)
- return attempt_database_fix();
- rc = check_table_integrity(NULL);
- if (rc)
- error_report("Databse integrity errors reported");
+ if (mark_database_to_recover(NULL, database))
+ error_report("Database is corrupted will attempt to fix");
+ return SQLITE_CORRUPT;
}
return 1;
}
@@ -389,64 +381,47 @@ int sql_init_database(db_check_action_type_t rebuild, int memory)
char sqlite_database[FILENAME_MAX + 1];
int rc;
- if (likely(!memory))
+ if (likely(!memory)) {
+ snprintfz(sqlite_database, FILENAME_MAX, "%s/.netdata-meta.db.recover", netdata_configured_cache_dir);
+ rc = unlink(sqlite_database);
snprintfz(sqlite_database, FILENAME_MAX, "%s/netdata-meta.db", netdata_configured_cache_dir);
+
+ if (rc == 0 || (rebuild & DB_CHECK_RECOVER)) {
+ char new_sqlite_database[FILENAME_MAX + 1];
+ snprintfz(new_sqlite_database, FILENAME_MAX, "%s/netdata-meta-recover.db", netdata_configured_cache_dir);
+ recover_database(sqlite_database, new_sqlite_database);
+ if (rebuild & DB_CHECK_RECOVER)
+ return 0;
+ }
+ }
else
strcpy(sqlite_database, ":memory:");
rc = sqlite3_open(sqlite_database, &db_meta);
if (rc != SQLITE_OK) {
error_report("Failed to initialize database at %s, due to \"%s\"", sqlite_database, sqlite3_errstr(rc));
+ analytics_set_data_str(&analytics_data.netdata_fail_reason, sqlite3_errstr(sqlite3_extended_errcode(db_meta)));
sqlite3_close(db_meta);
db_meta = NULL;
return 1;
}
- if (rebuild & (DB_CHECK_INTEGRITY | DB_CHECK_FIX_DB)) {
- int errors_detected = 0;
- if (!(rebuild & DB_CHECK_CONT))
- netdata_log_info("Running database check on %s", sqlite_database);
-
- if (check_table_integrity("chart")) {
- errors_detected++;
- if (rebuild & DB_CHECK_FIX_DB)
- rebuild_chart();
- else
- error_report("Errors reported -- run with -W sqlite-fix");
- }
-
- if (check_table_integrity("dimension")) {
- errors_detected++;
- if (rebuild & DB_CHECK_FIX_DB)
- rebuild_dimension();
- else
- error_report("Errors reported -- run with -W sqlite-fix");
- }
-
- if (!errors_detected) {
- if (check_table_integrity(NULL))
- error_report("Errors reported");
- }
- }
-
if (rebuild & DB_CHECK_RECLAIM_SPACE) {
- if (!(rebuild & DB_CHECK_CONT))
- netdata_log_info("Reclaiming space of %s", sqlite_database);
+ netdata_log_info("Reclaiming space of %s", sqlite_database);
rc = sqlite3_exec_monitored(db_meta, "VACUUM;", 0, 0, &err_msg);
if (rc != SQLITE_OK) {
error_report("Failed to execute VACUUM rc = %d (%s)", rc, err_msg);
sqlite3_free(err_msg);
}
- }
-
- if (rebuild && !(rebuild & DB_CHECK_CONT))
+ else {
+ (void) db_execute(db_meta, "select count(*) from sqlite_master limit 0");
+ (void) sqlite3_close(db_meta);
+ }
return 1;
+ }
netdata_log_info("SQLite database %s initialization", sqlite_database);
- char buf[1024 + 1] = "";
- const char *list[2] = { buf, NULL };
-
rc = sqlite3_create_function(db_meta, "u2h", 1, SQLITE_ANY | SQLITE_DETERMINISTIC, 0, sqlite_uuid_parse, 0, 0);
if (unlikely(rc != SQLITE_OK))
error_report("Failed to register internal u2h function");
@@ -464,44 +439,13 @@ int sql_init_database(db_check_action_type_t rebuild, int memory)
if (likely(!memory))
target_version = perform_database_migration(db_meta, DB_METADATA_VERSION);
- // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
- // PRAGMA schema.auto_vacuum = 0 | NONE | 1 | FULL | 2 | INCREMENTAL;
- snprintfz(buf, 1024, "PRAGMA auto_vacuum=%s;", config_get(CONFIG_SECTION_SQLITE, "auto vacuum", "INCREMENTAL"));
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
-
- // https://www.sqlite.org/pragma.html#pragma_synchronous
- // PRAGMA schema.synchronous = 0 | OFF | 1 | NORMAL | 2 | FULL | 3 | EXTRA;
- snprintfz(buf, 1024, "PRAGMA synchronous=%s;", config_get(CONFIG_SECTION_SQLITE, "synchronous", "NORMAL"));
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
-
- // https://www.sqlite.org/pragma.html#pragma_journal_mode
- // PRAGMA schema.journal_mode = DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
- snprintfz(buf, 1024, "PRAGMA journal_mode=%s;", config_get(CONFIG_SECTION_SQLITE, "journal mode", "WAL"));
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
-
- // https://www.sqlite.org/pragma.html#pragma_temp_store
- // PRAGMA temp_store = 0 | DEFAULT | 1 | FILE | 2 | MEMORY;
- snprintfz(buf, 1024, "PRAGMA temp_store=%s;", config_get(CONFIG_SECTION_SQLITE, "temp store", "MEMORY"));
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
-
- // https://www.sqlite.org/pragma.html#pragma_journal_size_limit
- // PRAGMA schema.journal_size_limit = N ;
- snprintfz(buf, 1024, "PRAGMA journal_size_limit=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "journal size limit", 16777216));
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
-
- // https://www.sqlite.org/pragma.html#pragma_cache_size
- // PRAGMA schema.cache_size = pages;
- // PRAGMA schema.cache_size = -kibibytes;
- snprintfz(buf, 1024, "PRAGMA cache_size=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "cache size", -2000));
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
-
- snprintfz(buf, 1024, "PRAGMA user_version=%d;", target_version);
- if(init_database_batch(db_meta, rebuild, 0, list)) return 1;
+ if (configure_sqlite_database(db_meta, target_version))
+ return 1;
- if (init_database_batch(db_meta, rebuild, 0, &database_config[0]))
+ if (init_database_batch(db_meta, &database_config[0]))
return 1;
- if (init_database_batch(db_meta, rebuild, 0, &database_cleanup[0]))
+ if (init_database_batch(db_meta, &database_cleanup[0]))
return 1;
netdata_log_info("SQLite database initialization completed");
@@ -543,7 +487,7 @@ int exec_statement_with_uuid(const char *sql, uuid_t *uuid)
rc = sqlite3_bind_blob(res, 1, uuid, sizeof(*uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host parameter to %s, rc = %d", sql, rc);
+ error_report("Failed to bind UUID parameter to %s, rc = %d", sql, rc);
goto skip;
}
@@ -665,44 +609,6 @@ failed:
return rc - 1;
}
-#define SQL_SELECT_HOST_BY_NODE_ID "select host_id from node_instance where node_id = @node_id;"
-
-int get_host_id(uuid_t *node_id, uuid_t *host_id)
-{
- static __thread sqlite3_stmt *res = NULL;
- int rc;
-
- if (unlikely(!db_meta)) {
- if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
- error_report("Database has not been initialized");
- return 1;
- }
-
- if (unlikely(!res)) {
- rc = prepare_statement(db_meta, SQL_SELECT_HOST_BY_NODE_ID, &res);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to prepare statement to select node instance information for a node");
- return 1;
- }
- }
-
- rc = sqlite3_bind_blob(res, 1, node_id, sizeof(*node_id), SQLITE_STATIC);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to bind host_id parameter to select node instance information");
- goto failed;
- }
-
- rc = sqlite3_step_monitored(res);
- if (likely(rc == SQLITE_ROW && host_id))
- uuid_copy(*host_id, *((uuid_t *) sqlite3_column_blob(res, 0)));
-
-failed:
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
- error_report("Failed to reset the prepared statement when selecting node instance information");
-
- return (rc == SQLITE_ROW) ? 0 : -1;
-}
-
#define SQL_SELECT_NODE_ID "SELECT node_id FROM node_instance WHERE host_id = @host_id AND node_id IS NOT NULL;"
int get_node_id(uuid_t *host_id, uuid_t *node_id)
@@ -826,6 +732,8 @@ struct node_instance_list *get_node_list(void)
uuid_t *host_id = (uuid_t *)sqlite3_column_blob(res, 1);
uuid_unparse_lower(*host_id, host_guid);
RRDHOST *host = rrdhost_find_by_guid(host_guid);
+ if (!host)
+ continue;
if (rrdhost_flag_check(host, RRDHOST_FLAG_PENDING_CONTEXT_LOAD)) {
netdata_log_info("ACLK: 'host:%s' skipping get node list because context is initializing", rrdhost_hostname(host));
continue;
@@ -856,7 +764,7 @@ failed:
void sql_load_node_id(RRDHOST *host)
{
- static __thread sqlite3_stmt *res = NULL;
+ sqlite3_stmt *res = NULL;
int rc;
if (unlikely(!db_meta)) {
@@ -865,13 +773,11 @@ void sql_load_node_id(RRDHOST *host)
return;
}
- if (unlikely(!res)) {
- rc = prepare_statement(db_meta, SQL_GET_HOST_NODE_ID, &res);
- if (unlikely(rc != SQLITE_OK)) {
- error_report("Failed to prepare statement to fetch node id");
- return;
- };
- }
+ rc = sqlite3_prepare_v2(db_meta, SQL_GET_HOST_NODE_ID, -1, &res, 0);
+ if (unlikely(rc != SQLITE_OK)) {
+ error_report("Failed to prepare statement to fetch node id");
+ return;
+ };
rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
if (unlikely(rc != SQLITE_OK)) {
@@ -888,8 +794,8 @@ void sql_load_node_id(RRDHOST *host)
}
failed:
- if (unlikely(sqlite3_reset(res) != SQLITE_OK))
- error_report("Failed to reset the prepared statement when loading node instance information");
+ if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
+ error_report("Failed to finalize the prepared statement when loading node instance information");
};
@@ -926,11 +832,11 @@ skip:
#define SELECT_HOST_LABELS "SELECT label_key, label_value, source_type FROM host_label WHERE host_id = @host_id " \
"AND label_key IS NOT NULL AND label_value IS NOT NULL;"
-DICTIONARY *sql_load_host_labels(uuid_t *host_id)
+RRDLABELS *sql_load_host_labels(uuid_t *host_id)
{
int rc;
- DICTIONARY *labels = NULL;
+ RRDLABELS *labels = NULL;
sqlite3_stmt *res = NULL;
rc = sqlite3_prepare_v2(db_meta, SELECT_HOST_LABELS, -1, &res, 0);
@@ -948,11 +854,7 @@ DICTIONARY *sql_load_host_labels(uuid_t *host_id)
labels = rrdlabels_create();
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
- rrdlabels_add(
- labels,
- (const char *)sqlite3_column_text(res, 0),
- (const char *)sqlite3_column_text(res, 1),
- sqlite3_column_int(res, 2));
+ rrdlabels_add(labels, (const char *)sqlite3_column_text(res, 0), (const char *)sqlite3_column_text(res, 1), sqlite3_column_int(res, 2));
}
skip: