Skip to content

Commit

Permalink
Code improvements (netdata#16104)
Browse files Browse the repository at this point in the history
* Remove unused functions

* No need for prepare statement because the function is not used frequently

* Remove db_meta check, already assumed valid

* Remove D_ACLK_SYNC and D_METADATALOG, fix log message

* Reuse prepared statements per run to avoid sql parsing all the time

* Keep rowid in charts and dimensions

* Host and chart labels keep rowids

* Don't store internal flags

* Remove commented out code

* Formatting

* Fix algorithm when updating dimension
  • Loading branch information
stelfrag authored Oct 6, 2023
1 parent 003bb6d commit 18e729d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 171 deletions.
16 changes: 2 additions & 14 deletions database/sqlite/sqlite_aclk.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ static void aclk_database_enq_cmd(struct aclk_database_cmd *cmd)
uv_mutex_unlock(&aclk_sync_config.cmd_mutex);

/* wake up event loop */
int rc = uv_async_send(&aclk_sync_config.async);
if (unlikely(rc))
netdata_log_debug(D_ACLK_SYNC, "Failed to wake up event loop");
(void) uv_async_send(&aclk_sync_config.async);
}

enum {
Expand Down Expand Up @@ -226,14 +224,8 @@ static void sql_delete_aclk_table_list(char *host_guid)
uuid_unparse_lower(host_uuid, host_str);
uuid_unparse_lower_fix(&host_uuid, uuid_str);

netdata_log_debug(D_ACLK_SYNC, "Checking if I should delete aclk tables for node %s", host_str);

if (is_host_available(&host_uuid)) {
netdata_log_debug(D_ACLK_SYNC, "Host %s exists, not deleting aclk sync tables", host_str);
if (is_host_available(&host_uuid))
return;
}

netdata_log_debug(D_ACLK_SYNC, "Host %s does NOT exist, can delete aclk sync tables", host_str);

sqlite3_stmt *res = NULL;
BUFFER *sql = buffer_create(ACLK_SYNC_QUERY_SIZE, &netdata_buffers_statistics.buffers_sqlite);
Expand Down Expand Up @@ -265,7 +257,6 @@ static void sql_delete_aclk_table_list(char *host_guid)

static int sql_check_aclk_table(void *data __maybe_unused, int argc __maybe_unused, char **argv __maybe_unused, char **column __maybe_unused)
{
netdata_log_debug(D_ACLK_SYNC,"Scheduling aclk sync table check for node %s", (char *) argv[0]);
struct aclk_database_cmd cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = ACLK_DATABASE_DELETE_HOST;
Expand All @@ -280,7 +271,6 @@ static int sql_check_aclk_table(void *data __maybe_unused, int argc __maybe_unus
static void sql_check_aclk_table_list(void)
{
char *err_msg = NULL;
netdata_log_debug(D_ACLK_SYNC,"Cleaning tables for nodes that do not exist");
int rc = sqlite3_exec_monitored(db_meta, SQL_SELECT_ACLK_ACTIVE_LIST, sql_check_aclk_table, NULL, &err_msg);
if (rc != SQLITE_OK) {
error_report("Query failed when trying to check for obsolete ACLK sync tables, %s", err_msg);
Expand All @@ -305,7 +295,6 @@ static int sql_maint_aclk_sync_database(void *data __maybe_unused, int argc __ma
static void sql_maint_aclk_sync_database_all(void)
{
char *err_msg = NULL;
netdata_log_debug(D_ACLK_SYNC,"Cleaning tables for nodes that do not exist");
int rc = sqlite3_exec_monitored(db_meta, SQL_SELECT_ACLK_ALERT_LIST, sql_maint_aclk_sync_database, NULL, &err_msg);
if (rc != SQLITE_OK) {
error_report("Query failed when trying to check for obsolete ACLK sync tables, %s", err_msg);
Expand Down Expand Up @@ -444,7 +433,6 @@ static void aclk_synchronization(void *arg __maybe_unused)
sql_process_queue_removed_alerts_to_aclk(cmd.param[0]);
break;
default:
netdata_log_debug(D_ACLK_SYNC, "%s: default.", __func__);
break;
}
if (cmd.completion)
Expand Down
75 changes: 13 additions & 62 deletions database/sqlite/sqlite_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,10 @@ static void recover_database(const char *sqlite_database, const char *new_sqlite
int execute_insert(sqlite3_stmt *res)
{
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);
}
else {
if (rc == SQLITE_CORRUPT)
(void) mark_database_to_recover(res, NULL);
error_report("SQLite error %d", rc);
break;
}
rc = sqlite3_step_monitored(res);
if (rc == SQLITE_CORRUPT) {
(void)mark_database_to_recover(res, NULL);
error_report("SQLite error %d", rc);
}
return rc;
}
Expand Down Expand Up @@ -320,7 +312,6 @@ 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 initialization, rc = %d (%s)", rc, err_msg);
Expand Down Expand Up @@ -496,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;
}

Expand Down Expand Up @@ -618,44 +609,6 @@ int update_node_id(uuid_t *host_id, uuid_t *node_id)
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)
Expand Down Expand Up @@ -811,7 +764,7 @@ struct node_instance_list *get_node_list(void)

void sql_load_node_id(RRDHOST *host)
{
static __thread sqlite3_stmt *res = NULL;
sqlite3_stmt *res = NULL;
int rc;

if (unlikely(!db_meta)) {
Expand All @@ -820,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)) {
Expand All @@ -843,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");
};


Expand Down
2 changes: 0 additions & 2 deletions database/sqlite/sqlite_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ void initialize_thread_key_pool(void);

// Look up functions
int get_node_id(uuid_t *host_id, uuid_t *node_id);
int get_host_id(uuid_t *node_id, uuid_t *host_id);
struct node_instance_list *get_node_list(void);
void sql_load_node_id(RRDHOST *host);
char *get_hostname_by_node_id(char *node_id);

// Help build archived hosts in memory when agent starts
void sql_build_host_system_info(uuid_t *host_id, struct rrdhost_system_info *system_info);
Expand Down
Loading

0 comments on commit 18e729d

Please sign in to comment.