@cryptotaxi247 / netdata-1 / commits / d258177fb

Reduce workload during cleanup (#15919)

* Add index to improve health cleanup * Re arrange query to use index * Check less entries during cleanup to prevent CPU spike

Stelios Fragkakis committed Sep 5, 2023 at 22:22 UTC d258177fbec076bca2af85a78e30ccc16f761592
3 files changed +6 -4
database/sqlite/sqlite_functions.c
+1
@@ -59,6 +59,7 @@ const char *database_config[] = {
59 "CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id);",
60 "CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id);",
61 "CREATE INDEX IF NOT EXISTS health_log_d_ind_5 ON health_log_detail (health_log_id, unique_id DESC);",
62 + "CREATE INDEX IF NOT EXISTS health_log_d_ind_6 on health_log_detail (health_log_id, when_key)",
63
64 NULL
65 };
database/sqlite/sqlite_health.c
+2 -2
@@ -425,7 +425,7 @@ done:
425 */
426
427 #define SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED "DELETE FROM health_log_detail WHERE health_log_id IN " \
428 - "(SELECT health_log_id FROM health_log WHERE host_id = @host_id) AND when_key + @history < unixepoch() " \
428 + "(SELECT health_log_id FROM health_log WHERE host_id = @host_id) AND when_key < unixepoch() - @history " \
429 "AND updated_by_id <> 0 AND transition_id NOT IN " \
430 "(SELECT last_transition_id FROM health_log hl WHERE hl.host_id = @host_id);"
431
@@ -434,7 +434,7 @@ done:
434 "AND unique_id IN (SELECT hld.unique_id FROM health_log hl, health_log_detail hld WHERE " \
435 "hl.host_id = @host_id AND hl.health_log_id = hld.health_log_id) " \
436 "AND health_log_id IN (SELECT health_log_id FROM health_log WHERE host_id = @host_id) " \
437 - "AND when_key + @history < unixepoch() " \
437 + "AND when_key < unixepoch() - @history " \
438 "AND updated_by_id <> 0 AND transition_id NOT IN " \
439 "(SELECT last_transition_id FROM health_log hl WHERE hl.host_id = @host_id);", guid
440
database/sqlite/sqlite_metadata.c
+3 -2
@@ -730,7 +730,8 @@ static bool run_cleanup_loop(
730
731 time_t start_running = now_monotonic_sec();
732 bool time_expired = false;
733 - while (!time_expired && sqlite3_step_monitored(res) == SQLITE_ROW && *total_deleted < cleanup_threshold) {
733 + while (!time_expired && sqlite3_step_monitored(res) == SQLITE_ROW && *total_deleted < cleanup_threshold &&
734 + *total_checked < cleanup_threshold) {
735 if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
736 break;
737
@@ -745,7 +746,7 @@ static bool run_cleanup_loop(
746 (*total_checked)++;
747 time_expired = ((now_monotonic_sec() - start_running) > run_threshold);
748 }
748 - return time_expired;
749 + return time_expired || (*total_deleted == cleanup_threshold) || (*total_checked == cleanup_threshold);
750 }
751
752