Improve shutdown and datafile rotation (#19991)
* Remove additional precompiled statements * Handle coverity warning * Fix null host check in aclk_host_state_update_auto condition * Add check if migration_v2 is running before database rotation attempt * Use size_t to calculate deleted bytes
Stelios Fragkakis committed
Mar 29, 2025 at 23:13 UTC
3ecdec6751fd6660f7fd8bef15bdf417debd0e24
4 files changed
+20
-31
src/database/engine/rrdengine.c
+4
-4
@@ -1249,7 +1249,7 @@ void datafile_delete(struct rrdengine_instance *ctx, struct rrdengine_datafile *
1249
worker_is_busy(UV_EVENT_DBENGINE_DATAFILE_DELETE);
1250
1251
struct rrdengine_journalfile *journal_file;
1252
- unsigned deleted_bytes, journal_file_bytes, datafile_bytes;
1252
+ size_t deleted_bytes, journal_file_bytes, datafile_bytes;
1253
int ret;
1254
char path[RRDENG_PATH_MAX];
1255
@@ -1300,7 +1300,7 @@ void datafile_delete(struct rrdengine_instance *ctx, struct rrdengine_datafile *
1300
freez(datafile);
1301
1302
ctx_current_disk_space_decrease(ctx, deleted_bytes);
1303
- netdata_log_info("DBENGINE: reclaimed %u bytes of disk space.", deleted_bytes);
1303
+ netdata_log_info("DBENGINE: reclaimed %zu bytes of disk space.", deleted_bytes);
1304
}
1305
1306
static void *database_rotate_tp_worker(struct rrdengine_instance *ctx __maybe_unused, void *data __maybe_unused, struct completion *completion __maybe_unused, uv_work_t *uv_work_req __maybe_unused) {
@@ -2025,8 +2025,8 @@ void dbengine_event_loop(void* arg) {
2025
case RRDENG_OPCODE_DATABASE_ROTATE: {
2026
struct rrdengine_instance *ctx = cmd.ctx;
2027
if (!__atomic_load_n(&ctx->atomic.now_deleting_files, __ATOMIC_RELAXED) &&
2028
- ctx->datafiles.first->next != NULL &&
2029
- ctx->datafiles.first->next->next != NULL &&
2028
+ !__atomic_load_n(&ctx->atomic.migration_to_v2_running, __ATOMIC_RELAXED) &&
2029
+ ctx->datafiles.first->next != NULL && ctx->datafiles.first->next->next != NULL &&
2030
rrdeng_ctx_tier_cap_exceeded(ctx)) {
2031
2032
__atomic_store_n(&ctx->atomic.now_deleting_files, true, __ATOMIC_RELAXED);
src/database/sqlite/sqlite_aclk.c
+1
-1
@@ -521,7 +521,7 @@ static void node_update_timer_cb(uv_timer_t *handle)
521
struct aclk_sync_cfg_t *ahc = handle->data;
522
RRDHOST *host = ahc->host;
523
524
- if(aclk_host_state_update_auto(host))
524
+ if(!host || aclk_host_state_update_auto(host))
525
uv_timer_stop(&ahc->timer);
526
}
527
src/database/sqlite/sqlite_health.c
+14
-25
@@ -987,37 +987,26 @@ void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, time_t after, const ch
987
{
988
unsigned int max = host->health_log.max;
989
990
- static __thread sqlite3_stmt *stmt_no_chart = NULL;
991
- static __thread sqlite3_stmt *stmt_with_chart = NULL;
992
-
993
- sqlite3_stmt **active_stmt;
990
sqlite3_stmt *stmt_query;
991
992
int rc;
993
998
- active_stmt = chart ? &stmt_with_chart : &stmt_no_chart;
999
-
1000
- if (!*active_stmt) {
1001
-
1002
- BUFFER *command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
1003
- buffer_sprintf(command, SQL_SELECT_HEALTH_LOG);
994
+ BUFFER *command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
995
+ buffer_sprintf(command, SQL_SELECT_HEALTH_LOG);
996
1005
- if (chart)
1006
- buffer_strcat(command, " AND hl.chart = @chart ");
997
+ if (chart)
998
+ buffer_strcat(command, " AND hl.chart = @chart ");
999
1008
- buffer_strcat(command, " ORDER BY hld.unique_id DESC LIMIT @limit");
1000
+ buffer_strcat(command, " ORDER BY hld.unique_id DESC LIMIT @limit");
1001
1010
- rc = prepare_statement(db_meta, buffer_tostring(command), active_stmt);
1011
- buffer_free(command);
1002
+ rc = PREPARE_STATEMENT(db_meta, buffer_tostring(command), &stmt_query);
1003
+ buffer_free(command);
1004
1013
- if (unlikely(rc != SQLITE_OK)) {
1014
- error_report("Failed to prepare statement SQL_SELECT_HEALTH_LOG");
1015
- return;
1016
- }
1005
+ if (unlikely(rc != SQLITE_OK)) {
1006
+ error_report("Failed to prepare statement SQL_SELECT_HEALTH_LOG");
1007
+ return;
1008
}
1009
1019
- stmt_query = *active_stmt;
1020
-
1010
int param = 0;
1011
rc = sqlite3_bind_blob(stmt_query, ++param, &host->host_id.uuid, sizeof(host->host_id.uuid), SQLITE_STATIC);
1012
if (unlikely(rc != SQLITE_OK)) {
@@ -1127,7 +1116,7 @@ void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, time_t after, const ch
1116
buffer_json_finalize(wb);
1117
1118
finish:
1130
- SQLITE_RESET(stmt_query);
1119
+ SQLITE_FINALIZE(stmt_query);
1120
}
1121
1122
#define SQL_COPY_HEALTH_LOG(table) "INSERT OR IGNORE INTO health_log (host_id, alarm_id, config_hash_id, name, chart, family, exec, recipient, units, chart_context) SELECT ?1, alarm_id, config_hash_id, name, chart, family, exec, recipient, units, chart_context from %s", table
@@ -1330,7 +1319,7 @@ bool sql_find_alert_transition(
1319
void (*cb)(const char *machine_guid, const char *context, time_t alert_id, void *data),
1320
void *data)
1321
{
1333
- static __thread sqlite3_stmt *res = NULL;
1322
+ sqlite3_stmt *res = NULL;
1323
1324
char machine_guid[UUID_STR_LEN];
1325
@@ -1338,7 +1327,7 @@ bool sql_find_alert_transition(
1327
if (uuid_parse(transition, transition_uuid))
1328
return false;
1329
1341
- if (!PREPARE_COMPILED_STATEMENT(db_meta, SQL_GET_ALARM_ID_FROM_TRANSITION_ID, &res))
1330
+ if (!PREPARE_STATEMENT(db_meta, SQL_GET_ALARM_ID_FROM_TRANSITION_ID, &res))
1331
return false;
1332
1333
bool ok = false;
@@ -1355,7 +1344,7 @@ bool sql_find_alert_transition(
1344
1345
done:
1346
REPORT_BIND_FAIL(res, param);
1358
- SQLITE_RESET(res);
1347
+ SQLITE_FINALIZE(res);
1348
return ok;
1349
}
1350
src/database/sqlite/sqlite_metadata.c
+1
-1
@@ -2076,7 +2076,7 @@ size_t populate_metrics_from_database(void *mrg, void (*populate_cb)(void *mrg,
2076
}
2077
2078
if (local_meta_db)
2079
- db_execute(local_meta_db, "PRAGMA cache_size=10000");
2079
+ (void) db_execute(local_meta_db, "PRAGMA cache_size=10000");
2080
2081
if (!PREPARE_STATEMENT(local_meta_db ? local_meta_db : db_meta, GET_UUID_LIST, &res)) {
2082
sqlite3_close(local_meta_db);