From bb789d1ac0708819df7d16dbaf5bf126f652dc1a Mon Sep 17 00:00:00 2001 From: Unbit Date: Wed, 13 Feb 2013 08:56:43 +0100 Subject: [PATCH] fixed cheaper --- core/master.c | 40 +++++++++++++++++++++++++--------------- core/master_checks.c | 18 ++++++++++++++++++ core/master_utils.c | 5 +---- core/uwsgi.c | 1 + uwsgi.h | 4 ++++ 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/core/master.c b/core/master.c index c322b082..33b43a7d 100644 --- a/core/master.c +++ b/core/master.c @@ -165,23 +165,15 @@ void suspend_resume_them_all(int signum) { void uwsgi_master_check_mercy() { - int i, waitpid_status; + int i; for (i = 1; i <= uwsgi.numproc; i++) { if (uwsgi.workers[i].pid > 0 && uwsgi.workers[i].cursed_at) { if (uwsgi_now() > uwsgi.workers[i].no_mercy_at) { uwsgi_log("worker %d (pid: %d) is taking too much time to die...NO MERCY !!!\n", i, uwsgi.workers[i].pid); - if (!kill(uwsgi.workers[i].pid, SIGKILL)) { - if (waitpid(uwsgi.workers[i].pid, &waitpid_status, 0) < 0) { - uwsgi_error("uwsgi_master_check_mercy()/waitpid()"); - } - } - else { + if (kill(uwsgi.workers[i].pid, SIGKILL)) { uwsgi_error("uwsgi_master_check_mercy()/kill()"); } - uwsgi.workers[i].pid = 0; - uwsgi.workers[i].cursed_at = 0; - uwsgi.workers[i].no_mercy_at = 0; } } } @@ -484,6 +476,7 @@ int master_loop(char **argv, char **environ) { uwsgi_check_touches(uwsgi.touch_logrotate); uwsgi_check_touches(uwsgi.touch_logreopen); uwsgi_check_touches(uwsgi.touch_chain_reload); + uwsgi_check_touches(uwsgi.touch_workers_reload); uwsgi_check_touches(uwsgi.touch_gracefully_stop); // setup cheaper algos (can be stacked) @@ -530,6 +523,9 @@ int master_loop(char **argv, char **environ) { return -1; } + // check chain reload + uwsgi_master_check_chain(); + // check if some worker is taking too much to die... uwsgi_master_check_mercy(); @@ -745,25 +741,34 @@ int master_loop(char **argv, char **environ) { if (!uwsgi_instance_is_reloading && !uwsgi_instance_is_dying) { char *touched = uwsgi_check_touches(uwsgi.touch_reload); if (touched) { - uwsgi_log("*** %s has been touched... grace them all !!! ***\n", touched); + uwsgi_log_verbose("*** %s has been touched... grace them all !!! ***\n", touched); uwsgi_block_signal(SIGHUP); grace_them_all(0); uwsgi_unblock_signal(SIGHUP); continue; } - touched = uwsgi_check_touches(uwsgi.touch_chain_reload); + touched = uwsgi_check_touches(uwsgi.touch_workers_reload); if (touched) { + uwsgi_log_verbose("*** %s has been touched... workers reload !!! ***\n", touched); uwsgi_block_signal(SIGHUP); - uwsgi_log("*** %s has been touched... chain reload !!! ***\n", touched); for(i=1;i<=uwsgi.numproc;i++) { if (uwsgi.workers[i].pid > 0) { - kill(uwsgi.workers[i].pid, SIGHUP); + uwsgi_curse(i, SIGHUP); } } uwsgi_unblock_signal(SIGHUP); continue; } - + touched = uwsgi_check_touches(uwsgi.touch_chain_reload); + if (touched) { + if (uwsgi.status.chain_reloading == 0) { + uwsgi_log_verbose("*** %s has been touched... chain reload !!! ***\n", touched); + uwsgi.status.chain_reloading = 1; + } + else { + uwsgi_log_verbose("*** %s has been touched... but chain reload is already running ***\n", touched); + } + } } continue; @@ -908,6 +913,11 @@ next: gettimeofday(&last_respawn, NULL); uwsgi.respawn_delta = last_respawn.tv_sec; + // are we chain reloading it ? + if (uwsgi.status.chain_reloading == uwsgi.mywid) { + uwsgi.status.chain_reloading++; + } + // respawn the worker (if needed) if (uwsgi_respawn_worker(uwsgi.mywid)) return 0; diff --git a/core/master_checks.c b/core/master_checks.c index 6076ae6c..90fe9172 100644 --- a/core/master_checks.c +++ b/core/master_checks.c @@ -32,6 +32,24 @@ int uwsgi_master_check_reload(char **argv) { return 0; } +// check for chain reload +void uwsgi_master_check_chain() { + if (!uwsgi.status.chain_reloading) return; + if (uwsgi.status.chain_reloading > uwsgi.numproc) { + uwsgi.status.chain_reloading = 0; + uwsgi_log_verbose("chain reloading complete\n"); + } + int i; + uwsgi_block_signal(SIGHUP); + for(i=1;i<=uwsgi.numproc;i++) { + if (uwsgi.workers[i].pid > 0 && uwsgi.workers[i].cheaped == 0 && uwsgi.workers[i].cursed_at == 0 && i == uwsgi.status.chain_reloading) { + uwsgi_curse(i, SIGHUP); + break; + } + } + uwsgi_unblock_signal(SIGHUP); +} + // special function for assuming all of the workers are dead void uwsgi_master_commit_status() { diff --git a/core/master_utils.c b/core/master_utils.c index ed87c90b..6b2c685a 100644 --- a/core/master_utils.c +++ b/core/master_utils.c @@ -135,10 +135,7 @@ int uwsgi_calc_cheaper(void) { #endif uwsgi.workers[oldest_worker].cheaped = 1; uwsgi.workers[oldest_worker].manage_next_request = 0; - // TODO fix here - uwsgi.workers[oldest_worker].cursed_at = now; - // wakeup task in case of wait - (void) kill(uwsgi.workers[oldest_worker].pid, SIGWINCH); + uwsgi_curse(oldest_worker, SIGWINCH); } } diff --git a/core/uwsgi.c b/core/uwsgi.c index 2f88ab3e..2e32e55f 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -301,6 +301,7 @@ static struct uwsgi_option uwsgi_base_options[] = { #endif {"never-swap", no_argument, 0, "lock all memory pages avoiding swapping", uwsgi_opt_true, &uwsgi.never_swap, 0}, {"touch-reload", required_argument, 0, "reload uWSGI if the specified file is modified/touched", uwsgi_opt_add_string_list, &uwsgi.touch_reload, UWSGI_OPT_MASTER}, + {"touch-workers-reload", required_argument, 0, "trigger reload of (only) workers if the specified file is modified/touched", uwsgi_opt_add_string_list, &uwsgi.touch_workers_reload, UWSGI_OPT_MASTER}, {"touch-chain-reload", required_argument, 0, "trigger chain reload if the specified file is modified/touched", uwsgi_opt_add_string_list, &uwsgi.touch_chain_reload, UWSGI_OPT_MASTER}, {"touch-logrotate", required_argument, 0, "trigger logrotation if the specified file is modified/touched", uwsgi_opt_add_string_list, &uwsgi.touch_logrotate, UWSGI_OPT_MASTER | UWSGI_OPT_LOG_MASTER}, {"touch-logreopen", required_argument, 0, "trigger log reopen if the specified file is modified/touched", uwsgi_opt_add_string_list, &uwsgi.touch_logreopen, UWSGI_OPT_MASTER | UWSGI_OPT_LOG_MASTER}, diff --git a/uwsgi.h b/uwsgi.h index 1fbad783..08845246 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -3803,10 +3803,14 @@ int uwsgi_master_check_daemons_death(int); void uwsgi_master_check_death(void); int uwsgi_master_check_reload(char **); void uwsgi_master_commit_status(void); +void uwsgi_master_check_chain(void); void uwsgi_master_fix_request_counters(void); int uwsgi_master_manage_events(int); +void uwsgi_block_signal(int); +void uwsgi_unblock_signal(int); + #define uwsgi_response_add_connection_close(x) uwsgi_response_add_header(x, "Connection", 10, "close", 5) #define uwsgi_response_add_content_type(x, y, z) uwsgi_response_add_header(x, "Content-Type", 12, y, z)