Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH 0/2] box: implement on_shutdown triggers
@ 2018-11-22 17:20 Serge Petrenko
  2018-11-22 17:20 ` [tarantool-patches] [PATCH 1/2] " Serge Petrenko
  2018-11-22 17:20 ` [tarantool-patches] [PATCH 2/2] box: introduce on_shutdown triggers to lua Serge Petrenko
  0 siblings, 2 replies; 4+ messages in thread
From: Serge Petrenko @ 2018-11-22 17:20 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

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.

Konstantin Osipov (1):
  box: implement on_shutdown triggers

Serge Petrenko (1):
  box: introduce on_shutdown triggers to lua

 src/box/box.cc         | 10 ++++++++++
 src/box/box.h          | 11 +++++++++++
 src/box/lua/ctl.c      | 21 +++++++++++++++++++++
 src/main.cc            |  2 ++
 test/box/misc.result   | 34 ++++++++++++++++++++++++++++++++++
 test/box/misc.test.lua | 14 ++++++++++++++
 6 files changed, 92 insertions(+)

-- 
2.17.2 (Apple Git-113)

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

* [tarantool-patches] [PATCH 1/2] box: implement on_shutdown triggers
  2018-11-22 17:20 [tarantool-patches] [PATCH 0/2] box: implement on_shutdown triggers Serge Petrenko
@ 2018-11-22 17:20 ` Serge Petrenko
  2018-11-22 17:20 ` [tarantool-patches] [PATCH 2/2] box: introduce on_shutdown triggers to lua Serge Petrenko
  1 sibling, 0 replies; 4+ messages in thread
From: Serge Petrenko @ 2018-11-22 17:20 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 | 10 ++++++++++
 src/box/box.h  | 11 +++++++++++
 src/main.cc    |  2 ++
 3 files changed, 23 insertions(+)

diff --git a/src/box/box.cc b/src/box/box.cc
index 7a1171c62..d8be85b3a 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,14 @@ box_set_replicaset_uuid(const struct tt_uuid *replicaset_uuid)
 		diag_raise();
 }
 
+void
+box_run_on_shutdown_triggers(void)
+{
+	if (is_box_configured) {
+		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 2/2] box: introduce on_shutdown triggers to lua
  2018-11-22 17:20 [tarantool-patches] [PATCH 0/2] box: implement on_shutdown triggers Serge Petrenko
  2018-11-22 17:20 ` [tarantool-patches] [PATCH 1/2] " Serge Petrenko
@ 2018-11-22 17:20 ` Serge Petrenko
  2018-11-23 10:37   ` [tarantool-patches] " Konstantin Osipov
  1 sibling, 1 reply; 4+ messages in thread
From: Serge Petrenko @ 2018-11-22 17:20 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] Re: [PATCH 2/2] box: introduce on_shutdown triggers to lua
  2018-11-22 17:20 ` [tarantool-patches] [PATCH 2/2] box: introduce on_shutdown triggers to lua Serge Petrenko
@ 2018-11-23 10:37   ` Konstantin Osipov
  0 siblings, 0 replies; 4+ messages in thread
From: Konstantin Osipov @ 2018-11-23 10:37 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Serge Petrenko

* Serge Petrenko <sergepetrenko@tarantool.org> [18/11/22 20:21]:
> Make it possible to register on_shutdown triggers from lua via
> box.ctl.on_shutdown()

OK, this is a nice step forward, can we add shutdown reason to the
event and marshal it to Lua? 

Why are you calling the trigger only in case box is configured?
Why should it matter?

The ticket is missing a docbot plea.

What if on_shutdown trigger yields? We either should document that
it can't yield or test that it can yield. Besides, it can do some
very nasty things, like use 'box', or sockets, or seep. We should
(if possible) test and document what it can and cannot do.

> 
> 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)
> 

-- 
Konstantin Osipov, Moscow, Russia, +7 903 626 22 32
http://tarantool.io - www.twitter.com/kostja_osipov

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

end of thread, other threads:[~2018-11-23 10:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-22 17:20 [tarantool-patches] [PATCH 0/2] box: implement on_shutdown triggers Serge Petrenko
2018-11-22 17:20 ` [tarantool-patches] [PATCH 1/2] " Serge Petrenko
2018-11-22 17:20 ` [tarantool-patches] [PATCH 2/2] box: introduce on_shutdown triggers to lua Serge Petrenko
2018-11-23 10:37   ` [tarantool-patches] " Konstantin Osipov

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