Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v2 0/3] box: implement on_shutdown triggers
@ 2018-11-26 14:25 Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 1/3] " Serge Petrenko
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Serge Petrenko @ 2018-11-26 14:25 UTC (permalink / raw)
  To: kostja, tarantool-patches; +Cc: Serge Petrenko

This patchset adds on_shutdown triggers which can be registered from lua 
via box.ctl.on_shutdown() and are run on tarantool shutdown.

Issue: https://github.com/tarantool/tarantool/issues/1607
Branch: https://github.com/tarantool/tarantool/tree/sp/gh-1607-on-exit-triggers

Changes in v2: 
  - added 3rd patch which passes signals to triggers.
  - run on_shutdown triggers even if box is not configured.
  - add a documentation request to the 3rd patch.

First patch was originally made by Kostja and implemented on_shutdown
triggers. I had to alter it a bit so that on shutdown box_free() is
called after coio_shutdown(). Otherwise tarantool hung on shutdown
occasionally. I also factored out trigger_run() into a separate function 
box_run_on_shutdown_triggers(), so that on_shutdown triggers are still
run before anything else is freed or shut down.

Second patch expands box.ctl interface by introducing box.ctl.on_shutdown()
function to register on_shutdown triggers from lua.

Third patch adds the ability to pass signal which caused termination to
the on_shutdown trigger.

Konstantin Osipov (1):
  box: implement on_shutdown triggers

Serge Petrenko (2):
  box: introduce on_shutdown triggers to lua
  box: pass signal to on_shutdown triggers

 src/box/box.cc         |  8 ++++++++
 src/box/box.h          | 12 ++++++++++++
 src/box/lua/ctl.c      | 23 ++++++++++++++++++++++
 src/main.cc            | 10 +++++++++-
 test/box/misc.result   | 44 ++++++++++++++++++++++++++++++++++++++++++
 test/box/misc.test.lua | 17 ++++++++++++++++
 6 files changed, 113 insertions(+), 1 deletion(-)

-- 
2.17.2 (Apple Git-113)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] [PATCH v2 1/3] box: implement on_shutdown triggers
  2018-11-26 14:25 [tarantool-patches] [PATCH v2 0/3] box: implement on_shutdown triggers Serge Petrenko
@ 2018-11-26 14:25 ` Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 2/3] box: introduce on_shutdown triggers to lua Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers Serge Petrenko
  2 siblings, 0 replies; 4+ messages in thread
From: Serge Petrenko @ 2018-11-26 14:25 UTC (permalink / raw)
  To: kostja, tarantool-patches

From: Konstantin Osipov <kostja@tarantool.org>

@sergepetrenko:
Factor out running on_shutdown triggers from box_free() into a separate
function.
In tarantool_free() run on_shutdown triggers before coio_shutdown(), but
leave box_free() after coio_shutdown(). Otherwise tarantool hangs on
shutdown occasionally.
---
 src/box/box.cc |  8 ++++++++
 src/box/box.h  | 11 +++++++++++
 src/main.cc    |  2 ++
 3 files changed, 21 insertions(+)

diff --git a/src/box/box.cc b/src/box/box.cc
index 7a1171c62..72049fabf 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -78,6 +78,8 @@ static char status[64] = "unknown";
 /** box.stat rmean */
 struct rmean *rmean_box;
 
+struct rlist on_shutdown = RLIST_HEAD_INITIALIZER(on_shutdown);
+
 static void title(const char *new_status)
 {
 	snprintf(status, sizeof(status), "%s", new_status);
@@ -1645,6 +1647,12 @@ box_set_replicaset_uuid(const struct tt_uuid *replicaset_uuid)
 		diag_raise();
 }
 
+void
+box_run_on_shutdown_triggers(void)
+{
+	trigger_run(&on_shutdown, NULL);
+}
+
 void
 box_free(void)
 {
diff --git a/src/box/box.h b/src/box/box.h
index 9f5b3acbd..e2c3aad9e 100644
--- a/src/box/box.h
+++ b/src/box/box.h
@@ -63,6 +63,11 @@ struct vclock;
  */
 extern const struct vclock *box_vclock;
 
+struct trigger;
+
+/** Invoked on box shutdown. */
+extern struct rlist on_shutdown;
+
 /*
  * Initialize box library
  * @throws C++ exception
@@ -76,6 +81,12 @@ box_init(void);
 void
 box_free(void);
 
+/**
+ * Run on_shutdown triggers.
+ */
+void
+box_run_on_shutdown_triggers(void);
+
 /**
  * Load configuration for box library.
  * Panics on error.
diff --git a/src/main.cc b/src/main.cc
index a36a2b0d0..987c53834 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -544,6 +544,8 @@ tarantool_free(void)
 	if (!cord_is_main())
 		return;
 
+	box_run_on_shutdown_triggers();
+
 	/* Shutdown worker pool. Waits until threads terminate. */
 	coio_shutdown();
 
-- 
2.17.2 (Apple Git-113)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] [PATCH v2 2/3] box: introduce on_shutdown triggers to lua
  2018-11-26 14:25 [tarantool-patches] [PATCH v2 0/3] box: implement on_shutdown triggers Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 1/3] " Serge Petrenko
@ 2018-11-26 14:25 ` Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers Serge Petrenko
  2 siblings, 0 replies; 4+ messages in thread
From: Serge Petrenko @ 2018-11-26 14:25 UTC (permalink / raw)
  To: kostja, tarantool-patches; +Cc: Serge Petrenko

Make it possible to register on_shutdown triggers from lua via
box.ctl.on_shutdown()

Part of #1607
---
 src/box/lua/ctl.c      | 21 +++++++++++++++++++++
 test/box/misc.result   | 34 ++++++++++++++++++++++++++++++++++
 test/box/misc.test.lua | 14 ++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/src/box/lua/ctl.c b/src/box/lua/ctl.c
index 9a105ed5c..dad39515c 100644
--- a/src/box/lua/ctl.c
+++ b/src/box/lua/ctl.c
@@ -37,6 +37,7 @@
 #include <lualib.h>
 
 #include "lua/utils.h"
+#include "lua/trigger.h"
 
 #include "box/box.h"
 
@@ -64,9 +65,29 @@ 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.
+ */
+static int
+lbox_push_on_shutdown(struct lua_State *L, void *event)
+{
+	(void)L;
+	(void)event;
+	return 0;
+}
+
+static int
+lbox_ctl_on_shutdown(struct lua_State *L)
+{
+	return lbox_trigger_reset(L, 2, &on_shutdown, lbox_push_on_shutdown,
+				  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/test/box/misc.result b/test/box/misc.result
index 4ee4797d0..fd55b8cf1 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -1205,3 +1205,37 @@ 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
+---
+...
+_ = box.ctl.on_shutdown(f)
+---
+...
+_ = box.ctl.on_shutdown(g)
+---
+...
+_ = box.ctl.on_shutdown(h, g)
+---
+...
+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
+...
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index ee81c7be1..a4b38b403 100644
--- a/test/box/misc.test.lua
+++ b/test/box/misc.test.lua
@@ -342,3 +342,17 @@ 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
+_ = box.ctl.on_shutdown(f)
+_ = box.ctl.on_shutdown(g)
+_ = box.ctl.on_shutdown(h, g)
+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})
-- 
2.17.2 (Apple Git-113)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers
  2018-11-26 14:25 [tarantool-patches] [PATCH v2 0/3] box: implement on_shutdown triggers Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 1/3] " Serge Petrenko
  2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 2/3] box: introduce on_shutdown triggers to lua Serge Petrenko
@ 2018-11-26 14:25 ` Serge Petrenko
  2 siblings, 0 replies; 4+ messages in thread
From: Serge Petrenko @ 2018-11-26 14:25 UTC (permalink / raw)
  To: kostja, tarantool-patches; +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)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-11-26 14:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-26 14:25 [tarantool-patches] [PATCH v2 0/3] box: implement on_shutdown triggers Serge Petrenko
2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 1/3] " Serge Petrenko
2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 2/3] box: introduce on_shutdown triggers to lua Serge Petrenko
2018-11-26 14:25 ` [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers Serge Petrenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox