Skip to content

Commit

Permalink
mxqd: Refactor main loop
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
donald committed Jul 6, 2017
1 parent adbe312 commit ed54cf5
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions mxqd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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_running<server->slots && 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 ***/

Expand Down

0 comments on commit ed54cf5

Please sign in to comment.