fix(emqx_config): delete the emqx_config_handler:remove_config/2

This commit is contained in:
Shawn 2021-08-03 14:13:35 +08:00
parent b8253ee94f
commit c05e92a7cc
2 changed files with 19 additions and 22 deletions

View File

@ -156,14 +156,14 @@ update(KeyPath, UpdateReq) ->
-spec update(module(), emqx_map_lib:config_key_path(), update_request()) -> -spec update(module(), emqx_map_lib:config_key_path(), update_request()) ->
ok | {error, term()}. ok | {error, term()}.
update(SchemaModule, KeyPath, UpdateReq) -> update(SchemaModule, KeyPath, UpdateReq) ->
emqx_config_handler:update_config(SchemaModule, KeyPath, UpdateReq). emqx_config_handler:update_config(SchemaModule, KeyPath, {update, UpdateReq}).
-spec remove(emqx_map_lib:config_key_path()) -> ok | {error, term()}. -spec remove(emqx_map_lib:config_key_path()) -> ok | {error, term()}.
remove(KeyPath) -> remove(KeyPath) ->
remove(emqx_schema, KeyPath). remove(emqx_schema, KeyPath).
remove(SchemaModule, KeyPath) -> remove(SchemaModule, KeyPath) ->
emqx_config_handler:remove_config(SchemaModule, KeyPath). emqx_config_handler:update_config(SchemaModule, KeyPath, remove).
-spec get_raw(emqx_map_lib:config_key_path()) -> term(). -spec get_raw(emqx_map_lib:config_key_path()) -> term().
get_raw(KeyPath) -> do_get(?RAW_CONF, KeyPath). get_raw(KeyPath) -> do_get(?RAW_CONF, KeyPath).

View File

@ -25,7 +25,6 @@
-export([ start_link/0 -export([ start_link/0
, add_handler/2 , add_handler/2
, update_config/3 , update_config/3
, remove_config/2
, merge_to_old_config/2 , merge_to_old_config/2
]). ]).
@ -38,10 +37,10 @@
code_change/3]). code_change/3]).
-define(MOD, {mod}). -define(MOD, {mod}).
-define(REMOVE_CONF, '$remove_config').
-type handler_name() :: module(). -type handler_name() :: module().
-type handlers() :: #{emqx_config:config_key() => handlers(), ?MOD => handler_name()}. -type handlers() :: #{emqx_config:config_key() => handlers(), ?MOD => handler_name()}.
-type update_args() :: {update, emqx_config:update_request()} | remove.
-optional_callbacks([ pre_config_update/2 -optional_callbacks([ pre_config_update/2
, post_config_update/3 , post_config_update/3
@ -61,15 +60,10 @@
start_link() -> start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, {}, []). gen_server:start_link({local, ?MODULE}, ?MODULE, {}, []).
-spec update_config(module(), emqx_config:config_key_path(), emqx_config:update_request()) -> -spec update_config(module(), emqx_config:config_key_path(), update_args()) ->
ok | {error, term()}. ok | {error, term()}.
update_config(SchemaModule, ConfKeyPath, UpdateReq) when UpdateReq =/= ?REMOVE_CONF -> update_config(SchemaModule, ConfKeyPath, UpdateArgs) ->
gen_server:call(?MODULE, {change_config, SchemaModule, ConfKeyPath, UpdateReq}). gen_server:call(?MODULE, {change_config, SchemaModule, ConfKeyPath, UpdateArgs}).
-spec remove_config(module(), emqx_config:config_key_path()) ->
ok | {error, term()}.
remove_config(SchemaModule, ConfKeyPath) ->
gen_server:call(?MODULE, {change_config, SchemaModule, ConfKeyPath, ?REMOVE_CONF}).
-spec add_handler(emqx_config:config_key_path(), handler_name()) -> ok. -spec add_handler(emqx_config:config_key_path(), handler_name()) -> ok.
add_handler(ConfKeyPath, HandlerName) -> add_handler(ConfKeyPath, HandlerName) ->
@ -86,15 +80,15 @@ handle_call({add_child, ConfKeyPath, HandlerName}, _From,
{reply, ok, State#{handlers => {reply, ok, State#{handlers =>
emqx_map_lib:deep_put(ConfKeyPath, Handlers, #{?MOD => HandlerName})}}; emqx_map_lib:deep_put(ConfKeyPath, Handlers, #{?MOD => HandlerName})}};
handle_call({change_config, SchemaModule, ConfKeyPath, UpdateReq}, _From, handle_call({change_config, SchemaModule, ConfKeyPath, UpdateArgs}, _From,
#{handlers := Handlers} = State) -> #{handlers := Handlers} = State) ->
OldConf = emqx_config:get_root(ConfKeyPath), OldConf = emqx_config:get_root(ConfKeyPath),
OldRawConf = emqx_config:get_root_raw(ConfKeyPath), OldRawConf = emqx_config:get_root_raw(ConfKeyPath),
Result = try Result = try
{NewRawConf, OverrideConf} = process_upadate_request(ConfKeyPath, OldRawConf, {NewRawConf, OverrideConf} = process_upadate_request(ConfKeyPath, OldRawConf,
Handlers, UpdateReq), Handlers, UpdateArgs),
{AppEnvs, CheckedConf} = emqx_config:check_config(SchemaModule, NewRawConf), {AppEnvs, CheckedConf} = emqx_config:check_config(SchemaModule, NewRawConf),
_ = do_post_config_update(ConfKeyPath, Handlers, OldConf, CheckedConf, UpdateReq), _ = do_post_config_update(ConfKeyPath, Handlers, OldConf, CheckedConf, UpdateArgs),
emqx_config:save_configs(AppEnvs, CheckedConf, NewRawConf, OverrideConf) emqx_config:save_configs(AppEnvs, CheckedConf, NewRawConf, OverrideConf)
catch Error:Reason:ST -> catch Error:Reason:ST ->
?LOG(error, "change_config failed: ~p", [{Error, Reason, ST}]), ?LOG(error, "change_config failed: ~p", [{Error, Reason, ST}]),
@ -118,12 +112,12 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->
{ok, State}. {ok, State}.
process_upadate_request(ConfKeyPath, OldRawConf, _Handlers, ?REMOVE_CONF) -> process_upadate_request(ConfKeyPath, OldRawConf, _Handlers, remove) ->
BinKeyPath = bin_path(ConfKeyPath), BinKeyPath = bin_path(ConfKeyPath),
NewRawConf = emqx_map_lib:deep_remove(BinKeyPath, OldRawConf), NewRawConf = emqx_map_lib:deep_remove(BinKeyPath, OldRawConf),
OverrideConf = emqx_map_lib:deep_remove(BinKeyPath, emqx_config:read_override_conf()), OverrideConf = emqx_map_lib:deep_remove(BinKeyPath, emqx_config:read_override_conf()),
{NewRawConf, OverrideConf}; {NewRawConf, OverrideConf};
process_upadate_request(ConfKeyPath, OldRawConf, Handlers, UpdateReq) -> process_upadate_request(ConfKeyPath, OldRawConf, Handlers, {update, UpdateReq}) ->
NewRawConf = do_update_config(ConfKeyPath, Handlers, OldRawConf, UpdateReq), NewRawConf = do_update_config(ConfKeyPath, Handlers, OldRawConf, UpdateReq),
OverrideConf = update_override_config(NewRawConf), OverrideConf = update_override_config(NewRawConf),
{NewRawConf, OverrideConf}. {NewRawConf, OverrideConf}.
@ -136,14 +130,14 @@ do_update_config([ConfKey | ConfKeyPath], Handlers, OldRawConf, UpdateReq) ->
NewUpdateReq = do_update_config(ConfKeyPath, SubHandlers, SubOldRawConf, UpdateReq), NewUpdateReq = do_update_config(ConfKeyPath, SubHandlers, SubOldRawConf, UpdateReq),
call_pre_config_update(Handlers, OldRawConf, #{bin(ConfKey) => NewUpdateReq}). call_pre_config_update(Handlers, OldRawConf, #{bin(ConfKey) => NewUpdateReq}).
do_post_config_update([], Handlers, OldConf, NewConf, UpdateReq) -> do_post_config_update([], Handlers, OldConf, NewConf, UpdateArgs) ->
call_post_config_update(Handlers, OldConf, NewConf, UpdateReq); call_post_config_update(Handlers, OldConf, NewConf, up_req(UpdateArgs));
do_post_config_update([ConfKey | ConfKeyPath], Handlers, OldConf, NewConf, UpdateReq) -> do_post_config_update([ConfKey | ConfKeyPath], Handlers, OldConf, NewConf, UpdateArgs) ->
SubOldConf = get_sub_config(ConfKey, OldConf), SubOldConf = get_sub_config(ConfKey, OldConf),
SubNewConf = get_sub_config(ConfKey, NewConf), SubNewConf = get_sub_config(ConfKey, NewConf),
SubHandlers = maps:get(ConfKey, Handlers, #{}), SubHandlers = maps:get(ConfKey, Handlers, #{}),
_ = do_post_config_update(ConfKeyPath, SubHandlers, SubOldConf, SubNewConf, UpdateReq), _ = do_post_config_update(ConfKeyPath, SubHandlers, SubOldConf, SubNewConf, UpdateArgs),
call_post_config_update(Handlers, OldConf, NewConf, UpdateReq). call_post_config_update(Handlers, OldConf, NewConf, up_req(UpdateArgs)).
get_sub_config(ConfKey, Conf) when is_map(Conf) -> get_sub_config(ConfKey, Conf) when is_map(Conf) ->
maps:get(ConfKey, Conf, undefined); maps:get(ConfKey, Conf, undefined);
@ -178,6 +172,9 @@ update_override_config(RawConf) ->
OldConf = emqx_config:read_override_conf(), OldConf = emqx_config:read_override_conf(),
maps:merge(OldConf, RawConf). maps:merge(OldConf, RawConf).
up_req(remove) -> '$remove';
up_req({update, Req}) -> Req.
bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath]. bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath].
bin(A) when is_atom(A) -> atom_to_binary(A, utf8); bin(A) when is_atom(A) -> atom_to_binary(A, utf8);