Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko <sergepetrenko@tarantool.org>
To: kostja@tarantool.org, tarantool-patches@freelists.org
Cc: Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers
Date: Mon, 26 Nov 2018 17:25:06 +0300	[thread overview]
Message-ID: <9c927ffbe8d5060807eede3caf15680b4b4457d6.1543241956.git.sergepetrenko@tarantool.org> (raw)
In-Reply-To: <cover.1543241956.git.sergepetrenko@tarantool.org>
In-Reply-To: <cover.1543241956.git.sergepetrenko@tarantool.org>

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)

      parent reply	other threads:[~2018-11-26 14:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-26 14:25 [tarantool-patches] [PATCH v2 0/3] box: implement " 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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9c927ffbe8d5060807eede3caf15680b4b4457d6.1543241956.git.sergepetrenko@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH v2 3/3] box: pass signal to on_shutdown triggers' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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