From dc1ba51935f4ce89cac360c66b9560217f0437d0 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 30 Jul 2021 14:20:54 +0800 Subject: [PATCH] fix(emqx_config): always check config files using richmap at emqx bootup --- apps/emqx/src/emqx_config.erl | 15 +++++---------- apps/emqx/src/emqx_config_handler.erl | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/apps/emqx/src/emqx_config.erl b/apps/emqx/src/emqx_config.erl index f9a788147..43a41cdc6 100644 --- a/apps/emqx/src/emqx_config.erl +++ b/apps/emqx/src/emqx_config.erl @@ -195,14 +195,14 @@ init_load(SchemaModule, Conf) when is_list(Conf) orelse is_binary(Conf) -> error(failed_to_load_hocon_conf) end; init_load(SchemaModule, RawRichConf) when is_map(RawRichConf) -> - RawConf = hocon_schema:richmap_to_map(RawRichConf), %% check with richmap for line numbers in error reports (future enhancement) Opts = #{return_plain => true, nullable => true }, %% this call throws exception in case of check failure - {_AppEnvs, CheckedConf} = check_config(SchemaModule, RawConf, Opts), - ok = save_to_config_map(CheckedConf, RawConf). + {_AppEnvs, CheckedConf} = hocon_schema:map_translate(SchemaModule, RawRichConf, Opts), + ok = save_to_config_map(emqx_map_lib:unsafe_atom_key_map(CheckedConf), + hocon_schema:richmap_to_map(RawRichConf)). -spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf} when AppEnvs :: app_envs(), CheckedConf :: config(). @@ -211,11 +211,6 @@ check_config(SchemaModule, RawConf) -> nullable => true, is_richmap => false }, - check_config(SchemaModule, RawConf, Opts). - --spec check_config(module(), raw_config(), map()) -> {AppEnvs, CheckedConf} - when AppEnvs :: app_envs(), CheckedConf :: config(). -check_config(SchemaModule, RawConf, Opts) -> {AppEnvs, CheckedConf} = hocon_schema:map_translate(SchemaModule, RawConf, Opts), Conf = maps:with(maps:keys(RawConf), CheckedConf), @@ -225,8 +220,8 @@ check_config(SchemaModule, RawConf, Opts) -> read_override_conf() -> load_hocon_file(emqx_override_conf_name(), map). --spec save_configs(app_envs(), raw_config(), config(), raw_config()) -> ok | {error, term()}. -save_configs(_AppEnvs, RawConf, Conf, OverrideConf) -> +-spec save_configs(app_envs(), config(), raw_config(), raw_config()) -> ok | {error, term()}. +save_configs(_AppEnvs, Conf, RawConf, OverrideConf) -> %% 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), diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index 7c56a6e2c..b57c561cf 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -95,7 +95,7 @@ handle_call({change_config, SchemaModule, ConfKeyPath, UpdateReq}, _From, Handlers, UpdateReq), {AppEnvs, CheckedConf} = emqx_config:check_config(SchemaModule, NewRawConf), do_post_config_update(ConfKeyPath, Handlers, OldConf, CheckedConf, UpdateReq), - emqx_config:save_configs(AppEnvs, NewRawConf, CheckedConf, OverrideConf) + emqx_config:save_configs(AppEnvs, CheckedConf, NewRawConf, OverrideConf) catch Error:Reason:ST -> ?LOG(error, "change_config failed: ~p", [{Error, Reason, ST}]), {error, Reason}