From 3a511c6229b56ec34f5f987961ee811b4ba82473 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Sat, 15 Apr 2023 19:41:07 +0800 Subject: [PATCH 1/3] fix(gw): load emqx applications before hocon configs checking --- apps/emqx_gateway/src/emqx_gateway_app.erl | 2 +- apps/emqx_gateway/src/emqx_gateway_schema.erl | 2 +- apps/emqx_gateway/src/emqx_gateway_utils.erl | 6 +++--- bin/nodetool | 16 +++++++++++++++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_app.erl b/apps/emqx_gateway/src/emqx_gateway_app.erl index 01a1aaddd..f0406bcaa 100644 --- a/apps/emqx_gateway/src/emqx_gateway_app.erl +++ b/apps/emqx_gateway/src/emqx_gateway_app.erl @@ -45,7 +45,7 @@ load_default_gateway_applications() -> fun(Def) -> load_gateway_application(Def) end, - emqx_gateway_utils:find_gateway_definations() + emqx_gateway_utils:find_gateway_definitions() ). load_gateway_application( diff --git a/apps/emqx_gateway/src/emqx_gateway_schema.erl b/apps/emqx_gateway/src/emqx_gateway_schema.erl index f0e65627f..8c80fc1fa 100644 --- a/apps/emqx_gateway/src/emqx_gateway_schema.erl +++ b/apps/emqx_gateway/src/emqx_gateway_schema.erl @@ -75,7 +75,7 @@ fields(gateway) -> } )} end, - emqx_gateway_utils:find_gateway_definations() + emqx_gateway_utils:find_gateway_definitions() ); fields(clientinfo_override) -> [ diff --git a/apps/emqx_gateway/src/emqx_gateway_utils.erl b/apps/emqx_gateway/src/emqx_gateway_utils.erl index 07185ef42..7a0188387 100644 --- a/apps/emqx_gateway/src/emqx_gateway_utils.erl +++ b/apps/emqx_gateway/src/emqx_gateway_utils.erl @@ -47,7 +47,7 @@ listener_chain/3, make_deprecated_paths/1, make_compatible_schema/2, - find_gateway_definations/0 + find_gateway_definitions/0 ]). -export([stringfy/1]). @@ -564,8 +564,8 @@ make_compatible_schema2(Path, SchemaFun) -> Schema ). --spec find_gateway_definations() -> list(gateway_def()). -find_gateway_definations() -> +-spec find_gateway_definitions() -> list(gateway_def()). +find_gateway_definitions() -> lists:flatten( lists:map( fun(App) -> diff --git a/bin/nodetool b/bin/nodetool index 9a5d5e069..9f32f9b3b 100755 --- a/bin/nodetool +++ b/bin/nodetool @@ -362,6 +362,20 @@ add_libs_dir() -> add_lib_dir(RootDir, Name, Vsn) -> LibDir = filename:join([RootDir, lib, atom_to_list(Name) ++ "-" ++ Vsn, ebin]), case code:add_patha(LibDir) of - true -> ok; + true -> + %% load all applications into application controller, before performing + %% the configuration check of HOCON + %% + %% It helps to implement the feature of dynamically searching schema. + %% See `emqx_gateway_schema:fields(gateway)` + is_emqx_application(Name) andalso application:load(Name), + ok; {error, _} -> error(LibDir) end. + +is_emqx_application(Name) when is_atom(Name) -> + is_emqx_application(atom_to_list(Name)); +is_emqx_application("emqx_" ++ _Rest) -> + true; +is_emqx_application(_) -> + false. From 73c15d9668e552d4b1bf3caad0b04eca8b2b20b9 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Sat, 15 Apr 2023 21:18:37 +0800 Subject: [PATCH 2/3] chore: update changes --- bin/nodetool | 11 +++++++++-- changes/ce/fix-10410.en.md | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changes/ce/fix-10410.en.md diff --git a/bin/nodetool b/bin/nodetool index 9f32f9b3b..8170e68e2 100755 --- a/bin/nodetool +++ b/bin/nodetool @@ -272,7 +272,7 @@ chkconfig(File) -> end. check_license(Config) -> - ok = application:load(emqx_license), + ok = ensure_application_load(emqx_license), %% This checks formal license validity to ensure %% that the node can successfully start with the given license. @@ -368,7 +368,7 @@ add_lib_dir(RootDir, Name, Vsn) -> %% %% It helps to implement the feature of dynamically searching schema. %% See `emqx_gateway_schema:fields(gateway)` - is_emqx_application(Name) andalso application:load(Name), + is_emqx_application(Name) andalso ensure_application_load(Name), ok; {error, _} -> error(LibDir) end. @@ -379,3 +379,10 @@ is_emqx_application("emqx_" ++ _Rest) -> true; is_emqx_application(_) -> false. + +ensure_application_load(Name) -> + case application:load(Name) of + ok -> ok; + {error, {already_loaded, _}} -> ok; + {error, Reason} -> error({failed_to_load_application, Name, Reason}) + end. diff --git a/changes/ce/fix-10410.en.md b/changes/ce/fix-10410.en.md new file mode 100644 index 000000000..48b55ea31 --- /dev/null +++ b/changes/ce/fix-10410.en.md @@ -0,0 +1,2 @@ +Fix EMQX starting failed once any gateways configured in emqx.conf. +This issue was first introduced in v5.0.22 via [#10278](https://github.com/emqx/emqx/pull/10278). From 549283ae75941a5ec9d361fa32d0a10f9fe8dd99 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Mon, 17 Apr 2023 17:35:50 +0800 Subject: [PATCH 3/3] chore: update changes/ce/fix-10410.en.md Co-authored-by: Zaiming (Stone) Shi --- changes/ce/fix-10410.en.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changes/ce/fix-10410.en.md b/changes/ce/fix-10410.en.md index 48b55ea31..aa219c96e 100644 --- a/changes/ce/fix-10410.en.md +++ b/changes/ce/fix-10410.en.md @@ -1,2 +1,2 @@ -Fix EMQX starting failed once any gateways configured in emqx.conf. -This issue was first introduced in v5.0.22 via [#10278](https://github.com/emqx/emqx/pull/10278). +Fix config check failed when gateways are configured in emqx.conf. +This issue was first introduced in v5.0.22 via [#10278](https://github.com/emqx/emqx/pull/10278), the boot-time config check was missing.