From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id CF0AF2D5F1 for ; Mon, 26 Nov 2018 09:25:38 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lFhMm-9tOv01 for ; Mon, 26 Nov 2018 09:25:38 -0500 (EST) Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [94.100.177.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 8715B2B1A4 for ; Mon, 26 Nov 2018 09:25:38 -0500 (EST) From: Serge Petrenko Subject: [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers Date: Mon, 26 Nov 2018 17:25:06 +0300 Message-Id: <9c927ffbe8d5060807eede3caf15680b4b4457d6.1543241956.git.sergepetrenko@tarantool.org> In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: kostja@tarantool.org, tarantool-patches@freelists.org Cc: Serge Petrenko Start passing signal which caused termination to on_shutdown triggers. Closes #1607 @TarantoolBot document Title: Document box.ctl.on_shutdown triggers on_shutdown triggers may be set similar to space:on_replace triggers: ``` box.ctl.on_shutdown(new_trigger, old_trigger) ``` The triggers will be run when tarantool exits either normally, or due to receiving a signal `SIGTERM`, `SIGINT`, `SIGHUP`. The signal which caused termination (if any) is available to the trigger as 1st parameter. Limitations: a trigger cannot perform DML, DDL requests, call fiber.yield(), fiber.sleep(), use net.box and console modules - it leads to undefined behaviour. --- src/box/box.cc | 4 ++-- src/box/box.h | 3 ++- src/box/lua/ctl.c | 12 +++++++----- src/main.cc | 10 ++++++++-- test/box/misc.result | 10 ++++++++++ test/box/misc.test.lua | 3 +++ 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/box/box.cc b/src/box/box.cc index 72049fabf..c0e62c061 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -1648,9 +1648,9 @@ box_set_replicaset_uuid(const struct tt_uuid *replicaset_uuid) } void -box_run_on_shutdown_triggers(void) +box_run_on_shutdown_triggers(int sig) { - trigger_run(&on_shutdown, NULL); + trigger_run(&on_shutdown, sig != 0 ? &sig : NULL); } void diff --git a/src/box/box.h b/src/box/box.h index e2c3aad9e..376550c80 100644 --- a/src/box/box.h +++ b/src/box/box.h @@ -83,9 +83,10 @@ box_free(void); /** * Run on_shutdown triggers. + * \param sig signal which caused termination. */ void -box_run_on_shutdown_triggers(void); +box_run_on_shutdown_triggers(int sig); /** * Load configuration for box library. diff --git a/src/box/lua/ctl.c b/src/box/lua/ctl.c index dad39515c..980feedd3 100644 --- a/src/box/lua/ctl.c +++ b/src/box/lua/ctl.c @@ -65,15 +65,17 @@ lbox_ctl_wait_rw(struct lua_State *L) return 0; } -/* - * This is a placeholder. It will be used when we decide to pass - * a signal which caused termination to the trigger. +/** + * Push a signal which caused termination on lua stack + * to be used by on_shutdown triggers. */ static int lbox_push_on_shutdown(struct lua_State *L, void *event) { - (void)L; - (void)event; + if (event != NULL) { + lua_pushinteger(L, *(int *)event); + return 1; + } return 0; } diff --git a/src/main.cc b/src/main.cc index 987c53834..4ef45fafa 100644 --- a/src/main.cc +++ b/src/main.cc @@ -89,6 +89,12 @@ static const int ev_sig_count = sizeof(ev_sigs)/sizeof(*ev_sigs); static double start_time; +/** + * The signal which caused termination to be passed + * to on_shutdown triggers. + */ +static int caught_sig = 0; + double tarantool_uptime(void) { @@ -113,7 +119,6 @@ sig_checkpoint(ev_loop * /* loop */, struct ev_signal * /* w */, static void signal_cb(ev_loop *loop, struct ev_signal *w, int revents) { - (void) w; (void) revents; /** @@ -125,6 +130,7 @@ signal_cb(ev_loop *loop, struct ev_signal *w, int revents) */ if (pid_file) say_crit("got signal %d - %s", w->signum, strsignal(w->signum)); + caught_sig = w->signum; start_loop = false; /* Terminate the main event loop */ ev_break(loop, EVBREAK_ALL); @@ -544,7 +550,7 @@ tarantool_free(void) if (!cord_is_main()) return; - box_run_on_shutdown_triggers(); + box_run_on_shutdown_triggers(caught_sig); /* Shutdown worker pool. Waits until threads terminate. */ coio_shutdown(); diff --git a/test/box/misc.result b/test/box/misc.result index fd55b8cf1..33dadef29 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -1217,6 +1217,9 @@ g = function() print('on_shutdown 2') end h = function() print('on_shutdown 3') end --- ... +i = function(a) print(string.format('signal is %d', a)) end +--- +... _ = box.ctl.on_shutdown(f) --- ... @@ -1226,6 +1229,9 @@ _ = box.ctl.on_shutdown(g) _ = box.ctl.on_shutdown(h, g) --- ... +_ = box.ctl.on_shutdown(i) +--- +... test_run:cmd('restart server default') test_run:grep_log('default', 'on_shutdown 1', nil, {noreset=true}) --- @@ -1239,3 +1245,7 @@ test_run:grep_log('default', 'on_shutdown 3', nil, {noreset=true}) --- - on_shutdown 3 ... +test_run:grep_log('default', 'signal is.*', nil, {noreset=true}) +--- +- signal is 15 +... diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua index a4b38b403..df42f28d3 100644 --- a/test/box/misc.test.lua +++ b/test/box/misc.test.lua @@ -349,10 +349,13 @@ s:drop() f = function() print('on_shutdown 1') end g = function() print('on_shutdown 2') end h = function() print('on_shutdown 3') end +i = function(a) print(string.format('signal is %d', a)) end _ = box.ctl.on_shutdown(f) _ = box.ctl.on_shutdown(g) _ = box.ctl.on_shutdown(h, g) +_ = box.ctl.on_shutdown(i) test_run:cmd('restart server default') test_run:grep_log('default', 'on_shutdown 1', nil, {noreset=true}) test_run:grep_log('default', 'on_shutdown 2', nil, {noreset=true}) test_run:grep_log('default', 'on_shutdown 3', nil, {noreset=true}) +test_run:grep_log('default', 'signal is.*', nil, {noreset=true}) -- 2.17.2 (Apple Git-113)