Skip to content

Commit

Permalink
avoid dbengine event loop starvation by running uv_run periodically (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsaou authored Feb 17, 2025
1 parent c19ae2a commit 12930b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/database/engine/rrdengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,18 +524,19 @@ static inline bool rrdeng_cmd_has_waiting_opcodes_in_lower_priorities(STORAGE_PR
static inline struct rrdeng_cmd rrdeng_deq_cmd(bool from_worker) {
struct rrdeng_cmd *cmd = NULL;
enum LIBUV_WORKERS_STATUS status = work_request_full();

STORAGE_PRIORITY min_priority, max_priority;
min_priority = STORAGE_PRIORITY_INTERNAL_DBENGINE;
max_priority = (status != LIBUV_WORKERS_RELAXED) ? STORAGE_PRIORITY_INTERNAL_DBENGINE : STORAGE_PRIORITY_INTERNAL_MAX_DONT_USE - 1;

if(from_worker) {
if(unlikely(from_worker)) {
if(status == LIBUV_WORKERS_CRITICAL)
return opcode_empty;

min_priority = STORAGE_PRIORITY_INTERNAL_QUERY_PREP;
max_priority = STORAGE_PRIORITY_BEST_EFFORT;
}
else {
min_priority = STORAGE_PRIORITY_INTERNAL_DBENGINE;
max_priority = (status != LIBUV_WORKERS_RELAXED) ? STORAGE_PRIORITY_INTERNAL_DBENGINE : STORAGE_PRIORITY_INTERNAL_MAX_DONT_USE - 1;
}

// find an opcode to execute from the queue
spinlock_lock(&rrdeng_main.cmd_queue.unsafe.spinlock);
Expand Down Expand Up @@ -1916,7 +1917,15 @@ void dbengine_event_loop(void* arg) {
uv_run(&main->loop, UV_RUN_DEFAULT);

/* wait for commands */
size_t count = 0;
do {
count++;

if(count % 100 == 0) {
worker_is_idle();
uv_run(&main->loop, UV_RUN_NOWAIT);
}

worker_is_busy(RRDENG_OPCODE_MAX);
cmd = rrdeng_deq_cmd(RRDENG_OPCODE_NOOP);
opcode = cmd.opcode;
Expand Down
2 changes: 1 addition & 1 deletion src/database/storage-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ typedef enum __attribute__ ((__packed__)) storage_priority {
// query priorities
STORAGE_PRIORITY_HIGH,
STORAGE_PRIORITY_NORMAL,
STORAGE_PRIORITY_SYNCHRONOUS_FIRST,
STORAGE_PRIORITY_LOW,
STORAGE_PRIORITY_BEST_EFFORT,

// synchronous query, not to be dispatched to workers or queued
STORAGE_PRIORITY_SYNCHRONOUS,
STORAGE_PRIORITY_SYNCHRONOUS_FIRST,

STORAGE_PRIORITY_INTERNAL_MAX_DONT_USE,
} STORAGE_PRIORITY;
Expand Down

0 comments on commit 12930b2

Please sign in to comment.