fix: source bridges missing after restore the backup files

This commit is contained in:
Shawn 2024-04-03 11:57:01 +08:00
parent 2e528d1dd8
commit 319ec50c0d
6 changed files with 83 additions and 22 deletions

View File

@ -16,9 +16,14 @@
-module(emqx_config_backup).
-callback import_config(RawConf :: map()) ->
{ok, #{
-type ok_result() :: #{
root_key => emqx_utils_maps:config_key(),
changed => [emqx_utils_maps:config_key_path()]
}}
| {error, #{root_key => emqx_utils_maps:config_key(), reason => term()}}.
}.
-type error_result() :: #{root_key => emqx_utils_maps:config_key(), reason => term()}.
-callback import_config(RawConf :: map()) ->
{ok, ok_result()}
| {error, error_result()}
| {results, {[ok_result()], [error_result()]}}.

View File

@ -2,7 +2,7 @@
{application, emqx, [
{id, "emqx"},
{description, "EMQX Core"},
{vsn, "5.2.0"},
{vsn, "5.2.1"},
{modules, []},
{registered, []},
{applications, [

View File

@ -1,7 +1,7 @@
%% -*- mode: erlang -*-
{application, emqx_bridge, [
{description, "EMQX bridges"},
{vsn, "0.1.34"},
{vsn, "0.1.35"},
{registered, [emqx_bridge_sup]},
{mod, {emqx_bridge_app, []}},
{applications, [

View File

@ -1030,7 +1030,26 @@ bridge_v2_type_to_connector_type(Type) ->
import_config(RawConf) ->
%% actions structure
emqx_bridge:import_config(RawConf, <<"actions">>, ?ROOT_KEY_ACTIONS, config_key_path()).
ActionRes = emqx_bridge:import_config(
RawConf, <<"actions">>, ?ROOT_KEY_ACTIONS, config_key_path()
),
SourceRes = emqx_bridge:import_config(
RawConf, <<"sources">>, ?ROOT_KEY_SOURCES, config_key_path_sources()
),
combine_import_results([ActionRes, SourceRes]).
combine_import_results(Results0) ->
Results = lists:foldr(
fun
({ok, OkRes}, {OkAcc, ErrAcc}) ->
{[OkRes | OkAcc], ErrAcc};
({error, ErrRes}, {OkAcc, ErrAcc}) ->
{OkAcc, [ErrRes | ErrAcc]}
end,
{[], []},
Results0
),
{results, Results}.
%%====================================================================
%% Config Update Handler API

View File

@ -773,23 +773,42 @@ validate_cluster_hocon(RawConf) ->
do_import_conf(RawConf, Opts) ->
GenConfErrs = filter_errors(maps:from_list(import_generic_conf(RawConf))),
maybe_print_conf_errors(GenConfErrs, Opts),
Errors =
lists:foldl(
fun(Module, ErrorsAcc) ->
case Module:import_config(RawConf) of
{ok, #{changed := Changed}} ->
maybe_print_changed(Changed, Opts),
ErrorsAcc;
{error, #{root_key := RootKey, reason := Reason}} ->
ErrorsAcc#{[RootKey] => Reason}
end
end,
GenConfErrs,
sort_importer_modules(find_behaviours(emqx_config_backup))
),
Modules = sort_importer_modules(find_behaviours(emqx_config_backup)),
Errors = lists:foldl(print_ok_results_collect_errors(RawConf, Opts), GenConfErrs, Modules),
maybe_print_conf_errors(Errors, Opts),
Errors.
print_ok_results_collect_errors(RawConf, Opts) ->
fun(Module, Errors) ->
case Module:import_config(RawConf) of
{results, {OkResults, ErrResults}} ->
print_ok_results(OkResults, Opts),
collect_errors(ErrResults, Errors);
{ok, OkResult} ->
print_ok_results([OkResult], Opts),
Errors;
{error, ErrResult} ->
collect_errors([ErrResult], Errors)
end
end.
print_ok_results(Results, Opts) ->
lists:foreach(
fun(#{changed := Changed}) ->
maybe_print_changed(Changed, Opts)
end,
Results
).
collect_errors(Results, Errors) ->
lists:foldr(
fun(#{root_key := RootKey, reason := Reason}, Acc) ->
Acc#{[RootKey] => Reason}
end,
Errors,
Results
).
sort_importer_modules(Modules) ->
lists:sort(
fun(M1, M2) -> order(M1, ?IMPORT_ORDER) =< order(M2, ?IMPORT_ORDER) end,

View File

@ -0,0 +1,18 @@
Cannot import `sources` from backup files.
Before the fix, the following configs in backup files cannot be imported:
```
sources {
mqtt {
source_c384b174 {
connector = source_connector_c8287217
enable = true
parameters {
qos = 0
topic = "t/#"
}
}
}
}
```