Skip to content

Commit

Permalink
refactor(mgmt): move persistent session counting function to mgmt module
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesmg committed Feb 21, 2024
1 parent 529211b commit 03138f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
22 changes: 1 addition & 21 deletions apps/emqx/src/emqx_persistent_session_ds_state.erl
Expand Up @@ -36,11 +36,7 @@
-export([get_rank/2, put_rank/3, del_rank/2, fold_ranks/3]).
-export([get_subscriptions/1, put_subscription/4, del_subscription/3]).

-export([
make_session_iterator/0,
session_iterator_next/2,
session_count/0
]).
-export([make_session_iterator/0, session_iterator_next/2]).

-export_type([
t/0,
Expand Down Expand Up @@ -369,12 +365,6 @@ del_rank(Key, Rec) ->
fold_ranks(Fun, Acc, Rec) ->
gen_fold(ranks, Fun, Acc, Rec).

-spec session_count() -> non_neg_integer().
session_count() ->
%% N.B.: this is potentially costly. Should not be called in hot paths.
%% `mnesia:table_info(_, size)' is always zero for rocksdb, so we need to traverse...
do_session_count(make_session_iterator(), 0).

-spec make_session_iterator() -> session_iterator().
make_session_iterator() ->
mnesia:dirty_first(?session_tab).
Expand Down Expand Up @@ -577,16 +567,6 @@ ro_transaction(Fun) ->

%%

do_session_count('$end_of_table', N) ->
N;
do_session_count(Cursor, N) ->
case session_iterator_next(Cursor, 1) of
{[], _} ->
N;
{_, NextCursor} ->
do_session_count(NextCursor, N + 1)
end.

-compile({inline, check_sequence/1}).

-ifdef(CHECK_SEQNO).
Expand Down
18 changes: 17 additions & 1 deletion apps/emqx_management/src/emqx_mgmt_api_clients.erl
Expand Up @@ -916,7 +916,7 @@ add_persistent_session_count(QueryState0 = #{total := Totals0}) ->
%% to traverse the whole table), but also hard to deduplicate live connections
%% from it... So this count will possibly overshoot the true count of
%% sessions.
SessionCount = emqx_persistent_session_ds_state:session_count(),
SessionCount = persistent_session_count(),
Totals = Totals0#{undefined => SessionCount},
QueryState0#{total := Totals};
false ->
Expand Down Expand Up @@ -965,6 +965,22 @@ no_persistent_sessions() ->
true
end.

-spec persistent_session_count() -> non_neg_integer().
persistent_session_count() ->
%% N.B.: this is potentially costly. Should not be called in hot paths.
%% `mnesia:table_info(_, size)' is always zero for rocksdb, so we need to traverse...
do_persistent_session_count(init_persistent_session_iterator(), 0).

do_persistent_session_count('$end_of_table', N) ->
N;
do_persistent_session_count(Cursor, N) ->
case emqx_persistent_session_ds_state:session_iterator_next(Cursor, 1) of
{[], _} ->
N;
{_, NextCursor} ->
do_persistent_session_count(NextCursor, N + 1)
end.

do_persistent_session_query(ResultAcc, QueryState) ->
case emqx_persistent_message:is_persistence_enabled() of
true ->
Expand Down

0 comments on commit 03138f8

Please sign in to comment.