Explorar o código

chore: update cluster_hocon_file/0 function

Zhongwen Deng %!s(int64=2) %!d(string=hai) anos
pai
achega
8facd130f6

+ 7 - 15
apps/emqx/src/emqx_config.erl

@@ -418,7 +418,7 @@ do_parse_hocon(false, Conf, IncDirs) ->
         true ->
             hocon:binary(Conf, Opts);
         false ->
-            ClusterFile = cluster_hocon_file(#{override_to => cluster}),
+            ClusterFile = cluster_hocon_file(),
             hocon:files([ClusterFile | Conf], Opts)
     end.
 
@@ -493,7 +493,7 @@ fill_defaults(SchemaMod, RawConf, Opts0) ->
 delete_override_conf_files() ->
     F1 = deprecated_conf_file(#{override_to => local}),
     F2 = deprecated_conf_file(#{override_to => cluster}),
-    F3 = cluster_hocon_file(#{override_to => cluster}),
+    F3 = cluster_hocon_file(),
     ok = ensure_file_deleted(F1),
     ok = ensure_file_deleted(F2),
     ok = ensure_file_deleted(F3).
@@ -510,7 +510,7 @@ read_override_conf(#{} = Opts) ->
     File =
         case has_deprecated_file() of
             true -> deprecated_conf_file(Opts);
-            false -> cluster_hocon_file(Opts)
+            false -> cluster_hocon_file()
         end,
     load_hocon_file(File, map).
 
@@ -531,16 +531,8 @@ deprecated_conf_file(Which) when is_atom(Which) ->
     application:get_env(emqx, Which, undefined).
 
 %% The newer version cluster-wide config persistence file.
-cluster_hocon_file(Opts) when is_map(Opts) ->
-    Key =
-        case maps:get(override_to, Opts, cluster) of
-            %% no local config file support
-            local -> undefined;
-            cluster -> cluster_hocon_file
-        end,
-    application:get_env(emqx, Key, undefined);
-cluster_hocon_file(Which) when is_atom(Which) ->
-    application:get_env(emqx, Which, undefined).
+cluster_hocon_file() ->
+    application:get_env(emqx, cluster_hocon_file, undefined).
 
 -spec save_schema_mod_and_names(module()) -> ok.
 save_schema_mod_and_names(SchemaMod) ->
@@ -619,8 +611,8 @@ save_to_override_conf(true, RawConf, Opts) ->
                     {error, Reason}
             end
     end;
-save_to_override_conf(false, RawConf, Opts) ->
-    case cluster_hocon_file(Opts) of
+save_to_override_conf(false, RawConf, _Opts) ->
+    case cluster_hocon_file() of
         undefined ->
             ok;
         FileName ->

+ 9 - 17
apps/emqx_conf/test/emqx_conf_app_SUITE.erl

@@ -59,14 +59,15 @@ t_copy_new_data_dir(_Config) ->
     %% 1. Start all nodes
     [First | Rest] = Nodes = start_cluster(Cluster),
     try
+        File = "/configs/cluster.hocon",
         assert_config_load_done(Nodes),
-        rpc:call(First, ?MODULE, create_data_dir, []),
+        rpc:call(First, ?MODULE, create_data_dir, [File]),
         {[ok, ok, ok], []} = rpc:multicall(Nodes, application, stop, [emqx_conf]),
         {[ok, ok, ok], []} = rpc:multicall(Nodes, ?MODULE, set_data_dir_env, []),
         ok = rpc:call(First, application, start, [emqx_conf]),
         {[ok, ok], []} = rpc:multicall(Rest, application, start, [emqx_conf]),
 
-        assert_data_copy_done(Nodes, "/configs/cluster.hocon"),
+        assert_data_copy_done(Nodes, File),
         stop_cluster(Nodes),
         ok
     after
@@ -82,14 +83,15 @@ t_copy_deprecated_data_dir(_Config) ->
     %% 1. Start all nodes
     [First | Rest] = Nodes = start_cluster(Cluster),
     try
+        File = "/configs/cluster-override.conf",
         assert_config_load_done(Nodes),
-        rpc:call(First, ?MODULE, create_deprecated_data_dir, []),
+        rpc:call(First, ?MODULE, create_data_dir, [File]),
         {[ok, ok, ok], []} = rpc:multicall(Nodes, application, stop, [emqx_conf]),
         {[ok, ok, ok], []} = rpc:multicall(Nodes, ?MODULE, set_data_dir_env, []),
         ok = rpc:call(First, application, start, [emqx_conf]),
         {[ok, ok], []} = rpc:multicall(Rest, application, start, [emqx_conf]),
 
-        assert_data_copy_done(Nodes, "/configs/cluster-override.conf"),
+        assert_data_copy_done(Nodes, File),
         stop_cluster(Nodes),
         ok
     after
@@ -100,7 +102,7 @@ t_copy_deprecated_data_dir(_Config) ->
 %% Helper functions
 %%------------------------------------------------------------------------------
 
-create_data_dir() ->
+create_data_dir(File) ->
     Node = atom_to_list(node()),
     ok = filelib:ensure_dir(Node ++ "/certs/"),
     ok = filelib:ensure_dir(Node ++ "/authz/"),
@@ -108,17 +110,7 @@ create_data_dir() ->
     ok = file:write_file(Node ++ "/certs/fake-cert", list_to_binary(Node)),
     ok = file:write_file(Node ++ "/authz/fake-authz", list_to_binary(Node)),
     Telemetry = <<"telemetry.enable = false">>,
-    ok = file:write_file(Node ++ "/configs/cluster.hocon", Telemetry).
-
-create_deprecated_data_dir() ->
-    Node = atom_to_list(node()),
-    ok = filelib:ensure_dir(Node ++ "/certs/"),
-    ok = filelib:ensure_dir(Node ++ "/authz/"),
-    ok = filelib:ensure_dir(Node ++ "/configs/"),
-    ok = file:write_file(Node ++ "/certs/fake-cert", list_to_binary(Node)),
-    ok = file:write_file(Node ++ "/authz/fake-authz", list_to_binary(Node)),
-    Telemetry = <<"telemetry.enable = false">>,
-    ok = file:write_file(Node ++ "/configs/cluster-override.conf", Telemetry).
+    ok = file:write_file(Node ++ File, Telemetry).
 
 set_data_dir_env() ->
     Node = atom_to_list(node()),
@@ -134,7 +126,7 @@ set_data_dir_env() ->
     application:set_env(emqx, config_files, [NewConfigFile]),
     application:set_env(emqx, data_dir, Node),
     %% We set env both cluster.hocon and cluster-override.conf, but only one will be used
-    application:set_env(emqx, cluster_conf_file, Node ++ "/configs/cluster.hocon"),
+    application:set_env(emqx, cluster_hocon_file, Node ++ "/configs/cluster.hocon"),
     application:set_env(emqx, cluster_override_conf_file, Node ++ "/configs/cluster-override.conf"),
     ok.
 

+ 13 - 4
apps/emqx_management/test/emqx_mgmt_api_configs_SUITE.erl

@@ -62,8 +62,8 @@ t_update(_Config) ->
     ?assertEqual(BusyPort, not BusyPort1),
     assert_busy_port(BusyPort1),
     %% Make sure the override config is updated, and remove the default value.
-    ?assertEqual(
-        #{<<"vm">> => #{<<"busy_port">> => BusyPort1}},
+    ?assertMatch(
+        #{<<"vm">> := #{<<"busy_port">> := BusyPort1}},
         maps:get(<<"sysmon">>, emqx_config:read_override_conf(#{override_to => cluster}))
     ),
 
@@ -136,7 +136,7 @@ t_global_zone(_Config) ->
     {ok, #{}} = update_global_zone(NewZones),
     ?assertEqual(1, emqx_config:get_zone_conf(no_default, [mqtt, max_qos_allowed])),
     %% Make sure the override config is updated, and remove the default value.
-    ?assertEqual(#{<<"max_qos_allowed">> => 1}, read_conf(<<"mqtt">>)),
+    ?assertMatch(#{<<"max_qos_allowed">> := 1}, read_conf(<<"mqtt">>)),
 
     BadZones = emqx_map_lib:deep_put([<<"mqtt">>, <<"max_qos_allowed">>], Zones, 3),
     ?assertMatch({error, {"HTTP/1.1", 400, _}}, update_global_zone(BadZones)),
@@ -155,7 +155,16 @@ t_global_zone(_Config) ->
 
     DefaultZones = emqx_map_lib:deep_put([<<"mqtt">>, <<"max_qos_allowed">>], Zones, 2),
     {ok, #{}} = update_global_zone(DefaultZones),
-    ?assertEqual(undefined, read_conf(<<"mqtt">>)),
+    #{<<"mqtt">> := Mqtt} = emqx_config:fill_defaults(emqx_schema, #{<<"mqtt">> => #{}}, #{}),
+    Default = maps:map(
+        fun
+            (_, V) when is_boolean(V) -> V;
+            (_, V) when is_atom(V) -> atom_to_binary(V);
+            (_, V) -> V
+        end,
+        Mqtt
+    ),
+    ?assertEqual(Default, read_conf(<<"mqtt">>)),
     ok.
 
 get_global_zone() ->

+ 1 - 1
apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl

@@ -686,7 +686,7 @@ t_jq(_) ->
                 %% Got timeout as expected
                 got_timeout
         end,
-    ConfigRootKey = emqx_rule_engine_schema:namespace(),
+    _ConfigRootKey = emqx_rule_engine_schema:namespace(),
     ?assertThrow(
         {jq_exception, {timeout, _}},
         apply_func(jq, [TOProgram, <<"-2">>])

+ 7 - 0
changes/ce/feat-10156.en.md

@@ -0,0 +1,7 @@
+Change the priority of the configuration:
+1. If it is a new installation of EMQX, the priority of s configuration is `ENV > emqx.conf > HTTP API`.
+2. If it is an upgrade of EMQX, the priority of s configuration is the same as before `HTTP API > ENV > emqx.conf`.
+
+Deprecated data/configs/local-override.conf.
+
+Stabilizing the HTTP API for hot updates.