Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: multicall node_info to improve /node speed #9221

Merged
merged 6 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/emqx/priv/bpapi.versions
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{emqx_license,2}.
{emqx_management,1}.
{emqx_management,2}.
{emqx_management,3}.
{emqx_mgmt_api_plugins,1}.
{emqx_mgmt_cluster,1}.
{emqx_mgmt_trace,1}.
Expand Down
5 changes: 4 additions & 1 deletion apps/emqx/src/emqx_rpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ filter_result(Delivery) ->
max_client_num() ->
emqx:get_config([rpc, tcp_client_num], ?DefaultClientNum).

-spec unwrap_erpc(emqx_rpc:erpc(A)) -> A | {error, _Err}.
-spec unwrap_erpc(emqx_rpc:erpc(A) | [emqx_rpc:erpc(A)]) -> A | {error, _Err} | list().

unwrap_erpc(Res) when is_list(Res) ->
[unwrap_erpc(R) || R <- Res];
unwrap_erpc({ok, A}) ->
A;
unwrap_erpc({throw, A}) ->
Expand Down
63 changes: 33 additions & 30 deletions apps/emqx_management/src/emqx_mgmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ list_nodes() ->
Running = mria_mnesia:cluster_nodes(running),
Stopped = mria_mnesia:cluster_nodes(stopped),
DownNodes = lists:map(fun stopped_node_info/1, Stopped),
[{Node, node_info(Node)} || Node <- Running] ++ DownNodes.
[{Node, Info} || #{node := Node} = Info <- node_info(Running)] ++ DownNodes.

lookup_node(Node) -> node_info(Node).
lookup_node(Node) ->
[Info] = node_info([Node]),
Info.

node_info() ->
{UsedRatio, Total} = get_sys_memory(),
Expand Down Expand Up @@ -152,8 +154,8 @@ get_sys_memory() ->
{0, 0}
end.

node_info(Node) ->
wrap_rpc(emqx_management_proto_v2:node_info(Node)).
node_info(Nodes) ->
emqx_rpc:unwrap_erpc(emqx_management_proto_v3:node_info(Nodes)).

stopped_node_info(Node) ->
#{name => Node, node_status => 'stopped'}.
Expand All @@ -163,17 +165,19 @@ stopped_node_info(Node) ->
%%--------------------------------------------------------------------

list_brokers() ->
[{Node, broker_info(Node)} || Node <- mria_mnesia:running_nodes()].
Running = mria_mnesia:running_nodes(),
[{Node, Broker} || #{node := Node} = Broker <- broker_info(Running)].

lookup_broker(Node) ->
broker_info(Node).
[Broker] = broker_info([Node]),
Broker.

broker_info() ->
Info = maps:from_list([{K, iolist_to_binary(V)} || {K, V} <- emqx_sys:info()]),
Info#{node => node(), otp_release => otp_rel(), node_status => 'Running'}.

broker_info(Node) ->
wrap_rpc(emqx_management_proto_v2:broker_info(Node)).
broker_info(Nodes) ->
emqx_rpc:unwrap_erpc(emqx_management_proto_v3:broker_info(Nodes)).

%%--------------------------------------------------------------------
%% Metrics and Stats
Expand All @@ -183,7 +187,7 @@ get_metrics() ->
nodes_info_count([get_metrics(Node) || Node <- mria_mnesia:running_nodes()]).

get_metrics(Node) ->
wrap_rpc(emqx_proto_v1:get_metrics(Node)).
unwrap_rpc(emqx_proto_v1:get_metrics(Node)).

get_stats() ->
GlobalStatsKeys =
Expand Down Expand Up @@ -211,7 +215,7 @@ delete_keys(List, [Key | Keys]) ->
delete_keys(proplists:delete(Key, List), Keys).

get_stats(Node) ->
wrap_rpc(emqx_proto_v1:get_stats(Node)).
unwrap_rpc(emqx_proto_v1:get_stats(Node)).

nodes_info_count(PropList) ->
NodeCount =
Expand Down Expand Up @@ -241,7 +245,7 @@ lookup_client({username, Username}, FormatFun) ->
]).

lookup_client(Node, Key, {M, F}) ->
case wrap_rpc(emqx_cm_proto_v1:lookup_client(Node, Key)) of
case unwrap_rpc(emqx_cm_proto_v1:lookup_client(Node, Key)) of
{error, Err} ->
{error, Err};
L ->
Expand All @@ -264,7 +268,7 @@ kickout_client({ClientID, FormatFun}) ->
end.

kickout_client(Node, ClientId) ->
wrap_rpc(emqx_cm_proto_v1:kickout_client(Node, ClientId)).
unwrap_rpc(emqx_cm_proto_v1:kickout_client(Node, ClientId)).

list_authz_cache(ClientId) ->
call_client(ClientId, list_authz_cache).
Expand All @@ -284,14 +288,14 @@ list_client_subscriptions(ClientId) ->
end.

client_subscriptions(Node, ClientId) ->
{Node, wrap_rpc(emqx_broker_proto_v1:list_client_subscriptions(Node, ClientId))}.
{Node, unwrap_rpc(emqx_broker_proto_v1:list_client_subscriptions(Node, ClientId))}.

clean_authz_cache(ClientId) ->
Results = [clean_authz_cache(Node, ClientId) || Node <- mria_mnesia:running_nodes()],
check_results(Results).

clean_authz_cache(Node, ClientId) ->
wrap_rpc(emqx_proto_v1:clean_authz_cache(Node, ClientId)).
unwrap_rpc(emqx_proto_v1:clean_authz_cache(Node, ClientId)).

clean_authz_cache_all() ->
Results = [{Node, clean_authz_cache_all(Node)} || Node <- mria_mnesia:running_nodes()],
Expand All @@ -308,10 +312,10 @@ wrap_results(Results) ->
end.

clean_authz_cache_all(Node) ->
wrap_rpc(emqx_proto_v1:clean_authz_cache(Node)).
unwrap_rpc(emqx_proto_v1:clean_authz_cache(Node)).

clean_pem_cache_all(Node) ->
wrap_rpc(emqx_proto_v1:clean_pem_cache(Node)).
unwrap_rpc(emqx_proto_v1:clean_pem_cache(Node)).

set_ratelimit_policy(ClientId, Policy) ->
call_client(ClientId, {ratelimit, Policy}).
Expand Down Expand Up @@ -357,7 +361,7 @@ do_call_client(ClientId, Req) ->

%% @private
call_client(Node, ClientId, Req) ->
wrap_rpc(emqx_management_proto_v2:call_client(Node, ClientId, Req)).
unwrap_rpc(emqx_management_proto_v3:call_client(Node, ClientId, Req)).

%%--------------------------------------------------------------------
%% Subscriptions
Expand All @@ -376,7 +380,7 @@ do_list_subscriptions() ->
end.

list_subscriptions(Node) ->
wrap_rpc(emqx_management_proto_v2:list_subscriptions(Node)).
unwrap_rpc(emqx_management_proto_v3:list_subscriptions(Node)).

list_subscriptions_via_topic(Topic, FormatFun) ->
lists:append([
Expand All @@ -385,7 +389,7 @@ list_subscriptions_via_topic(Topic, FormatFun) ->
]).

list_subscriptions_via_topic(Node, Topic, _FormatFun = {M, F}) ->
case wrap_rpc(emqx_broker_proto_v1:list_subscriptions_via_topic(Node, Topic)) of
case unwrap_rpc(emqx_broker_proto_v1:list_subscriptions_via_topic(Node, Topic)) of
{error, Reason} -> {error, Reason};
Result -> M:F(Result)
end.
Expand All @@ -394,7 +398,7 @@ lookup_subscriptions(ClientId) ->
lists:append([lookup_subscriptions(Node, ClientId) || Node <- mria_mnesia:running_nodes()]).

lookup_subscriptions(Node, ClientId) ->
wrap_rpc(emqx_broker_proto_v1:list_client_subscriptions(Node, ClientId)).
unwrap_rpc(emqx_broker_proto_v1:list_client_subscriptions(Node, ClientId)).

%%--------------------------------------------------------------------
%% PubSub
Expand All @@ -404,7 +408,7 @@ subscribe(ClientId, TopicTables) ->
subscribe(mria_mnesia:running_nodes(), ClientId, TopicTables).

subscribe([Node | Nodes], ClientId, TopicTables) ->
case wrap_rpc(emqx_management_proto_v2:subscribe(Node, ClientId, TopicTables)) of
case unwrap_rpc(emqx_management_proto_v3:subscribe(Node, ClientId, TopicTables)) of
{error, _} -> subscribe(Nodes, ClientId, TopicTables);
{subscribe, Res} -> {subscribe, Res, Node}
end;
Expand All @@ -431,7 +435,7 @@ unsubscribe(ClientId, Topic) ->
-spec unsubscribe([node()], emqx_types:clientid(), emqx_types:topic()) ->
{unsubscribe, _} | {error, channel_not_found}.
unsubscribe([Node | Nodes], ClientId, Topic) ->
case wrap_rpc(emqx_management_proto_v2:unsubscribe(Node, ClientId, Topic)) of
case unwrap_rpc(emqx_management_proto_v3:unsubscribe(Node, ClientId, Topic)) of
{error, _} -> unsubscribe(Nodes, ClientId, Topic);
Re -> Re
end;
Expand All @@ -454,7 +458,7 @@ unsubscribe_batch(ClientId, Topics) ->
-spec unsubscribe_batch([node()], emqx_types:clientid(), [emqx_types:topic()]) ->
{unsubscribe_batch, _} | {error, channel_not_found}.
unsubscribe_batch([Node | Nodes], ClientId, Topics) ->
case wrap_rpc(emqx_management_proto_v2:unsubscribe_batch(Node, ClientId, Topics)) of
case unwrap_rpc(emqx_management_proto_v3:unsubscribe_batch(Node, ClientId, Topics)) of
{error, _} -> unsubscribe_batch(Nodes, ClientId, Topics);
Re -> Re
end;
Expand All @@ -477,16 +481,16 @@ get_alarms(Type) ->
[{Node, get_alarms(Node, Type)} || Node <- mria_mnesia:running_nodes()].

get_alarms(Node, Type) ->
add_duration_field(wrap_rpc(emqx_proto_v1:get_alarms(Node, Type))).
add_duration_field(unwrap_rpc(emqx_proto_v1:get_alarms(Node, Type))).

deactivate(Node, Name) ->
wrap_rpc(emqx_proto_v1:deactivate_alarm(Node, Name)).
unwrap_rpc(emqx_proto_v1:deactivate_alarm(Node, Name)).

delete_all_deactivated_alarms() ->
[delete_all_deactivated_alarms(Node) || Node <- mria_mnesia:running_nodes()].

delete_all_deactivated_alarms(Node) ->
wrap_rpc(emqx_proto_v1:delete_all_deactivated_alarms(Node)).
unwrap_rpc(emqx_proto_v1:delete_all_deactivated_alarms(Node)).

add_duration_field(Alarms) ->
Now = erlang:system_time(microsecond),
Expand Down Expand Up @@ -523,10 +527,9 @@ delete_banned(Who) ->
%%--------------------------------------------------------------------
%% Internal Functions.
%%--------------------------------------------------------------------

wrap_rpc({badrpc, Reason}) ->
unwrap_rpc({badrpc, Reason}) ->
{error, Reason};
wrap_rpc(Res) ->
unwrap_rpc(Res) ->
Res.

otp_rel() ->
Expand All @@ -546,7 +549,7 @@ check_row_limit([Tab | Tables], Limit) ->
check_results(Results) ->
case lists:any(fun(Item) -> Item =:= ok end, Results) of
true -> ok;
false -> wrap_rpc(lists:last(Results))
false -> unwrap_rpc(lists:last(Results))
end.

max_row_limit() ->
Expand Down
80 changes: 80 additions & 0 deletions apps/emqx_management/src/proto/emqx_management_proto_v3.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_management_proto_v3).

-behaviour(emqx_bpapi).

-export([
introduced_in/0,

node_info/1,
broker_info/1,
list_subscriptions/1,

list_listeners/1,
subscribe/3,
unsubscribe/3,
unsubscribe_batch/3,

call_client/3,

get_full_config/1
]).

-include_lib("emqx/include/bpapi.hrl").

introduced_in() ->
"5.0.9".

-spec unsubscribe_batch(node(), emqx_types:clientid(), [emqx_types:topic()]) ->
{unsubscribe, _} | {error, _} | {badrpc, _}.
unsubscribe_batch(Node, ClientId, Topics) ->
rpc:call(Node, emqx_mgmt, do_unsubscribe_batch, [ClientId, Topics]).

-spec node_info([node()]) -> emqx_rpc:erpc_multicall(map()).
node_info(Nodes) ->
erpc:multicall(Nodes, emqx_mgmt, node_info, [], 30000).

-spec broker_info([node()]) -> emqx_rpc:erpc_multicall(map()).
broker_info(Nodes) ->
erpc:multicall(Nodes, emqx_mgmt, broker_info, [], 30000).

-spec list_subscriptions(node()) -> [map()] | {badrpc, _}.
list_subscriptions(Node) ->
rpc:call(Node, emqx_mgmt, do_list_subscriptions, []).

-spec list_listeners(node()) -> map() | {badrpc, _}.
list_listeners(Node) ->
rpc:call(Node, emqx_mgmt_api_listeners, do_list_listeners, []).

-spec subscribe(node(), emqx_types:clientid(), emqx_types:topic_filters()) ->
{subscribe, _} | {error, atom()} | {badrpc, _}.
subscribe(Node, ClientId, TopicTables) ->
rpc:call(Node, emqx_mgmt, do_subscribe, [ClientId, TopicTables]).

-spec unsubscribe(node(), emqx_types:clientid(), emqx_types:topic()) ->
{unsubscribe, _} | {error, _} | {badrpc, _}.
unsubscribe(Node, ClientId, Topic) ->
rpc:call(Node, emqx_mgmt, do_unsubscribe, [ClientId, Topic]).

-spec call_client(node(), emqx_types:clientid(), term()) -> term().
call_client(Node, ClientId, Req) ->
rpc:call(Node, emqx_mgmt, do_call_client, [ClientId, Req]).

-spec get_full_config(node()) -> map() | list() | {badrpc, _}.
get_full_config(Node) ->
rpc:call(Node, emqx_mgmt_api_configs, get_full_config, []).
48 changes: 48 additions & 0 deletions apps/emqx_management/test/emqx_mgmt_api_nodes_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,51 @@ t_node_metrics_api(_) ->
{error, {_, 400, _}},
emqx_mgmt_api_test_util:request_api(get, BadNodePath)
).

t_multiple_nodes_api(_) ->
net_kernel:start(['node_api@127.0.0.1', longnames]),
ct:timetrap({seconds, 120}),
snabbkaffe:fix_ct_logging(),
Seq1 = list_to_atom(atom_to_list(?MODULE) ++ "1"),
Seq2 = list_to_atom(atom_to_list(?MODULE) ++ "2"),
Cluster = [{Name, Opts}, {Name1, Opts1}] = cluster([{core, Seq1}, {core, Seq2}]),
ct:pal("Starting ~p", [Cluster]),
Node1 = emqx_common_test_helpers:start_slave(Name, Opts),
Node2 = emqx_common_test_helpers:start_slave(Name1, Opts1),
try
{200, NodesList} = rpc:call(Node1, emqx_mgmt_api_nodes, nodes, [get, #{}]),
All = [Node1, Node2],
lists:map(
fun(N) ->
N1 = maps:get(node, N),
?assertEqual(true, lists:member(N1, All))
end,
NodesList
),
?assertEqual(2, length(NodesList)),

{200, Node11} = rpc:call(Node1, emqx_mgmt_api_nodes, node, [
get, #{bindings => #{node => Node1}}
]),
?assertMatch(#{node := Node1}, Node11)
after
emqx_common_test_helpers:stop_slave(Node1),
emqx_common_test_helpers:stop_slave(Node2)
end,
ok.

cluster(Specs) ->
Env = [{emqx, boot_modules, []}],
emqx_common_test_helpers:emqx_cluster(Specs, [
{env, Env},
{apps, [emqx_conf]},
{load_schema, false},
{join_to, true},
{env_handler, fun
(emqx) ->
application:set_env(emqx, boot_modules, []),
ok;
(_) ->
ok
end}
]).
3 changes: 3 additions & 0 deletions changes/v5.0.10-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Enhancements

- Improve `/nodes` API responsiveness [#9221](https://github.com/emqx/emqx/pull/9221).

- Allow clear retained/delayed data when client is banned [#9139](https://github.com/emqx/emqx/pull/9139).

- Update `gen_rpc` library to version 3.0 [#9187](https://github.com/emqx/emqx/pull/9187).
Expand All @@ -13,6 +15,7 @@
- Now it is possible to opt out VM internal metrics in prometheus stats [#9222](https://github.com/emqx/emqx/pull/9222).
When system load is high, reporting too much metrics data may cause the prometheus stats API timeout.


## Bug fixes

- Fix error log message when `mechanism` is missing in authentication config [#8924](https://github.com/emqx/emqx/pull/8924).
Expand Down