Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Serge Petrenko <sergepetrenko@tarantool.org>
Cc: kostja@tarantool.org, tarantool-patches@freelists.org
Subject: Re: [PATCH v5 1/3] box: implement on_shutdown triggers
Date: Mon, 28 Jan 2019 11:43:25 +0300	[thread overview]
Message-ID: <20190128084325.2f2a6j6nyykjzkhl@esperanza> (raw)
In-Reply-To: <9c7b6a71c1092f04c4538fddc1b08199e01dd995.1548430046.git.sergepetrenko@tarantool.org>

The patch is generally OK. See a few really minor comments below.

On Fri, Jan 25, 2019 at 06:41:32PM +0300, Serge Petrenko wrote:
> 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;

Missing comment.

> +/** A trigger which will break the event loop on shutdown. */
> +static struct trigger shutdown_trig;

IMO break_loop_trigger would be a more appropriate name.

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

Should be static.

> +{
> +	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();

I guess the fiber should be made non-cancellable so that it couldn't be
occasionally woken up from Lua.

> +		/*
> +		 * 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);

  reply	other threads:[~2019-01-28  8:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-25 15:41 [PATCH v5 0/3] " Serge Petrenko
2019-01-25 15:41 ` [PATCH v5 1/3] " Serge Petrenko
2019-01-28  8:43   ` Vladimir Davydov [this message]
2019-01-29 14:47     ` Serge Petrenko
2019-01-25 15:41 ` [PATCH v5 2/3] lua: patch os.exit() to execute " Serge Petrenko
2019-01-28  8:45   ` Vladimir Davydov
2019-01-29 14:49     ` [tarantool-patches] " Serge Petrenko
2019-01-25 15:41 ` [PATCH v5 3/3] box: get rid of atexit() for calling cleanup routines Serge Petrenko
2019-01-30 10:52 ` [PATCH v5 0/3] box: implement on_shutdown triggers Vladimir Davydov

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=20190128084325.2f2a6j6nyykjzkhl@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=kostja@tarantool.org \
    --cc=sergepetrenko@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH v5 1/3] box: implement 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