Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: save app's env when bootstrapping and reloading #9750

Merged
merged 7 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion apps/emqx/src/config/emqx_config_logger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-behaviour(emqx_config_handler).

%% API
-export([add_handler/0, remove_handler/0]).
-export([add_handler/0, remove_handler/0, refresh_config/0]).
-export([post_config_update/5]).

-define(LOG, [log]).
Expand All @@ -31,6 +31,21 @@ remove_handler() ->
ok = emqx_config_handler:remove_handler(?LOG),
ok.

%% refresh logger config when booting, the override config may have changed after node start.
%% Kernel's app env is confirmed before the node starts,
%% but we only copy cluster-override.conf from other node after this node starts,
%% so we need to refresh the logger config after this node starts.
%% It will not affect the logger config when cluster-override.conf is unchanged.
refresh_config() ->
case emqx:get_raw_config(?LOG, undefined) of
%% no logger config when CT is running.
undefined ->
ok;
Log ->
{ok, _} = emqx:update_config(?LOG, Log),
zmstone marked this conversation as resolved.
Show resolved Hide resolved
ok
end.

post_config_update(?LOG, _Req, _NewConf, _OldConf, AppEnvs) ->
Kernel = proplists:get_value(kernel, AppEnvs),
NewHandlers = proplists:get_value(logger, Kernel, []),
Expand Down
24 changes: 12 additions & 12 deletions apps/emqx/src/emqx_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ init_load(SchemaMod, RawConf, Opts) when is_map(RawConf) ->
RootNames = get_root_names(),
RawConfAll = raw_conf_with_default(SchemaMod, RootNames, RawConfWithOverrides, Opts),
%% check configs against the schema
{_AppEnvs, CheckedConf} = check_config(SchemaMod, RawConfAll, #{}),
{AppEnvs, CheckedConf} = check_config(SchemaMod, RawConfAll, #{}),
save_to_app_env(AppEnvs),
ok = save_to_config_map(CheckedConf, RawConfAll).

%% keep the raw and non-raw conf has the same keys to make update raw conf easier.
Expand Down Expand Up @@ -534,23 +535,21 @@ get_root_names() ->
maps:get(names, persistent_term:get(?PERSIS_SCHEMA_MODS, #{names => []})).

-spec save_configs(app_envs(), config(), raw_config(), raw_config(), update_opts()) -> ok.
save_configs(_AppEnvs, Conf, RawConf, OverrideConf, Opts) ->
save_configs(AppEnvs, Conf, RawConf, OverrideConf, Opts) ->
%% We first try to save to override.conf, because saving to files is more error prone
%% than saving into memory.
ok = save_to_override_conf(OverrideConf, Opts),
%% We may need also support hot config update for the apps that use application envs.
%% If that is the case uncomment the following line to update the configs to app env
%save_to_app_env(_AppEnvs),
save_to_app_env(AppEnvs),
save_to_config_map(Conf, RawConf).

%% we ignore kernel app env,
%% because the old app env will be used in emqx_config_logger:post_config_update/5
-define(IGNORE_APPS, [kernel]).
zhongwencool marked this conversation as resolved.
Show resolved Hide resolved

-spec save_to_app_env([tuple()]) -> ok.
save_to_app_env(AppEnvs) ->
lists:foreach(
fun({AppName, Envs}) ->
[application:set_env(AppName, Par, Val) || {Par, Val} <- Envs]
end,
AppEnvs
).
save_to_app_env(AppEnvs0) ->
AppEnvs = lists:filter(fun({App, _}) -> not lists:member(App, ?IGNORE_APPS) end, AppEnvs0),
application:set_env(AppEnvs).

-spec save_to_config_map(config(), raw_config()) -> ok.
save_to_config_map(Conf, RawConf) ->
Expand Down Expand Up @@ -582,6 +581,7 @@ save_to_override_conf(RawConf, Opts) ->
add_handlers() ->
ok = emqx_config_logger:add_handler(),
emqx_sys_mon:add_handler(),
emqx_config_logger:refresh_config(),
ok.

remove_handlers() ->
Expand Down
4 changes: 3 additions & 1 deletion apps/emqx/test/emqx_common_test_helpers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ catch_call(F) ->
C:E:S ->
{crashed, {C, E, S}}
end.
force_set_config_file_paths(emqx_conf, Paths) ->
force_set_config_file_paths(emqx_conf, [Path] = Paths) ->
Bin = iolist_to_binary(io_lib:format("node.config_files = [~p]", [Path])),
ok = file:write_file(Path, Bin, [append]),
application:set_env(emqx, config_files, Paths);
force_set_config_file_paths(emqx, Paths) ->
application:set_env(emqx, config_files, Paths);
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_conf/src/emqx_conf_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ fields("node") ->
)},
{"config_files",
sc(
list(string()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we config node.config_files = ["test"]

node {
  name = "emqx@127.0.0.1"
  cookie = "emqxsecretcookie"
  data_dir = "data"
  config_files = ["test"]
}

the config schema check will fail by:

exception throw: {emqx_conf_schema,[#{kind => validation_error,
                                      path => "node.config_files",
                                      reason =>
                                          #{expected_type => <<"[string()]">>},
                                      value => [<<"test">>]}]}
  in function  hocon_tconf:assert_no_error/2 (hocon_tconf.erl, line 1142)
  in call from hocon_tconf:map/4 (hocon_tconf.erl, line 295)
  in call from hocon_cli:get_values/3 (hocon_cli.erl, line 194)
  in call from hocon_cli:multi_get/2 (hocon_cli.erl, line 182)
  in call from escript:run/2 (escript.erl, line 750)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_em/1
  in call from init:do_boot/3

So we change the type to hocon:array(string()).

hoconsc:array(string()),
#{
mapping => "emqx.config_files",
default => undefined,
Expand Down