From ed54cf5eeee34d5b240986941fddd2034b734a1b Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Thu, 6 Jul 2017 10:26:54 +0200 Subject: [PATCH] mxqd: Refactor main loop The server status if multiple times set from the main loop to IDLE,RUNNING,BACKFILL,CPUOPTIMAL or FULL based on server->slots_running, server->slots, server->threads_running. Factor this out into a separate function to avoid repetition. Add a call to this function before the main loop so that we immediately get a correct status after a server start. Simplify the loop code by setting server status at end of loop and avoid the continue pattern. As a side effect this also resolves a bug, that the server status was not updated in a loop iteration where new jobs were started. --- mxqd.c | 72 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/mxqd.c b/mxqd.c index 171bf159..8435e3bf 100644 --- a/mxqd.c +++ b/mxqd.c @@ -2325,6 +2325,25 @@ static void process_signal(struct mxq_server *server,int sig,int extra) } } +static void update_status(struct mxq_server *server) +{ + struct mxq_daemon *daemon = &server->daemon; + + if (!server->slots_running) { + mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_IDLE); + } else { + if (server->slots_running < server->slots) + mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_RUNNING); + else if (server->slots_running > server->slots) + mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_BACKFILL); + else + if (server->threads_running == server->slots) + mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_CPUOPTIMAL); + else + mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_FULL); + } +} + int main(int argc, char *argv[]) { int group_cnt; @@ -2411,6 +2430,7 @@ int main(int argc, char *argv[]) if (server->recoveronly) fail = 1; + update_status(server); mx_log_info("entering main loop"); while (!global_sigint_cnt && !global_sigterm_cnt && !global_sigquit_cnt && !global_sigrestart_cnt && !fail) { mx_log_debug("main loop - wait for signals max %ld sec",poll_interval.tv_sec); @@ -2432,51 +2452,23 @@ int main(int argc, char *argv[]) killall_over_time(server); killall_over_memory(server); - if (!server->group_cnt) { - assert(!server->jobs_running); - assert(!group_cnt); - mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_IDLE); - mx_log_debug("Nothing to do"); - continue; - } - - if (server->slots_running >= server->slots) { - if (server->threads_running == server->slots) { - mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_CPUOPTIMAL); - } else if (server->slots_running > server->slots) { - mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_BACKFILL); - } else { - mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_FULL); - } - mx_log_debug("All slots running"); - continue; - } - - slots_started=0; - do { - res = start_user_with_least_running_global_slot_count(server); - if (res>0) { - slots_started+=res; - } - } while (res>0); - if (slots_started) - mx_log_info("Main loop started %lu slots.", slots_started); - if (res<0) { + if (server->slots_runningslots && server->group_cnt) { + slots_started=0; + do { + res = start_user_with_least_running_global_slot_count(server); + if (res>0) { + slots_started+=res; + } + } while (res>0); + if (slots_started) + mx_log_info("Main loop started %lu slots.", slots_started); + if (res<0) { mx_log_info("No more slots started because we have users waiting for free slots"); mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_WAITING); continue; - } - - if (!slots_started && !slots_returned && !global_sigint_cnt && !global_sigterm_cnt) { - if (!server->jobs_running) { - mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_IDLE); - mx_log_debug("Tried Hard and nobody is doing anything."); - } else { - mxq_daemon_set_status(server->mysql, daemon, MXQ_DAEMON_STATUS_RUNNING); - mx_log_debug("Tried Hard. But have done nothing."); } - continue; } + update_status(server); } /*** clean up ***/