[Tarantool-patches] [PATCH 1/2] core: add setting error injections via env

Sergey Bronnikov sergeyb at tarantool.org
Fri Jan 29 16:36:54 MSK 2021


Thanks for the patch! See my 4 comments below.

On 28.01.2021 00:15, Artem Starshov wrote:
> Sometimes, it's useful to set error injections via environment
> variables and this commit adds this opportunity.
>
> e.g: `ERRINJ_WAL_WRITE=true tarantool` will be launched with
> ERRINJ_WAL_WRITE setted to true.
>
> Errinjs with bool parameters can should be set as "true", "TRUE",
> "false" or "FALSE".
>
> Errinjs with int or double parameters should be whole valid ("123s" is invalid).
> e.g. for int or double: "123", "-1", "2.34", "+2.34".
> ---
> @ChangeLog: Fixed -e option, when tarantool always entered interactive mode when
> stdin is a tty. Now, `tarantool -e 'print"Hello"'` doesn't enter interactive mode
> as it was before.

1. This commit is required by next commit with fix.

I propose to link it to issue too by adding "Part of #5040".

>
>   src/lib/core/errinj.c | 26 ++++++++++++++++++++++++++
>   src/lib/core/errinj.h |  5 +++++
>   src/main.cc           |  1 +
>   3 files changed, 32 insertions(+)
>
> diff --git a/src/lib/core/errinj.c b/src/lib/core/errinj.c
> index d3aa0ca4f..576811073 100644
> --- a/src/lib/core/errinj.c
> +++ b/src/lib/core/errinj.c
> @@ -66,3 +66,29 @@ int errinj_foreach(errinj_cb cb, void *cb_ctx) {
>   	}
>   	return 0;
>   }
> +
> +void errinj_scan_env() {

2. With function name it is not clear what function does.

Please consider rename. For example 'set_errinj_with_environment_vars' 
or something else.

> +    for (enum errinj_id i = 0 ; i < errinj_id_MAX ; i++) {
3. What for added these whitespaces before commas?
> +        struct errinj *inj = &errinjs[i];
> +        const char *env_value = getenv(inj->name);
> +        if (!env_value || *env_value == '\0')
> +            continue;
> +
> +        if (inj->type == ERRINJ_INT) {
> +            char *end;
> +            int64_t int_value = strtoll(env_value, &end, 10);
> +            if (*end == '\0')
> +                inj->iparam = int_value;
> +        } else if (inj->type == ERRINJ_BOOL) {
> +            if (strcmp(env_value, "false") == 0 || strcmp(env_value, "FALSE"))
> +                inj->bparam = false;
> +            else if (strcmp(env_value, "true") == 0 || strcmp(env_value, "TRUE"))
> +                inj->bparam = true;
> +        } else if (inj->type == ERRINJ_DOUBLE) {
> +            char *end;
> +            double double_value = strtod(env_value, &end);
> +            if (*end == '\0')
> +                inj->dparam = double_value;
> +        }
> +    }
> +}

4. Let's add a simple test that will pass three error injection 
variables with different types (bool, int and double) via environment

and check that error injections that same in Lua.

> diff --git a/src/lib/core/errinj.h b/src/lib/core/errinj.h
> index 814c57c2e..dc0657614 100644
> --- a/src/lib/core/errinj.h
> +++ b/src/lib/core/errinj.h
> @@ -168,6 +168,11 @@ typedef int (*errinj_cb)(struct errinj *e, void *cb_ctx);
>   int
>   errinj_foreach(errinj_cb cb, void *cb_ctx);
>   
> +/**
> + * Set injections by scanning ERRINJ_$(NAME) in environment variables
> + */
> +void errinj_scan_env();
> +
>   #ifdef NDEBUG
>   #  define ERROR_INJECT(ID, CODE)
>   #  define ERROR_INJECT_WHILE(ID, CODE)
> diff --git a/src/main.cc b/src/main.cc
> index 2fce81bb3..2d85c0b24 100644
> --- a/src/main.cc
> +++ b/src/main.cc
> @@ -710,6 +710,7 @@ main(int argc, char **argv)
>   	memtx_tx_manager_init();
>   	crypto_init();
>   	systemd_init();
> +	errinj_scan_env();
>   	tarantool_lua_init(tarantool_bin, main_argc, main_argv);
>   
>   	start_time = ev_monotonic_time();


More information about the Tarantool-patches mailing list