From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Serge Petrenko Subject: [PATCH v5 1/3] box: implement on_shutdown triggers Date: Fri, 25 Jan 2019 18:41:32 +0300 Message-Id: <9c7b6a71c1092f04c4538fddc1b08199e01dd995.1548430046.git.sergepetrenko@tarantool.org> In-Reply-To: References: In-Reply-To: References: To: vdavydov.dev@gmail.com, kostja@tarantool.org Cc: tarantool-patches@freelists.org, Serge Petrenko List-ID: Add on_shutdown triggers which are run by a preallocated fiber on shutdown and make it possible to register them via box.ctl.on_shutdown() Make use of the new triggers: now dedicate an on_shutdown trigger to break event loop instead of doing it explicitly from signal handler. The trigger is run last, so that all other on_shutdown triggers may yield, sleep and so on. Also make sure we can register lbox_triggers without push_event function in case we don't need one. Part of #1607 --- src/box/box.cc | 2 ++ src/box/box.h | 3 ++ src/box/lua/ctl.c | 8 +++++ src/lua/trigger.c | 5 ++- src/main.cc | 50 +++++++++++++++++++++++++++-- test/box/misc.result | 72 ++++++++++++++++++++++++++++++++++++++++++ test/box/misc.test.lua | 32 +++++++++++++++++++ 7 files changed, 169 insertions(+), 3 deletions(-) diff --git a/src/box/box.cc b/src/box/box.cc index 9f2fd6da1..fb595aeee 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -79,6 +79,8 @@ static char status[64] = "unknown"; /** box.stat rmean */ struct rmean *rmean_box; +struct rlist box_on_shutdown = RLIST_HEAD_INITIALIZER(box_on_shutdown); + static void title(const char *new_status) { snprintf(status, sizeof(status), "%s", new_status); diff --git a/src/box/box.h b/src/box/box.h index 6c6c319fc..53d88ab71 100644 --- a/src/box/box.h +++ b/src/box/box.h @@ -64,6 +64,9 @@ struct vclock; */ extern const struct vclock *box_vclock; +/** Invoked on box shutdown. */ +extern struct rlist box_on_shutdown; + /* * Initialize box library * @throws C++ exception diff --git a/src/box/lua/ctl.c b/src/box/lua/ctl.c index 9a105ed5c..7010be138 100644 --- a/src/box/lua/ctl.c +++ b/src/box/lua/ctl.c @@ -37,6 +37,7 @@ #include #include "lua/utils.h" +#include "lua/trigger.h" #include "box/box.h" @@ -64,9 +65,16 @@ lbox_ctl_wait_rw(struct lua_State *L) return 0; } +static int +lbox_ctl_on_shutdown(struct lua_State *L) +{ + return lbox_trigger_reset(L, 2, &box_on_shutdown, NULL, NULL); +} + static const struct luaL_Reg lbox_ctl_lib[] = { {"wait_ro", lbox_ctl_wait_ro}, {"wait_rw", lbox_ctl_wait_rw}, + {"on_shutdown", lbox_ctl_on_shutdown}, {NULL, NULL} }; diff --git a/src/lua/trigger.c b/src/lua/trigger.c index 2c2ede212..ec4d8aab3 100644 --- a/src/lua/trigger.c +++ b/src/lua/trigger.c @@ -91,7 +91,10 @@ lbox_trigger_run(struct trigger *ptr, void *event) } int top = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, trigger->ref); - int nargs = trigger->push_event(L, event); + int nargs = 0; + if (trigger->push_event != NULL) { + nargs = trigger->push_event(L, event); + } if (luaT_call(L, nargs, LUA_MULTRET)) { luaL_unref(tarantool_L, LUA_REGISTRYINDEX, coro_ref); diag_raise(); diff --git a/src/main.cc b/src/main.cc index 993355ac4..96df9e851 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; +/** A preallocated fiber to run on_shutdown triggers. */ +static struct fiber *on_shutdown_fiber = NULL; +static bool is_shutting_down = false; +/** A trigger which will break the event loop on shutdown. */ +static struct trigger shutdown_trig; + double tarantool_uptime(void) { @@ -119,9 +125,33 @@ sig_checkpoint(ev_loop * /* loop */, struct ev_signal * /* w */, fiber_wakeup(f); } +static int +on_shutdown_f(va_list ap) +{ + (void) ap; + trigger_run(&box_on_shutdown, NULL); + return 0; +} + +void +tarantool_exit(void) +{ + if (is_shutting_down) { + /* + * We are already running on_shutdown triggers, + * and will exit as soon as they'll finish. + * Do not execute them twice. + */ + return; + } + is_shutting_down = true; + fiber_wakeup(on_shutdown_fiber); +} + static void signal_cb(ev_loop *loop, struct ev_signal *w, int revents) { + (void) loop; (void) w; (void) revents; @@ -135,8 +165,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)); start_loop = false; - /* Terminate the main event loop */ - ev_break(loop, EVBREAK_ALL); + tarantool_exit(); } static void @@ -628,6 +657,12 @@ print_help(const char *program) puts("to see online documentation, submit bugs or contribute a patch."); } +static void +break_loop(struct trigger *, void *) +{ + ev_break(loop(), EVBREAK_ALL); +} + int main(int argc, char **argv) { @@ -751,6 +786,17 @@ main(int argc, char **argv) try { box_init(); box_lua_init(tarantool_L); + /* Reserve a fiber to run on_shutdown triggers. */ + on_shutdown_fiber = fiber_new("on_exit", on_shutdown_f); + if (on_shutdown_fiber == NULL) + diag_raise(); + /* + * Register a on_shutdown trigger which will break the + * main event loop. The trigger will be the last to run + * since it's the first one we register. + */ + trigger_create(&shutdown_trig, break_loop, NULL, NULL); + trigger_add(&box_on_shutdown, &shutdown_trig); /* main core cleanup routine */ atexit(tarantool_free); diff --git a/test/box/misc.result b/test/box/misc.result index c3cabcc8a..6912915c1 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -1249,3 +1249,75 @@ box.cfg{too_long_threshold = too_long_threshold} s:drop() --- ... +-- +-- gh-1607: on_shutdown triggers. +-- +f = function() print('on_shutdown 1') end +--- +... +g = function() print('on_shutdown 2') end +--- +... +h = function() print('on_shutdown 3') end +--- +... +-- Check that on_shutdown triggers may yield +-- and perform some complicated actions. +fiber = require('fiber') +--- +... +test_run:cmd("setopt delimiter ';'") +--- +- true +... +trig = function() + fiber.sleep(0.01) + fiber.yield() + box.schema.space.create("shutdown") + box.space.shutdown:create_index("pk") + box.space.shutdown:insert{1,2,3} + print('on_shutdown 4') +end; +--- +... +test_run:cmd("setopt delimiter ''"); +--- +- true +... +_ = box.ctl.on_shutdown(f) +--- +... +_ = box.ctl.on_shutdown(g) +--- +... +-- Check that replacing triggers works +_ = box.ctl.on_shutdown(h, g) +--- +... +_ = box.ctl.on_shutdown(trig) +--- +... +test_run:cmd('restart server default') +test_run:grep_log('default', 'on_shutdown 1', nil, {noreset=true}) +--- +- on_shutdown 1 +... +test_run:grep_log('default', 'on_shutdown 2', nil, {noreset=true}) +--- +- null +... +test_run:grep_log('default', 'on_shutdown 3', nil, {noreset=true}) +--- +- on_shutdown 3 +... +test_run:grep_log('default', 'on_shutdown 4', nil, {noreset=true}) +--- +- on_shutdown 4 +... +box.space.shutdown:select{} +--- +- - [1, 2, 3] +... +box.space.shutdown:drop() +--- +... diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua index cc6cb34fb..f1c9d8e8c 100644 --- a/test/box/misc.test.lua +++ b/test/box/misc.test.lua @@ -352,3 +352,35 @@ rows == expected_rows lsn == expected_lsn box.cfg{too_long_threshold = too_long_threshold} s:drop() + +-- +-- gh-1607: on_shutdown triggers. +-- +f = function() print('on_shutdown 1') end +g = function() print('on_shutdown 2') end +h = function() print('on_shutdown 3') end +-- Check that on_shutdown triggers may yield +-- and perform some complicated actions. +fiber = require('fiber') +test_run:cmd("setopt delimiter ';'") +trig = function() + fiber.sleep(0.01) + fiber.yield() + box.schema.space.create("shutdown") + box.space.shutdown:create_index("pk") + box.space.shutdown:insert{1,2,3} + print('on_shutdown 4') +end; +test_run:cmd("setopt delimiter ''"); +_ = box.ctl.on_shutdown(f) +_ = box.ctl.on_shutdown(g) +-- Check that replacing triggers works +_ = box.ctl.on_shutdown(h, g) +_ = box.ctl.on_shutdown(trig) +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', 'on_shutdown 4', nil, {noreset=true}) +box.space.shutdown:select{} +box.space.shutdown:drop() -- 2.17.2 (Apple Git-113)