fix(emqx_slow_subs): limit the max size of top-k table

This commit is contained in:
lafirest 2021-12-30 10:00:43 +08:00
parent d769401869
commit d60c586bfb
6 changed files with 12 additions and 4 deletions

View File

@ -30,6 +30,7 @@
-export([ node_query/5
, cluster_query/4
, select_table_with_count/5
, b2i/1
]).
-export([do_query/6]).

View File

@ -18,6 +18,8 @@
-define(INDEX(Latency, ClientId), {Latency, ClientId}).
-define(MAX_TAB_SIZE, 1000).
-record(top_k, { index :: index()
, type :: emqx_message_latency_stats:latency_type()
, last_update_time :: pos_integer()

View File

@ -251,7 +251,8 @@ publish(TickTime, Notices) ->
ok.
load() ->
MaxSize = emqx:get_config([emqx_slow_subs, top_k_num]),
MaxSizeT = emqx:get_config([emqx_slow_subs, top_k_num]),
MaxSize = erlang:min(MaxSizeT, ?MAX_TAB_SIZE),
_ = emqx:hook('message.slow_subs_stats',
{?MODULE, on_stats_update, [#{max_size => MaxSize}]}
),

View File

@ -87,7 +87,11 @@ slow_subs(delete, _) ->
ok = emqx_slow_subs:clear_history(),
{204};
slow_subs(get, #{query_string := QS}) ->
slow_subs(get, #{query_string := QST}) ->
LimitT = maps:get(<<"limit">>, QST, ?MAX_TAB_SIZE),
Limit = erlang:min(?MAX_TAB_SIZE, emqx_mgmt_api:b2i(LimitT)),
Page = maps:get(<<"page">>, QST, 1),
QS = QST#{<<"limit">> => Limit, <<"page">> => Page},
Data = emqx_mgmt_api:paginate({?TOPK_TAB, [{traverse, last_prev}]}, QS, ?FORMAT_FUN),
{200, Data}.

View File

@ -14,7 +14,7 @@ fields("emqx_slow_subs") ->
"The latency threshold for statistics, the minimum value is 100ms")}
, {expire_interval,
sc(emqx_schema:duration_ms(),
"5m",
"300s",
"The eviction time of the record, which in the statistics record table")}
, {top_k_num,
sc(integer(),

View File

@ -90,7 +90,7 @@ t_log_and_pub(_) ->
?assert(RecSum >= 5),
?assert(lists:all(fun(E) -> E =< 3 end, Recs)),
timer:sleep(2000),
timer:sleep(3000),
?assert(ets:info(?TOPK_TAB, size) =:= 0),
[Client ! stop || Client <- Clients],
ok.