Skip to content

Commit

Permalink
Reorganize and cleanup database related code (netdata#17101)
Browse files Browse the repository at this point in the history
* Rearrange code

* Explicit init of sqlite library and shutdown
Function to close all databases

* Add functions to return db size

* Prevent overflow

* Address review comments

* Initialize sqlite library before ml_init

* Fix unittest
  • Loading branch information
stelfrag authored Mar 5, 2024
1 parent 935ffce commit 1116111
Show file tree
Hide file tree
Showing 13 changed files with 799 additions and 770 deletions.
18 changes: 11 additions & 7 deletions src/daemon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,10 @@ void netdata_cleanup_and_exit(int ret, const char *action, const char *action_re
#endif
}

sql_close_context_database();
watcher_step_complete(WATCHER_STEP_ID_CLOSE_SQL_CONTEXT_DB);
sqlite_close_databases();
watcher_step_complete(WATCHER_STEP_ID_CLOSE_SQL_DATABASES);
sqlite_library_shutdown();

sql_close_database();
watcher_step_complete(WATCHER_STEP_ID_CLOSE_SQL_MAIN_DB);

// unlink the pid
if(pidfile[0]) {
Expand Down Expand Up @@ -1500,22 +1499,24 @@ int main(int argc, char **argv) {
#endif

if(strcmp(optarg, "sqlite-meta-recover") == 0) {
sql_init_database(DB_CHECK_RECOVER, 0);
sql_init_meta_database(DB_CHECK_RECOVER, 0);
return 0;
}

if(strcmp(optarg, "sqlite-compact") == 0) {
sql_init_database(DB_CHECK_RECLAIM_SPACE, 0);
sql_init_meta_database(DB_CHECK_RECLAIM_SPACE, 0);
return 0;
}

if(strcmp(optarg, "sqlite-analyze") == 0) {
sql_init_database(DB_CHECK_ANALYZE, 0);
sql_init_meta_database(DB_CHECK_ANALYZE, 0);
return 0;
}

if(strcmp(optarg, "unittest") == 0) {
unittest_running = true;
if (sqlite_library_init())
return 1;

if (pluginsd_parser_unittest()) return 1;
if (unit_test_static_threads()) return 1;
Expand All @@ -1539,6 +1540,7 @@ int main(int argc, char **argv) {
if (ctx_unittest()) return 1;
if (uuid_unittest()) return 1;
if (dyncfg_unittest()) return 1;
sqlite_library_shutdown();
fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
return 0;
}
Expand Down Expand Up @@ -2054,6 +2056,8 @@ int main(int argc, char **argv) {
exit(1);
}
}
if (sqlite_library_init())
fatal("Failed to initialize sqlite library");

// --------------------------------------------------------------------
// Initialize ML configuration
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ void dbengine_stress_test(unsigned TEST_DURATION_SEC, unsigned DSET_CHARTS, unsi

fprintf(stderr, "Initializing localhost with hostname 'dbengine-stress-test'\n");

(void) sql_init_database(DB_CHECK_NONE, 1);
(void)sql_init_meta_database(DB_CHECK_NONE, 1);
host = dbengine_rrdhost_find_or_create("dbengine-stress-test");
if (NULL == host)
return;
Expand Down
9 changes: 3 additions & 6 deletions src/daemon/watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ void *watcher_main(void *arg)
watcher_wait_for_step(WATCHER_STEP_ID_WAIT_FOR_DBENGINE_COLLECTORS_TO_FINISH);
watcher_wait_for_step(WATCHER_STEP_ID_WAIT_FOR_DBENGINE_MAIN_CACHE_TO_FINISH_FLUSHING);
watcher_wait_for_step(WATCHER_STEP_ID_STOP_DBENGINE_TIERS);
watcher_wait_for_step(WATCHER_STEP_ID_CLOSE_SQL_CONTEXT_DB);
watcher_wait_for_step(WATCHER_STEP_ID_CLOSE_SQL_MAIN_DB);
watcher_wait_for_step(WATCHER_STEP_ID_CLOSE_SQL_DATABASES);
watcher_wait_for_step(WATCHER_STEP_ID_REMOVE_PID_FILE);
watcher_wait_for_step(WATCHER_STEP_ID_FREE_OPENSSL_STRUCTURES);
watcher_wait_for_step(WATCHER_STEP_ID_REMOVE_INCOMPLETE_SHUTDOWN_FILE);
Expand Down Expand Up @@ -146,10 +145,8 @@ void watcher_thread_start() {
"wait for dbengine main cache to finish flushing";
watcher_steps[WATCHER_STEP_ID_STOP_DBENGINE_TIERS].msg =
"stop dbengine tiers";
watcher_steps[WATCHER_STEP_ID_CLOSE_SQL_CONTEXT_DB].msg =
"close SQL context db";
watcher_steps[WATCHER_STEP_ID_CLOSE_SQL_MAIN_DB].msg =
"close SQL main db";
watcher_steps[WATCHER_STEP_ID_CLOSE_SQL_DATABASES].msg =
"close SQL databases";
watcher_steps[WATCHER_STEP_ID_REMOVE_PID_FILE].msg =
"remove pid file";
watcher_steps[WATCHER_STEP_ID_FREE_OPENSSL_STRUCTURES].msg =
Expand Down
3 changes: 1 addition & 2 deletions src/daemon/watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ typedef enum {
WATCHER_STEP_ID_WAIT_FOR_DBENGINE_COLLECTORS_TO_FINISH,
WATCHER_STEP_ID_WAIT_FOR_DBENGINE_MAIN_CACHE_TO_FINISH_FLUSHING,
WATCHER_STEP_ID_STOP_DBENGINE_TIERS,
WATCHER_STEP_ID_CLOSE_SQL_CONTEXT_DB,
WATCHER_STEP_ID_CLOSE_SQL_MAIN_DB,
WATCHER_STEP_ID_CLOSE_SQL_DATABASES,
WATCHER_STEP_ID_REMOVE_PID_FILE,
WATCHER_STEP_ID_FREE_OPENSSL_STRUCTURES,
WATCHER_STEP_ID_REMOVE_INCOMPLETE_SHUTDOWN_FILE,
Expand Down
2 changes: 1 addition & 1 deletion src/database/rrdhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ void dbengine_init(char *hostname) {
int rrd_init(char *hostname, struct rrdhost_system_info *system_info, bool unittest) {
rrdhost_init();

if (unlikely(sql_init_database(DB_CHECK_NONE, system_info ? 0 : 1))) {
if (unlikely(sql_init_meta_database(DB_CHECK_NONE, system_info ? 0 : 1))) {
if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
set_late_global_environment(system_info);
fatal("Failed to initialize SQLite");
Expand Down
30 changes: 10 additions & 20 deletions src/database/sqlite/sqlite_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,6 @@ int sql_init_context_database(int memory)
return 0;
}

/*
* Close the sqlite database
*/

void sql_close_context_database(void)
{
int rc;
if (unlikely(!db_context_meta))
return;

netdata_log_info("Closing context SQLite database");

rc = sqlite3_close_v2(db_context_meta);
if (unlikely(rc != SQLITE_OK))
error_report("Error %d while closing the context SQLite database, %s", rc, sqlite3_errstr(rc));
db_context_meta = NULL;
}

//
// Fetching data
//
Expand Down Expand Up @@ -422,6 +404,12 @@ int sql_context_cache_stats(int op)
return count;
}


uint64_t sqlite_get_context_space(void)
{
return sqlite_get_db_space(db_context_meta);
}

//
// TESTING FUNCTIONS
//
Expand Down Expand Up @@ -456,7 +444,8 @@ int ctx_unittest(void)
uuid_t host_uuid;
uuid_generate(host_uuid);

initialize_thread_key_pool();
if (sqlite_library_init())
return 1;

int rc = sql_init_context_database(1);

Expand Down Expand Up @@ -532,7 +521,8 @@ int ctx_unittest(void)
ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
netdata_log_info("List context end after delete");

sql_close_context_database();
sql_close_database(db_context_meta, "CONTEXT");
sqlite_library_shutdown();

return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/database/sqlite/sqlite_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int ctx_store_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data);
int ctx_delete_context(uuid_t *host_id, VERSIONED_CONTEXT_DATA *context_data);

int sql_init_context_database(int memory);
uint64_t sqlite_get_context_space(void);
void sql_close_context_database(void);
int ctx_unittest(void);
#endif //NETDATA_SQLITE_CONTEXT_H
Loading

0 comments on commit 1116111

Please sign in to comment.