[PATCH v5 1/3] box: implement on_shutdown triggers

Vladimir Davydov vdavydov.dev at gmail.com
Mon Jan 28 11:43:25 MSK 2019


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



More information about the Tarantool-patches mailing list