Просмотр исходного кода

fix: source bridges missing after restore the backup files

Shawn 1 год назад
Родитель
Сommit
319ec50c0d

+ 10 - 5
apps/emqx/src/bhvrs/emqx_config_backup.erl

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

+ 1 - 1
apps/emqx/src/emqx.app.src

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

+ 1 - 1
apps/emqx_bridge/src/emqx_bridge.app.src

@@ -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, [

+ 20 - 1
apps/emqx_bridge/src/emqx_bridge_v2.erl

@@ -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

+ 33 - 14
apps/emqx_management/src/emqx_mgmt_data_backup.erl

@@ -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,

+ 18 - 0
changes/ce/fix-12826.en.md

@@ -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/#"
+      }
+    }
+  }
+}
+```