@cryptotaxi247 / netdata-1 / commits / 018b1fc74

Avoid indexing journal files when db rotation is running (#20204)

Refactor atomic checks into a reusable macro Consolidated repeated atomic condition checks into a single macro `NOT_INDEXING_OR_DELETING_FILES` for better code reuse and readability. Removed redundant retry logic in the indexing worker to simplify the flow.

Stelios Fragkakis committed Apr 29, 2025 at 23:44 UTC 018b1fc74983e9337135551f6b884df015e97782
1 file changed +7 -16
src/database/engine/rrdengine.c
+7 -16
@@ -1673,19 +1673,10 @@ NOT_INLINE_HOT void pdc_route_synchronously_first(struct rrdengine_instance *ctx
1673 pdc_to_epdl_router(ctx, pdc, epdl_populate_pages_synchronously, epdl_populate_pages_asynchronously);
1674 }
1675
1676 -#define MAX_RETRIES_TO_START_INDEX (100)
1676 static void *journal_v2_indexing_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) {
1677 unsigned count = 0;
1678 worker_is_busy(UV_EVENT_DBENGINE_JOURNAL_INDEX_WAIT);
1679
1681 - while (__atomic_load_n(&ctx->atomic.now_deleting_files, __ATOMIC_RELAXED) && count++ < MAX_RETRIES_TO_START_INDEX)
1682 - sleep_usec(100 * USEC_PER_MS);
1683 -
1684 - if (count == MAX_RETRIES_TO_START_INDEX) {
1685 - worker_is_idle();
1686 - return data;
1687 - }
1688 -
1680 struct rrdengine_datafile *datafile = ctx->datafiles.first;
1681 worker_is_busy(UV_EVENT_DBENGINE_JOURNAL_INDEX);
1682 count = 0;
@@ -2040,6 +2031,10 @@ void rrdeng_calculate_tier_disk_space_percentage(void)
2031 }
2032 }
2033
2034 +#define NOT_INDEXING_OR_DELETING_FILES(ctx) \
2035 + (!__atomic_load_n(&(ctx)->atomic.migration_to_v2_running, __ATOMIC_RELAXED) && \
2036 + !__atomic_load_n(&(ctx)->atomic.now_deleting_files, __ATOMIC_RELAXED))
2037 +
2038 void dbengine_event_loop(void* arg) {
2039 sanity_check();
2040 uv_thread_set_name_np("DBENGINE");
@@ -2183,8 +2178,7 @@ void dbengine_event_loop(void* arg) {
2178 case RRDENG_OPCODE_JOURNAL_INDEX: {
2179 struct rrdengine_instance *ctx = cmd.ctx;
2180 struct rrdengine_datafile *datafile = cmd.data;
2186 - if(!__atomic_load_n(&ctx->atomic.migration_to_v2_running, __ATOMIC_RELAXED) &&
2187 - ctx_is_available_for_queries(ctx)) {
2181 + if (NOT_INDEXING_OR_DELETING_FILES(ctx) && ctx_is_available_for_queries(ctx)) {
2182 __atomic_store_n(&ctx->atomic.migration_to_v2_running, true, __ATOMIC_RELAXED);
2183 work_dispatch(ctx, datafile, NULL, opcode, journal_v2_indexing_tp_worker, after_journal_v2_indexing);
2184 }
@@ -2193,11 +2187,8 @@ void dbengine_event_loop(void* arg) {
2187
2188 case RRDENG_OPCODE_DATABASE_ROTATE: {
2189 struct rrdengine_instance *ctx = cmd.ctx;
2196 - if (!__atomic_load_n(&ctx->atomic.now_deleting_files, __ATOMIC_RELAXED) &&
2197 - !__atomic_load_n(&ctx->atomic.migration_to_v2_running, __ATOMIC_RELAXED) &&
2198 - ctx->datafiles.first->next != NULL && ctx->datafiles.first->next->next != NULL &&
2199 - rrdeng_ctx_tier_cap_exceeded(ctx)) {
2200 -
2190 + if (NOT_INDEXING_OR_DELETING_FILES(ctx) && ctx->datafiles.first->next != NULL &&
2191 + ctx->datafiles.first->next->next != NULL && rrdeng_ctx_tier_cap_exceeded(ctx)) {
2192 __atomic_store_n(&ctx->atomic.now_deleting_files, true, __ATOMIC_RELAXED);
2193 work_dispatch(ctx, NULL, NULL, opcode, database_rotate_tp_worker, after_database_rotate);
2194 }