chore: update cluster_hocon_file/0 function

This commit is contained in:
Zhongwen Deng 2023-04-14 22:45:28 +08:00
parent 7c5dead03a
commit 8facd130f6
5 changed files with 37 additions and 37 deletions

View File

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

View File

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

View File

@ -62,8 +62,8 @@ t_update(_Config) ->
?assertEqual(BusyPort, not BusyPort1), ?assertEqual(BusyPort, not BusyPort1),
assert_busy_port(BusyPort1), assert_busy_port(BusyPort1),
%% Make sure the override config is updated, and remove the default value. %% Make sure the override config is updated, and remove the default value.
?assertEqual( ?assertMatch(
#{<<"vm">> => #{<<"busy_port">> => BusyPort1}}, #{<<"vm">> := #{<<"busy_port">> := BusyPort1}},
maps:get(<<"sysmon">>, emqx_config:read_override_conf(#{override_to => cluster})) maps:get(<<"sysmon">>, emqx_config:read_override_conf(#{override_to => cluster}))
), ),
@ -136,7 +136,7 @@ t_global_zone(_Config) ->
{ok, #{}} = update_global_zone(NewZones), {ok, #{}} = update_global_zone(NewZones),
?assertEqual(1, emqx_config:get_zone_conf(no_default, [mqtt, max_qos_allowed])), ?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. %% 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), BadZones = emqx_map_lib:deep_put([<<"mqtt">>, <<"max_qos_allowed">>], Zones, 3),
?assertMatch({error, {"HTTP/1.1", 400, _}}, update_global_zone(BadZones)), ?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), DefaultZones = emqx_map_lib:deep_put([<<"mqtt">>, <<"max_qos_allowed">>], Zones, 2),
{ok, #{}} = update_global_zone(DefaultZones), {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. ok.
get_global_zone() -> get_global_zone() ->

View File

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

View File

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